builtin/pack-refs: introduce new "--auto" flag

Calling git-pack-refs(1) will unconditionally cause it to pack all requested refs regardless of the current state of the ref database. For example: - With the "files" backend we will end up rewriting the complete "packed-refs" file even if only a single ref would require compaction. - With the "reftable" backend we will end up always compacting all tables into a single table. This behaviour can be completely unnecessary depending on the backend and is thus wasteful. With the introduction of the `PACK_REFS_AUTO` flag in the preceding commit we can improve this and let the backends decide for themselves whether to pack refs in the first place. Expose this functionality via a new "--auto" flag in git-pack-refs(1), which mirrors the same flag in both git-gc(1) and git-maintenance(1). Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 25, 2024 at 11:03 UTC 6dcffc68f4cea97d4b4af8a9add0daae54a5c292
4 files changed +57 -2
Documentation/git-pack-refs.txt
+14 -1
@@ -8,7 +8,7 @@ git-pack-refs - Pack heads and tags for efficient repository access
8 SYNOPSIS
9 --------
10 [verse]
11 -'git pack-refs' [--all] [--no-prune] [--include <pattern>] [--exclude <pattern>]
11 +'git pack-refs' [--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]
12
13 DESCRIPTION
14 -----------
@@ -60,6 +60,19 @@ with many branches of historical interests.
60 The command usually removes loose refs under `$GIT_DIR/refs`
61 hierarchy after packing them. This option tells it not to.
62
63 +--auto::
64 +
65 +Pack refs as needed depending on the current state of the ref database. The
66 +behavior depends on the ref format used by the repository and may change in the
67 +future.
68 ++
69 + - "files": No special handling for `--auto` has been implemented.
70 ++
71 + - "reftable": Tables are compacted such that they form a geometric
72 + sequence. For two tables N and N+1, where N+1 is newer, this
73 + maintains the property that N is at least twice as big as N+1. Only
74 + tables that violate this property are compacted.
75 +
76 --include <pattern>::
77
78 Pack refs based on a `glob(7)` pattern. Repetitions of this option
builtin/pack-refs.c
+2 -1
@@ -7,7 +7,7 @@
7 #include "revision.h"
8
9 static char const * const pack_refs_usage[] = {
10 - N_("git pack-refs [--all] [--no-prune] [--include <pattern>] [--exclude <pattern>]"),
10 + N_("git pack-refs [--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]"),
11 NULL
12 };
13
@@ -28,6 +28,7 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
28 struct option opts[] = {
29 OPT_BOOL(0, "all", &pack_all, N_("pack everything")),
30 OPT_BIT(0, "prune", &pack_refs_opts.flags, N_("prune loose refs (default)"), PACK_REFS_PRUNE),
31 + OPT_BIT(0, "auto", &pack_refs_opts.flags, N_("auto-pack refs as needed"), PACK_REFS_AUTO),
32 OPT_STRING_LIST(0, "include", pack_refs_opts.includes, N_("pattern"),
33 N_("references to include")),
34 OPT_STRING_LIST(0, "exclude", &option_excluded_refs, N_("pattern"),
t/t0601-reffiles-pack-refs.sh
+7
@@ -164,6 +164,13 @@ test_expect_success 'test --exclude takes precedence over --include' '
164 git pack-refs --include "refs/heads/pack*" --exclude "refs/heads/pack*" &&
165 test -f .git/refs/heads/dont_pack5'
166
167 +test_expect_success '--auto packs and prunes refs as usual' '
168 + git branch auto &&
169 + test_path_is_file .git/refs/heads/auto &&
170 + git pack-refs --auto --all &&
171 + test_path_is_missing .git/refs/heads/auto
172 +'
173 +
174 test_expect_success 'see if up-to-date packed refs are preserved' '
175 git branch q &&
176 git pack-refs --all --prune &&
t/t0610-reftable-basics.sh
+34
@@ -387,6 +387,40 @@ test_expect_success 'pack-refs: compaction raises locking errors' '
387 test_cmp expect err
388 '
389
390 +test_expect_success 'pack-refs: auto compaction' '
391 + test_when_finished "rm -rf repo" &&
392 + git init repo &&
393 + (
394 + cd repo &&
395 +
396 + test_commit A &&
397 +
398 + # The tables should have been auto-compacted, and thus auto
399 + # compaction should not have to do anything.
400 + ls -1 .git/reftable >tables-expect &&
401 + test_line_count = 4 tables-expect &&
402 + git pack-refs --auto &&
403 + ls -1 .git/reftable >tables-actual &&
404 + test_cmp tables-expect tables-actual &&
405 +
406 + # Lock all tables write some refs. Auto-compaction will be
407 + # unable to compact tables and thus fails gracefully, leaving
408 + # the stack in a sub-optimal state.
409 + ls .git/reftable/*.ref |
410 + while read table
411 + do
412 + touch "$table.lock" || exit 1
413 + done &&
414 + git branch B &&
415 + git branch C &&
416 + rm .git/reftable/*.lock &&
417 + test_line_count = 5 .git/reftable/tables.list &&
418 +
419 + git pack-refs --auto &&
420 + test_line_count = 1 .git/reftable/tables.list
421 + )
422 +'
423 +
424 test_expect_success 'pack-refs: prunes stale tables' '
425 test_when_finished "rm -rf repo" &&
426 git init repo &&