Merge branch 'rs/external-diff-tempfile'
[alt-git.git] / t / t7817-grep-sparse-checkout.sh
blobeb595645657fad7f1dce4cf5fca6ac57eeb6bfce
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-lib.sh
38 test_expect_success 'setup' '
39 echo "text" >a &&
40 echo "text" >b &&
41 mkdir dir &&
42 echo "text" >dir/c &&
44 git init sub &&
46 cd sub &&
47 mkdir A B &&
48 echo "text" >A/a &&
49 echo "text" >B/b &&
50 git add A B &&
51 git commit -m sub &&
52 git sparse-checkout init --cone &&
53 git sparse-checkout set B
54 ) &&
56 git init sub2 &&
58 cd sub2 &&
59 echo "text" >a &&
60 git add a &&
61 git commit -m sub2
62 ) &&
64 git submodule add ./sub &&
65 git submodule add ./sub2 &&
66 git add a b dir &&
67 git commit -m super &&
68 git sparse-checkout init --no-cone &&
69 git sparse-checkout set "/*" "!b" "!/*/" "sub" &&
71 git tag -am tag-to-commit tag-to-commit HEAD &&
72 tree=$(git rev-parse HEAD^{tree}) &&
73 git tag -am tag-to-tree tag-to-tree $tree &&
75 test_path_is_missing b &&
76 test_path_is_missing dir &&
77 test_path_is_missing sub/A &&
78 test_path_is_file a &&
79 test_path_is_file sub/B/b &&
80 test_path_is_file sub2/a &&
81 git branch -m main
84 # The test below covers a special case: the sparsity patterns exclude '/b' and
85 # sparse checkout is enabled, but the path exists in the working tree (e.g.
86 # manually created after `git sparse-checkout init`). Although b is marked
87 # as SKIP_WORKTREE, git grep should notice it IS present in the worktree and
88 # report it.
89 test_expect_success 'working tree grep honors sparse checkout' '
90 cat >expect <<-EOF &&
91 a:text
92 b:new-text
93 EOF
94 test_when_finished "rm -f b" &&
95 echo "new-text" >b &&
96 git grep "text" >actual &&
97 test_cmp expect actual
100 test_expect_success 'grep searches unmerged file despite not matching sparsity patterns' '
101 cat >expect <<-EOF &&
102 b:modified-b-in-branchX
103 b:modified-b-in-branchY
105 test_when_finished "test_might_fail git merge --abort && \
106 git checkout main && git sparse-checkout init" &&
108 git sparse-checkout disable &&
109 git checkout -b branchY main &&
110 test_commit modified-b-in-branchY b &&
111 git checkout -b branchX main &&
112 test_commit modified-b-in-branchX b &&
114 git sparse-checkout init &&
115 test_path_is_missing b &&
116 test_must_fail git merge branchY &&
117 git grep "modified-b" >actual &&
118 test_cmp expect actual
121 test_expect_success 'grep --cached searches entries with the SKIP_WORKTREE bit' '
122 cat >expect <<-EOF &&
123 a:text
124 b:text
125 dir/c:text
127 git grep --cached "text" >actual &&
128 test_cmp expect actual
131 # Note that sub2/ is present in the worktree but it is excluded by the sparsity
132 # patterns. We also explicitly mark it as SKIP_WORKTREE in case it got cleared
133 # by previous git commands. Thus sub2 starts as SKIP_WORKTREE but since it is
134 # present in the working tree, grep should recurse into it.
135 test_expect_success 'grep --recurse-submodules honors sparse checkout in submodule' '
136 cat >expect <<-EOF &&
137 a:text
138 sub/B/b:text
139 sub2/a:text
141 git update-index --skip-worktree sub2 &&
142 git grep --recurse-submodules "text" >actual &&
143 test_cmp expect actual
146 test_expect_success 'grep --recurse-submodules --cached searches entries with the SKIP_WORKTREE bit' '
147 cat >expect <<-EOF &&
148 a:text
149 b:text
150 dir/c:text
151 sub/A/a:text
152 sub/B/b:text
153 sub2/a:text
155 git grep --recurse-submodules --cached "text" >actual &&
156 test_cmp expect actual
159 test_expect_success 'working tree grep does not search the index with CE_VALID and SKIP_WORKTREE' '
160 cat >expect <<-EOF &&
161 a:text
163 test_when_finished "git update-index --no-assume-unchanged b" &&
164 git update-index --assume-unchanged b &&
165 git grep text >actual &&
166 test_cmp expect actual
169 test_expect_success 'grep --cached searches index entries with both CE_VALID and SKIP_WORKTREE' '
170 cat >expect <<-EOF &&
171 a:text
172 b:text
173 dir/c:text
175 test_when_finished "git update-index --no-assume-unchanged b" &&
176 git update-index --assume-unchanged b &&
177 git grep --cached text >actual &&
178 test_cmp expect actual
181 test_done