Raw
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Junio C Hamano
4 #
5
6 test_description='Test prune and reflog expiration'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
10 . ./test-lib.sh
11
12 check_have () {
13 gaah= &&
14 for N in "$@"
15 do
16 eval "o=\$$N" && git cat-file -t $o || {
17 echo Gaah $N
18 gaah=$N
19 break
20 }
21 done &&
22 test -z "$gaah"
23 }
24
25 check_fsck () {
26 git fsck --full >fsck.output
27 case "$1" in
28 '')
29 test_must_be_empty fsck.output ;;
30 *)
31 test_grep "$1" fsck.output ;;
32 esac
33 }
34
35 corrupt () {
36 mv .git/objects/$(test_oid_to_path $1) .git/$1
37 }
38
39 recover () {
40 aa=$(echo $1 | cut -c 1-2)
41 mkdir -p .git/objects/$aa
42 mv .git/$1 .git/objects/$(test_oid_to_path $1)
43 }
44
45 check_dont_have () {
46 gaah= &&
47 for N in "$@"
48 do
49 eval "o=\$$N"
50 git cat-file -t $o && {
51 echo Gaah $N
52 gaah=$N
53 break
54 }
55 done
56 test -z "$gaah"
57 }
58
59 test_expect_success setup '
60 mkdir -p A/B &&
61 echo rat >C &&
62 echo ox >A/D &&
63 echo tiger >A/B/E &&
64 git add . &&
65
66 test_tick && git commit -m rabbit &&
67 H=$(git rev-parse --verify HEAD) &&
68 A=$(git rev-parse --verify HEAD:A) &&
69 B=$(git rev-parse --verify HEAD:A/B) &&
70 C=$(git rev-parse --verify HEAD:C) &&
71 D=$(git rev-parse --verify HEAD:A/D) &&
72 E=$(git rev-parse --verify HEAD:A/B/E) &&
73 check_fsck &&
74
75 test_chmod +x C &&
76 git add C &&
77 test_tick && git commit -m dragon &&
78 L=$(git rev-parse --verify HEAD) &&
79 check_fsck &&
80
81 rm -f C A/B/E &&
82 echo snake >F &&
83 echo horse >A/G &&
84 git add F A/G &&
85 test_tick && git commit -a -m sheep &&
86 F=$(git rev-parse --verify HEAD:F) &&
87 G=$(git rev-parse --verify HEAD:A/G) &&
88 I=$(git rev-parse --verify HEAD:A) &&
89 J=$(git rev-parse --verify HEAD) &&
90 check_fsck &&
91
92 rm -f A/G &&
93 test_tick && git commit -a -m monkey &&
94 K=$(git rev-parse --verify HEAD) &&
95 check_fsck &&
96
97 check_have A B C D E F G H I J K L &&
98
99 git prune &&
100
101 check_have A B C D E F G H I J K L &&
102
103 check_fsck &&
104
105 git reflog refs/heads/main >output &&
106 test_line_count = 4 output
107 '
108
109 test_expect_success 'correct usage on sub-command -h' '
110 git reflog expire -h >err &&
111 test_grep "git reflog expire" err
112 '
113
114 test_expect_success 'correct usage on "git reflog show -h"' '
115 git reflog show -h >err &&
116 test_grep -F "git reflog [show]" err
117 '
118
119 test_expect_success 'pass through -- to sub-command' '
120 test_when_finished "rm -rf repo" &&
121 git init repo &&
122 test_commit -C repo message --a-file contents dash-tag &&
123
124 git -C repo reflog show -- --does-not-exist >out &&
125 test_must_be_empty out &&
126 git -C repo reflog show >expect &&
127 git -C repo reflog show -- --a-file >actual &&
128 test_cmp expect actual
129 '
130
131 test_expect_success rewind '
132 test_tick && git reset --hard HEAD~2 &&
133 test_path_is_file C &&
134 test_path_is_file A/B/E &&
135 test_path_is_missing F &&
136 test_path_is_missing A/G &&
137
138 check_have A B C D E F G H I J K L &&
139
140 git prune &&
141
142 check_have A B C D E F G H I J K L &&
143
144 git reflog refs/heads/main >output &&
145 test_line_count = 5 output
146 '
147
148 test_expect_success 'reflog expire should not barf on an annotated tag' '
149 test_when_finished "git tag -d v0.tag || :" &&
150 git -c core.logAllRefUpdates=always \
151 tag -a -m "tag name" v0.tag main &&
152 git reflog expire --dry-run refs/tags/v0.tag 2>err &&
153 test_grep ! "error: [Oo]bject .* not a commit" err
154 '
155
156 test_expect_success 'corrupt and check' '
157
158 corrupt $F &&
159 check_fsck "missing blob $F"
160
161 '
162
163 test_expect_success 'reflog expire --dry-run should not touch reflog' '
164
165 git reflog expire --dry-run \
166 --expire=$(($test_tick - 10000)) \
167 --expire-unreachable=$(($test_tick - 10000)) \
168 --stale-fix \
169 --all &&
170
171 git reflog refs/heads/main >output &&
172 test_line_count = 5 output &&
173
174 check_fsck "missing blob $F"
175 '
176
177 test_expect_success 'reflog expire' '
178
179 git reflog expire --verbose \
180 --expire=$(($test_tick - 10000)) \
181 --expire-unreachable=$(($test_tick - 10000)) \
182 --stale-fix \
183 --all &&
184
185 git reflog refs/heads/main >output &&
186 test_line_count = 2 output &&
187
188 check_fsck "dangling commit $K"
189 '
190
191 test_expect_success '--stale-fix handles missing objects generously' '
192 git -c core.logAllRefUpdates=false fast-import --date-format=now <<-EOS &&
193 commit refs/heads/stale-fix
194 mark :1
195 committer Author <a@uth.or> now
196 data <<EOF
197 start stale fix
198 EOF
199 M 100644 inline file
200 data <<EOF
201 contents
202 EOF
203 commit refs/heads/stale-fix
204 committer Author <a@uth.or> now
205 data <<EOF
206 stale fix branch tip
207 EOF
208 from :1
209 EOS
210
211 parent_oid=$(git rev-parse stale-fix^) &&
212 test_when_finished "recover $parent_oid" &&
213 corrupt $parent_oid &&
214 git reflog expire --stale-fix
215 '
216
217 test_expect_success 'prune and fsck' '
218
219 git prune &&
220 check_fsck &&
221
222 check_have A B C D E H L &&
223 check_dont_have F G I J K
224
225 '
226
227 test_expect_success 'recover and check' '
228
229 recover $F &&
230 check_fsck "dangling blob $F"
231
232 '
233
234 test_expect_success 'delete' '
235 echo 1 > C &&
236 test_tick &&
237 git commit -m rat C &&
238
239 echo 2 > C &&
240 test_tick &&
241 git commit -m ox C &&
242
243 echo 3 > C &&
244 test_tick &&
245 git commit -m tiger C &&
246
247 test_stdout_line_count = 5 git reflog &&
248 test_stdout_line_count = 5 git reflog show main &&
249
250 git reflog delete main@{1} &&
251 test_stdout_line_count = 4 git reflog show main &&
252 test_stdout_line_count = 5 git reflog &&
253 git reflog show main > output &&
254 ! grep ox < output &&
255
256 git reflog delete HEAD@{1} &&
257 test_stdout_line_count = 4 git reflog &&
258 test_stdout_line_count = 4 git reflog show main &&
259
260 git reflog delete main@{07.04.2005.15:15:00.-0700} &&
261 test_stdout_line_count = 3 git reflog show main &&
262 git reflog show main > output &&
263 ! grep dragon < output
264
265 '
266
267 test_expect_success 'rewind2' '
268
269 test_tick && git reset --hard HEAD~2 &&
270 git reflog refs/heads/main >output &&
271 test_line_count = 4 output
272 '
273
274 test_expect_success '--expire=never' '
275
276 git reflog expire --verbose \
277 --expire=never \
278 --expire-unreachable=never \
279 --all &&
280 git reflog refs/heads/main >output &&
281 test_line_count = 4 output
282 '
283
284 test_expect_success 'gc.reflogexpire=never' '
285 test_config gc.reflogexpire never &&
286 test_config gc.reflogexpireunreachable never &&
287
288 git reflog expire --verbose --all >output &&
289 test_line_count = 9 output &&
290
291 git reflog refs/heads/main >output &&
292 test_line_count = 4 output
293 '
294
295 test_expect_success 'gc.reflogexpire=false' '
296 test_config gc.reflogexpire false &&
297 test_config gc.reflogexpireunreachable false &&
298
299 git reflog expire --verbose --all &&
300 git reflog refs/heads/main >output &&
301 test_line_count = 4 output
302
303 '
304
305 test_expect_success 'git reflog expire unknown reference' '
306 test_config gc.reflogexpire never &&
307 test_config gc.reflogexpireunreachable never &&
308
309 test_must_fail git reflog expire main@{123} 2>stderr &&
310 test_grep "error: reflog could not be found: ${SQ}main@{123}${SQ}" stderr &&
311 test_must_fail git reflog expire does-not-exist 2>stderr &&
312 test_grep "error: reflog could not be found: ${SQ}does-not-exist${SQ}" stderr
313 '
314
315 test_expect_success 'checkout should not delete log for packed ref' '
316 test_stdout_line_count = 4 git reflog main &&
317 git branch foo &&
318 git pack-refs --all &&
319 git checkout foo &&
320 test_stdout_line_count = 4 git reflog main
321 '
322
323 test_expect_success 'stale dirs do not cause d/f conflicts (reflogs on)' '
324 test_when_finished "git branch -d one || git branch -d one/two" &&
325
326 git branch one/two main &&
327 echo "one/two@{0} branch: Created from main" >expect &&
328 git log -g --format="%gd %gs" one/two >actual &&
329 test_cmp expect actual &&
330 git branch -d one/two &&
331
332 # now logs/refs/heads/one is a stale directory, but
333 # we should move it out of the way to create "one" reflog
334 git branch one main &&
335 echo "one@{0} branch: Created from main" >expect &&
336 git log -g --format="%gd %gs" one >actual &&
337 test_cmp expect actual
338 '
339
340 test_expect_success 'stale dirs do not cause d/f conflicts (reflogs off)' '
341 test_when_finished "git branch -d one || git branch -d one/two" &&
342
343 git branch one/two main &&
344 echo "one/two@{0} branch: Created from main" >expect &&
345 git log -g --format="%gd %gs" one/two >actual &&
346 test_cmp expect actual &&
347 git branch -d one/two &&
348
349 # same as before, but we only create a reflog for "one" if
350 # it already exists, which it does not
351 git -c core.logallrefupdates=false branch one main &&
352 git log -g --format="%gd %gs" one >actual &&
353 test_must_be_empty actual
354 '
355
356 test_expect_success 'no segfaults for reflog containing non-commit sha1s' '
357 git update-ref --create-reflog -m "Creating ref" \
358 refs/tests/tree-in-reflog HEAD &&
359 git update-ref -m "Forcing tree" refs/tests/tree-in-reflog HEAD^{tree} &&
360 git update-ref -m "Restoring to commit" refs/tests/tree-in-reflog HEAD &&
361 git reflog refs/tests/tree-in-reflog
362 '
363
364 test_expect_failure 'reflog with non-commit entries displays all entries' '
365 git reflog refs/tests/tree-in-reflog >actual &&
366 test_line_count = 3 actual
367 '
368
369 test_expect_success 'continue walking past root commits' '
370 git init orphanage &&
371 (
372 cd orphanage &&
373 cat >expect <<-\EOF &&
374 HEAD@{0} commit (initial): orphan2-1
375 HEAD@{1} commit: orphan1-2
376 HEAD@{2} commit (initial): orphan1-1
377 HEAD@{3} commit (initial): initial
378 EOF
379 test_commit initial &&
380 git checkout --orphan orphan1 &&
381 test_commit orphan1-1 &&
382 test_commit orphan1-2 &&
383 git checkout --orphan orphan2 &&
384 test_commit orphan2-1 &&
385 git log -g --format="%gd %gs" >actual &&
386 test_cmp expect actual
387 )
388 '
389
390 test_expect_success 'expire with multiple worktrees' '
391 git init main-wt &&
392 (
393 cd main-wt &&
394 test_tick &&
395 test_commit foo &&
396 git worktree add link-wt &&
397 test_tick &&
398 test_commit -C link-wt foobar &&
399 test_tick &&
400 git reflog expire --verbose --all --expire=$test_tick &&
401 test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD >actual &&
402 test_must_be_empty actual
403 )
404 '
405
406 test_expect_success 'expire one of multiple worktrees' '
407 git init main-wt2 &&
408 (
409 cd main-wt2 &&
410 test_tick &&
411 test_commit foo &&
412 git worktree add link-wt &&
413 test_tick &&
414 test_commit -C link-wt foobar &&
415 test_tick &&
416 test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD \
417 >expect-link-wt &&
418 git reflog expire --verbose --all --expire=$test_tick \
419 --single-worktree &&
420 test-tool ref-store worktree:main for-each-reflog-ent HEAD \
421 >actual-main &&
422 test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD \
423 >actual-link-wt &&
424 test_must_be_empty actual-main &&
425 test_cmp expect-link-wt actual-link-wt
426 )
427 '
428
429 test_expect_success 'empty reflog' '
430 test_when_finished "rm -rf empty" &&
431 git init empty &&
432 test_commit -C empty A &&
433 test-tool ref-store main create-reflog refs/heads/foo &&
434 git -C empty reflog expire --all 2>err &&
435 test_must_be_empty err
436 '
437
438 test_expect_success 'list reflogs' '
439 test_when_finished "rm -rf repo" &&
440 git init repo &&
441 (
442 cd repo &&
443 git reflog list >actual &&
444 test_must_be_empty actual &&
445
446 test_commit A &&
447 cat >expect <<-EOF &&
448 HEAD
449 refs/heads/main
450 EOF
451 git reflog list >actual &&
452 test_cmp expect actual &&
453
454 git branch b &&
455 cat >expect <<-EOF &&
456 HEAD
457 refs/heads/b
458 refs/heads/main
459 EOF
460 git reflog list >actual &&
461 test_cmp expect actual
462 )
463 '
464
465 test_expect_success 'list reflogs with worktree' '
466 test_when_finished "rm -rf repo" &&
467 git init repo &&
468 (
469 cd repo &&
470
471 test_commit A &&
472 git worktree add wt &&
473 git -c core.logAllRefUpdates=always \
474 update-ref refs/worktree/main HEAD &&
475 git -c core.logAllRefUpdates=always \
476 update-ref refs/worktree/per-worktree HEAD &&
477 git -c core.logAllRefUpdates=always -C wt \
478 update-ref refs/worktree/per-worktree HEAD &&
479 git -c core.logAllRefUpdates=always -C wt \
480 update-ref refs/worktree/worktree HEAD &&
481
482 cat >expect <<-EOF &&
483 HEAD
484 refs/heads/main
485 refs/heads/wt
486 refs/worktree/main
487 refs/worktree/per-worktree
488 EOF
489 git reflog list >actual &&
490 test_cmp expect actual &&
491
492 cat >expect <<-EOF &&
493 HEAD
494 refs/heads/main
495 refs/heads/wt
496 refs/worktree/per-worktree
497 refs/worktree/worktree
498 EOF
499 git -C wt reflog list >actual &&
500 test_cmp expect actual
501 )
502 '
503
504 test_expect_success 'reflog list returns error with additional args' '
505 cat >expect <<-EOF &&
506 error: list does not accept arguments: ${SQ}bogus${SQ}
507 EOF
508 test_must_fail git reflog list bogus 2>err &&
509 test_cmp expect err
510 '
511
512 test_expect_success 'reflog for symref with unborn target can be listed' '
513 test_when_finished "rm -rf repo" &&
514 git init repo &&
515 (
516 cd repo &&
517 test_commit A &&
518 git symbolic-ref HEAD refs/heads/unborn &&
519 cat >expect <<-EOF &&
520 HEAD
521 refs/heads/main
522 EOF
523 git reflog list >actual &&
524 test_cmp expect actual
525 )
526 '
527
528 test_expect_success 'reflog with invalid object ID can be listed' '
529 test_when_finished "rm -rf repo" &&
530 git init repo &&
531 (
532 cd repo &&
533 test_commit A &&
534 test-tool ref-store main update-ref msg refs/heads/missing \
535 $(test_oid deadbeef) "$ZERO_OID" REF_SKIP_OID_VERIFICATION &&
536 cat >expect <<-EOF &&
537 HEAD
538 refs/heads/main
539 refs/heads/missing
540 EOF
541 git reflog list >actual &&
542 test_cmp expect actual
543 )
544 '
545
546 test_expect_success 'reflog drop non-existent ref' '
547 test_when_finished "rm -rf repo" &&
548 git init repo &&
549 (
550 cd repo &&
551 test_must_fail git reflog exists refs/heads/non-existent &&
552 test_must_fail git reflog drop refs/heads/non-existent 2>stderr &&
553 test_grep "error: reflog could not be found: ${SQ}refs/heads/non-existent${SQ}" stderr
554 )
555 '
556
557 test_expect_success 'reflog drop' '
558 test_when_finished "rm -rf repo" &&
559 git init repo &&
560 (
561 cd repo &&
562 test_commit A &&
563 test_commit_bulk --ref=refs/heads/branch 1 &&
564 git reflog exists refs/heads/main &&
565 git reflog exists refs/heads/branch &&
566 git reflog drop refs/heads/main &&
567 test_must_fail git reflog exists refs/heads/main &&
568 git reflog exists refs/heads/branch
569 )
570 '
571
572 test_expect_success 'reflog drop multiple references' '
573 test_when_finished "rm -rf repo" &&
574 git init repo &&
575 (
576 cd repo &&
577 test_commit A &&
578 test_commit_bulk --ref=refs/heads/branch 1 &&
579 git reflog exists refs/heads/main &&
580 git reflog exists refs/heads/branch &&
581 git reflog drop refs/heads/main refs/heads/branch &&
582 test_must_fail git reflog exists refs/heads/main &&
583 test_must_fail git reflog exists refs/heads/branch
584 )
585 '
586
587 test_expect_success 'reflog drop multiple references some non-existent' '
588 test_when_finished "rm -rf repo" &&
589 git init repo &&
590 (
591 cd repo &&
592 test_commit A &&
593 test_commit_bulk --ref=refs/heads/branch 1 &&
594 git reflog exists refs/heads/main &&
595 git reflog exists refs/heads/branch &&
596 test_must_fail git reflog exists refs/heads/non-existent &&
597 test_must_fail git reflog drop refs/heads/main refs/heads/non-existent refs/heads/branch 2>stderr &&
598 test_must_fail git reflog exists refs/heads/main &&
599 test_must_fail git reflog exists refs/heads/branch &&
600 test_must_fail git reflog exists refs/heads/non-existent &&
601 test_grep "error: reflog could not be found: ${SQ}refs/heads/non-existent${SQ}" stderr
602 )
603 '
604
605 test_expect_success 'reflog drop --all' '
606 test_when_finished "rm -rf repo" &&
607 git init repo &&
608 (
609 cd repo &&
610 test_commit A &&
611 test_commit_bulk --ref=refs/heads/branch 1 &&
612 git reflog exists refs/heads/main &&
613 git reflog exists refs/heads/branch &&
614 git reflog drop --all &&
615 test_must_fail git reflog exists refs/heads/main &&
616 test_must_fail git reflog exists refs/heads/branch
617 )
618 '
619
620 test_expect_success 'reflog drop --all multiple worktrees' '
621 test_when_finished "rm -rf repo" &&
622 test_when_finished "rm -rf wt" &&
623 git init repo &&
624 (
625 cd repo &&
626 test_commit A &&
627 git worktree add ../wt &&
628 test_commit_bulk -C ../wt --ref=refs/heads/branch 1 &&
629 git reflog exists refs/heads/main &&
630 git reflog exists refs/heads/branch &&
631 git reflog drop --all &&
632 test_must_fail git reflog exists refs/heads/main &&
633 test_must_fail git reflog exists refs/heads/branch
634 )
635 '
636
637 test_expect_success 'reflog drop --all --single-worktree' '
638 test_when_finished "rm -rf repo" &&
639 test_when_finished "rm -rf wt" &&
640 git init repo &&
641 (
642 cd repo &&
643 test_commit A &&
644 git worktree add ../wt &&
645 test_commit -C ../wt foobar &&
646 git reflog exists refs/heads/main &&
647 git reflog exists refs/heads/wt &&
648 test-tool ref-store worktree:wt reflog-exists HEAD &&
649 git reflog drop --all --single-worktree &&
650 test_must_fail git reflog exists refs/heads/main &&
651 test_must_fail git reflog exists refs/heads/wt &&
652 test_must_fail test-tool ref-store worktree:main reflog-exists HEAD &&
653 test-tool ref-store worktree:wt reflog-exists HEAD
654 )
655 '
656
657 test_expect_success 'reflog drop --all with reference' '
658 test_when_finished "rm -rf repo" &&
659 git init repo &&
660 (
661 cd repo &&
662 test_commit A &&
663 test_must_fail git reflog drop --all refs/heads/main 2>stderr &&
664 test_grep "usage: references specified along with --all" stderr
665 )
666 '
667
668 test_expect_success 'expire with pattern config' '
669 # Split refs/heads/ into two roots so we can apply config to each. Make
670 # two branches per root to verify that config is applied correctly
671 # multiple times.
672 git branch root1/branch1 &&
673 git branch root1/branch2 &&
674 git branch root2/branch1 &&
675 git branch root2/branch2 &&
676
677 test_config "gc.reflogexpire" "never" &&
678 test_config "gc.refs/heads/root2/*.reflogExpire" "now" &&
679 git reflog expire \
680 root1/branch1 root1/branch2 \
681 root2/branch1 root2/branch2 &&
682
683 cat >expect <<-\EOF &&
684 root1/branch1@{0}
685 root1/branch2@{0}
686 EOF
687 git log -g --branches="root*" --format=%gD >actual.raw &&
688 # The sole reflog entry of each branch points to the same commit, so
689 # the order in which they are shown is nondeterministic. We just care
690 # about the what was expired (and what was not), so sort to get a known
691 # order.
692 sort <actual.raw >actual.sorted &&
693 test_cmp expect actual.sorted
694 '
695
696 test_done