Sync with 'maint'
[alt-git.git] / t / t1410-reflog.sh
blob246a3f46abafdf0e24528be59b33a4987ff791c1
1 #!/bin/sh
3 # Copyright (c) 2007 Junio C Hamano
6 test_description='Test prune and reflog expiration'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 TEST_PASSES_SANITIZE_LEAK=true
11 . ./test-lib.sh
13 check_have () {
14 gaah= &&
15 for N in "$@"
17 eval "o=\$$N" && git cat-file -t $o || {
18 echo Gaah $N
19 gaah=$N
20 break
22 done &&
23 test -z "$gaah"
26 check_fsck () {
27 git fsck --full >fsck.output
28 case "$1" in
29 '')
30 test_must_be_empty fsck.output ;;
32 test_grep "$1" fsck.output ;;
33 esac
36 corrupt () {
37 mv .git/objects/$(test_oid_to_path $1) .git/$1
40 recover () {
41 aa=$(echo $1 | cut -c 1-2)
42 mkdir -p .git/objects/$aa
43 mv .git/$1 .git/objects/$(test_oid_to_path $1)
46 check_dont_have () {
47 gaah= &&
48 for N in "$@"
50 eval "o=\$$N"
51 git cat-file -t $o && {
52 echo Gaah $N
53 gaah=$N
54 break
56 done
57 test -z "$gaah"
60 test_expect_success setup '
61 mkdir -p A/B &&
62 echo rat >C &&
63 echo ox >A/D &&
64 echo tiger >A/B/E &&
65 git add . &&
67 test_tick && git commit -m rabbit &&
68 H=$(git rev-parse --verify HEAD) &&
69 A=$(git rev-parse --verify HEAD:A) &&
70 B=$(git rev-parse --verify HEAD:A/B) &&
71 C=$(git rev-parse --verify HEAD:C) &&
72 D=$(git rev-parse --verify HEAD:A/D) &&
73 E=$(git rev-parse --verify HEAD:A/B/E) &&
74 check_fsck &&
76 test_chmod +x C &&
77 git add C &&
78 test_tick && git commit -m dragon &&
79 L=$(git rev-parse --verify HEAD) &&
80 check_fsck &&
82 rm -f C A/B/E &&
83 echo snake >F &&
84 echo horse >A/G &&
85 git add F A/G &&
86 test_tick && git commit -a -m sheep &&
87 F=$(git rev-parse --verify HEAD:F) &&
88 G=$(git rev-parse --verify HEAD:A/G) &&
89 I=$(git rev-parse --verify HEAD:A) &&
90 J=$(git rev-parse --verify HEAD) &&
91 check_fsck &&
93 rm -f A/G &&
94 test_tick && git commit -a -m monkey &&
95 K=$(git rev-parse --verify HEAD) &&
96 check_fsck &&
98 check_have A B C D E F G H I J K L &&
100 git prune &&
102 check_have A B C D E F G H I J K L &&
104 check_fsck &&
106 git reflog refs/heads/main >output &&
107 test_line_count = 4 output
110 test_expect_success 'correct usage on sub-command -h' '
111 test_expect_code 129 git reflog expire -h >err &&
112 grep "git reflog expire" err
115 test_expect_success 'correct usage on "git reflog show -h"' '
116 test_expect_code 129 git reflog show -h >err &&
117 grep -F "git reflog [show]" err
120 test_expect_success 'pass through -- to sub-command' '
121 test_when_finished "rm -rf repo" &&
122 git init repo &&
123 test_commit -C repo message --a-file contents dash-tag &&
125 git -C repo reflog show -- --does-not-exist >out &&
126 test_must_be_empty out &&
127 git -C repo reflog show >expect &&
128 git -C repo reflog show -- --a-file >actual &&
129 test_cmp expect actual
132 test_expect_success rewind '
133 test_tick && git reset --hard HEAD~2 &&
134 test -f C &&
135 test -f A/B/E &&
136 ! test -f F &&
137 ! test -f A/G &&
139 check_have A B C D E F G H I J K L &&
141 git prune &&
143 check_have A B C D E F G H I J K L &&
145 git reflog refs/heads/main >output &&
146 test_line_count = 5 output
149 test_expect_success 'reflog expire should not barf on an annotated tag' '
150 test_when_finished "git tag -d v0.tag || :" &&
151 git -c core.logAllRefUpdates=always \
152 tag -a -m "tag name" v0.tag main &&
153 git reflog expire --dry-run refs/tags/v0.tag 2>err &&
154 test_grep ! "error: [Oo]bject .* not a commit" err
157 test_expect_success 'corrupt and check' '
159 corrupt $F &&
160 check_fsck "missing blob $F"
164 test_expect_success 'reflog expire --dry-run should not touch reflog' '
166 git reflog expire --dry-run \
167 --expire=$(($test_tick - 10000)) \
168 --expire-unreachable=$(($test_tick - 10000)) \
169 --stale-fix \
170 --all &&
172 git reflog refs/heads/main >output &&
173 test_line_count = 5 output &&
175 check_fsck "missing blob $F"
178 test_expect_success 'reflog expire' '
180 git reflog expire --verbose \
181 --expire=$(($test_tick - 10000)) \
182 --expire-unreachable=$(($test_tick - 10000)) \
183 --stale-fix \
184 --all &&
186 git reflog refs/heads/main >output &&
187 test_line_count = 2 output &&
189 check_fsck "dangling commit $K"
192 test_expect_success '--stale-fix handles missing objects generously' '
193 git -c core.logAllRefUpdates=false fast-import --date-format=now <<-EOS &&
194 commit refs/heads/stale-fix
195 mark :1
196 committer Author <a@uth.or> now
197 data <<EOF
198 start stale fix
200 M 100644 inline file
201 data <<EOF
202 contents
204 commit refs/heads/stale-fix
205 committer Author <a@uth.or> now
206 data <<EOF
207 stale fix branch tip
209 from :1
212 parent_oid=$(git rev-parse stale-fix^) &&
213 test_when_finished "recover $parent_oid" &&
214 corrupt $parent_oid &&
215 git reflog expire --stale-fix
218 test_expect_success 'prune and fsck' '
220 git prune &&
221 check_fsck &&
223 check_have A B C D E H L &&
224 check_dont_have F G I J K
228 test_expect_success 'recover and check' '
230 recover $F &&
231 check_fsck "dangling blob $F"
235 test_expect_success 'delete' '
236 echo 1 > C &&
237 test_tick &&
238 git commit -m rat C &&
240 echo 2 > C &&
241 test_tick &&
242 git commit -m ox C &&
244 echo 3 > C &&
245 test_tick &&
246 git commit -m tiger C &&
248 HEAD_entry_count=$(git reflog | wc -l) &&
249 main_entry_count=$(git reflog show main | wc -l) &&
251 test $HEAD_entry_count = 5 &&
252 test $main_entry_count = 5 &&
255 git reflog delete main@{1} &&
256 git reflog show main > output &&
257 test_line_count = $(($main_entry_count - 1)) output &&
258 test $HEAD_entry_count = $(git reflog | wc -l) &&
259 ! grep ox < output &&
261 main_entry_count=$(wc -l < output) &&
263 git reflog delete HEAD@{1} &&
264 test $(($HEAD_entry_count -1)) = $(git reflog | wc -l) &&
265 test $main_entry_count = $(git reflog show main | wc -l) &&
267 HEAD_entry_count=$(git reflog | wc -l) &&
269 git reflog delete main@{07.04.2005.15:15:00.-0700} &&
270 git reflog show main > output &&
271 test_line_count = $(($main_entry_count - 1)) output &&
272 ! grep dragon < output
276 test_expect_success 'rewind2' '
278 test_tick && git reset --hard HEAD~2 &&
279 git reflog refs/heads/main >output &&
280 test_line_count = 4 output
283 test_expect_success '--expire=never' '
285 git reflog expire --verbose \
286 --expire=never \
287 --expire-unreachable=never \
288 --all &&
289 git reflog refs/heads/main >output &&
290 test_line_count = 4 output
293 test_expect_success 'gc.reflogexpire=never' '
294 test_config gc.reflogexpire never &&
295 test_config gc.reflogexpireunreachable never &&
297 git reflog expire --verbose --all >output &&
298 test_line_count = 9 output &&
300 git reflog refs/heads/main >output &&
301 test_line_count = 4 output
304 test_expect_success 'gc.reflogexpire=false' '
305 test_config gc.reflogexpire false &&
306 test_config gc.reflogexpireunreachable false &&
308 git reflog expire --verbose --all &&
309 git reflog refs/heads/main >output &&
310 test_line_count = 4 output
314 test_expect_success 'git reflog expire unknown reference' '
315 test_config gc.reflogexpire never &&
316 test_config gc.reflogexpireunreachable never &&
318 test_must_fail git reflog expire main@{123} 2>stderr &&
319 test_grep "points nowhere" stderr &&
320 test_must_fail git reflog expire does-not-exist 2>stderr &&
321 test_grep "points nowhere" stderr
324 test_expect_success 'checkout should not delete log for packed ref' '
325 test $(git reflog main | wc -l) = 4 &&
326 git branch foo &&
327 git pack-refs --all &&
328 git checkout foo &&
329 test $(git reflog main | wc -l) = 4
332 test_expect_success 'stale dirs do not cause d/f conflicts (reflogs on)' '
333 test_when_finished "git branch -d one || git branch -d one/two" &&
335 git branch one/two main &&
336 echo "one/two@{0} branch: Created from main" >expect &&
337 git log -g --format="%gd %gs" one/two >actual &&
338 test_cmp expect actual &&
339 git branch -d one/two &&
341 # now logs/refs/heads/one is a stale directory, but
342 # we should move it out of the way to create "one" reflog
343 git branch one main &&
344 echo "one@{0} branch: Created from main" >expect &&
345 git log -g --format="%gd %gs" one >actual &&
346 test_cmp expect actual
349 test_expect_success 'stale dirs do not cause d/f conflicts (reflogs off)' '
350 test_when_finished "git branch -d one || git branch -d one/two" &&
352 git branch one/two main &&
353 echo "one/two@{0} branch: Created from main" >expect &&
354 git log -g --format="%gd %gs" one/two >actual &&
355 test_cmp expect actual &&
356 git branch -d one/two &&
358 # same as before, but we only create a reflog for "one" if
359 # it already exists, which it does not
360 git -c core.logallrefupdates=false branch one main &&
361 git log -g --format="%gd %gs" one >actual &&
362 test_must_be_empty actual
365 test_expect_success 'no segfaults for reflog containing non-commit sha1s' '
366 git update-ref --create-reflog -m "Creating ref" \
367 refs/tests/tree-in-reflog HEAD &&
368 git update-ref -m "Forcing tree" refs/tests/tree-in-reflog HEAD^{tree} &&
369 git update-ref -m "Restoring to commit" refs/tests/tree-in-reflog HEAD &&
370 git reflog refs/tests/tree-in-reflog
373 test_expect_failure 'reflog with non-commit entries displays all entries' '
374 git reflog refs/tests/tree-in-reflog >actual &&
375 test_line_count = 3 actual
378 test_expect_success 'continue walking past root commits' '
379 git init orphanage &&
381 cd orphanage &&
382 cat >expect <<-\EOF &&
383 HEAD@{0} commit (initial): orphan2-1
384 HEAD@{1} commit: orphan1-2
385 HEAD@{2} commit (initial): orphan1-1
386 HEAD@{3} commit (initial): initial
388 test_commit initial &&
389 git checkout --orphan orphan1 &&
390 test_commit orphan1-1 &&
391 test_commit orphan1-2 &&
392 git checkout --orphan orphan2 &&
393 test_commit orphan2-1 &&
394 git log -g --format="%gd %gs" >actual &&
395 test_cmp expect actual
399 test_expect_success 'expire with multiple worktrees' '
400 git init main-wt &&
402 cd main-wt &&
403 test_tick &&
404 test_commit foo &&
405 git worktree add link-wt &&
406 test_tick &&
407 test_commit -C link-wt foobar &&
408 test_tick &&
409 git reflog expire --verbose --all --expire=$test_tick &&
410 test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD >actual &&
411 test_must_be_empty actual
415 test_expect_success 'expire one of multiple worktrees' '
416 git init main-wt2 &&
418 cd main-wt2 &&
419 test_tick &&
420 test_commit foo &&
421 git worktree add link-wt &&
422 test_tick &&
423 test_commit -C link-wt foobar &&
424 test_tick &&
425 test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD \
426 >expect-link-wt &&
427 git reflog expire --verbose --all --expire=$test_tick \
428 --single-worktree &&
429 test-tool ref-store worktree:main for-each-reflog-ent HEAD \
430 >actual-main &&
431 test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD \
432 >actual-link-wt &&
433 test_must_be_empty actual-main &&
434 test_cmp expect-link-wt actual-link-wt
438 test_expect_success 'empty reflog' '
439 test_when_finished "rm -rf empty" &&
440 git init empty &&
441 test_commit -C empty A &&
442 test-tool ref-store main create-reflog refs/heads/foo &&
443 git -C empty reflog expire --all 2>err &&
444 test_must_be_empty err
447 test_expect_success 'list reflogs' '
448 test_when_finished "rm -rf repo" &&
449 git init repo &&
451 cd repo &&
452 git reflog list >actual &&
453 test_must_be_empty actual &&
455 test_commit A &&
456 cat >expect <<-EOF &&
457 HEAD
458 refs/heads/main
460 git reflog list >actual &&
461 test_cmp expect actual &&
463 git branch b &&
464 cat >expect <<-EOF &&
465 HEAD
466 refs/heads/b
467 refs/heads/main
469 git reflog list >actual &&
470 test_cmp expect actual
474 test_expect_success 'list reflogs with worktree' '
475 test_when_finished "rm -rf repo" &&
476 git init repo &&
478 cd repo &&
480 test_commit A &&
481 git worktree add wt &&
482 git -c core.logAllRefUpdates=always \
483 update-ref refs/worktree/main HEAD &&
484 git -c core.logAllRefUpdates=always \
485 update-ref refs/worktree/per-worktree HEAD &&
486 git -c core.logAllRefUpdates=always -C wt \
487 update-ref refs/worktree/per-worktree HEAD &&
488 git -c core.logAllRefUpdates=always -C wt \
489 update-ref refs/worktree/worktree HEAD &&
491 cat >expect <<-EOF &&
492 HEAD
493 refs/heads/main
494 refs/heads/wt
495 refs/worktree/main
496 refs/worktree/per-worktree
498 git reflog list >actual &&
499 test_cmp expect actual &&
501 cat >expect <<-EOF &&
502 HEAD
503 refs/heads/main
504 refs/heads/wt
505 refs/worktree/per-worktree
506 refs/worktree/worktree
508 git -C wt reflog list >actual &&
509 test_cmp expect actual
513 test_expect_success 'reflog list returns error with additional args' '
514 cat >expect <<-EOF &&
515 error: list does not accept arguments: ${SQ}bogus${SQ}
517 test_must_fail git reflog list bogus 2>err &&
518 test_cmp expect err
521 test_expect_success 'reflog for symref with unborn target can be listed' '
522 test_when_finished "rm -rf repo" &&
523 git init repo &&
525 cd repo &&
526 test_commit A &&
527 git symbolic-ref HEAD refs/heads/unborn &&
528 cat >expect <<-EOF &&
529 HEAD
530 refs/heads/main
532 git reflog list >actual &&
533 test_cmp expect actual
537 test_expect_success 'reflog with invalid object ID can be listed' '
538 test_when_finished "rm -rf repo" &&
539 git init repo &&
541 cd repo &&
542 test_commit A &&
543 test-tool ref-store main update-ref msg refs/heads/missing \
544 $(test_oid deadbeef) "$ZERO_OID" REF_SKIP_OID_VERIFICATION &&
545 cat >expect <<-EOF &&
546 HEAD
547 refs/heads/main
548 refs/heads/missing
550 git reflog list >actual &&
551 test_cmp expect actual
555 test_done