git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 14/15] builtin/gc: forward git-gc(1)'s `--auto` flag when packing refs

From
Patrick Steinhardt <ps@pks.im>
Date
Mar 18, 2024, 10:53 UTC
Message-ID
<474cf66b26f5e459dccd60a2dcd1a09eff86178c.1710706119.git.ps@pks.im>
In-Reply-To
<cover.1710706118.git.ps@pks.im>

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) as it won't ever end up executing the pack-refs task when run with `--auto`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/gc.c               | 21 ++++++++++++---------
 t/t0610-reftable-basics.sh | 19 ++++++++++++++++---
 2 files changed, 28 insertions(+), 12 deletions(-)
diff --git a/builtin/gc.c b/builtin/gc.c
index e0029c88f9..bf1f2a621a 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -212,6 +212,9 @@ static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *
 
 	cmd.git_cmd = 1;
 	strvec_pushl(&cmd.args, "pack-refs", "--all", "--prune", NULL);
+	if (opts->auto_flag)
+		strvec_push(&cmd.args, "--auto");
+
 	return run_command(&cmd);
 }
 
@@ -572,7 +575,7 @@ static int report_last_gc_error(void)
 	return ret;
 }
 
-static void gc_before_repack(void)
+static void gc_before_repack(struct maintenance_run_opts *opts)
 {
 	/*
 	 * We may be called twice, as both the pre- and
@@ -583,7 +586,7 @@ static void gc_before_repack(void)
 	if (done++)
 		return;
 
-	if (pack_refs && maintenance_task_pack_refs(NULL))
+	if (pack_refs && maintenance_task_pack_refs(opts))
 		die(FAILED_RUN, "pack-refs");
 
 	if (prune_reflogs) {
@@ -599,7 +602,6 @@ static void gc_before_repack(void)
 int cmd_gc(int argc, const char **argv, const char *prefix)
 {
 	int aggressive = 0;
-	int auto_gc = 0;
 	int quiet = 0;
 	int force = 0;
 	const char *name;
@@ -608,6 +610,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 	int keep_largest_pack = -1;
 	timestamp_t dummy;
 	struct child_process rerere_cmd = CHILD_PROCESS_INIT;
+	struct maintenance_run_opts opts = {0};
 
 	struct option builtin_gc_options[] = {
 		OPT__QUIET(&quiet, N_("suppress progress reporting")),
@@ -618,7 +621,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 		OPT_MAGNITUDE(0, "max-cruft-size", &max_cruft_size,
 			      N_("with --cruft, limit the size of new cruft packs")),
 		OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
-		OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"),
+		OPT_BOOL_F(0, "auto", &opts.auto_flag, N_("enable auto-gc mode"),
 			   PARSE_OPT_NOCOMPLETE),
 		OPT_BOOL_F(0, "force", &force,
 			   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)
 	if (quiet)
 		strvec_push(&repack, "-q");
 
-	if (auto_gc) {
+	if (opts.auto_flag) {
 		/*
 		 * Auto-gc should be least intrusive as possible.
 		 */
@@ -688,7 +691,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 
 			if (lock_repo_for_gc(force, &pid))
 				return 0;
-			gc_before_repack(); /* dies on failure */
+			gc_before_repack(&opts); /* dies on failure */
 			delete_tempfile(&pidfile);
 
 			/*
@@ -713,7 +716,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 
 	name = lock_repo_for_gc(force, &pid);
 	if (name) {
-		if (auto_gc)
+		if (opts.auto_flag)
 			return 0; /* be quiet on --auto */
 		die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
 		    name, (uintmax_t)pid);
@@ -728,7 +731,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 		atexit(process_log_file_at_exit);
 	}
 
-	gc_before_repack();
+	gc_before_repack(&opts);
 
 	if (!repository_format_precious_objects) {
 		struct child_process repack_cmd = CHILD_PROCESS_INIT;
@@ -783,7 +786,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 					     !quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0,
 					     NULL);
 
-	if (auto_gc && too_many_loose_objects())
+	if (opts.auto_flag && too_many_loose_objects())
 		warning(_("There are too many unreachable loose objects; "
 			"run 'git prune' to remove them."));
 
diff --git a/t/t0610-reftable-basics.sh b/t/t0610-reftable-basics.sh
index 6de7529575..b37d8bf3b1 100755
--- a/t/t0610-reftable-basics.sh
+++ b/t/t0610-reftable-basics.sh
@@ -387,7 +387,9 @@ test_expect_success 'pack-refs: compaction raises locking errors' '
 	test_cmp expect err
 '
 
