Skip to content

Commit 62aad18

Browse files
pcloudsgitster
authored andcommitted
gc --auto: do not lock refs in the background
9f673f9 (gc: config option for running --auto in background - 2014-02-08) puts "gc --auto" in background to reduce user's wait time. Part of the garbage collecting is pack-refs and pruning reflogs. These require locking some refs and may abort other processes trying to lock the same ref. If gc --auto is fired in the middle of a script, gc's holding locks in the background could fail the script, which could never happen before 9f673f9. Keep running pack-refs and "reflog --prune" in foreground to stop parallel ref updates. The remaining background operations (repack, prune and rerere) should not impact running git processes. Reported-by: Adam Borowski <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9f673f9 commit 62aad18

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

builtin/gc.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ static const char * const builtin_gc_usage[] = {
2626
};
2727

2828
static int pack_refs = 1;
29+
static int prune_reflogs = 1;
2930
static int aggressive_window = 250;
3031
static int gc_auto_threshold = 6700;
3132
static int gc_auto_pack_limit = 50;
@@ -252,6 +253,19 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
252253
return NULL;
253254
}
254255

256+
static int gc_before_repack(void)
257+
{
258+
if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
259+
return error(FAILED_RUN, pack_refs_cmd.argv[0]);
260+
261+
if (prune_reflogs && run_command_v_opt(reflog.argv, RUN_GIT_CMD))
262+
return error(FAILED_RUN, reflog.argv[0]);
263+
264+
pack_refs = 0;
265+
prune_reflogs = 0;
266+
return 0;
267+
}
268+
255269
int cmd_gc(int argc, const char **argv, const char *prefix)
256270
{
257271
int aggressive = 0;
@@ -313,12 +327,15 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
313327
fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
314328
fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
315329
}
316-
if (detach_auto)
330+
if (detach_auto) {
331+
if (gc_before_repack())
332+
return -1;
317333
/*
318334
* failure to daemonize is ok, we'll continue
319335
* in foreground
320336
*/
321337
daemonize();
338+
}
322339
} else
323340
add_repack_all_option();
324341

@@ -330,11 +347,8 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
330347
name, (uintmax_t)pid);
331348
}
332349

333-
if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
334-
return error(FAILED_RUN, pack_refs_cmd.argv[0]);
335-
336-
if (run_command_v_opt(reflog.argv, RUN_GIT_CMD))
337-
return error(FAILED_RUN, reflog.argv[0]);
350+
if (gc_before_repack())
351+
return -1;
338352

339353
if (run_command_v_opt(repack.argv, RUN_GIT_CMD))
340354
return error(FAILED_RUN, repack.argv[0]);

0 commit comments

Comments
 (0)