git-svn tests: rewrite brittle tests to use "--[no-]merges".
[git] / t / t0090-cache-tree.sh
1 #!/bin/sh
2
3 test_description="Test whether cache-tree is properly updated
4
5 Tests whether various commands properly update and/or rewrite the
6 cache-tree extension.
7 "
8  . ./test-lib.sh
9
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
15 }
16
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
27         do
28                 generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
29         done <subtrees
30 }
31
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
42         fi
43 }
44
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
50 }
51
52 test_no_cache_tree () {
53         >expect &&
54         cmp_cache_tree expect
55 }
56
57 test_expect_success 'initial commit has cache-tree' '
58         test_commit foo &&
59         test_cache_tree
60 '
61
62 test_expect_success 'read-tree HEAD establishes cache-tree' '
63         git read-tree HEAD &&
64         test_cache_tree
65 '
66
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
72 '
73
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
80 '
81
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)
102         EOF
103         cmp_cache_tree expect
104 '
105
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
111 '
112
113 test_expect_success 'write-tree establishes cache-tree' '
114         test-tool scrap-cache-tree &&
115         git write-tree &&
116         test_cache_tree
117 '
118
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
123 '
124
125 test_expect_success 'second commit has cache-tree' '
126         test_commit bar &&
127         test_cache_tree
128 '
129
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()
134         {
135                 return 42;
136         }
137         int bar()
138         {
139                 return 42;
140         }
141         EOT
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()
148         {
149                 return 43;
150         }
151         int bar()
152         {
153                 return 44;
154         }
155         EOT
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
160         EOF
161         test_cache_tree expected.status
162 '
163
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 &&
170
171         before=$(wc -c <.git/index) &&
172         git commit -m delete -p &&
173         after=$(wc -c <.git/index) &&
174
175         # double check that the index shrank
176         test $before -gt $after &&
177
178         # and that our index was not corrupted
179         git fsck
180 '
181
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
188 '
189
190 test_expect_success 'reset --hard gives cache-tree' '
191         test-tool scrap-cache-tree &&
192         git reset --hard &&
193         test_cache_tree
194 '
195
196 test_expect_success 'reset --hard without index gives cache-tree' '
197         rm -f .git/index &&
198         git reset --hard &&
199         test_cache_tree
200 '
201
202 test_expect_success 'checkout gives cache-tree' '
203         git tag current &&
204         git checkout HEAD^ &&
205         test_cache_tree
206 '
207
208 test_expect_success 'checkout -b gives cache-tree' '
209         git checkout current &&
210         git checkout -b prev HEAD^ &&
211         test_cache_tree
212 '
213
214 test_expect_success 'checkout -B gives cache-tree' '
215         git checkout current &&
216         git checkout -B prev HEAD^ &&
217         test_cache_tree
218 '
219
220 test_expect_success 'merge --ff-only maintains cache-tree' '
221         git checkout current &&
222         git checkout -b changes &&
223         test_commit llamas &&
224         test_commit pachyderm &&
225         test_cache_tree &&
226         git checkout current &&
227         test_cache_tree &&
228         git merge --ff-only changes &&
229         test_cache_tree
230 '
231
232 test_expect_success 'merge maintains cache-tree' '
233         git checkout current &&
234         git checkout -b changes2 &&
235         test_commit alpacas &&
236         test_cache_tree &&
237         git checkout current &&
238         test_commit struthio &&
239         test_cache_tree &&
240         git merge changes2 &&
241         test_cache_tree
242 '
243
244 test_expect_success 'partial commit gives cache-tree' '
245         git checkout -b partial no-children &&
246         test_commit one &&
247         test_commit two &&
248         echo "some change" >one.t &&
249         git add one.t &&
250         echo "some other change" >two.t &&
251         git commit two.t -m partial &&
252         cat <<-\EOF >expected.status &&
253         M  one.t
254         EOF
255         test_cache_tree expected.status
256 '
257
258 test_expect_success 'no phantom error when switching trees' '
259         mkdir newdir &&
260         >newdir/one &&
261         git add newdir/one &&
262         git checkout 2>errors &&
263         test_must_be_empty errors
264 '
265
266 test_expect_success 'switching trees does not invalidate shared index' '
267         (
268                 sane_unset GIT_TEST_SPLIT_INDEX &&
269                 git update-index --split-index &&
270                 >split &&
271                 git add split &&
272                 test-tool dump-split-index .git/index | grep -v ^own >before &&
273                 git commit -m "as-is" &&
274                 test-tool dump-split-index .git/index | grep -v ^own >after &&
275                 test_cmp before after
276         )
277 '
278
279 test_done