pack-refs: teach pack-refs --include option

Allow users to be more selective over which refs to pack by adding an --include option to git-pack-refs. The existing options allow some measure of selectivity. By default git-pack-refs packs all tags. --all can be used to include all refs, and the previous commit added the ability to exclude certain refs with --exclude. While these knobs give the user some selection over which refs to pack, it could be useful to give more control. For instance, a repository may have a set of branches that are rarely updated and would benefit from being packed. --include would allow the user to easily include a set of branches to be packed while leaving everything else unpacked. Signed-off-by: John Cai <johncai86@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

John Cai committed May 12, 2023 at 21:34 UTC 4fe42f326e10a547dc65dfe9e5ceaeeee02b98db
6 files changed +67 -12
Documentation/git-pack-refs.txt
+13 -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] [--exclude <pattern>]
11 +'git pack-refs' [--all] [--no-prune] [--include <pattern>] [--exclude <pattern>]
12
13 DESCRIPTION
14 -----------
@@ -60,6 +60,15 @@ 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 +--include <pattern>::
64 +
65 +Pack refs based on a `glob(7)` pattern. Repetitions of this option
66 +accumulate inclusion patterns. If a ref is both included in `--include` and
67 +`--exclude`, `--exclude` takes precedence. Using `--include` will preclude all
68 +tags from being included by default. Symbolic refs and broken refs will never
69 +be packed. When used with `--all`, it will be a noop. Use `--no-include` to clear
70 +and reset the list of patterns.
71 +
72 --exclude <pattern>::
73
74 Do not pack refs matching the given `glob(7)` pattern. Repetitions of this option
@@ -70,6 +79,9 @@ unpack it.
79 When used with `--all`, pack only loose refs which do not match any of
80 the provided `--exclude` patterns.
81
82 +When used with `--include`, refs provided to `--include`, minus refs that are
83 +provided to `--exclude` will be packed.
84 +
85
86 BUGS
87 ----
builtin/pack-refs.c
+13 -2
@@ -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] [--exclude <pattern>]"),
10 + N_("git pack-refs [--all] [--no-prune] [--include <pattern>] [--exclude <pattern>]"),
11 NULL
12 };
13
@@ -15,13 +15,18 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
15 {
16 unsigned int flags = PACK_REFS_PRUNE;
17 static struct ref_exclusions excludes = REF_EXCLUSIONS_INIT;
18 - struct pack_refs_opts pack_refs_opts = {.exclusions = &excludes, .flags = flags};
18 + static struct string_list included_refs = STRING_LIST_INIT_NODUP;
19 + struct pack_refs_opts pack_refs_opts = { .exclusions = &excludes,
20 + .includes = &included_refs,
21 + .flags = flags };
22 static struct string_list option_excluded_refs = STRING_LIST_INIT_NODUP;
23 struct string_list_item *item;
24
25 struct option opts[] = {
26 OPT_BIT(0, "all", &pack_refs_opts.flags, N_("pack everything"), PACK_REFS_ALL),
27 OPT_BIT(0, "prune", &pack_refs_opts.flags, N_("prune loose refs (default)"), PACK_REFS_PRUNE),
28 + OPT_STRING_LIST(0, "include", pack_refs_opts.includes, N_("pattern"),
29 + N_("references to include")),
30 OPT_STRING_LIST(0, "exclude", &option_excluded_refs, N_("pattern"),
31 N_("references to exclude")),
32 OPT_END(),
@@ -33,5 +38,11 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
38 for_each_string_list_item(item, &option_excluded_refs)
39 add_ref_exclusion(pack_refs_opts.exclusions, item->string);
40
41 + if (pack_refs_opts.flags & PACK_REFS_ALL)
42 + string_list_append(pack_refs_opts.includes, "*");
43 +
44 + if (!pack_refs_opts.includes->nr)
45 + string_list_append(pack_refs_opts.includes, "refs/tags/*");
46 +
47 return refs_pack_refs(get_main_ref_store(the_repository), &pack_refs_opts);
48 }
refs.h
+1
@@ -66,6 +66,7 @@ struct worktree;
66 struct pack_refs_opts {
67 unsigned int flags;
68 struct ref_exclusions *exclusions;
69 + struct string_list *includes;
70 };
71
72 const char *refs_resolve_ref_unsafe(struct ref_store *refs,
refs/files-backend.c
+10 -8
@@ -1176,18 +1176,13 @@ static int should_pack_ref(const char *refname,
1176 const struct object_id *oid, unsigned int ref_flags,
1177 struct pack_refs_opts *opts)
1178 {
1179 + struct string_list_item *item;
1180 +
1181 /* Do not pack per-worktree refs: */
1182 if (parse_worktree_ref(refname, NULL, NULL, NULL) !=
1183 REF_WORKTREE_SHARED)
1184 return 0;
1185
1184 - if (ref_excluded(opts->exclusions, refname))
1185 - return 0;
1186 -
1187 - /* Do not pack non-tags unless PACK_REFS_ALL is set: */
1188 - if (!(opts->flags & PACK_REFS_ALL) && !starts_with(refname, "refs/tags/"))
1189 - return 0;
1190 -
1186 /* Do not pack symbolic refs: */
1187 if (ref_flags & REF_ISSYMREF)
1188 return 0;
@@ -1196,7 +1191,14 @@ static int should_pack_ref(const char *refname,
1191 if (!ref_resolves_to_object(refname, the_repository, oid, ref_flags))
1192 return 0;
1193
1199 - return 1;
1194 + if (ref_excluded(opts->exclusions, refname))
1195 + return 0;
1196 +
1197 + for_each_string_list_item(item, opts->includes)
1198 + if (!wildmatch(item->string, refname, 0))
1199 + return 1;
1200 +
1201 + return 0;
1202 }
1203
1204 static int files_pack_refs(struct ref_store *ref_store,
t/helper/test-ref-store.c
+9 -1
@@ -5,6 +5,7 @@
5 #include "worktree.h"
6 #include "object-store.h"
7 #include "repository.h"
8 +#include "revision.h"
9
10 struct flag_definition {
11 const char *name;
@@ -116,7 +117,14 @@ static struct flag_definition pack_flags[] = { FLAG_DEF(PACK_REFS_PRUNE),
117 static int cmd_pack_refs(struct ref_store *refs, const char **argv)
118 {
119 unsigned int flags = arg_flags(*argv++, "flags", pack_flags);
119 - struct pack_refs_opts pack_opts = { .flags = flags };
120 + static struct ref_exclusions exclusions = REF_EXCLUSIONS_INIT;
121 + static struct string_list included_refs = STRING_LIST_INIT_NODUP;
122 + struct pack_refs_opts pack_opts = { .flags = flags,
123 + .exclusions = &exclusions,
124 + .includes = &included_refs };
125 +
126 + if (pack_opts.flags & PACK_REFS_ALL)
127 + string_list_append(pack_opts.includes, "*");
128
129 return refs_pack_refs(refs, &pack_opts);
130 }
t/t3210-pack-refs.sh
+21
@@ -124,6 +124,27 @@ test_expect_success 'test --no-exclude refs clears excluded refs' '
124 ! test -f .git/refs/heads/dont_pack3 &&
125 ! test -f .git/refs/heads/dont_pack4'
126
127 +test_expect_success 'test only included refs are packed' '
128 + git branch pack_this1 &&
129 + git branch pack_this2 &&
130 + git tag dont_pack5 &&
131 + git pack-refs --include "refs/heads/pack_this*" &&
132 + test -f .git/refs/tags/dont_pack5 &&
133 + ! test -f .git/refs/heads/pack_this1 &&
134 + ! test -f .git/refs/heads/pack_this2'
135 +
136 +test_expect_success 'test --no-include refs clears included refs' '
137 + git branch pack1 &&
138 + git branch pack2 &&
139 + git pack-refs --include "refs/heads/pack*" --no-include &&
140 + test -f .git/refs/heads/pack1 &&
141 + test -f .git/refs/heads/pack2'
142 +
143 +test_expect_success 'test --exclude takes precedence over --include' '
144 + git branch dont_pack5 &&
145 + git pack-refs --include "refs/heads/pack*" --exclude "refs/heads/pack*" &&
146 + test -f .git/refs/heads/dont_pack5'
147 +
148 test_expect_success \
149 'see if up-to-date packed refs are preserved' \
150 'git branch q &&