Skip to content

Commit

Permalink
Merge branch 'nd/gc-lock-against-each-other'
Browse files Browse the repository at this point in the history
* nd/gc-lock-against-each-other:
  gc: remove gc.pid file at end of execution
  • Loading branch information
Junio C Hamano committed Oct 30, 2013
2 parents 779503c + 4c5baf0 commit 414b703
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
24 changes: 24 additions & 0 deletions builtin/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")) {
Expand Down Expand Up @@ -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");

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

pidfile = git_pathdup("gc.pid");
sigchain_push_common(remove_pidfile_on_signal);
atexit(remove_pidfile);

return NULL;
}

Expand Down
5 changes: 5 additions & 0 deletions t/t6500-gc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ test_expect_success 'gc empty repository' '
git gc
'

test_expect_success 'gc does not leave behind pid file' '
git gc &&
test_path_is_missing .git/gc.pid
'

test_expect_success 'gc --gobbledegook' '
test_expect_code 129 git gc --nonsense 2>err &&
test_i18ngrep "[Uu]sage: git gc" err
Expand Down

0 comments on commit 414b703

Please sign in to comment.