Commit
b70101c22431bb77ef1d174cb7309f2eaab68c0e
worktree add: treat multiple matches with --guess-remote as an error
When 'git worktree add <path>' is invoked without <commit-ish> and with the --guess-remote option (or when worktree.guessRemote is set to true), it tries to find a remote-tracking branch matching the basename of <path>. Currently, the behavior when multiple matches are found is the same as when no match is found: it falls back to creating a branch from HEAD. This has been the behavior since 71d6682d8c (worktree: add --guess-remote option to add subcommand, 2017-11-29), when the option was first introduced. However, if the specified <path> matches any remote-tracking branch, we infer that the user intended to use one of the remote-tracking branches as the start-point rather than HEAD. So we abort the creation of the branch and worktree when there are multiple matches, and instruct the user to choose the start-point. Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Yoichi NAKAYAMA <yoichi.nakayama@gmail.com> · 2026-08-27 14:41 UTC · committed by Junio C Hamano
4 files changed,
+36
−6
- Documentation/config/worktree.adoc
- Documentation/git-worktree.adoc
- builtin/worktree.c
- t/t2400-worktree-add.sh
modified Documentation/config/worktree.adoc
| @@ -5,8 +5,9 @@ | |||
| 5 | 5 | set to true, `worktree add` tries to find a remote-tracking | |
| 6 | 6 | branch whose name uniquely matches the new branch name. If | |
| 7 | 7 | such a branch exists, it is checked out and set as "upstream" | |
| 8 | - | for the new branch. If no such match can be found, it falls | |
| 9 | - | back to creating a new branch from the current `HEAD`. | |
| 8 | + | for the new branch. If multiple matches are found, the command | |
| 9 | + | fails. If no such match can be found, it falls back to | |
| 10 | + | creating a new branch from the current `HEAD`. | |
| 10 | 11 | ||
| 11 | 12 | `worktree.useRelativePaths`:: | |
| 12 | 13 | Link worktrees using relative paths (when "`true`") or absolute | |
modified Documentation/git-worktree.adoc
| @@ -219,7 +219,9 @@ To remove a locked worktree, specify `--force` twice. | |||
| 219 | 219 | of creating a new branch from `HEAD`, if there exists a tracking | |
| 220 | 220 | branch in exactly one remote matching the basename of _<path>_, | |
| 221 | 221 | base the new branch on the remote-tracking branch, and mark | |
| 222 | - | the remote-tracking branch as "upstream" from the new branch. | |
| 222 | + | the remote-tracking branch as "upstream" from the new branch. If | |
| 223 | + | there are multiple matches, the command fails. If there is no | |
| 224 | + | match, the command falls back to creating a new branch from `HEAD`. | |
| 223 | 225 | + | |
| 224 | 226 | This can also be set up as the default behaviour by using the | |
| 225 | 227 | `worktree.guessRemote` config option. | |
modified builtin/worktree.c
| @@ -782,7 +782,7 @@ static void advise_disambiguating_remotes(const char *path, const char *branch, | |||
| 782 | 782 | branch, path, branch); | |
| 783 | 783 | } | |
| 784 | 784 | ||
| 785 | - | static char *dwim_branch(const char *path, char **new_branch) | |
| 785 | + | static char *dwim_branch(const struct add_opts *opts, const char *path, char **new_branch) | |
| 786 | 786 | { | |
| 787 | 787 | int n; | |
| 788 | 788 | int branch_exists; | |
| @@ -800,7 +800,21 @@ static char *dwim_branch(const char *path, char **new_branch) | |||
| 800 | 800 | *new_branch = branchname; | |
| 801 | 801 | if (guess_remote) { | |
| 802 | 802 | struct object_id oid; | |
| 803 | - | char *remote = unique_tracking_name(*new_branch, &oid, NULL, NULL); | |
| 803 | + | char *remote; | |
| 804 | + | int num_matches = 0; | |
| 805 | + | struct string_list matched_remote_names = STRING_LIST_INIT_DUP; | |
| 806 | + | ||
| 807 | + | remote = unique_tracking_name(*new_branch, &oid, &num_matches, | |
| 808 | + | &matched_remote_names); | |
| 809 | + | if (!remote && num_matches > 1) { | |
| 810 | + | if (!opts->quiet && | |
| 811 | + | advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) | |
| 812 | + | advise_disambiguating_remotes(path, *new_branch, | |
| 813 | + | &matched_remote_names); | |
| 814 | + | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 815 | + | *new_branch, num_matches); | |
| 816 | + | } | |
| 817 | + | string_list_clear(&matched_remote_names, 0); | |
| 804 | 818 | return remote; | |
| 805 | 819 | } | |
| 806 | 820 | return NULL; | |
| @@ -908,7 +922,7 @@ static int add(int ac, const char **av, const char *prefix, | |||
| 908 | 922 | opts.orphan = dwim_orphan(&opts, !!opt_track, 0); | |
| 909 | 923 | } else if (ac < 2) { | |
| 910 | 924 | /* DWIM: Guess branch name from path. */ | |
| 911 | - | char *s = dwim_branch(path, &new_branch_to_free); | |
| 925 | + | char *s = dwim_branch(&opts, path, &new_branch_to_free); | |
| 912 | 926 | if (s) | |
| 913 | 927 | branch = branch_to_free = s; | |
| 914 | 928 | new_branch = new_branch_to_free; | |
modified t/t2400-worktree-add.sh
| @@ -669,6 +669,19 @@ test_expect_success 'git worktree add --guess-remote sets up tracking' ' | |||
| 669 | 669 | test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo | |
| 670 | 670 | ) | |
| 671 | 671 | ' | |
| 672 | + | ||
| 673 | + | test_expect_success 'git worktree add --guess-remote fails if there are multiple matches' ' | |
| 674 | + | test_when_finished rm -rf repo_a repo_b foo && | |
| 675 | + | setup_remote_repo repo_a repo_b && | |
| 676 | + | ( | |
| 677 | + | cd repo_b && | |
| 678 | + | git remote add repo_a2 ../repo_a && | |
| 679 | + | git fetch repo_a2 && | |
| 680 | + | test_must_fail git worktree add --guess-remote ../foo 2>actual && | |
| 681 | + | test_grep "matched multiple (2) remote tracking branches" actual | |
| 682 | + | ) | |
| 683 | + | ' | |
| 684 | + | ||
| 672 | 685 | test_expect_success 'git worktree add --guess-remote sets up tracking (quiet)' ' | |
| 673 | 686 | test_when_finished rm -rf repo_a repo_b foo && | |
| 674 | 687 | setup_remote_repo repo_a repo_b && | |