builtin/reflog: stop storing default reflog expiry dates globally

When expiring reflog entries, it is possible to configure expiry dates that depend on the name of the reflog. This requires us to store a couple of different expiry dates: - The default expiry date for reflog entries that aren't otherwise specified. - The per-reflog expiry date. - The currently active set of expiry dates for a given reference. While the last item is stored in `struct reflog_expire_options`, the other items aren't, which makes it hard to reuse the structure in other places. Refactor the code so that the default expiry date is stored as part of the structure. The per-reflog expiry dates will be adapted accordingly in the subsequent commit. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Apr 8, 2025 at 08:22 UTC 85658275702bad04c916e2709fe33d2efc6de5e4
2 files changed +13 -15
builtin/reflog.c
+7 -15
@@ -63,9 +63,6 @@ static const char *const reflog_usage[] = {
63 NULL
64 };
65
66 -static timestamp_t default_reflog_expire;
67 -static timestamp_t default_reflog_expire_unreachable;
68 -
66 struct worktree_reflogs {
67 struct worktree *worktree;
68 struct string_list reflogs;
@@ -122,6 +119,7 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
119 static int reflog_expire_config(const char *var, const char *value,
120 const struct config_context *ctx, void *cb)
121 {
122 + struct reflog_expire_options *opts = cb;
123 const char *pattern, *key;
124 size_t pattern_len;
125 timestamp_t expire;
@@ -145,10 +143,10 @@ static int reflog_expire_config(const char *var, const char *value,
143 if (!pattern) {
144 switch (slot) {
145 case EXPIRE_TOTAL:
148 - default_reflog_expire = expire;
146 + opts->default_expire_total = expire;
147 break;
148 case EXPIRE_UNREACH:
151 - default_reflog_expire_unreachable = expire;
149 + opts->default_expire_unreachable = expire;
150 break;
151 }
152 return 0;
@@ -198,9 +196,9 @@ static void set_reflog_expiry_param(struct reflog_expire_options *cb, const char
196
197 /* Nothing matched -- use the default value */
198 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
201 - cb->expire_total = default_reflog_expire;
199 + cb->expire_total = cb->default_expire_total;
200 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
203 - cb->expire_unreachable = default_reflog_expire_unreachable;
201 + cb->expire_unreachable = cb->default_expire_unreachable;
202 }
203
204 static int expire_unreachable_callback(const struct option *opt,
@@ -276,8 +274,8 @@ static int cmd_reflog_list(int argc, const char **argv, const char *prefix,
274 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
275 struct repository *repo UNUSED)
276 {
279 - struct reflog_expire_options opts = { 0 };
277 timestamp_t now = time(NULL);
278 + struct reflog_expire_options opts = REFLOG_EXPIRE_OPTIONS_INIT(now);
279 int i, status, do_all, single_worktree = 0;
280 unsigned int flags = 0;
281 int verbose = 0;
@@ -308,17 +306,11 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
306 OPT_END()
307 };
308
311 - default_reflog_expire_unreachable = now - 30 * 24 * 3600;
312 - default_reflog_expire = now - 90 * 24 * 3600;
313 - git_config(reflog_expire_config, NULL);
309 + git_config(reflog_expire_config, &opts);
310
311 save_commit_buffer = 0;
312 do_all = status = 0;
313
318 - opts.explicit_expiry = 0;
319 - opts.expire_total = default_reflog_expire;
320 - opts.expire_unreachable = default_reflog_expire_unreachable;
321 -
314 argc = parse_options(argc, argv, prefix, options, reflog_expire_usage, 0);
315
316 if (verbose)
reflog.h
+6
@@ -5,10 +5,16 @@
5 struct reflog_expire_options {
6 int stalefix;
7 int explicit_expiry;
8 + timestamp_t default_expire_total;
9 timestamp_t expire_total;
10 + timestamp_t default_expire_unreachable;
11 timestamp_t expire_unreachable;
12 int recno;
13 };
14 +#define REFLOG_EXPIRE_OPTIONS_INIT(now) { \
15 + .default_expire_total = now - 30 * 24 * 3600, \
16 + .default_expire_unreachable = now - 90 * 24 * 3600, \
17 +}
18
19 struct expire_reflog_policy_cb {
20 enum {