]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0090-cache-tree.sh
The third batch
[thirdparty/git.git] / t / t0090-cache-tree.sh
CommitLineData
4eb0346f
TR
1#!/bin/sh
2
3test_description="Test whether cache-tree is properly updated
4
5Tests whether various commands properly update and/or rewrite the
6cache-tree extension.
7"
9afa46d4
ÆAB
8
9TEST_PASSES_SANITIZE_LEAK=true
4eb0346f
TR
10 . ./test-lib.sh
11
12cmp_cache_tree () {
06ccb29e 13 test-tool dump-cache-tree | sed -e '/#(ref)/d' >actual &&
2ece6ad2 14 sed "s/$OID_REGEX/SHA/" <actual >filtered &&
3f96d75e
ÆAB
15 test_cmp "$1" filtered &&
16 rm filtered
4eb0346f
TR
17}
18
19# We don't bother with actually checking the SHA1:
06ccb29e 20# test-tool dump-cache-tree already verifies that all existing data is
4eb0346f 21# correct.
fa6edee7 22generate_expected_cache_tree () {
ef839700
ÆAB
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
9c4d6c02 29 do
ef839700
ÆAB
30 generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
31 done <subtrees
9c4d6c02
DT
32}
33
9c4d6c02 34test_cache_tree () {
ef839700
ÆAB
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
44 fi
4eb0346f
TR
45}
46
47test_invalid_cache_tree () {
59a8adb6 48 printf "invalid %s ()\n" "" "$@" >expect &&
06ccb29e 49 test-tool dump-cache-tree |
59a8adb6
DT
50 sed -n -e "s/[0-9]* subtrees//" -e '/#(ref)/d' -e '/^invalid /p' >actual &&
51 test_cmp expect actual
4eb0346f
TR
52}
53
54test_no_cache_tree () {
3f96d75e 55 >expect &&
4eb0346f
TR
56 cmp_cache_tree expect
57}
58
9c4d6c02 59test_expect_success 'initial commit has cache-tree' '
4eb0346f 60 test_commit foo &&
9c4d6c02 61 test_cache_tree
4eb0346f
TR
62'
63
64test_expect_success 'read-tree HEAD establishes cache-tree' '
65 git read-tree HEAD &&
9c4d6c02 66 test_cache_tree
4eb0346f
TR
67'
68
69test_expect_success 'git-add invalidates cache-tree' '
70 test_when_finished "git reset --hard; git read-tree HEAD" &&
aecf567c 71 echo "I changed this file" >foo &&
4eb0346f
TR
72 git add foo &&
73 test_invalid_cache_tree
74'
75
59a8adb6
DT
76test_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
82'
83
84test_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 &&
3f96d75e
ÆAB
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
9c4d6c02
DT
97 cmp_cache_tree before &&
98 echo "I changed this file" >dir1/a &&
59a8adb6 99 git add dir1/a &&
3f96d75e
ÆAB
100 cat >expect <<-\EOF &&
101 invalid (2 subtrees)
102 invalid dir1/ (0 subtrees)
103 SHA dir2/ (1 entries, 0 subtrees)
104 EOF
9c4d6c02 105 cmp_cache_tree expect
59a8adb6
DT
106'
107
4eb0346f
TR
108test_expect_success 'update-index invalidates cache-tree' '
109 test_when_finished "git reset --hard; git read-tree HEAD" &&
aecf567c 110 echo "I changed this file" >foo &&
4eb0346f
TR
111 git update-index --add foo &&
112 test_invalid_cache_tree
113'
114
115test_expect_success 'write-tree establishes cache-tree' '
ff5fb8b0 116 test-tool scrap-cache-tree &&
4eb0346f 117 git write-tree &&
9c4d6c02 118 test_cache_tree
4eb0346f
TR
119'
120
ff5fb8b0 121test_expect_success 'test-tool scrap-cache-tree works' '
4eb0346f 122 git read-tree HEAD &&
ff5fb8b0 123 test-tool scrap-cache-tree &&
4eb0346f
TR
124 test_no_cache_tree
125'
126
11c8a74a 127test_expect_success 'second commit has cache-tree' '
4eb0346f 128 test_commit bar &&
9c4d6c02
DT
129 test_cache_tree
130'
131
5a97639b 132test_expect_success PERL 'commit --interactive gives cache-tree on partial commit' '
ef839700 133 test_when_finished "git reset --hard" &&
9c4d6c02
DT
134 cat <<-\EOT >foo.c &&
135 int foo()
136 {
137 return 42;
138 }
139 int bar()
140 {
141 return 42;
142 }
143 EOT
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()
150 {
151 return 43;
152 }
153 int bar()
154 {
155 return 44;
156 }
157 EOT
0590ff26 158 test_write_lines p 1 "" s n y q |
9c4d6c02 159 git commit --interactive -m foo &&
ef839700
ÆAB
160 cat <<-\EOF >expected.status &&
161 M foo.c
162 EOF
163 test_cache_tree expected.status
9c4d6c02
DT
164'
165
6c003d6f 166test_expect_success PERL 'commit -p with shrinking cache-tree' '
b0d3c42e 167 mkdir -p deep/very-long-subdir &&
168 echo content >deep/very-long-subdir/file &&
6c003d6f
JK
169 git add deep &&
170 git commit -m add &&
171 git rm -r deep &&
172
173 before=$(wc -c <.git/index) &&
174 git commit -m delete -p &&
175 after=$(wc -c <.git/index) &&
176
177 # double check that the index shrank
178 test $before -gt $after &&
179
180 # and that our index was not corrupted
181 git fsck
182'
183
9c4d6c02
DT
184test_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
4eb0346f
TR
190'
191
6c52ec8a 192test_expect_success 'reset --hard gives cache-tree' '
ff5fb8b0 193 test-tool scrap-cache-tree &&
4eb0346f 194 git reset --hard &&
9c4d6c02 195 test_cache_tree
4eb0346f
TR
196'
197
6c52ec8a 198test_expect_success 'reset --hard without index gives cache-tree' '
4eb0346f 199 rm -f .git/index &&
f222bd34 200 git clean -fd &&
4eb0346f 201 git reset --hard &&
9c4d6c02 202 test_cache_tree
4eb0346f
TR
203'
204
aecf567c
DT
205test_expect_success 'checkout gives cache-tree' '
206 git tag current &&
4eb0346f 207 git checkout HEAD^ &&
9c4d6c02 208 test_cache_tree
4eb0346f
TR
209'
210
aecf567c
DT
211test_expect_success 'checkout -b gives cache-tree' '
212 git checkout current &&
213 git checkout -b prev HEAD^ &&
9c4d6c02 214 test_cache_tree
aecf567c
DT
215'
216
217test_expect_success 'checkout -B gives cache-tree' '
218 git checkout current &&
219 git checkout -B prev HEAD^ &&
9c4d6c02
DT
220 test_cache_tree
221'
222
52fca218
BD
223test_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
233'
234
235test_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
245'
246
9c4d6c02
DT
247test_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 &&
ef839700
ÆAB
255 cat <<-\EOF >expected.status &&
256 M one.t
257 EOF
258 test_cache_tree expected.status
aecf567c
DT
259'
260
4ed115e9
JH
261test_expect_success 'no phantom error when switching trees' '
262 mkdir newdir &&
263 >newdir/one &&
264 git add newdir/one &&
265 git checkout 2>errors &&
ec10b018 266 test_must_be_empty errors
4ed115e9
JH
267'
268
475a3445 269test_expect_success 'switching trees does not invalidate shared index' '
456d7cd3
SG
270 (
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
279 )
475a3445
DT
280'
281
4eb0346f 282test_done