Skip to content

Commit 133578a

Browse files
committed
Merge branch 'jk/gc-pre-detach-under-hook' into maint
We run an early part of "git gc" that deals with refs before daemonising (and not under lock) even when running a background auto-gc, which caused multiple gc processes attempting to run the early part at the same time. This is now prevented by running the early part also under the GC lock. * jk/gc-pre-detach-under-hook: gc: run pre-detach operations under lock
2 parents 309ff91 + c45af94 commit 133578a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

builtin/gc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,12 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
413413
if (report_last_gc_error())
414414
return -1;
415415

416+
if (lock_repo_for_gc(force, &pid))
417+
return 0;
416418
if (gc_before_repack())
417419
return -1;
420+
delete_tempfile(&pidfile);
421+
418422
/*
419423
* failure to daemonize is ok, we'll continue
420424
* in foreground

t/t6500-gc.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,27 @@ test_expect_success 'background auto gc does not run if gc.log is present and re
9595
test_line_count = 1 packs
9696
'
9797

98+
test_expect_success 'background auto gc respects lock for all operations' '
99+
# make sure we run a background auto-gc
100+
test_commit make-pack &&
101+
git repack &&
102+
test_config gc.autopacklimit 1 &&
103+
test_config gc.autodetach true &&
104+
105+
# create a ref whose loose presence we can use to detect a pack-refs run
106+
git update-ref refs/heads/should-be-loose HEAD &&
107+
test_path_is_file .git/refs/heads/should-be-loose &&
108+
109+
# now fake a concurrent gc that holds the lock; we can use our
110+
# shell pid so that it looks valid.
111+
hostname=$(hostname || echo unknown) &&
112+
printf "$$ %s" "$hostname" >.git/gc.pid &&
113+
114+
# our gc should exit zero without doing anything
115+
run_and_wait_for_auto_gc &&
116+
test_path_is_file .git/refs/heads/should-be-loose
117+
'
118+
98119
# DO NOT leave a detached auto gc process running near the end of the
99120
# test script: it can run long enough in the background to racily
100121
# interfere with the cleanup in 'test_done'.

0 commit comments

Comments
 (0)