]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/pack-refs: introduce new "--auto" flag
authorPatrick Steinhardt <ps@pks.im>
Mon, 25 Mar 2024 10:03:20 +0000 (11:03 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 25 Mar 2024 16:54:07 +0000 (09:54 -0700)
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>
Documentation/git-pack-refs.txt
builtin/pack-refs.c
t/t0601-reffiles-pack-refs.sh
t/t0610-reftable-basics.sh

index 284956acb3c5e8bb168626c9729a17a799c0afd7..2dcabaf74cefb4111e94e92b8c7e47e8346a1d42 100644 (file)
@@ -8,7 +8,7 @@ git-pack-refs - Pack heads and tags for efficient repository access
 SYNOPSIS
 --------
 [verse]
-'git pack-refs' [--all] [--no-prune] [--include <pattern>] [--exclude <pattern>]
+'git pack-refs' [--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]
 
 DESCRIPTION
 -----------
@@ -60,6 +60,19 @@ with many branches of historical interests.
 The command usually removes loose refs under `$GIT_DIR/refs`
 hierarchy after packing them.  This option tells it not to.
 
+--auto::
+
+Pack refs as needed depending on the current state of the ref database. The
+behavior depends on the ref format used by the repository and may change in the
+future.
++
+       - "files": No special handling for `--auto` has been implemented.
++
+       - "reftable": Tables are compacted such that they form a geometric
+         sequence. For two tables N and N+1, where N+1 is newer, this
+         maintains the property that N is at least twice as big as N+1. Only
+         tables that violate this property are compacted.
+
 --include <pattern>::
 
 Pack refs based on a `glob(7)` pattern. Repetitions of this option
index ea2baeec76229cbe8a9e703ee73cefc2a3fa5e4e..db4082566634cbf5bdbb081d0807b0a82be7645e 100644 (file)
@@ -7,7 +7,7 @@
 #include "revision.h"
 
 static char const * const pack_refs_usage[] = {
-       N_("git pack-refs [--all] [--no-prune] [--include <pattern>] [--exclude <pattern>]"),
+       N_("git pack-refs [--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]"),
        NULL
 };
 
@@ -28,6 +28,7 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
        struct option opts[] = {
                OPT_BOOL(0, "all",   &pack_all, N_("pack everything")),
                OPT_BIT(0, "prune", &pack_refs_opts.flags, N_("prune loose refs (default)"), PACK_REFS_PRUNE),
+               OPT_BIT(0, "auto", &pack_refs_opts.flags, N_("auto-pack refs as needed"), PACK_REFS_AUTO),
                OPT_STRING_LIST(0, "include", pack_refs_opts.includes, N_("pattern"),
                        N_("references to include")),
                OPT_STRING_LIST(0, "exclude", &option_excluded_refs, N_("pattern"),
index b1cf5873471ce4166e9400242fa95e0a104e9e92..219a495451e9744a94cd42a118f334e8557fc6a7 100755 (executable)
@@ -164,6 +164,13 @@ test_expect_success 'test --exclude takes precedence over --include' '
        git pack-refs --include "refs/heads/pack*" --exclude "refs/heads/pack*" &&
        test -f .git/refs/heads/dont_pack5'
 
+test_expect_success '--auto packs and prunes refs as usual' '
+       git branch auto &&
+       test_path_is_file .git/refs/heads/auto &&
+       git pack-refs --auto --all &&
+       test_path_is_missing .git/refs/heads/auto
+'
+
 test_expect_success 'see if up-to-date packed refs are preserved' '
        git branch q &&
        git pack-refs --all --prune &&
index a53d1dc493adb876caf74491c3937c40ab41385a..6de7529575dc70bcef6ca9aeb4a9c45a5dbb48d4 100755 (executable)
@@ -387,6 +387,40 @@ test_expect_success 'pack-refs: compaction raises locking errors' '
        test_cmp expect err
 '
 
+test_expect_success 'pack-refs: auto compaction' '
+       test_when_finished "rm -rf repo" &&
+       git init repo &&
+       (
+               cd repo &&
+
+               test_commit A &&
+
+               # 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 &&
+               ls -1 .git/reftable >tables-actual &&
+               test_cmp tables-expect tables-actual &&
+
+               # 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.
+               ls .git/reftable/*.ref |
+               while read table
+               do
+                       touch "$table.lock" || exit 1
+               done &&
+               git branch B &&
+               git branch C &&
+               rm .git/reftable/*.lock &&
+               test_line_count = 5 .git/reftable/tables.list &&
+
+               git pack-refs --auto &&
+               test_line_count = 1 .git/reftable/tables.list
+       )
+'
+
 test_expect_success 'pack-refs: prunes stale tables' '
        test_when_finished "rm -rf repo" &&
        git init repo &&