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

[PATCH v2 3/9] builtin/maintenance: introduce "geometric-repack" task

From
Patrick Steinhardt <ps@pks.im>
Date
Oct 21, 2025, 14:13 UTC
Message-ID
<20251021-pks-maintenance-geometric-strategy-v2-3-f0d727832b80@pks.im>
In-Reply-To
<20251021-pks-maintenance-geometric-strategy-v2-0-f0d727832b80@pks.im>

Introduce a new "geometric-repack" task. This task uses our geometric repack infrastructure as provided by git-repack(1) itself, which is a strategy that especially hosting providers tend to use to amortize the costs of repacking objects.

There is one issue though with geometric repacks, namely that they unconditionally pack all loose objects, regardless of whether or not they are reachable. This is done because it means that we can completely skip the reachability step, which significantly speeds up the operation. But it has the big downside that we are unable to expire objects over time.

To address this issue we thus use a split strategy in this new task: whenever a geometric repack would merge together all packs, we instead do an all-into-one repack. By default, these all-into-one repacks have cruft packs enabled, so unreachable objects would now be written into their own pack. Consequently, they won't be soaked up during geometric repacking anymore and can be expired with the next full repack, assuming that their expiry date has surpassed.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 Documentation/config/maintenance.adoc |  11 +++
 builtin/gc.c                          | 102 +++++++++++++++++++++++++
 t/t7900-maintenance.sh                | 139 ++++++++++++++++++++++++++++++++++
 3 files changed, 252 insertions(+)
diff --git a/Documentation/config/maintenance.adoc b/Documentation/config/maintenance.adoc
index 2f719342183..26dc5de423f 100644
--- a/Documentation/config/maintenance.adoc
+++ b/Documentation/config/maintenance.adoc
@@ -75,6 +75,17 @@ maintenance.incremental-repack.auto::
 	number of pack-files not in the multi-pack-index is at least the value
 	of `maintenance.incremental-repack.auto`. The default value is 10.
 
+maintenance.geometric-repack.auto::
+	This integer config option controls how often the `geometric-repack`
+	task should be run as part of `git maintenance run --auto`. If zero,
+	then the `geometric-repack` task will not run with the `--auto`
+	option. A negative value will force the task to run every time.
+	Otherwise, a positive value implies the command should run either when
+	there are packfiles that need to be merged together to retain the
+	geometric progression, or when there are at least this many loose
+	objects that would be written into a new packfile. The default value is
+	100.
+
 maintenance.reflog-expire.auto::
 	This integer config option controls how often the `reflog-expire` task
 	should be run as part of `git maintenance run --auto`. If zero, then
diff --git a/builtin/gc.c b/builtin/gc.c
index 026d3a1d714..2c9ecd464d2 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -34,6 +34,7 @@
 #include "pack-objects.h"
 #include "path.h"
 #include "reflog.h"
+#include "repack.h"
 #include "rerere.h"
 #include "blob.h"
 #include "tree.h"
@@ -254,6 +255,7 @@ enum maintenance_task_label {
 	TASK_PREFETCH,
 	TASK_LOOSE_OBJECTS,
 	TASK_INCREMENTAL_REPACK,
+	TASK_GEOMETRIC_REPACK,
 	TASK_GC,
 	TASK_COMMIT_GRAPH,
 	TASK_PACK_REFS,
@@ -1566,6 +1568,101 @@ static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts
 	return 0;
 }
 
