read-tree: narrow scope of index expansion for '--prefix'
[alt-git.git] / t / t0090-cache-tree.sh
blob906757264844b67b0c72914b3aa2a6d02e17101c
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.
8 . ./test-lib.sh
10 cmp_cache_tree () {
11 test-tool dump-cache-tree | sed -e '/#(ref)/d' >actual &&
12 sed "s/$OID_REGEX/SHA/" <actual >filtered &&
13 test_cmp "$1" filtered &&
14 rm filtered
17 # We don't bother with actually checking the SHA1:
18 # test-tool dump-cache-tree already verifies that all existing data is
19 # correct.
20 generate_expected_cache_tree () {
21 pathspec="$1" &&
22 dir="$2${2:+/}" &&
23 git ls-tree --name-only HEAD -- "$pathspec" >files &&
24 git ls-tree --name-only -d HEAD -- "$pathspec" >subtrees &&
25 printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) &&
26 while read subtree
28 generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
29 done <subtrees
32 test_cache_tree () {
33 generate_expected_cache_tree "." >expect &&
34 cmp_cache_tree expect &&
35 rm expect actual files subtrees &&
36 git status --porcelain -- ':!status' ':!expected.status' >status &&
37 if test -n "$1"
38 then
39 test_cmp "$1" status
40 else
41 test_must_be_empty status
45 test_invalid_cache_tree () {
46 printf "invalid %s ()\n" "" "$@" >expect &&
47 test-tool dump-cache-tree |
48 sed -n -e "s/[0-9]* subtrees//" -e '/#(ref)/d' -e '/^invalid /p' >actual &&
49 test_cmp expect actual
52 test_no_cache_tree () {
53 >expect &&
54 cmp_cache_tree expect
57 test_expect_success 'initial commit has cache-tree' '
58 test_commit foo &&
59 test_cache_tree
62 test_expect_success 'read-tree HEAD establishes cache-tree' '
63 git read-tree HEAD &&
64 test_cache_tree
67 test_expect_success 'git-add invalidates cache-tree' '
68 test_when_finished "git reset --hard; git read-tree HEAD" &&
69 echo "I changed this file" >foo &&
70 git add foo &&
71 test_invalid_cache_tree
74 test_expect_success 'git-add in subdir invalidates cache-tree' '
75 test_when_finished "git reset --hard; git read-tree HEAD" &&
76 mkdir dirx &&
77 echo "I changed this file" >dirx/foo &&
78 git add dirx/foo &&
79 test_invalid_cache_tree
82 test_expect_success 'git-add in subdir does not invalidate sibling cache-tree' '
83 git tag no-children &&
84 test_when_finished "git reset --hard no-children; git read-tree HEAD" &&
85 mkdir dir1 dir2 &&
86 test_commit dir1/a &&
87 test_commit dir2/b &&
88 echo "I changed this file" >dir1/a &&
89 test_when_finished "rm before" &&
90 cat >before <<-\EOF &&
91 SHA (3 entries, 2 subtrees)
92 SHA dir1/ (1 entries, 0 subtrees)
93 SHA dir2/ (1 entries, 0 subtrees)
94 EOF
95 cmp_cache_tree before &&
96 echo "I changed this file" >dir1/a &&
97 git add dir1/a &&
98 cat >expect <<-\EOF &&
99 invalid (2 subtrees)
100 invalid dir1/ (0 subtrees)
101 SHA dir2/ (1 entries, 0 subtrees)
103 cmp_cache_tree expect
106 test_expect_success 'update-index invalidates cache-tree' '
107 test_when_finished "git reset --hard; git read-tree HEAD" &&
108 echo "I changed this file" >foo &&
109 git update-index --add foo &&
110 test_invalid_cache_tree
113 test_expect_success 'write-tree establishes cache-tree' '
114 test-tool scrap-cache-tree &&
115 git write-tree &&
116 test_cache_tree
119 test_expect_success 'test-tool scrap-cache-tree works' '
120 git read-tree HEAD &&
121 test-tool scrap-cache-tree &&
122 test_no_cache_tree
125 test_expect_success 'second commit has cache-tree' '
126 test_commit bar &&
127 test_cache_tree
130 test_expect_success PERL 'commit --interactive gives cache-tree on partial commit' '
131 test_when_finished "git reset --hard" &&
132 cat <<-\EOT >foo.c &&
133 int foo()
135 return 42;
137 int bar()
139 return 42;
142 git add foo.c &&
143 test_invalid_cache_tree &&
144 git commit -m "add a file" &&
145 test_cache_tree &&
146 cat <<-\EOT >foo.c &&
147 int foo()
149 return 43;
151 int bar()
153 return 44;
156 test_write_lines p 1 "" s n y q |
157 git commit --interactive -m foo &&
158 cat <<-\EOF >expected.status &&
159 M foo.c
161 test_cache_tree expected.status
164 test_expect_success PERL 'commit -p with shrinking cache-tree' '
165 mkdir -p deep/very-long-subdir &&
166 echo content >deep/very-long-subdir/file &&
167 git add deep &&
168 git commit -m add &&
169 git rm -r deep &&
171 before=$(wc -c <.git/index) &&
172 git commit -m delete -p &&
173 after=$(wc -c <.git/index) &&
175 # double check that the index shrank
176 test $before -gt $after &&
178 # and that our index was not corrupted
179 git fsck
182 test_expect_success 'commit in child dir has cache-tree' '
183 mkdir dir &&
184 >dir/child.t &&
185 git add dir/child.t &&
186 git commit -m dir/child.t &&
187 test_cache_tree
190 test_expect_success 'reset --hard gives cache-tree' '
191 test-tool scrap-cache-tree &&
192 git reset --hard &&
193 test_cache_tree
196 test_expect_success 'reset --hard without index gives cache-tree' '
197 rm -f .git/index &&
198 git clean -fd &&
199 git reset --hard &&
200 test_cache_tree
203 test_expect_success 'checkout gives cache-tree' '
204 git tag current &&
205 git checkout HEAD^ &&
206 test_cache_tree
209 test_expect_success 'checkout -b gives cache-tree' '
210 git checkout current &&
211 git checkout -b prev HEAD^ &&
212 test_cache_tree
215 test_expect_success 'checkout -B gives cache-tree' '
216 git checkout current &&
217 git checkout -B prev HEAD^ &&
218 test_cache_tree
221 test_expect_success 'merge --ff-only maintains cache-tree' '
222 git checkout current &&
223 git checkout -b changes &&
224 test_commit llamas &&
225 test_commit pachyderm &&
226 test_cache_tree &&
227 git checkout current &&
228 test_cache_tree &&
229 git merge --ff-only changes &&
230 test_cache_tree
233 test_expect_success 'merge maintains cache-tree' '
234 git checkout current &&
235 git checkout -b changes2 &&
236 test_commit alpacas &&
237 test_cache_tree &&
238 git checkout current &&
239 test_commit struthio &&
240 test_cache_tree &&
241 git merge changes2 &&
242 test_cache_tree
245 test_expect_success 'partial commit gives cache-tree' '
246 git checkout -b partial no-children &&
247 test_commit one &&
248 test_commit two &&
249 echo "some change" >one.t &&
250 git add one.t &&
251 echo "some other change" >two.t &&
252 git commit two.t -m partial &&
253 cat <<-\EOF >expected.status &&
254 M one.t
256 test_cache_tree expected.status
259 test_expect_success 'no phantom error when switching trees' '
260 mkdir newdir &&
261 >newdir/one &&
262 git add newdir/one &&
263 git checkout 2>errors &&
264 test_must_be_empty errors
267 test_expect_success 'switching trees does not invalidate shared index' '
269 sane_unset GIT_TEST_SPLIT_INDEX &&
270 git update-index --split-index &&
271 >split &&
272 git add split &&
273 test-tool dump-split-index .git/index | grep -v ^own >before &&
274 git commit -m "as-is" &&
275 test-tool dump-split-index .git/index | grep -v ^own >after &&
276 test_cmp before after
280 test_done