Sync with 'maint'
[alt-git.git] / t / t7817-grep-sparse-checkout.sh
blob0ba7817fb76bc12abce00ddaaa2109a53a77ac84
1 #!/bin/sh
3 test_description='grep in sparse checkout
5 This test creates a repo with the following structure:
8 |-- a
9 |-- b
10 |-- dir
11 | `-- c
12 |-- sub
13 | |-- A
14 | | `-- a
15 | `-- B
16 | `-- b
17 `-- sub2
18 `-- a
20 Where the outer repository has non-cone mode sparsity patterns, sub is a
21 submodule with cone mode sparsity patterns and sub2 is a submodule that is
22 excluded by the superproject sparsity patterns. The resulting sparse checkout
23 should leave the following structure in the working tree:
26 |-- a
27 |-- sub
28 | `-- B
29 | `-- b
30 `-- sub2
31 `-- a
33 But note that sub2 should have the SKIP_WORKTREE bit set.
36 TEST_PASSES_SANITIZE_LEAK=true
37 . ./test-lib.sh
39 test_expect_success 'setup' '
40 echo "text" >a &&
41 echo "text" >b &&
42 mkdir dir &&
43 echo "text" >dir/c &&
45 git init sub &&
47 cd sub &&
48 mkdir A B &&
49 echo "text" >A/a &&
50 echo "text" >B/b &&
51 git add A B &&
52 git commit -m sub &&
53 git sparse-checkout init --cone &&
54 git sparse-checkout set B
55 ) &&
57 git init sub2 &&
59 cd sub2 &&
60 echo "text" >a &&
61 git add a &&
62 git commit -m sub2
63 ) &&
65 git submodule add ./sub &&
66 git submodule add ./sub2 &&
67 git add a b dir &&
68 git commit -m super &&
69 git sparse-checkout init --no-cone &&
70 git sparse-checkout set "/*" "!b" "!/*/" "sub" &&
72 git tag -am tag-to-commit tag-to-commit HEAD &&
73 tree=$(git rev-parse HEAD^{tree}) &&
74 git tag -am tag-to-tree tag-to-tree $tree &&
76 test_path_is_missing b &&
77 test_path_is_missing dir &&
78 test_path_is_missing sub/A &&
79 test_path_is_file a &&
80 test_path_is_file sub/B/b &&
81 test_path_is_file sub2/a &&
82 git branch -m main
85 # The test below covers a special case: the sparsity patterns exclude '/b' and
86 # sparse checkout is enabled, but the path exists in the working tree (e.g.
87 # manually created after `git sparse-checkout init`). Although b is marked
88 # as SKIP_WORKTREE, git grep should notice it IS present in the worktree and
89 # report it.
90 test_expect_success 'working tree grep honors sparse checkout' '
91 cat >expect <<-EOF &&
92 a:text
93 b:new-text
94 EOF
95 test_when_finished "rm -f b" &&
96 echo "new-text" >b &&
97 git grep "text" >actual &&
98 test_cmp expect actual
101 test_expect_success 'grep searches unmerged file despite not matching sparsity patterns' '
102 cat >expect <<-EOF &&
103 b:modified-b-in-branchX
104 b:modified-b-in-branchY
106 test_when_finished "test_might_fail git merge --abort && \
107 git checkout main && git sparse-checkout init" &&
109 git sparse-checkout disable &&
110 git checkout -b branchY main &&
111 test_commit modified-b-in-branchY b &&
112 git checkout -b branchX main &&
113 test_commit modified-b-in-branchX b &&
115 git sparse-checkout init &&
116 test_path_is_missing b &&
117 test_must_fail git merge branchY &&
118 git grep "modified-b" >actual &&
119 test_cmp expect actual
122 test_expect_success 'grep --cached searches entries with the SKIP_WORKTREE bit' '
123 cat >expect <<-EOF &&
124 a:text
125 b:text
126 dir/c:text
128 git grep --cached "text" >actual &&
129 test_cmp expect actual
132 # Note that sub2/ is present in the worktree but it is excluded by the sparsity
133 # patterns. We also explicitly mark it as SKIP_WORKTREE in case it got cleared
134 # by previous git commands. Thus sub2 starts as SKIP_WORKTREE but since it is
135 # present in the working tree, grep should recurse into it.
136 test_expect_success 'grep --recurse-submodules honors sparse checkout in submodule' '
137 cat >expect <<-EOF &&
138 a:text
139 sub/B/b:text
140 sub2/a:text
142 git update-index --skip-worktree sub2 &&
143 git grep --recurse-submodules "text" >actual &&
144 test_cmp expect actual
147 test_expect_success 'grep --recurse-submodules --cached searches entries with the SKIP_WORKTREE bit' '
148 cat >expect <<-EOF &&
149 a:text
150 b:text
151 dir/c:text
152 sub/A/a:text
153 sub/B/b:text
154 sub2/a:text
156 git grep --recurse-submodules --cached "text" >actual &&
157 test_cmp expect actual
160 test_expect_success 'working tree grep does not search the index with CE_VALID and SKIP_WORKTREE' '
161 cat >expect <<-EOF &&
162 a:text
164 test_when_finished "git update-index --no-assume-unchanged b" &&
165 git update-index --assume-unchanged b &&
166 git grep text >actual &&
167 test_cmp expect actual
170 test_expect_success 'grep --cached searches index entries with both CE_VALID and SKIP_WORKTREE' '
171 cat >expect <<-EOF &&
172 a:text
173 b:text
174 dir/c:text
176 test_when_finished "git update-index --no-assume-unchanged b" &&
177 git update-index --assume-unchanged b &&
178 git grep --cached text >actual &&
179 test_cmp expect actual
182 test_done