+static int maintenance_task_geometric_repack(struct maintenance_run_opts *opts,
+					     struct gc_config *cfg)
+{
+	struct pack_geometry geometry = {
+		.split_factor = 2,
+	};
+	struct pack_objects_args po_args = {
+		.local = 1,
+	};
+	struct existing_packs existing_packs = EXISTING_PACKS_INIT;
+	struct string_list kept_packs = STRING_LIST_INIT_DUP;
+	struct child_process child = CHILD_PROCESS_INIT;
+	int ret;
+
+	existing_packs.repo = the_repository;
+	existing_packs_collect(&existing_packs, &kept_packs);
+	pack_geometry_init(&geometry, &existing_packs, &po_args);
+	pack_geometry_split(&geometry);
+
+	child.git_cmd = 1;
+
+	strvec_pushl(&child.args, "repack", "-d", "-l", NULL);
+	if (geometry.split < geometry.pack_nr)
+		strvec_push(&child.args, "--geometric=2");
+	else
+		add_repack_all_option(cfg, NULL, &child.args);
+	if (opts->quiet)
+		strvec_push(&child.args, "--quiet");
+	if (the_repository->settings.core_multi_pack_index)
+		strvec_push(&child.args, "--write-midx");
+
+	if (run_command(&child)) {
+		ret = error(_("failed to perform geometric repack"));
+		goto out;
+	}
+
+	ret = 0;
+
+out:
+	existing_packs_release(&existing_packs);
+	pack_geometry_release(&geometry);
+	return ret;
+}
+
+static int geometric_repack_auto_condition(struct gc_config *cfg UNUSED)
+{
+	struct pack_geometry geometry = {
+		.split_factor = 2,
+	};
+	struct pack_objects_args po_args = {
+		.local = 1,
+	};
+	struct existing_packs existing_packs = EXISTING_PACKS_INIT;
+	struct string_list kept_packs = STRING_LIST_INIT_DUP;
+	int auto_value = 100;
+	int ret;
+
+	repo_config_get_int(the_repository, "maintenance.geometric-repack.auto",
+			    &auto_value);
+	if (!auto_value)
+		return 0;
+	if (auto_value < 0)
+		return 1;
+
+	existing_packs.repo = the_repository;
+	existing_packs_collect(&existing_packs, &kept_packs);
+	pack_geometry_init(&geometry, &existing_packs, &po_args);
+	pack_geometry_split(&geometry);
+
+	/*
+	 * When we'd merge at least two packs with one another we always
+	 * perform the repack.
+	 */
+	if (geometry.split) {
+		ret = 1;
+		goto out;
+	}
+
+	/*
+	 * Otherwise, we estimate the number of loose objects to determine
+	 * whether we want to create a new packfile or not.
+	 */
+	if (too_many_loose_objects(auto_value)) {
+		ret = 1;
+		goto out;
+	}
+
+	ret = 0;
+
+out:
+	existing_packs_release(&existing_packs);
+	pack_geometry_release(&geometry);
+	return ret;
+}
+
 typedef int (*maintenance_task_fn)(struct maintenance_run_opts *opts,
 				   struct gc_config *cfg);
 typedef int (*maintenance_auto_fn)(struct gc_config *cfg);
@@ -1608,6 +1705,11 @@ static const struct maintenance_task tasks[] = {
 		.background = maintenance_task_incremental_repack,
 		.auto_condition = incremental_repack_auto_condition,
 	},
+	[TASK_GEOMETRIC_REPACK] = {
+		.name = "geometric-repack",
+		.background = maintenance_task_geometric_repack,
+		.auto_condition = geometric_repack_auto_condition,
+	},
 	[TASK_GC] = {
 		.name = "gc",
 		.foreground = maintenance_task_gc_foreground,
diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh
index ddd273d8dc2..60029a65a35 100755
--- a/t/t7900-maintenance.sh
+++ b/t/t7900-maintenance.sh
@@ -465,6 +465,145 @@ test_expect_success 'maintenance.incremental-repack.auto (when config is unset)'
 	)
 '
 
