path: root/builtin/gc.c
diff options
authorJonathan Nieder <>2013-10-16 23:11:46 (GMT)
committerJunio C Hamano <>2013-10-18 19:45:24 (GMT)
commit4c5baf0273ae3382dbaca81fee0331cada5899ea (patch)
tree29831095ae20dd0e97494cdd28ab35dec3dbec3a /builtin/gc.c
parent64a99eb4760de2ce2f0c04e146c0a55c34f50f20 (diff)
gc: remove file at end of execution
This file isn't really harmful, but isn't useful either, and can create minor annoyance for the user: * It's confusing, as the presence of a *.pid file often implies that a process is currently running. A user running "ls .git/" and finding this file may incorrectly guess that a "git gc" is currently running. * Leaving this file means that a "git gc" in an already gc-ed repo is no-longer a no-op. A user running "git gc" in a set of repositories, and then synchronizing this set (e.g. rsync -av, unison, ...) will see all the files as changed, which creates useless noise. This patch unlinks the file after the garbage collection is done, so that is actually present only during execution. Future versions of Git may want to use the information left in the file (e.g. for policies like "don't attempt to run a gc if one has already been ran less than X hours ago"). If so, this patch can safely be reverted. For now, let's not bother the users. Explained-by: Matthieu Moy <> Signed-off-by: Jonathan Nieder <> Improved-by: Nguyễn Thái Ngọc Duy <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/gc.c')
1 files changed, 24 insertions, 0 deletions
diff --git a/builtin/gc.c b/builtin/gc.c
index 6e0d81a..dd910fb 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -14,6 +14,7 @@
#include "cache.h"
#include "parse-options.h"
#include "run-command.h"
+#include "sigchain.h"
#include "argv-array.h"
#define FAILED_RUN "failed to run %s"
@@ -35,6 +36,21 @@ static struct argv_array repack = ARGV_ARRAY_INIT;
static struct argv_array prune = ARGV_ARRAY_INIT;
static struct argv_array rerere = ARGV_ARRAY_INIT;
+static char *pidfile;
+static void remove_pidfile(void)
+ if (pidfile)
+ unlink(pidfile);
+static void remove_pidfile_on_signal(int signo)
+ remove_pidfile();
+ sigchain_pop(signo);
+ raise(signo);
static int gc_config(const char *var, const char *value, void *cb)
if (!strcmp(var, "gc.packrefs")) {
@@ -179,6 +195,10 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
FILE *fp;
int fd, should_exit;
+ if (pidfile)
+ /* already locked */
+ return NULL;
if (gethostname(my_host, sizeof(my_host)))
strcpy(my_host, "unknown");
@@ -219,6 +239,10 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
+ pidfile = git_pathdup("");
+ sigchain_push_common(remove_pidfile_on_signal);
+ atexit(remove_pidfile);
return NULL;