@matheusbsbs / matheus-bagliano / commits / bfda204ade
Fork 0

checkout, restore: make pathspec recursive

The pathspec given to git checkout and git restore is used with both tree_entry_interesting (via read_tree_recursive) and match_pathspec (via ce_path_match). The latter effectively only supports recursive matching regardless of the value of the pathspec flag "recursive", which is unset here. That causes different match results for pathspecs with wildcards, and can lead checkout and restore in no-overlay mode to remove entries instead of modifying them. Enable recursive matching for both checkout and restore to make matching consistent. Setting the flag in checkout_main() technically also affects git switch, but since that command doesn't accept pathspecs at all this has no actual consequence. Reported-by: Sergii Shkarnikov <sergii.shkarnikov@globallogic.com> Initial-test-by: Sergii Shkarnikov <sergii.shkarnikov@globallogic.com> Helped-by: Jeff King <peff@peff.net> Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Aug 22, 2020 at 10:57 UTC bfda204ade8a19357cf073c98dc3a5043533b4c4
3 files changed +30 -3
builtin/checkout.c
+2
@@ -1660,6 +1660,8 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
1660 die(_("--pathspec-file-nul requires --pathspec-from-file"));
1661 }
1662
1663 + opts->pathspec.recursive = 1;
1664 +
1665 if (opts->pathspec.nr) {
1666 if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge)
1667 die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n"
t/t2025-checkout-no-overlay.sh
+12
@@ -44,4 +44,16 @@ test_expect_success '--no-overlay --theirs with D/F conflict deletes file' '
44 test_path_is_missing file1
45 '
46
47 +test_expect_success 'wildcard pathspec matches file in subdirectory' '
48 + git reset --hard &&
49 + mkdir subdir &&
50 + test_commit file3-1 subdir/file3 &&
51 + test_commit file3-2 subdir/file3 &&
52 +
53 + git checkout --no-overlay file3-1 "*file3" &&
54 + echo file3-1 >expect &&
55 + test_path_is_file subdir/file3 &&
56 + test_cmp expect subdir/file3
57 +'
58 +
59 test_done
t/t2072-restore-pathspec-file.sh
+16 -3
@@ -9,18 +9,21 @@ test_tick
9 test_expect_success setup '
10 test_commit file0 &&
11
12 + mkdir dir1 &&
13 + echo 1 >dir1/file &&
14 echo 1 >fileA.t &&
15 echo 1 >fileB.t &&
16 echo 1 >fileC.t &&
17 echo 1 >fileD.t &&
16 - git add fileA.t fileB.t fileC.t fileD.t &&
18 + git add dir1 fileA.t fileB.t fileC.t fileD.t &&
19 git commit -m "files 1" &&
20
21 + echo 2 >dir1/file &&
22 echo 2 >fileA.t &&
23 echo 2 >fileB.t &&
24 echo 2 >fileC.t &&
25 echo 2 >fileD.t &&
23 - git add fileA.t fileB.t fileC.t fileD.t &&
26 + git add dir1 fileA.t fileB.t fileC.t fileD.t &&
27 git commit -m "files 2" &&
28
29 git tag checkpoint
@@ -31,7 +34,7 @@ restore_checkpoint () {
34 }
35
36 verify_expect () {
34 - git status --porcelain --untracked-files=no -- fileA.t fileB.t fileC.t fileD.t >actual &&
37 + git status --porcelain --untracked-files=no -- dir1 fileA.t fileB.t fileC.t fileD.t >actual &&
38 test_cmp expect actual
39 }
40
@@ -136,4 +139,14 @@ test_expect_success 'only touches what was listed' '
139 verify_expect
140 '
141
142 +test_expect_success 'wildcard pathspec matches file in subdirectory' '
143 + restore_checkpoint &&
144 +
145 + echo "*file" | git restore --pathspec-from-file=- --source=HEAD^1 &&
146 + cat >expect <<-\EOF &&
147 + M dir1/file
148 + EOF
149 + verify_expect
150 +'
151 +
152 test_done