]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0601-reffiles-pack-refs.sh
The third batch
[thirdparty/git.git] / t / t0601-reffiles-pack-refs.sh
CommitLineData
919a3c98
CC
1#!/bin/sh
2#
3# Copyright (c) 2005 Amos Waterland
4# Copyright (c) 2006 Christian Couder
5#
6
7test_description='git pack-refs should not change the branch semantic
8
9This test runs git pack-refs and git show-ref and checks that the branch
10semantic is still the same.
11'
ca13c3e9 12
d6c6b108 13GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7 14export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
ca13c3e9
PS
15GIT_TEST_DEFAULT_REF_FORMAT=files
16export GIT_TEST_DEFAULT_REF_FORMAT
334afbc7 17
b2e5d75d 18TEST_PASSES_SANITIZE_LEAK=true
919a3c98
CC
19. ./test-lib.sh
20
cbe73331
JK
21test_expect_success 'enable reflogs' '
22 git config core.logallrefupdates true
23'
3b463c3f 24
3c2f5d26
JC
25test_expect_success 'prepare a trivial repository' '
26 echo Hello > A &&
27 git update-index --add A &&
28 git commit -m "Initial commit." &&
29 HEAD=$(git rev-parse --verify HEAD)
30'
919a3c98 31
ed12124d
PS
32test_expect_success 'pack-refs --prune --all' '
33 test_path_is_missing .git/packed-refs &&
34 git pack-refs --no-prune --all &&
35 test_path_is_file .git/packed-refs &&
36 N=$(find .git/refs -type f | wc -l) &&
04530307 37 test "$N" != 0 &&
ed12124d
PS
38
39 git pack-refs --prune --all &&
40 test_path_is_file .git/packed-refs &&
41 N=$(find .git/refs -type f) &&
04530307
JC
42 test -z "$N"
43'
44
919a3c98
CC
45SHA1=
46
3c2f5d26
JC
47test_expect_success 'see if git show-ref works as expected' '
48 git branch a &&
49 SHA1=$(cat .git/refs/heads/a) &&
50 echo "$SHA1 refs/heads/a" >expect &&
51 git show-ref a >result &&
52 test_cmp expect result
53'
54
55test_expect_success 'see if a branch still exists when packed' '
56 git branch b &&
57 git pack-refs --all &&
58 rm -f .git/refs/heads/b &&
59 echo "$SHA1 refs/heads/b" >expect &&
60 git show-ref b >result &&
61 test_cmp expect result
62'
919a3c98 63
41ac414e 64test_expect_success 'git branch c/d should barf if branch c exists' '
3c2f5d26
JC
65 git branch c &&
66 git pack-refs --all &&
67 rm -f .git/refs/heads/c &&
68 test_must_fail git branch c/d
41ac414e 69'
919a3c98 70
3c2f5d26
JC
71test_expect_success 'see if a branch still exists after git pack-refs --prune' '
72 git branch e &&
73 git pack-refs --all --prune &&
74 echo "$SHA1 refs/heads/e" >expect &&
75 git show-ref e >result &&
76 test_cmp expect result
77'
919a3c98 78
41ac414e 79test_expect_success 'see if git pack-refs --prune remove ref files' '
3c2f5d26
JC
80 git branch f &&
81 git pack-refs --all --prune &&
82 ! test -f .git/refs/heads/f
41ac414e 83'
919a3c98 84
be7c6d46 85test_expect_success 'see if git pack-refs --prune removes empty dirs' '
3c2f5d26
JC
86 git branch r/s/t &&
87 git pack-refs --all --prune &&
88 ! test -e .git/refs/heads/r
be7c6d46
GP
89'
90
3c2f5d26
JC
91test_expect_success 'git branch g should work when git branch g/h has been deleted' '
92 git branch g/h &&
93 git pack-refs --all --prune &&
94 git branch -d g/h &&
95 git branch g &&
96 git pack-refs --all &&
97 git branch -d g
98'
919a3c98 99
41ac414e 100test_expect_success 'git branch i/j/k should barf if branch i exists' '
3c2f5d26
JC
101 git branch i &&
102 git pack-refs --all --prune &&
103 test_must_fail git branch i/j/k
104'
105
106test_expect_success 'test git branch k after branch k/l/m and k/lm have been deleted' '
107 git branch k/l &&
108 git branch k/lm &&
109 git branch -d k/l &&
110 git branch k/l/m &&
111 git branch -d k/l/m &&
112 git branch -d k/lm &&
113 git branch k
114'
115
116test_expect_success 'test git branch n after some branch deletion and pruning' '
117 git branch n/o &&
118 git branch n/op &&
119 git branch -d n/o &&
120 git branch n/o/p &&
121 git branch -d n/op &&
122 git pack-refs --all --prune &&
123 git branch -d n/o/p &&
124 git branch n
125'
14c8a681 126
826ae79f
JC
127test_expect_success 'test excluded refs are not packed' '
128 git branch dont_pack1 &&
129 git branch dont_pack2 &&
130 git branch pack_this &&
131 git pack-refs --all --exclude "refs/heads/dont_pack*" &&
132 test -f .git/refs/heads/dont_pack1 &&
133 test -f .git/refs/heads/dont_pack2 &&
134 ! test -f .git/refs/heads/pack_this'
135
136test_expect_success 'test --no-exclude refs clears excluded refs' '
137 git branch dont_pack3 &&
138 git branch dont_pack4 &&
139 git pack-refs --all --exclude "refs/heads/dont_pack*" --no-exclude &&
140 ! test -f .git/refs/heads/dont_pack3 &&
141 ! test -f .git/refs/heads/dont_pack4'
142
4fe42f32
JC
143test_expect_success 'test only included refs are packed' '
144 git branch pack_this1 &&
145 git branch pack_this2 &&
146 git tag dont_pack5 &&
147 git pack-refs --include "refs/heads/pack_this*" &&
148 test -f .git/refs/tags/dont_pack5 &&
149 ! test -f .git/refs/heads/pack_this1 &&
150 ! test -f .git/refs/heads/pack_this2'
151
152test_expect_success 'test --no-include refs clears included refs' '
153 git branch pack1 &&
154 git branch pack2 &&
155 git pack-refs --include "refs/heads/pack*" --no-include &&
156 test -f .git/refs/heads/pack1 &&
157 test -f .git/refs/heads/pack2'
158
159test_expect_success 'test --exclude takes precedence over --include' '
160 git branch dont_pack5 &&
161 git pack-refs --include "refs/heads/pack*" --exclude "refs/heads/pack*" &&
162 test -f .git/refs/heads/dont_pack5'
163
6dcffc68
PS
164test_expect_success '--auto packs and prunes refs as usual' '
165 git branch auto &&
166 test_path_is_file .git/refs/heads/auto &&
167 git pack-refs --auto --all &&
168 test_path_is_missing .git/refs/heads/auto
169'
170
3c2f5d26
JC
171test_expect_success 'see if up-to-date packed refs are preserved' '
172 git branch q &&
173 git pack-refs --all --prune &&
174 git update-ref refs/heads/q refs/heads/q &&
175 ! test -f .git/refs/heads/q
176'
5bdd8d4a 177
1b555932 178test_expect_success 'pack, prune and repack' '
0cb0e143 179 git tag foo &&
5be60078
JH
180 git pack-refs --all --prune &&
181 git show-ref >all-of-them &&
182 git pack-refs &&
183 git show-ref >again &&
4fdf71be 184 test_cmp all-of-them again
1b555932
LT
185'
186
0a0b8d15
MH
187test_expect_success 'explicit pack-refs with dangling packed reference' '
188 git commit --allow-empty -m "soon to be garbage-collected" &&
189 git pack-refs --all &&
190 git reset --hard HEAD^ &&
191 git reflog expire --expire=all --all &&
192 git prune --expire=all &&
193 git pack-refs --all 2>result &&
ec21ac8c 194 test_must_be_empty result
0a0b8d15
MH
195'
196
197test_expect_success 'delete ref with dangling packed version' '
198 git checkout -b lamb &&
199 git commit --allow-empty -m "future garbage" &&
200 git pack-refs --all &&
201 git reset --hard HEAD^ &&
d6c6b108 202 git checkout main &&
0a0b8d15
MH
203 git reflog expire --expire=all --all &&
204 git prune --expire=all &&
205 git branch -d lamb 2>result &&
ec21ac8c 206 test_must_be_empty result
0a0b8d15
MH
207'
208
ab292bc4 209test_expect_success 'delete ref while another dangling packed ref' '
0a0b8d15
MH
210 git branch lamb &&
211 git commit --allow-empty -m "future garbage" &&
212 git pack-refs --all &&
213 git reset --hard HEAD^ &&
214 git reflog expire --expire=all --all &&
215 git prune --expire=all &&
216 git branch -d lamb 2>result &&
ec21ac8c 217 test_must_be_empty result
0a0b8d15
MH
218'
219
afd11d3e
JK
220test_expect_success 'pack ref directly below refs/' '
221 git update-ref refs/top HEAD &&
222 git pack-refs --all --prune &&
223 grep refs/top .git/packed-refs &&
224 test_path_is_missing .git/refs/top
225'
226
ce414b33
DT
227test_expect_success 'do not pack ref in refs/bisect' '
228 git update-ref refs/bisect/local HEAD &&
229 git pack-refs --all --prune &&
230 ! grep refs/bisect/local .git/packed-refs >/dev/null &&
231 test_path_is_file .git/refs/bisect/local
232'
233
cbe73331
JK
234test_expect_success 'disable reflogs' '
235 git config core.logallrefupdates false &&
236 rm -rf .git/logs
237'
238
239test_expect_success 'create packed foo/bar/baz branch' '
240 git branch foo/bar/baz &&
241 git pack-refs --all --prune &&
242 test_path_is_missing .git/refs/heads/foo/bar/baz &&
d0ab0584 243 test_must_fail git reflog exists refs/heads/foo/bar/baz
cbe73331
JK
244'
245
246test_expect_success 'notice d/f conflict with existing directory' '
247 test_must_fail git branch foo &&
248 test_must_fail git branch foo/bar
249'
250
251test_expect_success 'existing directory reports concrete ref' '
252 test_must_fail git branch foo 2>stderr &&
6789275d 253 test_grep refs/heads/foo/bar/baz stderr
cbe73331
JK
254'
255
256test_expect_success 'notice d/f conflict with existing ref' '
257 test_must_fail git branch foo/bar/baz/extra &&
258 test_must_fail git branch foo/bar/baz/lots/of/extra/components
259'
260
9308b7f3 261test_expect_success 'reject packed-refs with unterminated line' '
02a1a420
MH
262 cp .git/packed-refs .git/packed-refs.bak &&
263 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
264 printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs &&
265 echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err &&
266 test_must_fail git for-each-ref >out 2>err &&
267 test_cmp expected_err err
268'
269
9308b7f3 270test_expect_success 'reject packed-refs containing junk' '
02a1a420
MH
271 cp .git/packed-refs .git/packed-refs.bak &&
272 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
273 printf "%s\n" "bogus content" >>.git/packed-refs &&
274 echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err &&
275 test_must_fail git for-each-ref >out 2>err &&
276 test_cmp expected_err err
277'
278
9308b7f3 279test_expect_success 'reject packed-refs with a short SHA-1' '
02a1a420
MH
280 cp .git/packed-refs .git/packed-refs.bak &&
281 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
282 printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs &&
283 printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err &&
284 test_must_fail git for-each-ref >out 2>err &&
285 test_cmp expected_err err
286'
287
f4ab4f3a
MH
288test_expect_success 'timeout if packed-refs.lock exists' '
289 LOCK=.git/packed-refs.lock &&
290 >"$LOCK" &&
291 test_when_finished "rm -f $LOCK" &&
292 test_must_fail git pack-refs --all --prune
293'
294
295test_expect_success 'retry acquiring packed-refs.lock' '
296 LOCK=.git/packed-refs.lock &&
297 >"$LOCK" &&
3ea67379 298 test_when_finished "wait && rm -f $LOCK" &&
f4ab4f3a 299 {
3ea67379 300 ( sleep 1 && rm -f $LOCK ) &
f4ab4f3a
MH
301 } &&
302 git -c core.packedrefstimeout=3000 pack-refs --all --prune
303'
304
198b808e
MH
305test_expect_success SYMLINKS 'pack symlinked packed-refs' '
306 # First make sure that symlinking works when reading:
d6c6b108 307 git update-ref refs/heads/lossy refs/heads/main &&
198b808e
MH
308 git for-each-ref >all-refs-before &&
309 mv .git/packed-refs .git/my-deviant-packed-refs &&
310 ln -s my-deviant-packed-refs .git/packed-refs &&
311 git for-each-ref >all-refs-linked &&
312 test_cmp all-refs-before all-refs-linked &&
313 git pack-refs --all --prune &&
314 git for-each-ref >all-refs-packed &&
315 test_cmp all-refs-before all-refs-packed &&
316 test -h .git/packed-refs &&
7c0afdf2 317 test "$(test_readlink .git/packed-refs)" = "my-deviant-packed-refs"
198b808e
MH
318'
319
f0de1084
JC
320# The 'packed-refs' file is stored directly in .git/. This means it is global
321# to the repository, and can only contain refs that are shared across all
322# worktrees.
323test_expect_success 'refs/worktree must not be packed' '
324 test_commit initial &&
325 test_commit wt1 &&
326 test_commit wt2 &&
327 git worktree add wt1 wt1 &&
328 git worktree add wt2 wt2 &&
329 git checkout initial &&
330 git update-ref refs/worktree/foo HEAD &&
331 git -C wt1 update-ref refs/worktree/foo HEAD &&
332 git -C wt2 update-ref refs/worktree/foo HEAD &&
333 git pack-refs --all &&
334 test_path_is_missing .git/refs/tags/wt1 &&
335 test_path_is_file .git/refs/worktree/foo &&
336 test_path_is_file .git/worktrees/wt1/refs/worktree/foo &&
337 test_path_is_file .git/worktrees/wt2/refs/worktree/foo
338'
339
fa1033ac
JC
340# we do not want to count on running pack-refs to
341# actually pack it, as it is perfectly reasonable to
342# skip processing a broken ref
343test_expect_success 'create packed-refs file with broken ref' '
344 test_tick && git commit --allow-empty -m one &&
345 recoverable=$(git rev-parse HEAD) &&
346 test_tick && git commit --allow-empty -m two &&
347 missing=$(git rev-parse HEAD) &&
348 rm -f .git/refs/heads/main &&
349 cat >.git/packed-refs <<-EOF &&
350 $missing refs/heads/main
351 $recoverable refs/heads/other
352 EOF
353 echo $missing >expect &&
354 git rev-parse refs/heads/main >actual &&
355 test_cmp expect actual
356'
357
358test_expect_success 'pack-refs does not silently delete broken packed ref' '
359 git pack-refs --all --prune &&
360 git rev-parse refs/heads/main >actual &&
361 test_cmp expect actual
362'
363
364test_expect_success 'pack-refs does not drop broken refs during deletion' '
365 git update-ref -d refs/heads/other &&
366 git rev-parse refs/heads/main >actual &&
367 test_cmp expect actual
368'
369
9f6714ab
PS
370test_expect_success 'maintenance --auto unconditionally packs loose refs' '
371 git update-ref refs/heads/something HEAD &&
372 test_path_is_file .git/refs/heads/something &&
373 git rev-parse refs/heads/something >expect &&
374 git maintenance run --task=pack-refs --auto &&
375 test_path_is_missing .git/refs/heads/something &&
376 git rev-parse refs/heads/something >actual &&
377 test_cmp expect actual
378'
379
919a3c98 380test_done