Git fork

builtin/maintenance: introduce "rerere-gc" task

While git-gc(1) knows to garbage collect the rerere cache,
git-maintenance(1) does not yet have a task for this cleanup. Introduce
a new "rerere-gc" task to plug this gap.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Patrick Steinhardt and committed by
Junio C Hamano
283621a5 255251cc

+94
+9
Documentation/config/maintenance.adoc
··· 84 84 expired reflog entries in the "HEAD" reflog is at least the value of 85 85 `maintenance.loose-objects.auto`. The default value is 100. 86 86 87 + maintenance.rerere-gc.auto:: 88 + This integer config option controls how often the `rerere-gc` task 89 + should be run as part of `git maintenance run --auto`. If zero, then 90 + the `rerere-gc` task will not run with the `--auto` option. A negative 91 + value will force the task to run every time. Otherwise, any positive 92 + value implies the command will run when the "rr-cache" directory exists 93 + and has at least one entry, regardless of whether it is stale or not. 94 + This heuristic may be refined in the future. The default value is 1. 95 + 87 96 maintenance.worktree-prune.auto:: 88 97 This integer config option controls how often the `worktree-prune` task 89 98 should be run as part of `git maintenance run --auto`. If zero, then
+4
Documentation/git-maintenance.adoc
··· 166 166 The `reflog-expire` task deletes any entries in the reflog older than the 167 167 expiry threshold. See linkgit:git-reflog[1] for more information. 168 168 169 + rerere-gc:: 170 + The `rerere-gc` task invokes garbage collection for stale entries in 171 + the rerere cache. See linkgit:git-rerere[1] for more information. 172 + 169 173 worktree-prune:: 170 174 The `worktree-prune` task deletes stale or broken worktrees. See 171 175 linkit:git-worktree[1] for more information.
+37
builtin/gc.c
··· 16 16 #include "builtin.h" 17 17 #include "abspath.h" 18 18 #include "date.h" 19 + #include "dir.h" 19 20 #include "environment.h" 20 21 #include "hex.h" 21 22 #include "config.h" ··· 33 34 #include "pack-objects.h" 34 35 #include "path.h" 35 36 #include "reflog.h" 37 + #include "rerere.h" 36 38 #include "blob.h" 37 39 #include "tree.h" 38 40 #include "promisor-remote.h" ··· 391 393 rerere_cmd.git_cmd = 1; 392 394 strvec_pushl(&rerere_cmd.args, "rerere", "gc", NULL); 393 395 return run_command(&rerere_cmd); 396 + } 397 + 398 + static int rerere_gc_condition(struct gc_config *cfg UNUSED) 399 + { 400 + struct strbuf path = STRBUF_INIT; 401 + int should_gc = 0, limit = 1; 402 + DIR *dir = NULL; 403 + 404 + git_config_get_int("maintenance.rerere-gc.auto", &limit); 405 + if (limit <= 0) { 406 + should_gc = limit < 0; 407 + goto out; 408 + } 409 + 410 + /* 411 + * We skip garbage collection in case we either have no "rr-cache" 412 + * directory or when it doesn't contain at least one entry. 413 + */ 414 + repo_git_path_replace(the_repository, &path, "rr-cache"); 415 + dir = opendir(path.buf); 416 + if (!dir) 417 + goto out; 418 + should_gc = !!readdir_skip_dot_and_dotdot(dir); 419 + 420 + out: 421 + strbuf_release(&path); 422 + if (dir) 423 + closedir(dir); 424 + return should_gc; 394 425 } 395 426 396 427 static int too_many_loose_objects(struct gc_config *cfg) ··· 1511 1542 TASK_PACK_REFS, 1512 1543 TASK_REFLOG_EXPIRE, 1513 1544 TASK_WORKTREE_PRUNE, 1545 + TASK_RERERE_GC, 1514 1546 1515 1547 /* Leave as final value */ 1516 1548 TASK__COUNT ··· 1556 1588 "worktree-prune", 1557 1589 maintenance_task_worktree_prune, 1558 1590 worktree_prune_condition, 1591 + }, 1592 + [TASK_RERERE_GC] = { 1593 + "rerere-gc", 1594 + maintenance_task_rerere_gc, 1595 + rerere_gc_condition, 1559 1596 }, 1560 1597 }; 1561 1598
+44
t/t7900-maintenance.sh
··· 564 564 test_path_is_missing .git/worktrees/worktree 565 565 ' 566 566 567 + test_expect_rerere_gc () { 568 + negate= 569 + if test "$1" = "!" 570 + then 571 + negate="!" 572 + shift 573 + fi 574 + 575 + rm -f "rerere-gc.txt" && 576 + GIT_TRACE2_EVENT="$(pwd)/rerere-gc.txt" "$@" && 577 + test_subcommand $negate git rerere gc <rerere-gc.txt 578 + } 579 + 580 + test_expect_success 'rerere-gc task without --auto always collects garbage' ' 581 + test_expect_rerere_gc git maintenance run --task=rerere-gc 582 + ' 583 + 584 + test_expect_success 'rerere-gc task with --auto only prunes with prunable entries' ' 585 + test_when_finished "rm -rf .git/rr-cache" && 586 + test_expect_rerere_gc ! git maintenance run --auto --task=rerere-gc && 587 + mkdir .git/rr-cache && 588 + test_expect_rerere_gc ! git maintenance run --auto --task=rerere-gc && 589 + : >.git/rr-cache/entry && 590 + test_expect_rerere_gc git maintenance run --auto --task=rerere-gc 591 + ' 592 + 593 + test_expect_success 'rerere-gc task with --auto honors maintenance.rerere-gc.auto' ' 594 + test_when_finished "rm -rf .git/rr-cache" && 595 + 596 + # A negative value should always prune. 597 + test_expect_rerere_gc git -c maintenance.rerere-gc.auto=-1 maintenance run --auto --task=rerere-gc && 598 + 599 + # A positive value prunes when there is at least one entry. 600 + test_expect_rerere_gc ! git -c maintenance.rerere-gc.auto=9000 maintenance run --auto --task=rerere-gc && 601 + mkdir .git/rr-cache && 602 + test_expect_rerere_gc ! git -c maintenance.rerere-gc.auto=9000 maintenance run --auto --task=rerere-gc && 603 + : >.git/rr-cache/entry-1 && 604 + test_expect_rerere_gc git -c maintenance.rerere-gc.auto=9000 maintenance run --auto --task=rerere-gc && 605 + 606 + # Zero should never prune. 607 + : >.git/rr-cache/entry-1 && 608 + test_expect_rerere_gc ! git -c maintenance.rerere-gc.auto=0 maintenance run --auto --task=rerere-gc 609 + ' 610 + 567 611 test_expect_success '--auto and --schedule incompatible' ' 568 612 test_must_fail git maintenance run --auto --schedule=daily 2>err && 569 613 test_grep "at most one" err