Skip to content

Commit 414b703

Browse files
committed
Merge branch 'nd/gc-lock-against-each-other'
* nd/gc-lock-against-each-other: gc: remove gc.pid file at end of execution
2 parents 779503c + 4c5baf0 commit 414b703

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

builtin/gc.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "cache.h"
1515
#include "parse-options.h"
1616
#include "run-command.h"
17+
#include "sigchain.h"
1718
#include "argv-array.h"
1819

1920
#define FAILED_RUN "failed to run %s"
@@ -35,6 +36,21 @@ static struct argv_array repack = ARGV_ARRAY_INIT;
3536
static struct argv_array prune = ARGV_ARRAY_INIT;
3637
static struct argv_array rerere = ARGV_ARRAY_INIT;
3738

39+
static char *pidfile;
40+
41+
static void remove_pidfile(void)
42+
{
43+
if (pidfile)
44+
unlink(pidfile);
45+
}
46+
47+
static void remove_pidfile_on_signal(int signo)
48+
{
49+
remove_pidfile();
50+
sigchain_pop(signo);
51+
raise(signo);
52+
}
53+
3854
static int gc_config(const char *var, const char *value, void *cb)
3955
{
4056
if (!strcmp(var, "gc.packrefs")) {
@@ -179,6 +195,10 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
179195
FILE *fp;
180196
int fd, should_exit;
181197

198+
if (pidfile)
199+
/* already locked */
200+
return NULL;
201+
182202
if (gethostname(my_host, sizeof(my_host)))
183203
strcpy(my_host, "unknown");
184204

@@ -219,6 +239,10 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
219239
strbuf_release(&sb);
220240
commit_lock_file(&lock);
221241

242+
pidfile = git_pathdup("gc.pid");
243+
sigchain_push_common(remove_pidfile_on_signal);
244+
atexit(remove_pidfile);
245+
222246
return NULL;
223247
}
224248

t/t6500-gc.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ test_expect_success 'gc empty repository' '
99
git gc
1010
'
1111

12+
test_expect_success 'gc does not leave behind pid file' '
13+
git gc &&
14+
test_path_is_missing .git/gc.pid
15+
'
16+
1217
test_expect_success 'gc --gobbledegook' '
1318
test_expect_code 129 git gc --nonsense 2>err &&
1419
test_i18ngrep "[Uu]sage: git gc" err

0 commit comments

Comments
 (0)