gc: refactor a "call me once" pattern
Change an idiom we're using to ensure that gc_before_repack() only does work once (see 62aad1849f ("gc --auto: do not lock refs in the background", 2014-05-25)) to be more obvious. Nothing except this function cares about the "pack_refs" and "prune_reflogs" variables, so let's not leave the reader wondering if they're being zero'd out for later use somewhere else. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
Mar 15, 2019 at 16:59 UTC
cd8eb3a094c51482c80a97589e4aff53af2c7c63
1 file changed
+9
-3
builtin/gc.c
+9
-3
@@ -489,14 +489,20 @@ done:
489
490
static void gc_before_repack(void)
491
{
492
+ /*
493
+ * We may be called twice, as both the pre- and
494
+ * post-daemonized phases will call us, but running these
495
+ * commands more than once is pointless and wasteful.
496
+ */
497
+ static int done = 0;
498
+ if (done++)
499
+ return;
500
+
501
if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
502
die(FAILED_RUN, pack_refs_cmd.argv[0]);
503
504
if (prune_reflogs && run_command_v_opt(reflog.argv, RUN_GIT_CMD))
505
die(FAILED_RUN, reflog.argv[0]);
497
-
498
- pack_refs = 0;
499
- prune_reflogs = 0;
506
}
507
508
int cmd_gc(int argc, const char **argv, const char *prefix)