Re: [PATCH v8 4/4] worktree add: treat multiple matches with --guess-remote as an error
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Aug 25, 2026, 21:31 UTC
- Message-ID
- <xmqqcxv5lwe8.fsf@gitster.g>
- In-Reply-To
- <927856e0a07cf6d9e7e1093a4fbf979584141224.1787691875.git.gitgitgadget@gmail.com>
"Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 18 quoted lines
> +static void advise_disambiguating_remotes(const char *path, const char *branch,
> + const struct string_list *matched_remote_names)
> +{
> + struct string_list_item *item;
> +
> + advise(_("Branch name '%s' appears in multiple remotes:"), branch);
> + for_each_string_list_item(item, matched_remote_names) {
> + advise(_(" %s"), item->string);
> + }
> + advise(_("If you meant to create a worktree from a remote tracking branch on\n"
> + "<remote>, you can do so by:\n"
> + "\n"
> + " git worktree add -b %s %s <remote>/%s\n"
> + "\n"
> + "If you'd like to always prefer some remote, e.g. 'origin',\n"
> + "consider setting checkout.defaultRemote=origin in your config."),
> + branch, path, branch);
> +}Wasn't this function added in this series somewhere earlier in the topic? If we add it high enough when we did so, we wouldn't have to move it higher like this patch does.
Show 28 quoted lines
> +static char *dwim_branch(const struct add_opts *opts, const char *path, char **new_branch)
> {
> int n;
> int branch_exists;
> @@ -782,31 +801,26 @@ static char *dwim_branch(const char *path, char **new_branch)
> *new_branch = branchname;
> if (guess_remote) {
> struct object_id oid;
> - char *remote = unique_tracking_name(*new_branch, &oid, NULL, NULL);
> + char *remote;
> + int num_matches = 0;
> + struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
> +
> + remote = unique_tracking_name(*new_branch, &oid, &num_matches,
> + &matched_remote_names);
> + if (!remote && num_matches > 1) {
> + if (!opts->quiet &&
> + advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME))
> + advise_disambiguating_remotes(path, *new_branch,
> + &matched_remote_names);
> + die(_("'%s' matched multiple (%d) remote tracking branches"),
> + *new_branch, num_matches);
> + }
> + string_list_clear(&matched_remote_names, 0);
> return remote;
> }
> return NULL;
> }Looking good.
Thanks.