3 test_description
='grep in sparse checkout
5 This test creates a repo with the following structure:
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:
33 But note that sub2 should have the SKIP_WORKTREE bit set.
36 TEST_PASSES_SANITIZE_LEAK
=true
39 test_expect_success
'setup' '
53 git sparse-checkout init --cone &&
54 git sparse-checkout set B
65 git submodule add ./sub &&
66 git submodule add ./sub2 &&
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 &&
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
90 test_expect_success
'working tree grep honors sparse checkout' '
95 test_when_finished "rm -f 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 &&
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 &&
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 &&
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 &&
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 &&
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