Skip to content

Commit bfc2f9e

Browse files
pks-tgitster
authored andcommitted
builtin/gc: forward git-gc(1)'s --auto flag when packing refs
Forward the `--auto` flag to git-pack-refs(1) when it has been invoked with this flag itself. This does not change anything for the "files" backend, which will continue to eagerly pack refs. But it does ensure that the "reftable" backend only compacts refs as required. This change does not impact git-maintenance(1) because this command will in fact never run the pack-refs task when run with `--auto`. This issue will be addressed in a subsequent commit. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 77257e3 commit bfc2f9e

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

builtin/gc.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *
212212

213213
cmd.git_cmd = 1;
214214
strvec_pushl(&cmd.args, "pack-refs", "--all", "--prune", NULL);
215+
if (opts->auto_flag)
216+
strvec_push(&cmd.args, "--auto");
217+
215218
return run_command(&cmd);
216219
}
217220

@@ -572,7 +575,7 @@ static int report_last_gc_error(void)
572575
return ret;
573576
}
574577

575-
static void gc_before_repack(void)
578+
static void gc_before_repack(struct maintenance_run_opts *opts)
576579
{
577580
/*
578581
* We may be called twice, as both the pre- and
@@ -583,7 +586,7 @@ static void gc_before_repack(void)
583586
if (done++)
584587
return;
585588

586-
if (pack_refs && maintenance_task_pack_refs(NULL))
589+
if (pack_refs && maintenance_task_pack_refs(opts))
587590
die(FAILED_RUN, "pack-refs");
588591

589592
if (prune_reflogs) {
@@ -599,7 +602,6 @@ static void gc_before_repack(void)
599602
int cmd_gc(int argc, const char **argv, const char *prefix)
600603
{
601604
int aggressive = 0;
602-
int auto_gc = 0;
603605
int quiet = 0;
604606
int force = 0;
605607
const char *name;
@@ -608,6 +610,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
608610
int keep_largest_pack = -1;
609611
timestamp_t dummy;
610612
struct child_process rerere_cmd = CHILD_PROCESS_INIT;
613+
struct maintenance_run_opts opts = {0};
611614

612615
struct option builtin_gc_options[] = {
613616
OPT__QUIET(&quiet, N_("suppress progress reporting")),
@@ -618,7 +621,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
618621
OPT_MAGNITUDE(0, "max-cruft-size", &max_cruft_size,
619622
N_("with --cruft, limit the size of new cruft packs")),
620623
OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
621-
OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"),
624+
OPT_BOOL_F(0, "auto", &opts.auto_flag, N_("enable auto-gc mode"),
622625
PARSE_OPT_NOCOMPLETE),
623626
OPT_BOOL_F(0, "force", &force,
624627
N_("force running gc even if there may be another gc running"),
@@ -663,7 +666,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
663666
if (quiet)
664667
strvec_push(&repack, "-q");
665668

666-
if (auto_gc) {
669+
if (opts.auto_flag) {
667670
/*
668671
* Auto-gc should be least intrusive as possible.
669672
*/
@@ -688,7 +691,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
688691

689692
if (lock_repo_for_gc(force, &pid))
690693
return 0;
691-
gc_before_repack(); /* dies on failure */
694+
gc_before_repack(&opts); /* dies on failure */
692695
delete_tempfile(&pidfile);
693696

694697
/*
@@ -713,7 +716,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
713716

714717
name = lock_repo_for_gc(force, &pid);
715718
if (name) {
716-
if (auto_gc)
719+
if (opts.auto_flag)
717720
return 0; /* be quiet on --auto */
718721
die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
719722
name, (uintmax_t)pid);
@@ -728,7 +731,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
728731
atexit(process_log_file_at_exit);
729732
}
730733

731-
gc_before_repack();
734+
gc_before_repack(&opts);
732735

733736
if (!repository_format_precious_objects) {
734737
struct child_process repack_cmd = CHILD_PROCESS_INIT;
@@ -783,7 +786,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
783786
!quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0,
784787
NULL);
785788

786-
if (auto_gc && too_many_loose_objects())
789+
if (opts.auto_flag && too_many_loose_objects())
787790
warning(_("There are too many unreachable loose objects; "
788791
"run 'git prune' to remove them."));
789792

t/t0610-reftable-basics.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,22 +387,34 @@ test_expect_success 'pack-refs: compaction raises locking errors' '
387387
test_cmp expect err
388388
'
389389

390-
test_expect_success 'pack-refs: auto compaction' '
390+
for command in pack-refs gc
391+
do
392+
test_expect_success "$command: auto compaction" '
391393
test_when_finished "rm -rf repo" &&
392394
git init repo &&
393395
(
394396
cd repo &&
395397
396398
test_commit A &&
397399
400+
# We need a bit of setup to ensure that git-gc(1) actually
401+
# triggers, and that it does not write anything to the refdb.
402+
git config gc.auto 1 &&
403+
git config gc.autoDetach 0 &&
404+
git config gc.reflogExpire never &&
405+
git config gc.reflogExpireUnreachable never &&
406+
test_oid blob17_1 | git hash-object -w --stdin &&
407+
398408
# The tables should have been auto-compacted, and thus auto
399409
# compaction should not have to do anything.
400410
ls -1 .git/reftable >tables-expect &&
401411
test_line_count = 4 tables-expect &&
402-
git pack-refs --auto &&
412+
git $command --auto &&
403413
ls -1 .git/reftable >tables-actual &&
404414
test_cmp tables-expect tables-actual &&
405415
416+
test_oid blob17_2 | git hash-object -w --stdin &&
417+
406418
# Lock all tables write some refs. Auto-compaction will be
407419
# unable to compact tables and thus fails gracefully, leaving
408420
# the stack in a sub-optimal state.
@@ -416,10 +428,11 @@ test_expect_success 'pack-refs: auto compaction' '
416428
rm .git/reftable/*.lock &&
417429
test_line_count = 5 .git/reftable/tables.list &&
418430
419-
git pack-refs --auto &&
431+
git $command --auto &&
420432
test_line_count = 1 .git/reftable/tables.list
421433
)
422434
'
435+
done
423436

424437
test_expect_success 'pack-refs: prunes stale tables' '
425438
test_when_finished "rm -rf repo" &&

0 commit comments

Comments
 (0)