Skip to content

Commit 6dcffc6

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a75dc71 commit 6dcffc6

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

Documentation/git-pack-refs.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-pack-refs - Pack heads and tags for efficient repository access
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git pack-refs' [--all] [--no-prune] [--include <pattern>] [--exclude <pattern>]
11+
'git pack-refs' [--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]
1212

1313
DESCRIPTION
1414
-----------
@@ -60,6 +60,19 @@ with many branches of historical interests.
6060
The command usually removes loose refs under `$GIT_DIR/refs`
6161
hierarchy after packing them. This option tells it not to.
6262

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+
6376
--include <pattern>::
6477

6578
Pack refs based on a `glob(7)` pattern. Repetitions of this option

builtin/pack-refs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "revision.h"
88

99
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>]"),
1111
NULL
1212
};
1313

@@ -28,6 +28,7 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
2828
struct option opts[] = {
2929
OPT_BOOL(0, "all", &pack_all, N_("pack everything")),
3030
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),
3132
OPT_STRING_LIST(0, "include", pack_refs_opts.includes, N_("pattern"),
3233
N_("references to include")),
3334
OPT_STRING_LIST(0, "exclude", &option_excluded_refs, N_("pattern"),

t/t0601-reffiles-pack-refs.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ test_expect_success 'test --exclude takes precedence over --include' '
164164
git pack-refs --include "refs/heads/pack*" --exclude "refs/heads/pack*" &&
165165
test -f .git/refs/heads/dont_pack5'
166166

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+
167174
test_expect_success 'see if up-to-date packed refs are preserved' '
168175
git branch q &&
169176
git pack-refs --all --prune &&

t/t0610-reftable-basics.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,40 @@ test_expect_success 'pack-refs: compaction raises locking errors' '
387387
test_cmp expect err
388388
'
389389

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+
390424
test_expect_success 'pack-refs: prunes stale tables' '
391425
test_when_finished "rm -rf repo" &&
392426
git init repo &&

0 commit comments

Comments
 (0)