Skip to content

Commit d0e70cd

Browse files
committed
Merge branch 'am/checkout-file-and-ref-ref-ambiguity'
"git checkout X" did not correctly fail when X is not a local branch but could name more than one remote-tracking branches (i.e. to be dwimmed as the starting point to create a corresponding local branch), which has been corrected. * am/checkout-file-and-ref-ref-ambiguity: checkout: don't revert file on ambiguous tracking branches parse_branchname_arg(): extract part as new function
2 parents 76c57fe + fa74180 commit d0e70cd

File tree

2 files changed

+65
-34
lines changed

2 files changed

+65
-34
lines changed

builtin/checkout.c

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,12 +1117,43 @@ static void setup_new_branch_info_and_source_tree(
11171117
}
11181118
}
11191119

1120+
static const char *parse_remote_branch(const char *arg,
1121+
struct object_id *rev,
1122+
int could_be_checkout_paths)
1123+
{
1124+
int num_matches = 0;
1125+
const char *remote = unique_tracking_name(arg, rev, &num_matches);
1126+
1127+
if (remote && could_be_checkout_paths) {
1128+
die(_("'%s' could be both a local file and a tracking branch.\n"
1129+
"Please use -- (and optionally --no-guess) to disambiguate"),
1130+
arg);
1131+
}
1132+
1133+
if (!remote && num_matches > 1) {
1134+
if (advice_checkout_ambiguous_remote_branch_name) {
1135+
advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n"
1136+
"you can do so by fully qualifying the name with the --track option:\n"
1137+
"\n"
1138+
" git checkout --track origin/<name>\n"
1139+
"\n"
1140+
"If you'd like to always have checkouts of an ambiguous <name> prefer\n"
1141+
"one remote, e.g. the 'origin' remote, consider setting\n"
1142+
"checkout.defaultRemote=origin in your config."));
1143+
}
1144+
1145+
die(_("'%s' matched multiple (%d) remote tracking branches"),
1146+
arg, num_matches);
1147+
}
1148+
1149+
return remote;
1150+
}
1151+
11201152
static int parse_branchname_arg(int argc, const char **argv,
11211153
int dwim_new_local_branch_ok,
11221154
struct branch_info *new_branch_info,
11231155
struct checkout_opts *opts,
1124-
struct object_id *rev,
1125-
int *dwim_remotes_matched)
1156+
struct object_id *rev)
11261157
{
11271158
const char **new_branch = &opts->new_branch;
11281159
int argcount = 0;
@@ -1227,13 +1258,9 @@ static int parse_branchname_arg(int argc, const char **argv,
12271258
recover_with_dwim = 0;
12281259

12291260
if (recover_with_dwim) {
1230-
const char *remote = unique_tracking_name(arg, rev,
1231-
dwim_remotes_matched);
1261+
const char *remote = parse_remote_branch(arg, rev,
1262+
could_be_checkout_paths);
12321263
if (remote) {
1233-
if (could_be_checkout_paths)
1234-
die(_("'%s' could be both a local file and a tracking branch.\n"
1235-
"Please use -- (and optionally --no-guess) to disambiguate"),
1236-
arg);
12371264
*new_branch = arg;
12381265
arg = remote;
12391266
/* DWIMmed to create local branch, case (3).(b) */
@@ -1498,7 +1525,6 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
14981525
const char * const usagestr[])
14991526
{
15001527
struct branch_info new_branch_info;
1501-
int dwim_remotes_matched = 0;
15021528
int parseopt_flags = 0;
15031529

15041530
memset(&new_branch_info, 0, sizeof(new_branch_info));
@@ -1606,8 +1632,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
16061632
opts->track == BRANCH_TRACK_UNSPECIFIED &&
16071633
!opts->new_branch;
16081634
int n = parse_branchname_arg(argc, argv, dwim_ok,
1609-
&new_branch_info, opts, &rev,
1610-
&dwim_remotes_matched);
1635+
&new_branch_info, opts, &rev);
16111636
argv += n;
16121637
argc -= n;
16131638
} else if (!opts->accept_ref && opts->from_treeish) {
@@ -1684,28 +1709,10 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
16841709
}
16851710

16861711
UNLEAK(opts);
1687-
if (opts->patch_mode || opts->pathspec.nr) {
1688-
int ret = checkout_paths(opts, new_branch_info.name);
1689-
if (ret && dwim_remotes_matched > 1 &&
1690-
advice_checkout_ambiguous_remote_branch_name)
1691-
advise(_("'%s' matched more than one remote tracking branch.\n"
1692-
"We found %d remotes with a reference that matched. So we fell back\n"
1693-
"on trying to resolve the argument as a path, but failed there too!\n"
1694-
"\n"
1695-
"If you meant to check out a remote tracking branch on, e.g. 'origin',\n"
1696-
"you can do so by fully qualifying the name with the --track option:\n"
1697-
"\n"
1698-
" git checkout --track origin/<name>\n"
1699-
"\n"
1700-
"If you'd like to always have checkouts of an ambiguous <name> prefer\n"
1701-
"one remote, e.g. the 'origin' remote, consider setting\n"
1702-
"checkout.defaultRemote=origin in your config."),
1703-
argv[0],
1704-
dwim_remotes_matched);
1705-
return ret;
1706-
} else {
1712+
if (opts->patch_mode || opts->pathspec.nr)
1713+
return checkout_paths(opts, new_branch_info.name);
1714+
else
17071715
return checkout_branch(opts, &new_branch_info);
1708-
}
17091716
}
17101717

17111718
int cmd_checkout(int argc, const char **argv, const char *prefix)

t/t2024-checkout-dwim.sh

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ test_expect_success 'setup' '
3737
git checkout -b foo &&
3838
test_commit a_foo &&
3939
git checkout -b bar &&
40-
test_commit a_bar
40+
test_commit a_bar &&
41+
git checkout -b ambiguous_branch_and_file &&
42+
test_commit a_ambiguous_branch_and_file
4143
) &&
4244
git init repo_b &&
4345
(
@@ -46,7 +48,9 @@ test_expect_success 'setup' '
4648
git checkout -b foo &&
4749
test_commit b_foo &&
4850
git checkout -b baz &&
49-
test_commit b_baz
51+
test_commit b_baz &&
52+
git checkout -b ambiguous_branch_and_file &&
53+
test_commit b_ambiguous_branch_and_file
5054
) &&
5155
git remote add repo_a repo_a &&
5256
git remote add repo_b repo_b &&
@@ -75,6 +79,26 @@ test_expect_success 'checkout of branch from multiple remotes fails #1' '
7579
test_branch master
7680
'
7781

82+
test_expect_success 'when arg matches multiple remotes, do not fallback to interpreting as pathspec' '
83+
# create a file with name matching remote branch name
84+
git checkout -b t_ambiguous_branch_and_file &&
85+
>ambiguous_branch_and_file &&
86+
git add ambiguous_branch_and_file &&
87+
git commit -m "ambiguous_branch_and_file" &&
88+
89+
# modify file to verify that it will not be touched by checkout
90+
test_when_finished "git checkout -- ambiguous_branch_and_file" &&
91+
echo "file contents" >ambiguous_branch_and_file &&
92+
cp ambiguous_branch_and_file expect &&
93+
94+
test_must_fail git checkout ambiguous_branch_and_file 2>err &&
95+
96+
test_i18ngrep "matched multiple (2) remote tracking branches" err &&
97+
98+
# file must not be altered
99+
test_cmp expect ambiguous_branch_and_file
100+
'
101+
78102
test_expect_success 'checkout of branch from multiple remotes fails with advice' '
79103
git checkout -B master &&
80104
test_might_fail git branch -D foo &&

0 commit comments

Comments
 (0)