Git fork

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>

authored by

Patrick Steinhardt and committed by
Junio C Hamano
d2b084c6 697202b0

+9 -15
+9 -15
builtin/gc.c
··· 816 816 return ret; 817 817 } 818 818 819 - static void gc_before_repack(struct maintenance_run_opts *opts, 820 - struct gc_config *cfg) 819 + static int gc_before_repack(struct maintenance_run_opts *opts, 820 + struct gc_config *cfg) 821 821 { 822 - /* 823 - * We may be called twice, as both the pre- and 824 - * post-daemonized phases will call us, but running these 825 - * commands more than once is pointless and wasteful. 826 - */ 827 - static int done = 0; 828 - if (done++) 829 - return; 830 - 831 822 if (cfg->pack_refs && maintenance_task_pack_refs(opts, cfg)) 832 - die(FAILED_RUN, "pack-refs"); 823 + return error(FAILED_RUN, "pack-refs"); 833 824 if (cfg->prune_reflogs && maintenance_task_reflog_expire(opts, cfg)) 834 - die(FAILED_RUN, "reflog"); 825 + return error(FAILED_RUN, "reflog"); 826 + return 0; 835 827 } 836 828 837 829 int cmd_gc(int argc, ··· 965 957 goto out; 966 958 } 967 959 968 - gc_before_repack(&opts, &cfg); /* dies on failure */ 960 + if (gc_before_repack(&opts, &cfg) < 0) 961 + die(NULL); 969 962 delete_tempfile(&pidfile); 970 963 971 964 /* ··· 995 988 free(path); 996 989 } 997 990 998 - gc_before_repack(&opts, &cfg); 991 + if (opts.detach <= 0) 992 + gc_before_repack(&opts, &cfg); 999 993 1000 994 if (!repository_format_precious_objects) { 1001 995 struct child_process repack_cmd = CHILD_PROCESS_INIT;