Git 2.45
[git/gitster.git] / t / t0090-cache-tree.sh
blobd8e2fc42e15c73dfffebba57071b8d3e4bfd1ed1
1 #!/bin/sh
3 test_description="Test whether cache-tree is properly updated
5 Tests whether various commands properly update and/or rewrite the
6 cache-tree extension.
9 TEST_PASSES_SANITIZE_LEAK=true
10 . ./test-lib.sh
12 cmp_cache_tree () {
13 test-tool dump-cache-tree | sed -e '/#(ref)/d' >actual &&
14 sed "s/$OID_REGEX/SHA/" <actual >filtered &&
15 test_cmp "$1" filtered &&
16 rm filtered
19 # We don't bother with actually checking the SHA1:
20 # test-tool dump-cache-tree already verifies that all existing data is
21 # correct.
22 generate_expected_cache_tree () {
23 pathspec="$1" &&
24 dir="$2${2:+/}" &&
25 git ls-tree --name-only HEAD -- "$pathspec" >files &&
26 git ls-tree --name-only -d HEAD -- "$pathspec" >subtrees &&
27 printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) &&
28 while read subtree
30 generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
31 done <subtrees
34 test_cache_tree () {
35 generate_expected_cache_tree "." >expect &&
36 cmp_cache_tree expect &&
37 rm expect actual files subtrees &&
38 git status --porcelain -- ':!status' ':!expected.status' >status &&
39 if test -n "$1"
40 then
41 test_cmp "$1" status
42 else
43 test_must_be_empty status
47 test_invalid_cache_tree () {
48 printf "invalid %s ()\n" "" "$@" >expect &&
49 test-tool dump-cache-tree |
50 sed -n -e "s/[0-9]* subtrees//" -e '/#(ref)/d' -e '/^invalid /p' >actual &&
51 test_cmp expect actual
54 test_no_cache_tree () {
55 >expect &&
56 cmp_cache_tree expect
59 test_expect_success 'initial commit has cache-tree' '
60 test_commit foo &&
61 test_cache_tree
64 test_expect_success 'read-tree HEAD establishes cache-tree' '
65 git read-tree HEAD &&
66 test_cache_tree
69 test_expect_success 'git-add invalidates cache-tree' '
70 test_when_finished "git reset --hard; git read-tree HEAD" &&
71 echo "I changed this file" >foo &&
72 git add foo &&
73 test_invalid_cache_tree
76 test_expect_success 'git-add in subdir invalidates cache-tree' '
77 test_when_finished "git reset --hard; git read-tree HEAD" &&
78 mkdir dirx &&
79 echo "I changed this file" >dirx/foo &&
80 git add dirx/foo &&
81 test_invalid_cache_tree
84 test_expect_success 'git-add in subdir does not invalidate sibling cache-tree' '
85 git tag no-children &&
86 test_when_finished "git reset --hard no-children; git read-tree HEAD" &&
87 mkdir dir1 dir2 &&
88 test_commit dir1/a &&
89 test_commit dir2/b &&
90 echo "I changed this file" >dir1/a &&
91 test_when_finished "rm before" &&
92 cat >before <<-\EOF &&
93 SHA (3 entries, 2 subtrees)
94 SHA dir1/ (1 entries, 0 subtrees)
95 SHA dir2/ (1 entries, 0 subtrees)
96 EOF
97 cmp_cache_tree before &&
98 echo "I changed this file" >dir1/a &&
99 git add dir1/a &&
100 cat >expect <<-\EOF &&
101 invalid (2 subtrees)
102 invalid dir1/ (0 subtrees)
103 SHA dir2/ (1 entries, 0 subtrees)
105 cmp_cache_tree expect
108 test_expect_success 'update-index invalidates cache-tree' '
109 test_when_finished "git reset --hard; git read-tree HEAD" &&
110 echo "I changed this file" >foo &&
111 git update-index --add foo &&
112 test_invalid_cache_tree
115 test_expect_success 'write-tree establishes cache-tree' '
116 test-tool scrap-cache-tree &&
117 git write-tree &&
118 test_cache_tree
121 test_expect_success 'test-tool scrap-cache-tree works' '
122 git read-tree HEAD &&
123 test-tool scrap-cache-tree &&
124 test_no_cache_tree
127 test_expect_success 'second commit has cache-tree' '
128 test_commit bar &&
129 test_cache_tree
132 test_expect_success PERL 'commit --interactive gives cache-tree on partial commit' '
133 test_when_finished "git reset --hard" &&
134 cat <<-\EOT >foo.c &&
135 int foo()
137 return 42;
139 int bar()
141 return 42;
144 git add foo.c &&
145 test_invalid_cache_tree &&
146 git commit -m "add a file" &&
147 test_cache_tree &&
148 cat <<-\EOT >foo.c &&
149 int foo()
151 return 43;
153 int bar()
155 return 44;
158 test_write_lines p 1 "" s n y q |
159 git commit --interactive -m foo &&
160 cat <<-\EOF >expected.status &&
161 M foo.c
163 test_cache_tree expected.status
166 test_expect_success PERL 'commit -p with shrinking cache-tree' '
167 mkdir -p deep/very-long-subdir &&
168 echo content >deep/very-long-subdir/file &&
169 git add deep &&
170 git commit -m add &&
171 git rm -r deep &&
173 before=$(wc -c <.git/index) &&
174 git commit -m delete -p &&
175 after=$(wc -c <.git/index) &&
177 # double check that the index shrank
178 test $before -gt $after &&
180 # and that our index was not corrupted
181 git fsck
184 test_expect_success 'commit in child dir has cache-tree' '
185 mkdir dir &&
186 >dir/child.t &&
187 git add dir/child.t &&
188 git commit -m dir/child.t &&
189 test_cache_tree
192 test_expect_success 'reset --hard gives cache-tree' '
193 test-tool scrap-cache-tree &&
194 git reset --hard &&
195 test_cache_tree
198 test_expect_success 'reset --hard without index gives cache-tree' '
199 rm -f .git/index &&
200 git clean -fd &&
201 git reset --hard &&
202 test_cache_tree
205 test_expect_success 'checkout gives cache-tree' '
206 git tag current &&
207 git checkout HEAD^ &&
208 test_cache_tree
211 test_expect_success 'checkout -b gives cache-tree' '
212 git checkout current &&
213 git checkout -b prev HEAD^ &&
214 test_cache_tree
217 test_expect_success 'checkout -B gives cache-tree' '
218 git checkout current &&
219 git checkout -B prev HEAD^ &&
220 test_cache_tree
223 test_expect_success 'merge --ff-only maintains cache-tree' '
224 git checkout current &&
225 git checkout -b changes &&
226 test_commit llamas &&
227 test_commit pachyderm &&
228 test_cache_tree &&
229 git checkout current &&
230 test_cache_tree &&
231 git merge --ff-only changes &&
232 test_cache_tree
235 test_expect_success 'merge maintains cache-tree' '
236 git checkout current &&
237 git checkout -b changes2 &&
238 test_commit alpacas &&
239 test_cache_tree &&
240 git checkout current &&
241 test_commit struthio &&
242 test_cache_tree &&
243 git merge changes2 &&
244 test_cache_tree
247 test_expect_success 'partial commit gives cache-tree' '
248 git checkout -b partial no-children &&
249 test_commit one &&
250 test_commit two &&
251 echo "some change" >one.t &&
252 git add one.t &&
253 echo "some other change" >two.t &&
254 git commit two.t -m partial &&
255 cat <<-\EOF >expected.status &&
256 M one.t
258 test_cache_tree expected.status
261 test_expect_success 'no phantom error when switching trees' '
262 mkdir newdir &&
263 >newdir/one &&
264 git add newdir/one &&
265 git checkout 2>errors &&
266 test_must_be_empty errors
269 test_expect_success 'switching trees does not invalidate shared index' '
271 sane_unset GIT_TEST_SPLIT_INDEX &&
272 git update-index --split-index &&
273 >split &&
274 git add split &&
275 test-tool dump-split-index .git/index | grep -v ^own >before &&
276 git commit -m "as-is" &&
277 test-tool dump-split-index .git/index | grep -v ^own >after &&
278 test_cmp before after
282 test_done