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
17 eval "o=\$$N" && git cat-file
-t $o ||
{
27 git fsck
--full >fsck.output
30 test_must_be_empty fsck.output
;;
32 test_grep
"$1" fsck.output
;;
37 mv .git
/objects
/$
(test_oid_to_path
$1) .git
/$1
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)
51 git cat-file
-t $o && {
60 test_expect_success setup
'
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) &&
78 test_tick && git commit -m dragon &&
79 L=$(git rev-parse --verify HEAD) &&
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) &&
94 test_tick && git commit -a -m monkey &&
95 K=$(git rev-parse --verify HEAD) &&
98 check_have A B C D E F G H I J K L &&
102 check_have A B C D E F G H I J K L &&
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" &&
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 &&
139 check_have A B C D E F G H I J K L &&
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' '
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)) \
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)) \
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
196 committer Author <a@uth.or> now
204 commit refs/heads/stale-fix
205 committer Author <a@uth.or> now
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' '
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' '
231 check_fsck "dangling blob $F"
235 test_expect_success
'delete' '
238 git commit -m rat C &&
242 git commit -m ox C &&
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 \
287 --expire-unreachable=never \
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 &&
327 git pack-refs --all &&
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 &&
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' '
405 git worktree add link-wt &&
407 test_commit -C link-wt foobar &&
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' '
421 git worktree add link-wt &&
423 test_commit -C link-wt foobar &&
425 test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD \
427 git reflog expire --verbose --all --expire=$test_tick \
429 test-tool ref-store worktree:main for-each-reflog-ent HEAD \
431 test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD \
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" &&
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" &&
452 git reflog list >actual &&
453 test_must_be_empty actual &&
456 cat >expect <<-EOF &&
460 git reflog list >actual &&
461 test_cmp expect actual &&
464 cat >expect <<-EOF &&
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" &&
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 &&
496 refs/worktree/per-worktree
498 git reflog list >actual &&
499 test_cmp expect actual &&
501 cat >expect <<-EOF &&
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 &&
521 test_expect_success
'reflog for symref with unborn target can be listed' '
522 test_when_finished "rm -rf repo" &&
527 git symbolic-ref HEAD refs/heads/unborn &&
528 cat >expect <<-EOF &&
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" &&
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 &&
550 git reflog list >actual &&
551 test_cmp expect actual