Sync with 'master'
[alt-git.git] / t / t0601-reffiles-pack-refs.sh
blobd8cbd3f202b5f00c9a94c5a2ea2dced6606b6f4d
1 #!/bin/sh
3 # Copyright (c) 2005 Amos Waterland
4 # Copyright (c) 2006 Christian Couder
7 test_description='git pack-refs should not change the branch semantic
9 This test runs git pack-refs and git show-ref and checks that the branch
10 semantic is still the same.
13 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
14 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
15 GIT_TEST_DEFAULT_REF_FORMAT=files
16 export GIT_TEST_DEFAULT_REF_FORMAT
18 TEST_PASSES_SANITIZE_LEAK=true
19 . ./test-lib.sh
21 test_expect_success 'enable reflogs' '
22 git config core.logallrefupdates true
25 test_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)
32 test_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) &&
37 test "$N" != 0 &&
39 git pack-refs --prune --all &&
40 test_path_is_file .git/packed-refs &&
41 N=$(find .git/refs -type f) &&
42 test -z "$N"
45 SHA1=
47 test_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
55 test_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
64 test_expect_success 'git branch c/d should barf if branch c exists' '
65 git branch c &&
66 git pack-refs --all &&
67 rm -f .git/refs/heads/c &&
68 test_must_fail git branch c/d
71 test_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
79 test_expect_success 'see if git pack-refs --prune remove ref files' '
80 git branch f &&
81 git pack-refs --all --prune &&
82 ! test -f .git/refs/heads/f
85 test_expect_success 'see if git pack-refs --prune removes empty dirs' '
86 git branch r/s/t &&
87 git pack-refs --all --prune &&
88 ! test -e .git/refs/heads/r
91 test_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
100 test_expect_success 'git branch i/j/k should barf if branch i exists' '
101 git branch i &&
102 git pack-refs --all --prune &&
103 test_must_fail git branch i/j/k
106 test_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
116 test_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
127 test_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'
136 test_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'
143 test_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'
152 test_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'
159 test_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'
164 test_expect_success 'see if up-to-date packed refs are preserved' '
165 git branch q &&
166 git pack-refs --all --prune &&
167 git update-ref refs/heads/q refs/heads/q &&
168 ! test -f .git/refs/heads/q
171 test_expect_success 'pack, prune and repack' '
172 git tag foo &&
173 git pack-refs --all --prune &&
174 git show-ref >all-of-them &&
175 git pack-refs &&
176 git show-ref >again &&
177 test_cmp all-of-them again
180 test_expect_success 'explicit pack-refs with dangling packed reference' '
181 git commit --allow-empty -m "soon to be garbage-collected" &&
182 git pack-refs --all &&
183 git reset --hard HEAD^ &&
184 git reflog expire --expire=all --all &&
185 git prune --expire=all &&
186 git pack-refs --all 2>result &&
187 test_must_be_empty result
190 test_expect_success 'delete ref with dangling packed version' '
191 git checkout -b lamb &&
192 git commit --allow-empty -m "future garbage" &&
193 git pack-refs --all &&
194 git reset --hard HEAD^ &&
195 git checkout main &&
196 git reflog expire --expire=all --all &&
197 git prune --expire=all &&
198 git branch -d lamb 2>result &&
199 test_must_be_empty result
202 test_expect_success 'delete ref while another dangling packed ref' '
203 git branch lamb &&
204 git commit --allow-empty -m "future garbage" &&
205 git pack-refs --all &&
206 git reset --hard HEAD^ &&
207 git reflog expire --expire=all --all &&
208 git prune --expire=all &&
209 git branch -d lamb 2>result &&
210 test_must_be_empty result
213 test_expect_success 'pack ref directly below refs/' '
214 git update-ref refs/top HEAD &&
215 git pack-refs --all --prune &&
216 grep refs/top .git/packed-refs &&
217 test_path_is_missing .git/refs/top
220 test_expect_success 'do not pack ref in refs/bisect' '
221 git update-ref refs/bisect/local HEAD &&
222 git pack-refs --all --prune &&
223 ! grep refs/bisect/local .git/packed-refs >/dev/null &&
224 test_path_is_file .git/refs/bisect/local
227 test_expect_success 'disable reflogs' '
228 git config core.logallrefupdates false &&
229 rm -rf .git/logs
232 test_expect_success 'create packed foo/bar/baz branch' '
233 git branch foo/bar/baz &&
234 git pack-refs --all --prune &&
235 test_path_is_missing .git/refs/heads/foo/bar/baz &&
236 test_must_fail git reflog exists refs/heads/foo/bar/baz
239 test_expect_success 'notice d/f conflict with existing directory' '
240 test_must_fail git branch foo &&
241 test_must_fail git branch foo/bar
244 test_expect_success 'existing directory reports concrete ref' '
245 test_must_fail git branch foo 2>stderr &&
246 test_grep refs/heads/foo/bar/baz stderr
249 test_expect_success 'notice d/f conflict with existing ref' '
250 test_must_fail git branch foo/bar/baz/extra &&
251 test_must_fail git branch foo/bar/baz/lots/of/extra/components
254 test_expect_success 'reject packed-refs with unterminated line' '
255 cp .git/packed-refs .git/packed-refs.bak &&
256 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
257 printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs &&
258 echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err &&
259 test_must_fail git for-each-ref >out 2>err &&
260 test_cmp expected_err err
263 test_expect_success 'reject packed-refs containing junk' '
264 cp .git/packed-refs .git/packed-refs.bak &&
265 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
266 printf "%s\n" "bogus content" >>.git/packed-refs &&
267 echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err &&
268 test_must_fail git for-each-ref >out 2>err &&
269 test_cmp expected_err err
272 test_expect_success 'reject packed-refs with a short SHA-1' '
273 cp .git/packed-refs .git/packed-refs.bak &&
274 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
275 printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs &&
276 printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err &&
277 test_must_fail git for-each-ref >out 2>err &&
278 test_cmp expected_err err
281 test_expect_success 'timeout if packed-refs.lock exists' '
282 LOCK=.git/packed-refs.lock &&
283 >"$LOCK" &&
284 test_when_finished "rm -f $LOCK" &&
285 test_must_fail git pack-refs --all --prune
288 test_expect_success 'retry acquiring packed-refs.lock' '
289 LOCK=.git/packed-refs.lock &&
290 >"$LOCK" &&
291 test_when_finished "wait && rm -f $LOCK" &&
293 ( sleep 1 && rm -f $LOCK ) &
294 } &&
295 git -c core.packedrefstimeout=3000 pack-refs --all --prune
298 test_expect_success SYMLINKS 'pack symlinked packed-refs' '
299 # First make sure that symlinking works when reading:
300 git update-ref refs/heads/lossy refs/heads/main &&
301 git for-each-ref >all-refs-before &&
302 mv .git/packed-refs .git/my-deviant-packed-refs &&
303 ln -s my-deviant-packed-refs .git/packed-refs &&
304 git for-each-ref >all-refs-linked &&
305 test_cmp all-refs-before all-refs-linked &&
306 git pack-refs --all --prune &&
307 git for-each-ref >all-refs-packed &&
308 test_cmp all-refs-before all-refs-packed &&
309 test -h .git/packed-refs &&
310 test "$(test_readlink .git/packed-refs)" = "my-deviant-packed-refs"
313 # The 'packed-refs' file is stored directly in .git/. This means it is global
314 # to the repository, and can only contain refs that are shared across all
315 # worktrees.
316 test_expect_success 'refs/worktree must not be packed' '
317 test_commit initial &&
318 test_commit wt1 &&
319 test_commit wt2 &&
320 git worktree add wt1 wt1 &&
321 git worktree add wt2 wt2 &&
322 git checkout initial &&
323 git update-ref refs/worktree/foo HEAD &&
324 git -C wt1 update-ref refs/worktree/foo HEAD &&
325 git -C wt2 update-ref refs/worktree/foo HEAD &&
326 git pack-refs --all &&
327 test_path_is_missing .git/refs/tags/wt1 &&
328 test_path_is_file .git/refs/worktree/foo &&
329 test_path_is_file .git/worktrees/wt1/refs/worktree/foo &&
330 test_path_is_file .git/worktrees/wt2/refs/worktree/foo
333 # we do not want to count on running pack-refs to
334 # actually pack it, as it is perfectly reasonable to
335 # skip processing a broken ref
336 test_expect_success 'create packed-refs file with broken ref' '
337 test_tick && git commit --allow-empty -m one &&
338 recoverable=$(git rev-parse HEAD) &&
339 test_tick && git commit --allow-empty -m two &&
340 missing=$(git rev-parse HEAD) &&
341 rm -f .git/refs/heads/main &&
342 cat >.git/packed-refs <<-EOF &&
343 $missing refs/heads/main
344 $recoverable refs/heads/other
346 echo $missing >expect &&
347 git rev-parse refs/heads/main >actual &&
348 test_cmp expect actual
351 test_expect_success 'pack-refs does not silently delete broken packed ref' '
352 git pack-refs --all --prune &&
353 git rev-parse refs/heads/main >actual &&
354 test_cmp expect actual
357 test_expect_success 'pack-refs does not drop broken refs during deletion' '
358 git update-ref -d refs/heads/other &&
359 git rev-parse refs/heads/main >actual &&
360 test_cmp expect actual
363 for command in "git pack-refs --all --auto" "git maintenance run --task=pack-refs --auto"
365 test_expect_success "$command does not repack below 16 refs without packed-refs" '
366 test_when_finished "rm -rf repo" &&
367 git init repo &&
369 cd repo &&
370 git config set maintenance.auto false &&
371 git commit --allow-empty --message "initial" &&
373 # Create 14 additional references, which brings us to
374 # 15 together with the default branch.
375 printf "create refs/heads/loose-%d HEAD\n" $(test_seq 14) >stdin &&
376 git update-ref --stdin <stdin &&
377 test_path_is_missing .git/packed-refs &&
378 git pack-refs --auto --all &&
379 test_path_is_missing .git/packed-refs &&
381 # Create the 16th reference, which should cause us to repack.
382 git update-ref refs/heads/loose-15 HEAD &&
383 git pack-refs --auto --all &&
384 test_path_is_file .git/packed-refs
388 test_expect_success "$command does not repack below 16 refs with small packed-refs" '
389 test_when_finished "rm -rf repo" &&
390 git init repo &&
392 cd repo &&
393 git config set maintenance.auto false &&
394 git commit --allow-empty --message "initial" &&
396 git pack-refs --all &&
397 test_line_count = 2 .git/packed-refs &&
399 # Create 15 loose references.
400 printf "create refs/heads/loose-%d HEAD\n" $(test_seq 15) >stdin &&
401 git update-ref --stdin <stdin &&
402 git pack-refs --auto --all &&
403 test_line_count = 2 .git/packed-refs &&
405 # Create the 16th loose reference, which should cause us to repack.
406 git update-ref refs/heads/loose-17 HEAD &&
407 git pack-refs --auto --all &&
408 test_line_count = 18 .git/packed-refs
412 test_expect_success "$command scales with size of packed-refs" '
413 test_when_finished "rm -rf repo" &&
414 git init repo &&
416 cd repo &&
417 git config set maintenance.auto false &&
418 git commit --allow-empty --message "initial" &&
420 # Create 99 packed refs. This should cause the heuristic
421 # to require more than the minimum amount of loose refs.
422 test_seq 99 |
423 while read i
425 printf "create refs/heads/packed-%d HEAD\n" $i || return 1
426 done >stdin &&
427 git update-ref --stdin <stdin &&
428 git pack-refs --all &&
429 test_line_count = 101 .git/packed-refs &&
431 # Create 24 loose refs, which should not yet cause us to repack.
432 printf "create refs/heads/loose-%d HEAD\n" $(test_seq 24) >stdin &&
433 git update-ref --stdin <stdin &&
434 git pack-refs --auto --all &&
435 test_line_count = 101 .git/packed-refs &&
437 # Create another handful of refs to cross the border.
438 # Note that we explicitly do not check for strict
439 # boundaries here, as this also depends on the size of
440 # the object hash.
441 printf "create refs/heads/addn-%d HEAD\n" $(test_seq 10) >stdin &&
442 git update-ref --stdin <stdin &&
443 git pack-refs --auto --all &&
444 test_line_count = 135 .git/packed-refs
447 done
449 test_done