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>
This commit is contained in:
Patrick Steinhardt 2024-03-25 11:03:20 +01:00 committed by Junio C Hamano
parent a75dc71f37
commit 6dcffc68f4
4 changed files with 57 additions and 2 deletions

View 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

View 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"),

View File

@ -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 &&

View File

@ -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 &&