blob: 906757264844b67b0c72914b3aa2a6d02e17101c [file] [log] [blame]
Thomas Rast4eb03462011-12-06 18:43:36 +01001#!/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"
8 . ./test-lib.sh
9
10cmp_cache_tree () {
Nguyễn Thái Ngọc Duy06ccb292018-03-24 08:44:39 +010011 test-tool dump-cache-tree | sed -e '/#(ref)/d' >actual &&
brian m. carlson2ece6ad2018-05-13 02:24:15 +000012 sed "s/$OID_REGEX/SHA/" <actual >filtered &&
Ævar Arnfjörð Bjarmason3f96d752021-01-23 14:00:37 +010013 test_cmp "$1" filtered &&
14 rm filtered
Thomas Rast4eb03462011-12-06 18:43:36 +010015}
16
17# We don't bother with actually checking the SHA1:
Nguyễn Thái Ngọc Duy06ccb292018-03-24 08:44:39 +010018# test-tool dump-cache-tree already verifies that all existing data is
Thomas Rast4eb03462011-12-06 18:43:36 +010019# correct.
Ævar Arnfjörð Bjarmasonfa6edee2021-01-23 14:00:39 +010020generate_expected_cache_tree () {
Ævar Arnfjörð Bjarmasonef839702021-01-23 14:00:40 +010021 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
David Turner9c4d6c02014-07-13 13:28:19 -070027 do
Ævar Arnfjörð Bjarmasonef839702021-01-23 14:00:40 +010028 generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
29 done <subtrees
David Turner9c4d6c02014-07-13 13:28:19 -070030}
31
David Turner9c4d6c02014-07-13 13:28:19 -070032test_cache_tree () {
Ævar Arnfjörð Bjarmasonef839702021-01-23 14:00:40 +010033 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
Thomas Rast4eb03462011-12-06 18:43:36 +010043}
44
45test_invalid_cache_tree () {
David Turner59a8adb2014-07-10 17:31:25 -070046 printf "invalid %s ()\n" "" "$@" >expect &&
Nguyễn Thái Ngọc Duy06ccb292018-03-24 08:44:39 +010047 test-tool dump-cache-tree |
David Turner59a8adb2014-07-10 17:31:25 -070048 sed -n -e "s/[0-9]* subtrees//" -e '/#(ref)/d' -e '/^invalid /p' >actual &&
49 test_cmp expect actual
Thomas Rast4eb03462011-12-06 18:43:36 +010050}
51
52test_no_cache_tree () {
Ævar Arnfjörð Bjarmason3f96d752021-01-23 14:00:37 +010053 >expect &&
Thomas Rast4eb03462011-12-06 18:43:36 +010054 cmp_cache_tree expect
55}
56
David Turner9c4d6c02014-07-13 13:28:19 -070057test_expect_success 'initial commit has cache-tree' '
Thomas Rast4eb03462011-12-06 18:43:36 +010058 test_commit foo &&
David Turner9c4d6c02014-07-13 13:28:19 -070059 test_cache_tree
Thomas Rast4eb03462011-12-06 18:43:36 +010060'
61
62test_expect_success 'read-tree HEAD establishes cache-tree' '
63 git read-tree HEAD &&
David Turner9c4d6c02014-07-13 13:28:19 -070064 test_cache_tree
Thomas Rast4eb03462011-12-06 18:43:36 +010065'
66
67test_expect_success 'git-add invalidates cache-tree' '
68 test_when_finished "git reset --hard; git read-tree HEAD" &&
David Turneraecf5672014-07-05 21:06:56 -070069 echo "I changed this file" >foo &&
Thomas Rast4eb03462011-12-06 18:43:36 +010070 git add foo &&
71 test_invalid_cache_tree
72'
73
David Turner59a8adb2014-07-10 17:31:25 -070074test_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
82test_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 &&
Ævar Arnfjörð Bjarmason3f96d752021-01-23 14:00:37 +010089 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
David Turner9c4d6c02014-07-13 13:28:19 -070095 cmp_cache_tree before &&
96 echo "I changed this file" >dir1/a &&
David Turner59a8adb2014-07-10 17:31:25 -070097 git add dir1/a &&
Ævar Arnfjörð Bjarmason3f96d752021-01-23 14:00:37 +010098 cat >expect <<-\EOF &&
99 invalid (2 subtrees)
100 invalid dir1/ (0 subtrees)
101 SHA dir2/ (1 entries, 0 subtrees)
102 EOF
David Turner9c4d6c02014-07-13 13:28:19 -0700103 cmp_cache_tree expect
David Turner59a8adb2014-07-10 17:31:25 -0700104'
105
Thomas Rast4eb03462011-12-06 18:43:36 +0100106test_expect_success 'update-index invalidates cache-tree' '
107 test_when_finished "git reset --hard; git read-tree HEAD" &&
David Turneraecf5672014-07-05 21:06:56 -0700108 echo "I changed this file" >foo &&
Thomas Rast4eb03462011-12-06 18:43:36 +0100109 git update-index --add foo &&
110 test_invalid_cache_tree
111'
112
113test_expect_success 'write-tree establishes cache-tree' '
Nguyễn Thái Ngọc Duyff5fb8b2018-03-24 08:44:56 +0100114 test-tool scrap-cache-tree &&
Thomas Rast4eb03462011-12-06 18:43:36 +0100115 git write-tree &&
David Turner9c4d6c02014-07-13 13:28:19 -0700116 test_cache_tree
Thomas Rast4eb03462011-12-06 18:43:36 +0100117'
118
Nguyễn Thái Ngọc Duyff5fb8b2018-03-24 08:44:56 +0100119test_expect_success 'test-tool scrap-cache-tree works' '
Thomas Rast4eb03462011-12-06 18:43:36 +0100120 git read-tree HEAD &&
Nguyễn Thái Ngọc Duyff5fb8b2018-03-24 08:44:56 +0100121 test-tool scrap-cache-tree &&
Thomas Rast4eb03462011-12-06 18:43:36 +0100122 test_no_cache_tree
123'
124
Thomas Rast11c8a742011-12-06 18:43:38 +0100125test_expect_success 'second commit has cache-tree' '
Thomas Rast4eb03462011-12-06 18:43:36 +0100126 test_commit bar &&
David Turner9c4d6c02014-07-13 13:28:19 -0700127 test_cache_tree
128'
129
Jeff King5a976392014-11-18 12:22:31 -0500130test_expect_success PERL 'commit --interactive gives cache-tree on partial commit' '
Ævar Arnfjörð Bjarmasonef839702021-01-23 14:00:40 +0100131 test_when_finished "git reset --hard" &&
David Turner9c4d6c02014-07-13 13:28:19 -0700132 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
Eric Sunshine0590ff22018-07-01 20:23:42 -0400156 test_write_lines p 1 "" s n y q |
David Turner9c4d6c02014-07-13 13:28:19 -0700157 git commit --interactive -m foo &&
Ævar Arnfjörð Bjarmasonef839702021-01-23 14:00:40 +0100158 cat <<-\EOF >expected.status &&
159 M foo.c
160 EOF
161 test_cache_tree expected.status
David Turner9c4d6c02014-07-13 13:28:19 -0700162'
163
Jeff King6c003d62018-09-04 19:36:43 -0400164test_expect_success PERL 'commit -p with shrinking cache-tree' '
brian m. carlsonb0d3c422019-06-28 22:59:25 +0000165 mkdir -p deep/very-long-subdir &&
166 echo content >deep/very-long-subdir/file &&
Jeff King6c003d62018-09-04 19:36:43 -0400167 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
David Turner9c4d6c02014-07-13 13:28:19 -0700182test_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
Thomas Rast4eb03462011-12-06 18:43:36 +0100188'
189
Thomas Rast6c52ec82011-12-06 18:43:39 +0100190test_expect_success 'reset --hard gives cache-tree' '
Nguyễn Thái Ngọc Duyff5fb8b2018-03-24 08:44:56 +0100191 test-tool scrap-cache-tree &&
Thomas Rast4eb03462011-12-06 18:43:36 +0100192 git reset --hard &&
David Turner9c4d6c02014-07-13 13:28:19 -0700193 test_cache_tree
Thomas Rast4eb03462011-12-06 18:43:36 +0100194'
195
Thomas Rast6c52ec82011-12-06 18:43:39 +0100196test_expect_success 'reset --hard without index gives cache-tree' '
Thomas Rast4eb03462011-12-06 18:43:36 +0100197 rm -f .git/index &&
Elijah Newrenf222bd32021-09-11 17:08:42 +0000198 git clean -fd &&
Thomas Rast4eb03462011-12-06 18:43:36 +0100199 git reset --hard &&
David Turner9c4d6c02014-07-13 13:28:19 -0700200 test_cache_tree
Thomas Rast4eb03462011-12-06 18:43:36 +0100201'
202
David Turneraecf5672014-07-05 21:06:56 -0700203test_expect_success 'checkout gives cache-tree' '
204 git tag current &&
Thomas Rast4eb03462011-12-06 18:43:36 +0100205 git checkout HEAD^ &&
David Turner9c4d6c02014-07-13 13:28:19 -0700206 test_cache_tree
Thomas Rast4eb03462011-12-06 18:43:36 +0100207'
208
David Turneraecf5672014-07-05 21:06:56 -0700209test_expect_success 'checkout -b gives cache-tree' '
210 git checkout current &&
211 git checkout -b prev HEAD^ &&
David Turner9c4d6c02014-07-13 13:28:19 -0700212 test_cache_tree
David Turneraecf5672014-07-05 21:06:56 -0700213'
214
215test_expect_success 'checkout -B gives cache-tree' '
216 git checkout current &&
217 git checkout -B prev HEAD^ &&
David Turner9c4d6c02014-07-13 13:28:19 -0700218 test_cache_tree
219'
220
Brian Degenhardt52fca212015-07-28 15:30:40 -0400221test_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
231'
232
233test_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
243'
244
David Turner9c4d6c02014-07-13 13:28:19 -0700245test_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 &&
Ævar Arnfjörð Bjarmasonef839702021-01-23 14:00:40 +0100253 cat <<-\EOF >expected.status &&
254 M one.t
255 EOF
256 test_cache_tree expected.status
David Turneraecf5672014-07-05 21:06:56 -0700257'
258
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700259test_expect_success 'no phantom error when switching trees' '
260 mkdir newdir &&
261 >newdir/one &&
262 git add newdir/one &&
263 git checkout 2>errors &&
SZEDER Gáborec10b012018-08-19 23:57:22 +0200264 test_must_be_empty errors
Junio C Hamano4ed115e2014-09-02 14:16:20 -0700265'
266
David Turner475a3442015-08-27 13:07:54 -0400267test_expect_success 'switching trees does not invalidate shared index' '
SZEDER Gábor456d7cd2018-09-06 04:48:07 +0200268 (
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
277 )
David Turner475a3442015-08-27 13:07:54 -0400278'
279
Thomas Rast4eb03462011-12-06 18:43:36 +0100280test_done