Merge branch 'km/avoid-cp-a'
[gitweb.git] / builtin / gc.c
index 25f2237c08f3b8fbc8f3554f3e22bf7e59e74b1c..85f5c2bc62ec1ead593f5aa2e3d7077e65f2f061 100644 (file)
@@ -16,6 +16,7 @@
 #include "run-command.h"
 #include "sigchain.h"
 #include "argv-array.h"
+#include "commit.h"
 
 #define FAILED_RUN "failed to run %s"
 
@@ -25,9 +26,11 @@ static const char * const builtin_gc_usage[] = {
 };
 
 static int pack_refs = 1;
+static int aggressive_depth = 250;
 static int aggressive_window = 250;
 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 struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT;
@@ -64,6 +67,10 @@ static int gc_config(const char *var, const char *value, void *cb)
                aggressive_window = git_config_int(var, value);
                return 0;
        }
+       if (!strcmp(var, "gc.aggressivedepth")) {
+               aggressive_depth = git_config_int(var, value);
+               return 0;
+       }
        if (!strcmp(var, "gc.auto")) {
                gc_auto_threshold = git_config_int(var, value);
                return 0;
@@ -72,6 +79,10 @@ static int gc_config(const char *var, const char *value, void *cb)
                gc_auto_pack_limit = git_config_int(var, value);
                return 0;
        }
+       if (!strcmp(var, "gc.autodetach")) {
+               detach_auto = git_config_bool(var, value);
+               return 0;
+       }
        if (!strcmp(var, "gc.pruneexpire")) {
                if (value && strcmp(value, "now")) {
                        unsigned long now = approxidate("now");
@@ -178,7 +189,7 @@ static int need_to_gc(void)
        else if (!too_many_loose_objects())
                return 0;
 
-       if (run_hook(NULL, "pre-auto-gc", NULL))
+       if (run_hook_le(NULL, "pre-auto-gc", NULL))
                return 0;
        return 1;
 }
@@ -187,13 +198,12 @@ static int need_to_gc(void)
 static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
 {
        static struct lock_file lock;
-       static char locking_host[128];
        char my_host[128];
        struct strbuf sb = STRBUF_INIT;
        struct stat st;
        uintmax_t pid;
        FILE *fp;
-       int fd, should_exit;
+       int fd;
 
        if (pidfile)
                /* already locked */
@@ -205,6 +215,8 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
        fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
                                       LOCK_DIE_ON_ERROR);
        if (!force) {
+               static char locking_host[128];
+               int should_exit;
                fp = fopen(git_path("gc.pid"), "r");
                memset(locking_host, 0, sizeof(locking_host));
                should_exit =
@@ -287,7 +299,8 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 
        if (aggressive) {
                argv_array_push(&repack, "-f");
-               argv_array_push(&repack, "--depth=250");
+               if (aggressive_depth > 0)
+                       argv_array_pushf(&repack, "--depth=%d", aggressive_depth);
                if (aggressive_window > 0)
                        argv_array_pushf(&repack, "--window=%d", aggressive_window);
        }
@@ -300,11 +313,19 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
                 */
                if (!need_to_gc())
                        return 0;
-               if (!quiet)
-                       fprintf(stderr,
-                                       _("Auto packing the repository for optimum performance. You may also\n"
-                                       "run \"git gc\" manually. See "
-                                       "\"git help gc\" for more information.\n"));
+               if (!quiet) {
+                       if (detach_auto)
+                               fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
+                       else
+                               fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
+                       fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
+               }
+               if (detach_auto)
+                       /*
+                        * failure to daemonize is ok, we'll continue
+                        * in foreground
+                        */
+                       daemonize();
        } else
                add_repack_all_option();