+run_and_verify_geometric_pack () {
+	EXPECTED_PACKS="$1" &&
+
+	# Verify that we perform a geometric repack.
+	rm -f "trace2.txt" &&
+	GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+		git maintenance run --task=geometric-repack 2>/dev/null &&
+	test_subcommand git repack -d -l --geometric=2 \
+		--quiet --write-midx <trace2.txt &&
+
+	# Verify that the number of packfiles matches our expectation.
+	ls -l .git/objects/pack/*.pack >packfiles &&
+	test_line_count = "$EXPECTED_PACKS" packfiles &&
+
+	# And verify that there are no loose objects anymore.
+	cat >expect <<-\EOF &&
+	info
+	pack
+	EOF
+	ls .git/objects >actual &&
+	test_cmp expect actual
+}
+
+test_expect_success 'geometric repacking task' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		git config set maintenance.auto false &&
+		test_commit initial &&
+
+		# The initial repack causes an all-into-one repack.
+		GIT_TRACE2_EVENT="$(pwd)/initial-repack.txt" \
+			git maintenance run --task=geometric-repack 2>/dev/null &&
+		test_subcommand git repack -d -l --cruft --cruft-expiration=2.weeks.ago --quiet --write-midx <initial-repack.txt &&
+
+		# Repacking should now cause a no-op geometric repack because
+		# no packfiles need to be combined.
+		ls -l .git/objects/pack >before &&
+		run_and_verify_geometric_pack 1 &&
+		ls -l .git/objects/pack >after &&
+		test_cmp before after &&
+
+		# This incremental change creates a new packfile that only
+		# soaks up loose objects. The packfiles are not getting merged
+		# at this point.
+		test_commit loose &&
+		run_and_verify_geometric_pack 2 &&
+
+		# Both packfiles have 3 objects, so the next run would cause us
+		# to merge all packfiles together. This should be turned into
+		# an all-into-one-repack.
+		GIT_TRACE2_EVENT="$(pwd)/all-into-one-repack.txt" \
+			git maintenance run --task=geometric-repack 2>/dev/null &&
+		test_subcommand git repack -d -l --cruft --cruft-expiration=2.weeks.ago --quiet --write-midx <all-into-one-repack.txt &&
+
+		# The geometric repack soaks up unreachable objects.
+		echo blob-1 | git hash-object -w --stdin -t blob &&
+		run_and_verify_geometric_pack 2 &&
+
+		# A second unreachable object should be written into another packfile.
+		echo blob-2 | git hash-object -w --stdin -t blob &&
+		run_and_verify_geometric_pack 3 &&
+
+		# And these two small packs should now be merged via the
+		# geometric repack. The large packfile should remain intact.
+		run_and_verify_geometric_pack 2 &&
+
+		# If we now add two more objects and repack twice we should
+		# then see another all-into-one repack. This time around
+		# though, as we have unreachable objects, we should also see a
+		# cruft pack.
+		echo blob-3 | git hash-object -w --stdin -t blob &&
+		echo blob-4 | git hash-object -w --stdin -t blob &&
+		run_and_verify_geometric_pack 3 &&
+		GIT_TRACE2_EVENT="$(pwd)/cruft-repack.txt" \
+			git maintenance run --task=geometric-repack 2>/dev/null &&
+		test_subcommand git repack -d -l --cruft --cruft-expiration=2.weeks.ago --quiet --write-midx <cruft-repack.txt &&
+		ls .git/objects/pack/*.pack >packs &&
+		test_line_count = 2 packs &&
+		ls .git/objects/pack/*.mtimes >cruft &&
+		test_line_count = 1 cruft
+	)
+'
+
+test_geometric_repack_needed () {
+	NEEDED="$1"
+	GEOMETRIC_CONFIG="$2" &&
+	rm -f trace2.txt &&
+	GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+		git ${GEOMETRIC_CONFIG:+-c maintenance.geometric-repack.$GEOMETRIC_CONFIG} \
+		maintenance run --auto --task=geometric-repack 2>/dev/null &&
+	case "$NEEDED" in
+	true)
+		test_grep "\[\"git\",\"repack\"," trace2.txt;;
+	false)
+		! test_grep "\[\"git\",\"repack\"," trace2.txt;;
+	*)
+		BUG "invalid parameter: $NEEDED";;
+	esac
+}
+
+test_expect_success 'geometric repacking with --auto' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+
+		# An empty repository does not need repacking, except when
+		# explicitly told to do it.
+		test_geometric_repack_needed false &&
+		test_geometric_repack_needed false auto=0 &&
+		test_geometric_repack_needed false auto=1 &&
+		test_geometric_repack_needed true auto=-1 &&
+
+		test_oid_init &&
+
+		# Loose objects cause a repack when crossing the limit. Note
+		# that the number of objects gets extrapolated by having a look
+		# at the "objects/17/" shard.
+		test_commit "$(test_oid blob17_1)" &&
+		test_geometric_repack_needed false &&
+		test_commit "$(test_oid blob17_2)" &&
+		test_geometric_repack_needed false auto=257 &&
+		test_geometric_repack_needed true auto=256 &&
+
+		# Force another repack.
+		test_commit first &&
+		test_commit second &&
+		test_geometric_repack_needed true auto=-1 &&
+
+		# We now have two packfiles that would be merged together. As
+		# such, the repack should always happen unless the user has
+		# disabled the auto task.
+		test_geometric_repack_needed false auto=0 &&
+		test_geometric_repack_needed true auto=9000
+	)
+'
+
 test_expect_success 'pack-refs task' '
 	for n in $(test_seq 1 5)
 	do
-- 
2.51.1.851.g4ebd6896fd.dirty
Previous: Patrick SteinhardtNext: Taylor Blau
Message 22 of 69 in “builtin/maintenance: introduce "geometric" strategy”
  1. 0/8 builtin/maintenance: introduce "geometric" strategyPatrick Steinhardt, Oct 16, 2025
  2. 1/8 builtin/gc: remove global `repack` variablePatrick Steinhardt, Oct 16, 2025
  3. Justin ToblerOct 16, 2025
  4. Taylor BlauOct 17, 2025
  5. 2/8 builtin/gc: make `too_many_loose_objects()` reusable without GC configPatrick Steinhardt, Oct 16, 2025
  6. Junio C HamanoOct 16, 2025
  7. 3/8 builtin/maintenance: introduce "geometric-repack" taskPatrick Steinhardt, Oct 16, 2025
  8. Justin ToblerOct 16, 2025
  9. Patrick SteinhardtOct 17, 2025
  10. Taylor BlauOct 17, 2025
  11. Patrick SteinhardtOct 21, 2025
  12. Taylor BlauOct 23, 2025
  13. Patrick SteinhardtOct 24, 2025
  14. 4/8 builtin/maintenance: don't silently ignore invalid strategyPatrick Steinhardt, Oct 16, 2025
  15. 5/8 builtin/maintenance: run maintenance tasks depending on typePatrick Steinhardt, Oct 16, 2025
  16. 6/8 builtin/maintenance: extend "maintenance.strategy" to manual maintenancePatrick Steinhardt, Oct 16, 2025
  17. 7/8 builtin/maintenance: make "gc" strategy accessiblePatrick Steinhardt, Oct 16, 2025
  18. 8/8 builtin/maintenance: introduce "geometric" strategyPatrick Steinhardt, Oct 16, 2025
  19. 0/9 builtin/maintenance: introduce "geometric" strategyPatrick Steinhardt, Oct 21, 2025
  20. 1/9 builtin/gc: remove global `repack` variablePatrick Steinhardt, Oct 21, 2025
  21. 2/9 builtin/gc: make `too_many_loose_objects()` reusable without GC configPatrick Steinhardt, Oct 21, 2025
  22. 3/9 builtin/maintenance: introduce "geometric-repack" taskPatrick Steinhardt, Oct 21, 2025
  23. Taylor BlauOct 23, 2025
  24. Patrick SteinhardtOct 24, 2025
  25. 4/9 builtin/maintenance: make the geometric factor configurablePatrick Steinhardt, Oct 21, 2025
  26. Taylor BlauOct 23, 2025
  27. Patrick SteinhardtOct 24, 2025
  28. Taylor BlauOct 24, 2025
  29. 5/9 builtin/maintenance: don't silently ignore invalid strategyPatrick Steinhardt, Oct 21, 2025
  30. Taylor BlauOct 23, 2025
  31. 6/9 builtin/maintenance: run maintenance tasks depending on typePatrick Steinhardt, Oct 21, 2025
  32. Taylor BlauOct 23, 2025
  33. 7/9 builtin/maintenance: extend "maintenance.strategy" to manual maintenancePatrick Steinhardt, Oct 21, 2025
  34. 8/9 builtin/maintenance: make "gc" strategy accessiblePatrick Steinhardt, Oct 21, 2025
  35. 9/9 builtin/maintenance: introduce "geometric" strategyPatrick Steinhardt, Oct 21, 2025
  36. Taylor BlauOct 23, 2025
  37. Patrick SteinhardtOct 24, 2025
  38. Junio C HamanoOct 23, 2025
  39. Taylor BlauOct 23, 2025
  40. 00/10 builtin/maintenance: introduce "geometric" strategyPatrick Steinhardt, Oct 24, 2025
  41. 01/10 builtin/gc: remove global `repack` variablePatrick Steinhardt, Oct 24, 2025
  42. 02/10 builtin/gc: make `too_many_loose_objects()` reusable without GC configPatrick Steinhardt, Oct 24, 2025
  43. 03/10 builtin/maintenance: introduce "geometric-repack" taskPatrick Steinhardt, Oct 24, 2025
  44. Jeff KingOct 25, 2025
  45. Patrick SteinhardtOct 27, 2025
  46. Jeff KingOct 27, 2025
  47. 04/10 builtin/maintenance: make the geometric factor configurablePatrick Steinhardt, Oct 24, 2025
  48. 05/10 builtin/maintenance: don't silently ignore invalid strategyPatrick Steinhardt, Oct 24, 2025
  49. 06/10 builtin/maintenance: improve readability of strategiesPatrick Steinhardt, Oct 24, 2025
  50. 07/10 builtin/maintenance: run maintenance tasks depending on typePatrick Steinhardt, Oct 24, 2025
  51. 08/10 builtin/maintenance: extend "maintenance.strategy" to manual maintenancePatrick Steinhardt, Oct 24, 2025
  52. 09/10 builtin/maintenance: make "gc" strategy accessiblePatrick Steinhardt, Oct 24, 2025
  53. 10/10 builtin/maintenance: introduce "geometric" strategyPatrick Steinhardt, Oct 24, 2025
  54. Taylor BlauOct 24, 2025
  55. Junio C HamanoOct 24, 2025
  56. 00/10 builtin/maintenance: introduce "geometric" strategyPatrick Steinhardt, Oct 27, 2025
  57. 01/10 builtin/gc: remove global `repack` variablePatrick Steinhardt, Oct 27, 2025
  58. 02/10 builtin/gc: make `too_many_loose_objects()` reusable without GC configPatrick Steinhardt, Oct 27, 2025
  59. 03/10 builtin/maintenance: introduce "geometric-repack" taskPatrick Steinhardt, Oct 27, 2025
  60. 04/10 builtin/maintenance: make the geometric factor configurablePatrick Steinhardt, Oct 27, 2025
  61. 05/10 builtin/maintenance: don't silently ignore invalid strategyPatrick Steinhardt, Oct 27, 2025
  62. 06/10 builtin/maintenance: improve readability of strategiesPatrick Steinhardt, Oct 27, 2025
  63. 07/10 builtin/maintenance: run maintenance tasks depending on typePatrick Steinhardt, Oct 27, 2025
  64. 08/10 builtin/maintenance: extend "maintenance.strategy" to manual maintenancePatrick Steinhardt, Oct 27, 2025
  65. 09/10 builtin/maintenance: make "gc" strategy accessiblePatrick Steinhardt, Oct 27, 2025
  66. 10/10 builtin/maintenance: introduce "geometric" strategyPatrick Steinhardt, Oct 27, 2025
  67. Junio C HamanoOct 27, 2025
  68. Patrick SteinhardtOct 27, 2025
  69. Junio C HamanoOct 27, 2025

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.