Browse Source

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>
maint
Nguyễn Thái Ngọc Duy 8 years ago committed by Junio C Hamano
parent
commit
b829b9439a
  1. 4
      builtin/checkout.c
  2. 9
      t/t2010-checkout-ambiguous.sh
  3. 12
      t/t2024-checkout-dwim.sh

4
builtin/checkout.c

@ -985,7 +985,7 @@ static int parse_branchname_arg(int argc, const char **argv,
int recover_with_dwim = dwim_new_local_branch_ok; int recover_with_dwim = dwim_new_local_branch_ok;


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

9
t/t2010-checkout-ambiguous.sh

@ -41,6 +41,15 @@ test_expect_success 'check ambiguity' '
test_must_fail git checkout world all test_must_fail git checkout world all
' '


test_expect_success 'check ambiguity in subdir' '
mkdir sub &&
# not ambiguous because sub/world does not exist
git -C sub checkout world ../all &&
echo hello >sub/world &&
# ambiguous because sub/world does exist
test_must_fail git -C sub checkout world ../all
'

test_expect_success 'disambiguate checking out from a tree-ish' ' test_expect_success 'disambiguate checking out from a tree-ish' '
echo bye > world && echo bye > world &&
git checkout world -- world && git checkout world -- world &&

12
t/t2024-checkout-dwim.sh

@ -174,6 +174,18 @@ test_expect_success 'checkout of branch with a file having the same name fails'
test_branch master test_branch master
' '


test_expect_success 'checkout of branch with a file in subdir having the same name fails' '
git checkout -B master &&
test_might_fail git branch -D spam &&

>spam &&
mkdir sub &&
mv spam sub/spam &&
test_must_fail git -C sub checkout spam &&
test_must_fail git rev-parse --verify refs/heads/spam &&
test_branch master
'

test_expect_success 'checkout <branch> -- succeeds, even if a file with the same name exists' ' test_expect_success 'checkout <branch> -- succeeds, even if a file with the same name exists' '
git checkout -B master && git checkout -B master &&
test_might_fail git branch -D spam && test_might_fail git branch -D spam &&

Loading…
Cancel
Save