-test_expect_success 'pack-refs: auto compaction' '
+for command in pack-refs gc
+do
+test_expect_success "$command: auto compaction" '
 	test_when_finished "rm -rf repo" &&
 	git init repo &&
 	(
@@ -395,14 +397,24 @@ test_expect_success 'pack-refs: auto compaction' '
 
 		test_commit A &&
 
+		# We need a bit of setup to ensure that git-gc(1) actually
+		# triggers, and that it does not write anything to the refdb.
+		git config gc.auto 1 &&
+		git config gc.autoDetach 0 &&
+		git config gc.reflogExpire never &&
+		git config gc.reflogExpireUnreachable never &&
+		test_oid blob17_1 | git hash-object -w --stdin &&
+
 		# The tables should have been auto-compacted, and thus auto
 		# compaction should not have to do anything.
 		ls -1 .git/reftable >tables-expect &&
 		test_line_count = 4 tables-expect &&
-		git pack-refs --auto &&
+		git $command --auto &&
 		ls -1 .git/reftable >tables-actual &&
 		test_cmp tables-expect tables-actual &&
 
+		test_oid blob17_2 | git hash-object -w --stdin &&
+
 		# Lock all tables write some refs. Auto-compaction will be
 		# unable to compact tables and thus fails gracefully, leaving
 		# the stack in a sub-optimal state.
@@ -416,10 +428,11 @@ test_expect_success 'pack-refs: auto compaction' '
 		rm .git/reftable/*.lock &&
 		test_line_count = 5 .git/reftable/tables.list &&
 
-		git pack-refs --auto &&
+		git $command --auto &&
 		test_line_count = 1 .git/reftable/tables.list
 	)
 '
+done
 
 test_expect_success 'pack-refs: prunes stale tables' '
 	test_when_finished "rm -rf repo" &&
-- 
2.44.0
Previous: Patrick SteinhardtNext: Karthik Nayak
Message 23 of 49 in “refs: introduce `--auto` to pack refs as needed”
  1. 00/15 refs: introduce `--auto` to pack refs as neededPatrick Steinhardt, Mar 18, 2024
  2. 01/15 reftable/stack: fix error handling in `reftable_stack_init_addition()`Patrick Steinhardt, Mar 18, 2024
  3. Karthik NayakMar 20, 2024
  4. Patrick SteinhardtMar 25, 2024
  5. 02/15 reftable/error: discern locked/outdated errorsPatrick Steinhardt, Mar 18, 2024
  6. 03/15 reftable/stack: use error codes when locking fails during compactionPatrick Steinhardt, Mar 18, 2024
  7. Karthik NayakMar 20, 2024
  8. Patrick SteinhardtMar 25, 2024
  9. 04/15 reftable/stack: gracefully handle failed auto-compaction due to locksPatrick Steinhardt, Mar 18, 2024
  10. Karthik NayakMar 20, 2024
  11. Patrick SteinhardtMar 25, 2024
  12. 05/15 refs/reftable: print errors on compaction failurePatrick Steinhardt, Mar 18, 2024
  13. 06/15 t/helper: drop pack-refs wrapperPatrick Steinhardt, Mar 18, 2024
  14. 07/15 refs: move `struct pack_refs_opts` to where it's usedPatrick Steinhardt, Mar 18, 2024
  15. 08/15 refs: remove `PACK_REFS_ALL` flagPatrick Steinhardt, Mar 18, 2024
  16. 09/15 refs/reftable: expose auto compaction via new flagPatrick Steinhardt, Mar 18, 2024
  17. 10/15 builtin/pack-refs: release allocated memoryPatrick Steinhardt, Mar 18, 2024
  18. Karthik NayakMar 20, 2024
  19. Patrick SteinhardtMar 25, 2024
  20. 11/15 builtin/pack-refs: introduce new "--auto" flagPatrick Steinhardt, Mar 18, 2024
  21. 12/15 builtin/gc: move `struct maintenance_run_opts`Patrick Steinhardt, Mar 18, 2024
  22. 13/15 t6500: extract objects with "17" prefixPatrick Steinhardt, Mar 18, 2024
  23. 14/15 builtin/gc: forward git-gc(1)'s `--auto` flag when packing refsPatrick Steinhardt, Mar 18, 2024
  24. Karthik NayakMar 20, 2024
  25. Patrick SteinhardtMar 25, 2024
  26. 15/15 builtin/gc: pack refs when using `git maintenance run --auto`Patrick Steinhardt, Mar 18, 2024
  27. Karthik NayakMar 20, 2024
  28. Patrick SteinhardtMar 25, 2024
  29. Karthik NayakMar 20, 2024
  30. Patrick SteinhardtMar 25, 2024
  31. 00/15 refs: introduce `--auto` to pack refs as neededPatrick Steinhardt, Mar 25, 2024
  32. 01/15 reftable/stack: fix error handling in `reftable_stack_init_addition()`Patrick Steinhardt, Mar 25, 2024
  33. 02/15 reftable/error: discern locked/outdated errorsPatrick Steinhardt, Mar 25, 2024
  34. 03/15 reftable/stack: use error codes when locking fails during compactionPatrick Steinhardt, Mar 25, 2024
  35. 04/15 reftable/stack: gracefully handle failed auto-compaction due to locksPatrick Steinhardt, Mar 25, 2024
  36. Patrick SteinhardtMar 25, 2024
  37. 05/15 refs/reftable: print errors on compaction failurePatrick Steinhardt, Mar 25, 2024
  38. 06/15 t/helper: drop pack-refs wrapperPatrick Steinhardt, Mar 25, 2024
  39. 07/15 refs: move `struct pack_refs_opts` to where it's usedPatrick Steinhardt, Mar 25, 2024
  40. 08/15 refs: remove `PACK_REFS_ALL` flagPatrick Steinhardt, Mar 25, 2024
  41. 09/15 refs/reftable: expose auto compaction via new flagPatrick Steinhardt, Mar 25, 2024
  42. 10/15 builtin/pack-refs: release allocated memoryPatrick Steinhardt, Mar 25, 2024
  43. 11/15 builtin/pack-refs: introduce new "--auto" flagPatrick Steinhardt, Mar 25, 2024
  44. 12/15 builtin/gc: move `struct maintenance_run_opts`Patrick Steinhardt, Mar 25, 2024
  45. 13/15 t6500: extract objects with "17" prefixPatrick Steinhardt, Mar 25, 2024
  46. 14/15 builtin/gc: forward git-gc(1)'s `--auto` flag when packing refsPatrick Steinhardt, Mar 25, 2024
  47. 15/15 builtin/gc: pack refs when using `git maintenance run --auto`Patrick Steinhardt, Mar 25, 2024
  48. Karthik NayakMar 25, 2024
  49. Junio C HamanoMar 25, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.