Skip to content

Commit b829b94

Browse files
pcloudsgitster
authored andcommitted
checkout: fix ambiguity check in subdir
The two functions in parse_branchname_arg(), verify_non_filename and check_filename, need correct prefix in order to reconstruct the paths and check for their existence. With NULL prefix, they just check paths at top dir instead. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 19e5656 commit b829b94

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

builtin/checkout.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ static int parse_branchname_arg(int argc, const char **argv,
985985
int recover_with_dwim = dwim_new_local_branch_ok;
986986

987987
if (!has_dash_dash &&
988-
(check_filename(NULL, arg) || !no_wildcard(arg)))
988+
(check_filename(opts->prefix, arg) || !no_wildcard(arg)))
989989
recover_with_dwim = 0;
990990
/*
991991
* Accept "git checkout foo" and "git checkout foo --"
@@ -1046,7 +1046,7 @@ static int parse_branchname_arg(int argc, const char **argv,
10461046
* it would be extremely annoying.
10471047
*/
10481048
if (argc)
1049-
verify_non_filename(NULL, arg);
1049+
verify_non_filename(opts->prefix, arg);
10501050
} else {
10511051
argcount++;
10521052
argv++;

t/t2010-checkout-ambiguous.sh

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ test_expect_success 'check ambiguity' '
4141
test_must_fail git checkout world all
4242
'
4343

44+
test_expect_success 'check ambiguity in subdir' '
45+
mkdir sub &&
46+
# not ambiguous because sub/world does not exist
47+
git -C sub checkout world ../all &&
48+
echo hello >sub/world &&
49+
# ambiguous because sub/world does exist
50+
test_must_fail git -C sub checkout world ../all
51+
'
52+
4453
test_expect_success 'disambiguate checking out from a tree-ish' '
4554
echo bye > world &&
4655
git checkout world -- world &&

t/t2024-checkout-dwim.sh

+12
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ test_expect_success 'checkout of branch with a file having the same name fails'
174174
test_branch master
175175
'
176176

177+
test_expect_success 'checkout of branch with a file in subdir having the same name fails' '
178+
git checkout -B master &&
179+
test_might_fail git branch -D spam &&
180+
181+
>spam &&
182+
mkdir sub &&
183+
mv spam sub/spam &&
184+
test_must_fail git -C sub checkout spam &&
185+
test_must_fail git rev-parse --verify refs/heads/spam &&
186+
test_branch master
187+
'
188+
177189
test_expect_success 'checkout <branch> -- succeeds, even if a file with the same name exists' '
178190
git checkout -B master &&
179191
test_might_fail git branch -D spam &&

0 commit comments

Comments
 (0)