Re: [PATCH v8 4/4] worktree add: treat multiple matches with --guess-remote as an error
From: Junio C Hamano <hidden>
Date: 2026-08-25 21:31:46
"Yoichi NAKAYAMA via GitGitGadget" [off-list ref] writes:
+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.
quoted hunk ↗ jump to hunk
+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.