cache-tree tests: remove unused $2 parameter
[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_rec () {
21         dir="$1${1:+/}" &&
22         # ls-files might have foo/bar, foo/bar/baz, and foo/bar/quux
23         # We want to count only foo because it's the only direct child
24         git ls-files >files &&
25         subtrees=$(grep / files|cut -d / -f 1|uniq) &&
26         subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 != "" {++c} END {print c}') &&
27         entries=$(wc -l <files) &&
28         printf "SHA $dir (%d entries, %d subtrees)\n" "$entries" "$subtree_count" &&
29         for subtree in $subtrees
30         do
31                 cd "$subtree"
32                 generate_expected_cache_tree_rec "$dir$subtree" || return 1
33                 cd ..
34         done
35 }
36
37 generate_expected_cache_tree () {
38         (
39                 generate_expected_cache_tree_rec
40         )
41 }
42
43 test_cache_tree () {
44         generate_expected_cache_tree >expect &&
45         cmp_cache_tree expect
46 }
47
48 test_invalid_cache_tree () {
49         printf "invalid                                  %s ()\n" "" "$@" >expect &&
50         test-tool dump-cache-tree |
51         sed -n -e "s/[0-9]* subtrees//" -e '/#(ref)/d' -e '/^invalid /p' >actual &&
52         test_cmp expect actual
53 }
54
55 test_no_cache_tree () {
56         >expect &&
57         cmp_cache_tree expect
58 }
59
60 test_expect_success 'initial commit has cache-tree' '
61         test_commit foo &&
62         test_cache_tree
63 '
64
65 test_expect_success 'read-tree HEAD establishes cache-tree' '
66         git read-tree HEAD &&
67         test_cache_tree
68 '
69
70 test_expect_success 'git-add invalidates cache-tree' '
71         test_when_finished "git reset --hard; git read-tree HEAD" &&
72         echo "I changed this file" >foo &&
73         git add foo &&
74         test_invalid_cache_tree
75 '
76
77 test_expect_success 'git-add in subdir invalidates cache-tree' '
78         test_when_finished "git reset --hard; git read-tree HEAD" &&
79         mkdir dirx &&
80         echo "I changed this file" >dirx/foo &&
81         git add dirx/foo &&
82         test_invalid_cache_tree
83 '
84
85 test_expect_success 'git-add in subdir does not invalidate sibling cache-tree' '
86         git tag no-children &&
87         test_when_finished "git reset --hard no-children; git read-tree HEAD" &&
88         mkdir dir1 dir2 &&
89         test_commit dir1/a &&
90         test_commit dir2/b &&
91         echo "I changed this file" >dir1/a &&
92         test_when_finished "rm before" &&
93         cat >before <<-\EOF &&
94         SHA  (3 entries, 2 subtrees)
95         SHA dir1/ (1 entries, 0 subtrees)
96         SHA dir2/ (1 entries, 0 subtrees)
97         EOF
98         cmp_cache_tree before &&
99         echo "I changed this file" >dir1/a &&
100         git add dir1/a &&
101         cat >expect <<-\EOF &&
102         invalid                                   (2 subtrees)
103         invalid                                  dir1/ (0 subtrees)
104         SHA dir2/ (1 entries, 0 subtrees)
105         EOF
106         cmp_cache_tree expect
107 '
108
109 test_expect_success 'update-index invalidates cache-tree' '
110         test_when_finished "git reset --hard; git read-tree HEAD" &&
111         echo "I changed this file" >foo &&
112         git update-index --add foo &&
113         test_invalid_cache_tree
114 '
115
116 test_expect_success 'write-tree establishes cache-tree' '
117         test-tool scrap-cache-tree &&
118         git write-tree &&
119         test_cache_tree
120 '
121
122 test_expect_success 'test-tool scrap-cache-tree works' '
123         git read-tree HEAD &&
124         test-tool scrap-cache-tree &&
125         test_no_cache_tree
126 '
127
128 test_expect_success 'second commit has cache-tree' '
129         test_commit bar &&
130         test_cache_tree
131 '
132
133 test_expect_success PERL 'commit --interactive gives cache-tree on partial commit' '
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
158         test_write_lines p 1 "" s n y q |
159         git commit --interactive -m foo &&
160         test_cache_tree
161 '
162
163 test_expect_success PERL 'commit -p with shrinking cache-tree' '
164         mkdir -p deep/very-long-subdir &&
165         echo content >deep/very-long-subdir/file &&
166         git add deep &&
167         git commit -m add &&
168         git rm -r deep &&
169
170         before=$(wc -c <.git/index) &&
171         git commit -m delete -p &&
172         after=$(wc -c <.git/index) &&
173
174         # double check that the index shrank
175         test $before -gt $after &&
176
177         # and that our index was not corrupted
178         git fsck
179 '
180
181 test_expect_success 'commit in child dir has cache-tree' '
182         mkdir dir &&
183         >dir/child.t &&
184         git add dir/child.t &&
185         git commit -m dir/child.t &&
186         test_cache_tree
187 '
188
189 test_expect_success 'reset --hard gives cache-tree' '
190         test-tool scrap-cache-tree &&
191         git reset --hard &&
192         test_cache_tree
193 '
194
195 test_expect_success 'reset --hard without index gives cache-tree' '
196         rm -f .git/index &&
197         git reset --hard &&
198         test_cache_tree
199 '
200
201 test_expect_success 'checkout gives cache-tree' '
202         git tag current &&
203         git checkout HEAD^ &&
204         test_cache_tree
205 '
206
207 test_expect_success 'checkout -b gives cache-tree' '
208         git checkout current &&
209         git checkout -b prev HEAD^ &&
210         test_cache_tree
211 '
212
213 test_expect_success 'checkout -B gives cache-tree' '
214         git checkout current &&
215         git checkout -B prev HEAD^ &&
216         test_cache_tree
217 '
218
219 test_expect_success 'merge --ff-only maintains cache-tree' '
220         git checkout current &&
221         git checkout -b changes &&
222         test_commit llamas &&
223         test_commit pachyderm &&
224         test_cache_tree &&
225         git checkout current &&
226         test_cache_tree &&
227         git merge --ff-only changes &&
228         test_cache_tree
229 '
230
231 test_expect_success 'merge maintains cache-tree' '
232         git checkout current &&
233         git checkout -b changes2 &&
234         test_commit alpacas &&
235         test_cache_tree &&
236         git checkout current &&
237         test_commit struthio &&
238         test_cache_tree &&
239         git merge changes2 &&
240         test_cache_tree
241 '
242
243 test_expect_success 'partial commit gives cache-tree' '
244         git checkout -b partial no-children &&
245         test_commit one &&
246         test_commit two &&
247         echo "some change" >one.t &&
248         git add one.t &&
249         echo "some other change" >two.t &&
250         git commit two.t -m partial &&
251         test_cache_tree
252 '
253
254 test_expect_success 'no phantom error when switching trees' '
255         mkdir newdir &&
256         >newdir/one &&
257         git add newdir/one &&
258         git checkout 2>errors &&
259         test_must_be_empty errors
260 '
261
262 test_expect_success 'switching trees does not invalidate shared index' '
263         (
264                 sane_unset GIT_TEST_SPLIT_INDEX &&
265                 git update-index --split-index &&
266                 >split &&
267                 git add split &&
268                 test-tool dump-split-index .git/index | grep -v ^own >before &&
269                 git commit -m "as-is" &&
270                 test-tool dump-split-index .git/index | grep -v ^own >after &&
271                 test_cmp before after
272         )
273 '
274
275 test_done