aboutsummaryrefslogtreecommitdiffstats
path: root/builtin
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2025-06-03 16:01:19 +0200
committerJunio C Hamano <gitster@pobox.com>2025-06-03 08:30:52 -0700
commitd2b084c66037f1a77b3e061ae7e4d96cbdbf6c05 (patch)
tree544cea981974816be57ec0fbbcaf5e6d278439c1 /builtin
parentusage: allow dying without writing an error message (diff)
downloadgit-d2b084c66037f1a77b3e061ae7e4d96cbdbf6c05.tar.gz
git-d2b084c66037f1a77b3e061ae7e4d96cbdbf6c05.zip
builtin/gc: avoid global state in `gc_before_repack()`
The `gc_before_repack()` should only ever run once in git-gc(1), but we may end up calling it twice when the "--detach" flag is passed. The duplicated call is avoided though via a static flag in this function. This pattern is somewhat unintuitive though. Refactor it to drop the static flag and instead guard the second call of `gc_before_repack()` via `opts.detach`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin')
-rw-r--r--builtin/gc.c24
1 files changed, 9 insertions, 15 deletions
diff --git a/builtin/gc.c b/builtin/gc.c
index fdd0dd09be..4a5c4b2044 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -816,22 +816,14 @@ done:
return ret;
}
-static void gc_before_repack(struct maintenance_run_opts *opts,
- struct gc_config *cfg)
+static int gc_before_repack(struct maintenance_run_opts *opts,
+ struct gc_config *cfg)
{
- /*
- * We may be called twice, as both the pre- and
- * post-daemonized phases will call us, but running these
- * commands more than once is pointless and wasteful.
- */
- static int done = 0;
- if (done++)
- return;
-
if (cfg->pack_refs && maintenance_task_pack_refs(opts, cfg))
- die(FAILED_RUN, "pack-refs");
+ return error(FAILED_RUN, "pack-refs");
if (cfg->prune_reflogs && maintenance_task_reflog_expire(opts, cfg))
- die(FAILED_RUN, "reflog");
+ return error(FAILED_RUN, "reflog");
+ return 0;
}
int cmd_gc(int argc,
@@ -965,7 +957,8 @@ int cmd_gc(int argc,
goto out;
}
- gc_before_repack(&opts, &cfg); /* dies on failure */
+ if (gc_before_repack(&opts, &cfg) < 0)
+ die(NULL);
delete_tempfile(&pidfile);
/*
@@ -995,7 +988,8 @@ int cmd_gc(int argc,
free(path);
}
- gc_before_repack(&opts, &cfg);
+ if (opts.detach <= 0)
+ gc_before_repack(&opts, &cfg);
if (!repository_format_precious_objects) {
struct child_process repack_cmd = CHILD_PROCESS_INIT;