Compare
9ef0ca05bc322810310c2bd0c183ba53bb249b8e..bf7d128b47775e2e8d213b8ba159aeb955583e69
Merge branch 'yn/worktree-ambiguous-remote-advice'
'git checkout' and 'git worktree add' makes guesses based on a name of a remote-tracking branch, but does not give an error when such a remote-tracking branch cannot be uniquely identified, which has been corrected. * yn/worktree-ambiguous-remote-advice: worktree add: treat multiple matches with --guess-remote as an error worktree add: improve message for ambiguous remote branch name checkout: improve message for ambiguous remote branch name checkout: extract function to display advice for ambiguous remotes
Junio C Hamano <gitster@pobox.com> · 2026-09-07 16:32 UTC
7 files changed,
+133
−44
- Documentation/config/worktree.adoc
- Documentation/git-worktree.adoc
- builtin/checkout.c
- builtin/worktree.c
- checkout.c
- checkout.h
- 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/checkout.c
| @@ -1343,13 +1343,51 @@ enum checkout_command { | |||
| 1343 | 1343 | CHECKOUT_RESTORE = 3, | |
| 1344 | 1344 | }; | |
| 1345 | 1345 | ||
| 1346 | + | static void advise_disambiguating_remotes(enum checkout_command which_command, | |
| 1347 | + | const char *branch, | |
| 1348 | + | const struct string_list *matched_remote_names) | |
| 1349 | + | { | |
| 1350 | + | const char *cmdname; | |
| 1351 | + | struct string_list_item *item; | |
| 1352 | + | ||
| 1353 | + | switch (which_command) { | |
| 1354 | + | case CHECKOUT_CHECKOUT: | |
| 1355 | + | cmdname = "checkout"; | |
| 1356 | + | break; | |
| 1357 | + | case CHECKOUT_SWITCH: | |
| 1358 | + | cmdname = "switch"; | |
| 1359 | + | break; | |
| 1360 | + | default: | |
| 1361 | + | BUG("command <%d> should not reach advise_disambiguating_remotes", | |
| 1362 | + | which_command); | |
| 1363 | + | break; | |
| 1364 | + | } | |
| 1365 | + | ||
| 1366 | + | advise(_("Branch name '%s' appears in multiple remotes:"), branch); | |
| 1367 | + | for_each_string_list_item(item, matched_remote_names) { | |
| 1368 | + | advise(_(" %s"), item->string); | |
| 1369 | + | } | |
| 1370 | + | advise(_("If you meant to check out a remote tracking branch on <remote>,\n" | |
| 1371 | + | "you can do so by fully qualifying the name with the --track option:\n" | |
| 1372 | + | "\n" | |
| 1373 | + | " git %s --track <remote>/%s\n" | |
| 1374 | + | "\n" | |
| 1375 | + | "If you'd like to always have checkouts of an ambiguous name prefer\n" | |
| 1376 | + | "one remote, e.g. the 'origin' remote, consider setting\n" | |
| 1377 | + | "checkout.defaultRemote=origin in your config."), | |
| 1378 | + | cmdname, branch); | |
| 1379 | + | } | |
| 1380 | + | ||
| 1346 | 1381 | static char *parse_remote_branch(const char *arg, | |
| 1347 | 1382 | struct object_id *rev, | |
| 1348 | 1383 | int could_be_checkout_paths, | |
| 1349 | 1384 | enum checkout_command which_command) | |
| 1350 | 1385 | { | |
| 1351 | 1386 | int num_matches = 0; | |
| 1352 | - | char *remote = unique_tracking_name(arg, rev, &num_matches); | |
| 1387 | + | struct string_list matched_remote_names = STRING_LIST_INIT_DUP; | |
| 1388 | + | ||
| 1389 | + | char *remote = unique_tracking_name(arg, rev, &num_matches, | |
| 1390 | + | &matched_remote_names); | |
| 1353 | 1391 | ||
| 1354 | 1392 | if (remote && could_be_checkout_paths) { | |
| 1355 | 1393 | die(_("'%s' could be both a local file and a tracking branch.\n" | |
| @@ -1358,37 +1396,15 @@ static char *parse_remote_branch(const char *arg, | |||
| 1358 | 1396 | } | |
| 1359 | 1397 | ||
| 1360 | 1398 | if (!remote && num_matches > 1) { | |
| 1361 | - | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) { | |
| 1362 | - | const char *cmdname; | |
| 1363 | - | ||
| 1364 | - | switch (which_command) { | |
| 1365 | - | case CHECKOUT_CHECKOUT: | |
| 1366 | - | cmdname = "checkout"; | |
| 1367 | - | break; | |
| 1368 | - | case CHECKOUT_SWITCH: | |
| 1369 | - | cmdname = "switch"; | |
| 1370 | - | break; | |
| 1371 | - | default: | |
| 1372 | - | BUG("command <%d> should not reach parse_remote_branch", | |
| 1373 | - | which_command); | |
| 1374 | - | break; | |
| 1375 | - | } | |
| 1376 | - | ||
| 1377 | - | advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n" | |
| 1378 | - | "you can do so by fully qualifying the name with the --track option:\n" | |
| 1379 | - | "\n" | |
| 1380 | - | " git %s --track origin/<name>\n" | |
| 1381 | - | "\n" | |
| 1382 | - | "If you'd like to always have checkouts of an ambiguous <name> prefer\n" | |
| 1383 | - | "one remote, e.g. the 'origin' remote, consider setting\n" | |
| 1384 | - | "checkout.defaultRemote=origin in your config."), | |
| 1385 | - | cmdname); | |
| 1386 | - | } | |
| 1387 | - | ||
| 1388 | - | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 1389 | - | arg, num_matches); | |
| 1399 | + | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) | |
| 1400 | + | advise_disambiguating_remotes(which_command, arg, | |
| 1401 | + | &matched_remote_names); | |
| 1402 | + | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 1403 | + | arg, num_matches); | |
| 1390 | 1404 | } | |
| 1391 | 1405 | ||
| 1406 | + | string_list_clear(&matched_remote_names, 0); | |
| 1407 | + | ||
| 1392 | 1408 | return remote; | |
| 1393 | 1409 | } | |
| 1394 | 1410 | ||
modified builtin/worktree.c
| @@ -764,7 +764,26 @@ static int dwim_orphan(const struct add_opts *opts, int opt_track, int remote) | |||
| 764 | 764 | return 1; | |
| 765 | 765 | } | |
| 766 | 766 | ||
| 767 | - | static char *dwim_branch(const char *path, char **new_branch) | |
| 767 | + | static void advise_disambiguating_remotes(const char *path, const char *branch, | |
| 768 | + | const struct string_list *matched_remote_names) | |
| 769 | + | { | |
| 770 | + | struct string_list_item *item; | |
| 771 | + | ||
| 772 | + | advise(_("Branch name '%s' appears in multiple remotes:"), branch); | |
| 773 | + | for_each_string_list_item(item, matched_remote_names) { | |
| 774 | + | advise(_(" %s"), item->string); | |
| 775 | + | } | |
| 776 | + | advise(_("If you meant to create a worktree from a remote tracking branch on\n" | |
| 777 | + | "<remote>, you can do so by:\n" | |
| 778 | + | "\n" | |
| 779 | + | " git worktree add -b %s %s <remote>/%s\n" | |
| 780 | + | "\n" | |
| 781 | + | "If you'd like to always prefer some remote, e.g. 'origin',\n" | |
| 782 | + | "consider setting checkout.defaultRemote=origin in your config."), | |
| 783 | + | branch, path, branch); | |
| 784 | + | } | |
| 785 | + | ||
| 786 | + | static char *dwim_branch(const struct add_opts *opts, const char *path, char **new_branch) | |
| 768 | 787 | { | |
| 769 | 788 | int n; | |
| 770 | 789 | int branch_exists; | |
| @@ -782,7 +801,21 @@ static char *dwim_branch(const char *path, char **new_branch) | |||
| 782 | 801 | *new_branch = branchname; | |
| 783 | 802 | if (guess_remote) { | |
| 784 | 803 | struct object_id oid; | |
| 785 | - | char *remote = unique_tracking_name(*new_branch, &oid, NULL); | |
| 804 | + | char *remote; | |
| 805 | + | int num_matches = 0; | |
| 806 | + | struct string_list matched_remote_names = STRING_LIST_INIT_DUP; | |
| 807 | + | ||
| 808 | + | remote = unique_tracking_name(*new_branch, &oid, &num_matches, | |
| 809 | + | &matched_remote_names); | |
| 810 | + | if (!remote && num_matches > 1) { | |
| 811 | + | if (!opts->quiet && | |
| 812 | + | advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) | |
| 813 | + | advise_disambiguating_remotes(path, *new_branch, | |
| 814 | + | &matched_remote_names); | |
| 815 | + | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 816 | + | *new_branch, num_matches); | |
| 817 | + | } | |
| 818 | + | string_list_clear(&matched_remote_names, 0); | |
| 786 | 819 | return remote; | |
| 787 | 820 | } | |
| 788 | 821 | return NULL; | |
| @@ -890,7 +923,7 @@ static int add(int ac, const char **av, const char *prefix, | |||
| 890 | 923 | opts.orphan = dwim_orphan(&opts, !!opt_track, 0); | |
| 891 | 924 | } else if (ac < 2) { | |
| 892 | 925 | /* DWIM: Guess branch name from path. */ | |
| 893 | - | char *s = dwim_branch(path, &new_branch_to_free); | |
| 926 | + | char *s = dwim_branch(&opts, path, &new_branch_to_free); | |
| 894 | 927 | if (s) | |
| 895 | 928 | branch = branch_to_free = s; | |
| 896 | 929 | new_branch = new_branch_to_free; | |
| @@ -901,17 +934,29 @@ static int add(int ac, const char **av, const char *prefix, | |||
| 901 | 934 | if (!strcmp(branch, "HEAD")) | |
| 902 | 935 | can_use_local_refs(&opts); | |
| 903 | 936 | } else if (ac == 2) { | |
| 904 | - | struct object_id oid; | |
| 905 | 937 | struct commit *commit; | |
| 906 | - | char *remote; | |
| 907 | 938 | ||
| 908 | 939 | commit = lookup_commit_reference_by_name(branch); | |
| 909 | 940 | if (!commit) { | |
| 910 | - | remote = unique_tracking_name(branch, &oid, NULL); | |
| 941 | + | struct object_id oid; | |
| 942 | + | char *remote; | |
| 943 | + | int num_matches = 0; | |
| 944 | + | struct string_list matched_remote_names = STRING_LIST_INIT_DUP; | |
| 945 | + | ||
| 946 | + | remote = unique_tracking_name(branch, &oid, &num_matches, | |
| 947 | + | &matched_remote_names); | |
| 911 | 948 | if (remote) { | |
| 912 | 949 | new_branch = branch; | |
| 913 | 950 | branch = new_branch_to_free = remote; | |
| 951 | + | } else if (num_matches > 1) { | |
| 952 | + | if (!opts.quiet && | |
| 953 | + | advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) | |
| 954 | + | advise_disambiguating_remotes(path, branch, | |
| 955 | + | &matched_remote_names); | |
| 956 | + | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 957 | + | branch, num_matches); | |
| 914 | 958 | } | |
| 959 | + | string_list_clear(&matched_remote_names, 0); | |
| 915 | 960 | } | |
| 916 | 961 | ||
| 917 | 962 | if (!strcmp(branch, "HEAD")) | |
modified checkout.c
| @@ -8,6 +8,7 @@ | |||
| 8 | 8 | #include "checkout.h" | |
| 9 | 9 | #include "config.h" | |
| 10 | 10 | #include "strbuf.h" | |
| 11 | + | #include "string-list.h" | |
| 11 | 12 | ||
| 12 | 13 | struct tracking_name_data { | |
| 13 | 14 | /* const */ char *src_ref; | |
| @@ -17,6 +18,7 @@ struct tracking_name_data { | |||
| 17 | 18 | const char *default_remote; | |
| 18 | 19 | char *default_dst_ref; | |
| 19 | 20 | struct object_id *default_dst_oid; | |
| 21 | + | struct string_list *remote_names; | |
| 20 | 22 | }; | |
| 21 | 23 | ||
| 22 | 24 | #define TRACKING_NAME_DATA_INIT { 0 } | |
| @@ -39,6 +41,8 @@ static int check_tracking_name(struct remote *remote, void *cb_data) | |||
| 39 | 41 | oidcpy(dst, cb->dst_oid); | |
| 40 | 42 | cb->default_dst_oid = dst; | |
| 41 | 43 | } | |
| 44 | + | if (cb->remote_names) | |
| 45 | + | string_list_append(cb->remote_names, remote->name); | |
| 42 | 46 | if (cb->dst_ref) { | |
| 43 | 47 | free(query.dst); | |
| 44 | 48 | return 0; | |
| @@ -48,14 +52,19 @@ static int check_tracking_name(struct remote *remote, void *cb_data) | |||
| 48 | 52 | } | |
| 49 | 53 | ||
| 50 | 54 | char *unique_tracking_name(const char *name, struct object_id *oid, | |
| 51 | - | int *dwim_remotes_matched) | |
| 55 | + | int *dwim_remotes_matched, | |
| 56 | + | struct string_list *dwim_remote_names) | |
| 52 | 57 | { | |
| 53 | 58 | struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT; | |
| 54 | 59 | const char *default_remote = NULL; | |
| 55 | - | if (!repo_config_get_string_tmp(the_repository, "checkout.defaultremote", &default_remote)) | |
| 60 | + | ||
| 61 | + | if (!repo_config_get_string_tmp(the_repository, | |
| 62 | + | "checkout.defaultremote", | |
| 63 | + | &default_remote)) | |
| 56 | 64 | cb_data.default_remote = default_remote; | |
| 57 | 65 | cb_data.src_ref = xstrfmt("refs/heads/%s", name); | |
| 58 | 66 | cb_data.dst_oid = oid; | |
| 67 | + | cb_data.remote_names = dwim_remote_names; | |
| 59 | 68 | for_each_remote(check_tracking_name, &cb_data); | |
| 60 | 69 | if (dwim_remotes_matched) | |
| 61 | 70 | *dwim_remotes_matched = cb_data.num_matches; | |
modified checkout.h
| @@ -3,6 +3,8 @@ | |||
| 3 | 3 | ||
| 4 | 4 | #include "hash.h" | |
| 5 | 5 | ||
| 6 | + | struct string_list; | |
| 7 | + | ||
| 6 | 8 | /* | |
| 7 | 9 | * Check if the branch name uniquely matches a branch name on a remote | |
| 8 | 10 | * tracking branch. Return the name of the remote if such a branch | |
| @@ -10,6 +12,7 @@ | |||
| 10 | 12 | */ | |
| 11 | 13 | char *unique_tracking_name(const char *name, | |
| 12 | 14 | struct object_id *oid, | |
| 13 | - | int *dwim_remotes_matched); | |
| 15 | + | int *dwim_remotes_matched, | |
| 16 | + | struct string_list *dwim_remote_names); | |
| 14 | 17 | ||
| 15 | 18 | #endif /* CHECKOUT_H */ | |
modified t/t2400-worktree-add.sh
| @@ -634,12 +634,12 @@ test_expect_success '"add" <path> <branch> does not dwim with -b' ' | |||
| 634 | 634 | test_expect_success '"add" <path> <branch> dwims with checkout.defaultRemote' ' | |
| 635 | 635 | test_when_finished rm -rf repo_upstream repo_dwim foo && | |
| 636 | 636 | setup_remote_repo repo_upstream repo_dwim && | |
| 637 | - | git init repo_dwim && | |
| 638 | 637 | ( | |
| 639 | 638 | cd repo_dwim && | |
| 640 | 639 | git remote add repo_upstream2 ../repo_upstream && | |
| 641 | 640 | git fetch repo_upstream2 && | |
| 642 | - | test_must_fail git worktree add ../foo foo && | |
| 641 | + | test_must_fail git worktree add ../foo foo 2>error.actual && | |
| 642 | + | test_grep "matched multiple (2) remote tracking branches" error.actual && | |
| 643 | 643 | git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo && | |
| 644 | 644 | git status -uno --porcelain >status.actual && | |
| 645 | 645 | test_must_be_empty status.actual | |
| @@ -679,6 +679,19 @@ test_expect_success 'git worktree add --guess-remote sets up tracking' ' | |||
| 679 | 679 | test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo | |
| 680 | 680 | ) | |
| 681 | 681 | ' | |
| 682 | + | ||
| 683 | + | test_expect_success 'git worktree add --guess-remote fails if there are multiple matches' ' | |
| 684 | + | test_when_finished rm -rf repo_a repo_b foo && | |
| 685 | + | setup_remote_repo repo_a repo_b && | |
| 686 | + | ( | |
| 687 | + | cd repo_b && | |
| 688 | + | git remote add repo_a2 ../repo_a && | |
| 689 | + | git fetch repo_a2 && | |
| 690 | + | test_must_fail git worktree add --guess-remote ../foo 2>actual && | |
| 691 | + | test_grep "matched multiple (2) remote tracking branches" actual | |
| 692 | + | ) | |
| 693 | + | ' | |
| 694 | + | ||
| 682 | 695 | test_expect_success 'git worktree add --guess-remote sets up tracking (quiet)' ' | |
| 683 | 696 | test_when_finished rm -rf repo_a repo_b foo && | |
| 684 | 697 | setup_remote_repo repo_a repo_b && | |