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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Sep 7, 2016 at 18:19 UTC
b829b9439adc12fa4fb33338694e7f1ad40254c1
3 files changed
+23
-2
builtin/checkout.c
+2
-2
@@ -985,7 +985,7 @@ static int parse_branchname_arg(int argc, const char **argv,
985
int recover_with_dwim = dwim_new_local_branch_ok;
986
987
if (!has_dash_dash &&
988
- (check_filename(NULL, arg) || !no_wildcard(arg)))
988
+ (check_filename(opts->prefix, arg) || !no_wildcard(arg)))
989
recover_with_dwim = 0;
990
/*
991
* Accept "git checkout foo" and "git checkout foo --"
@@ -1046,7 +1046,7 @@ static int parse_branchname_arg(int argc, const char **argv,
1046
* it would be extremely annoying.
1047
*/
1048
if (argc)
1049
- verify_non_filename(NULL, arg);
1049
+ verify_non_filename(opts->prefix, arg);
1050
} else {
1051
argcount++;
1052
argv++;
t/t2010-checkout-ambiguous.sh
+9
@@ -41,6 +41,15 @@ test_expect_success 'check ambiguity' '
41
test_must_fail git checkout world all
42
'
43
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
+
53
test_expect_success 'disambiguate checking out from a tree-ish' '
54
echo bye > world &&
55
git checkout world -- world &&
t/t2024-checkout-dwim.sh
+12
@@ -174,6 +174,18 @@ test_expect_success 'checkout of branch with a file having the same name fails'
174
test_branch master
175
'
176
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
+
189
test_expect_success 'checkout <branch> -- succeeds, even if a file with the same name exists' '
190
git checkout -B master &&
191
test_might_fail git branch -D spam &&