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
'corrupt and check' '
152 check_fsck "missing blob $F"
156 test_expect_success
'reflog expire --dry-run should not touch reflog' '
158 git reflog expire --dry-run \
159 --expire=$(($test_tick - 10000)) \
160 --expire-unreachable=$(($test_tick - 10000)) \
164 git reflog refs/heads/main >output &&
165 test_line_count = 5 output &&
167 check_fsck "missing blob $F"
170 test_expect_success
'reflog expire' '
172 git reflog expire --verbose \
173 --expire=$(($test_tick - 10000)) \
174 --expire-unreachable=$(($test_tick - 10000)) \
178 git reflog refs/heads/main >output &&
179 test_line_count = 2 output &&
181 check_fsck "dangling commit $K"
184 test_expect_success
'--stale-fix handles missing objects generously' '
185 git -c core.logAllRefUpdates=false fast-import --date-format=now <<-EOS &&
186 commit refs/heads/stale-fix
188 committer Author <a@uth.or> now
196 commit refs/heads/stale-fix
197 committer Author <a@uth.or> now
204 parent_oid=$(git rev-parse stale-fix^) &&
205 test_when_finished "recover $parent_oid" &&
206 corrupt $parent_oid &&
207 git reflog expire --stale-fix
210 test_expect_success
'prune and fsck' '
215 check_have A B C D E H L &&
216 check_dont_have F G I J K
220 test_expect_success
'recover and check' '
223 check_fsck "dangling blob $F"
227 test_expect_success
'delete' '
230 git commit -m rat C &&
234 git commit -m ox C &&
238 git commit -m tiger C &&
240 HEAD_entry_count=$(git reflog | wc -l) &&
241 main_entry_count=$(git reflog show main | wc -l) &&
243 test $HEAD_entry_count = 5 &&
244 test $main_entry_count = 5 &&
247 git reflog delete main@{1} &&
248 git reflog show main > output &&
249 test_line_count = $(($main_entry_count - 1)) output &&
250 test $HEAD_entry_count = $(git reflog | wc -l) &&
251 ! grep ox < output &&
253 main_entry_count=$(wc -l < output) &&
255 git reflog delete HEAD@{1} &&
256 test $(($HEAD_entry_count -1)) = $(git reflog | wc -l) &&
257 test $main_entry_count = $(git reflog show main | wc -l) &&
259 HEAD_entry_count=$(git reflog | wc -l) &&
261 git reflog delete main@{07.04.2005.15:15:00.-0700} &&
262 git reflog show main > output &&
263 test_line_count = $(($main_entry_count - 1)) output &&
264 ! grep dragon < output
268 test_expect_success
'rewind2' '
270 test_tick && git reset --hard HEAD~2 &&
271 git reflog refs/heads/main >output &&
272 test_line_count = 4 output
275 test_expect_success
'--expire=never' '
277 git reflog expire --verbose \
279 --expire-unreachable=never \
281 git reflog refs/heads/main >output &&
282 test_line_count = 4 output
285 test_expect_success
'gc.reflogexpire=never' '
286 test_config gc.reflogexpire never &&
287 test_config gc.reflogexpireunreachable never &&
289 git reflog expire --verbose --all >output &&
290 test_line_count = 9 output &&
292 git reflog refs/heads/main >output &&
293 test_line_count = 4 output
296 test_expect_success
'gc.reflogexpire=false' '
297 test_config gc.reflogexpire false &&
298 test_config gc.reflogexpireunreachable false &&
300 git reflog expire --verbose --all &&
301 git reflog refs/heads/main >output &&
302 test_line_count = 4 output
306 test_expect_success
'git reflog expire unknown reference' '
307 test_config gc.reflogexpire never &&
308 test_config gc.reflogexpireunreachable never &&
310 test_must_fail git reflog expire main@{123} 2>stderr &&
311 test_grep "points nowhere" stderr &&
312 test_must_fail git reflog expire does-not-exist 2>stderr &&
313 test_grep "points nowhere" stderr
316 test_expect_success
'checkout should not delete log for packed ref' '
317 test $(git reflog main | wc -l) = 4 &&
319 git pack-refs --all &&
321 test $(git reflog main | wc -l) = 4
324 test_expect_success
'stale dirs do not cause d/f conflicts (reflogs on)' '
325 test_when_finished "git branch -d one || git branch -d one/two" &&
327 git branch one/two main &&
328 echo "one/two@{0} branch: Created from main" >expect &&
329 git log -g --format="%gd %gs" one/two >actual &&
330 test_cmp expect actual &&
331 git branch -d one/two &&
333 # now logs/refs/heads/one is a stale directory, but
334 # we should move it out of the way to create "one" reflog
335 git branch one main &&
336 echo "one@{0} branch: Created from main" >expect &&
337 git log -g --format="%gd %gs" one >actual &&
338 test_cmp expect actual
341 test_expect_success
'stale dirs do not cause d/f conflicts (reflogs off)' '
342 test_when_finished "git branch -d one || git branch -d one/two" &&
344 git branch one/two main &&
345 echo "one/two@{0} branch: Created from main" >expect &&
346 git log -g --format="%gd %gs" one/two >actual &&
347 test_cmp expect actual &&
348 git branch -d one/two &&
350 # same as before, but we only create a reflog for "one" if
351 # it already exists, which it does not
352 git -c core.logallrefupdates=false branch one main &&
353 git log -g --format="%gd %gs" one >actual &&
354 test_must_be_empty actual
357 test_expect_success
'no segfaults for reflog containing non-commit sha1s' '
358 git update-ref --create-reflog -m "Creating ref" \
359 refs/tests/tree-in-reflog HEAD &&
360 git update-ref -m "Forcing tree" refs/tests/tree-in-reflog HEAD^{tree} &&
361 git update-ref -m "Restoring to commit" refs/tests/tree-in-reflog HEAD &&
362 git reflog refs/tests/tree-in-reflog
365 test_expect_failure
'reflog with non-commit entries displays all entries' '
366 git reflog refs/tests/tree-in-reflog >actual &&
367 test_line_count = 3 actual
370 test_expect_success
'continue walking past root commits' '
371 git init orphanage &&
374 cat >expect <<-\EOF &&
375 HEAD@{0} commit (initial): orphan2-1
376 HEAD@{1} commit: orphan1-2
377 HEAD@{2} commit (initial): orphan1-1
378 HEAD@{3} commit (initial): initial
380 test_commit initial &&
381 git checkout --orphan orphan1 &&
382 test_commit orphan1-1 &&
383 test_commit orphan1-2 &&
384 git checkout --orphan orphan2 &&
385 test_commit orphan2-1 &&
386 git log -g --format="%gd %gs" >actual &&
387 test_cmp expect actual
391 test_expect_success
'expire with multiple worktrees' '
397 git worktree add link-wt &&
399 test_commit -C link-wt foobar &&
401 git reflog expire --verbose --all --expire=$test_tick &&
402 test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD >actual &&
403 test_must_be_empty actual
407 test_expect_success
'expire one of multiple worktrees' '
413 git worktree add link-wt &&
415 test_commit -C link-wt foobar &&
417 test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD \
419 git reflog expire --verbose --all --expire=$test_tick \
421 test-tool ref-store worktree:main for-each-reflog-ent HEAD \
423 test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD \
425 test_must_be_empty actual-main &&
426 test_cmp expect-link-wt actual-link-wt
430 test_expect_success
'empty reflog' '
431 test_when_finished "rm -rf empty" &&
433 test_commit -C empty A &&
434 test-tool ref-store main create-reflog refs/heads/foo &&
435 git -C empty reflog expire --all 2>err &&
436 test_must_be_empty err
439 test_expect_success
'list reflogs' '
440 test_when_finished "rm -rf repo" &&
444 git reflog list >actual &&
445 test_must_be_empty actual &&
448 cat >expect <<-EOF &&
452 git reflog list >actual &&
453 test_cmp expect actual &&
456 cat >expect <<-EOF &&
461 git reflog list >actual &&
462 test_cmp expect actual
466 test_expect_success
'list reflogs with worktree' '
467 test_when_finished "rm -rf repo" &&
473 git worktree add wt &&
474 git -c core.logAllRefUpdates=always \
475 update-ref refs/worktree/main HEAD &&
476 git -c core.logAllRefUpdates=always \
477 update-ref refs/worktree/per-worktree HEAD &&
478 git -c core.logAllRefUpdates=always -C wt \
479 update-ref refs/worktree/per-worktree HEAD &&
480 git -c core.logAllRefUpdates=always -C wt \
481 update-ref refs/worktree/worktree HEAD &&
483 cat >expect <<-EOF &&
488 refs/worktree/per-worktree
490 git reflog list >actual &&
491 test_cmp expect actual &&
493 cat >expect <<-EOF &&
497 refs/worktree/per-worktree
498 refs/worktree/worktree
500 git -C wt reflog list >actual &&
501 test_cmp expect actual
505 test_expect_success
'reflog list returns error with additional args' '
506 cat >expect <<-EOF &&
507 error: list does not accept arguments: ${SQ}bogus${SQ}
509 test_must_fail git reflog list bogus 2>err &&
513 test_expect_success
'reflog for symref with unborn target can be listed' '
514 test_when_finished "rm -rf repo" &&
519 git symbolic-ref HEAD refs/heads/unborn &&
520 cat >expect <<-EOF &&
524 git reflog list >actual &&
525 test_cmp expect actual
529 test_expect_success
'reflog with invalid object ID can be listed' '
530 test_when_finished "rm -rf repo" &&
535 test-tool ref-store main update-ref msg refs/heads/missing \
536 $(test_oid deadbeef) "$ZERO_OID" REF_SKIP_OID_VERIFICATION &&
537 cat >expect <<-EOF &&
542 git reflog list >actual &&
543 test_cmp expect actual