Merge branch 'pt/am-builtin'
[gitweb.git] / builtin / gc.c
index 5c634afc0022c0ea7a8cd8c81727935ad79a6333..0ad8d30b56f89a9ea6ac3a9ee5ae4593ae9754a0 100644 (file)
@@ -11,6 +11,7 @@
  */
 
 #include "builtin.h"
+#include "tempfile.h"
 #include "lockfile.h"
 #include "parse-options.h"
 #include "run-command.h"
@@ -33,26 +34,26 @@ static int gc_auto_threshold = 6700;
 static int gc_auto_pack_limit = 50;
 static int detach_auto = 1;
 static const char *prune_expire = "2.weeks.ago";
+static const char *prune_worktrees_expire = "3.months.ago";
 
 static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT;
 static struct argv_array reflog = ARGV_ARRAY_INIT;
 static struct argv_array repack = ARGV_ARRAY_INIT;
 static struct argv_array prune = ARGV_ARRAY_INIT;
+static struct argv_array prune_worktrees = ARGV_ARRAY_INIT;
 static struct argv_array rerere = ARGV_ARRAY_INIT;
 
-static char *pidfile;
+static struct tempfile pidfile;
 
-static void remove_pidfile(void)
+static void git_config_date_string(const char *key, const char **output)
 {
-       if (pidfile)
-               unlink(pidfile);
-}
-
-static void remove_pidfile_on_signal(int signo)
-{
-       remove_pidfile();
-       sigchain_pop(signo);
-       raise(signo);
+       if (git_config_get_string_const(key, output))
+               return;
+       if (strcmp(*output, "now")) {
+               unsigned long now = approxidate("now");
+               if (approxidate(*output) >= now)
+                       git_die_config(key, _("Invalid %s: '%s'"), key, *output);
+       }
 }
 
 static void gc_config(void)
@@ -71,16 +72,8 @@ static void gc_config(void)
        git_config_get_int("gc.auto", &gc_auto_threshold);
        git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
        git_config_get_bool("gc.autodetach", &detach_auto);
-
-       if (!git_config_get_string_const("gc.pruneexpire", &prune_expire)) {
-               if (strcmp(prune_expire, "now")) {
-                       unsigned long now = approxidate("now");
-                       if (approxidate(prune_expire) >= now) {
-                               git_die_config("gc.pruneexpire", _("Invalid gc.pruneexpire: '%s'"),
-                                               prune_expire);
-                       }
-               }
-       }
+       git_config_date_string("gc.pruneexpire", &prune_expire);
+       git_config_date_string("gc.worktreepruneexpire", &prune_worktrees_expire);
        git_config(git_default_config, NULL);
 }
 
@@ -194,20 +187,22 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
        uintmax_t pid;
        FILE *fp;
        int fd;
+       char *pidfile_path;
 
-       if (pidfile)
+       if (is_tempfile_active(&pidfile))
                /* already locked */
                return NULL;
 
        if (gethostname(my_host, sizeof(my_host)))
                strcpy(my_host, "unknown");
 
-       fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
+       pidfile_path = git_pathdup("gc.pid");
+       fd = hold_lock_file_for_update(&lock, pidfile_path,
                                       LOCK_DIE_ON_ERROR);
        if (!force) {
                static char locking_host[128];
                int should_exit;
-               fp = fopen(git_path("gc.pid"), "r");
+               fp = fopen(pidfile_path, "r");
                memset(locking_host, 0, sizeof(locking_host));
                should_exit =
                        fp != NULL &&
@@ -231,6 +226,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
                        if (fd >= 0)
                                rollback_lock_file(&lock);
                        *ret_pid = pid;
+                       free(pidfile_path);
                        return locking_host;
                }
        }
@@ -240,11 +236,8 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
        write_in_full(fd, sb.buf, sb.len);
        strbuf_release(&sb);
        commit_lock_file(&lock);
-
-       pidfile = git_pathdup("gc.pid");
-       sigchain_push_common(remove_pidfile_on_signal);
-       atexit(remove_pidfile);
-
+       register_tempfile(&pidfile, pidfile_path);
+       free(pidfile_path);
        return NULL;
 }
 
@@ -287,7 +280,8 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
        argv_array_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
        argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL);
        argv_array_pushl(&repack, "repack", "-d", "-l", NULL);
-       argv_array_pushl(&prune, "prune", "--expire", NULL );
+       argv_array_pushl(&prune, "prune", "--expire", NULL);
+       argv_array_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
        argv_array_pushl(&rerere, "rerere", "gc", NULL);
 
        gc_config();
@@ -357,6 +351,12 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
                        return error(FAILED_RUN, prune.argv[0]);
        }
 
+       if (prune_worktrees_expire) {
+               argv_array_push(&prune_worktrees, prune_worktrees_expire);
+               if (run_command_v_opt(prune_worktrees.argv, RUN_GIT_CMD))
+                       return error(FAILED_RUN, prune_worktrees.argv[0]);
+       }
+
        if (run_command_v_opt(rerere.argv, RUN_GIT_CMD))
                return error(FAILED_RUN, rerere.argv[0]);