gc: save log from daemonized gc --auto and print it next time
[gitweb.git] / builtin / gc.c
index 8d219d8c42cb4dfb64468f9b4d82648bff646685..47fc1a6547f1aea81c7c86f62349791a7306a844 100644 (file)
@@ -11,7 +11,7 @@
  */
 
 #include "builtin.h"
-#include "cache.h"
+#include "lockfile.h"
 #include "parse-options.h"
 #include "run-command.h"
 #include "sigchain.h"
@@ -41,6 +41,7 @@ static struct argv_array prune = ARGV_ARRAY_INIT;
 static struct argv_array rerere = ARGV_ARRAY_INIT;
 
 static char *pidfile;
+static struct lock_file log_lock;
 
 static void remove_pidfile(void)
 {
@@ -55,44 +56,55 @@ static void remove_pidfile_on_signal(int signo)
        raise(signo);
 }
 
-static int gc_config(const char *var, const char *value, void *cb)
+static void process_log_file(void)
 {
-       if (!strcmp(var, "gc.packrefs")) {
+       struct stat st;
+       if (!fstat(log_lock.fd, &st) && st.st_size)
+               commit_lock_file(&log_lock);
+       else
+               rollback_lock_file(&log_lock);
+}
+
+static void process_log_file_at_exit(void)
+{
+       fflush(stderr);
+       process_log_file();
+}
+
+static void process_log_file_on_signal(int signo)
+{
+       process_log_file();
+       sigchain_pop(signo);
+       raise(signo);
+}
+
+static void gc_config(void)
+{
+       const char *value;
+
+       if (!git_config_get_value("gc.packrefs", &value)) {
                if (value && !strcmp(value, "notbare"))
                        pack_refs = -1;
                else
-                       pack_refs = git_config_bool(var, value);
-               return 0;
-       }
-       if (!strcmp(var, "gc.aggressivewindow")) {
-               aggressive_window = git_config_int(var, value);
-               return 0;
-       }
-       if (!strcmp(var, "gc.aggressivedepth")) {
-               aggressive_depth = git_config_int(var, value);
-               return 0;
+                       pack_refs = git_config_bool("gc.packrefs", value);
        }
-       if (!strcmp(var, "gc.auto")) {
-               gc_auto_threshold = git_config_int(var, value);
-               return 0;
-       }
-       if (!strcmp(var, "gc.autopacklimit")) {
-               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")) {
+
+       git_config_get_int("gc.aggressivewindow", &aggressive_window);
+       git_config_get_int("gc.aggressivedepth", &aggressive_depth);
+       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(value) >= now)
-                               return error(_("Invalid %s: '%s'"), var, value);
+                       if (approxidate(prune_expire) >= now) {
+                               git_die_config("gc.pruneexpire", _("Invalid gc.pruneexpire: '%s'"),
+                                               prune_expire);
+                       }
                }
-               return git_config_string(&prune_expire, var, value);
        }
-       return git_default_config(var, value, cb);
+       git_config(git_default_config, NULL);
 }
 
 static int too_many_loose_objects(void)
@@ -259,6 +271,24 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
        return NULL;
 }
 
+static int report_last_gc_error(void)
+{
+       struct strbuf sb = STRBUF_INIT;
+       int ret;
+
+       ret = strbuf_read_file(&sb, git_path("gc.log"), 0);
+       if (ret > 0)
+               return error(_("The last gc run reported the following. "
+                              "Please correct the root cause\n"
+                              "and remove %s.\n"
+                              "Automatic cleanup will not be performed "
+                              "until the file is removed.\n\n"
+                              "%s"),
+                            git_path("gc.log"), sb.buf);
+       strbuf_release(&sb);
+       return 0;
+}
+
 static int gc_before_repack(void)
 {
        if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
@@ -280,6 +310,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
        int force = 0;
        const char *name;
        pid_t pid;
+       int daemonized = 0;
 
        struct option builtin_gc_options[] = {
                OPT__QUIET(&quiet, N_("suppress progress reporting")),
@@ -301,7 +332,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
        argv_array_pushl(&prune, "prune", "--expire", NULL );
        argv_array_pushl(&rerere, "rerere", "gc", NULL);
 
-       git_config(gc_config, NULL);
+       gc_config();
 
        if (pack_refs < 0)
                pack_refs = !is_bare_repository();
@@ -335,13 +366,16 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
                        fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
                }
                if (detach_auto) {
+                       if (report_last_gc_error())
+                               return -1;
+
                        if (gc_before_repack())
                                return -1;
                        /*
                         * failure to daemonize is ok, we'll continue
                         * in foreground
                         */
-                       daemonize();
+                       daemonized = !daemonize();
                }
        } else
                add_repack_all_option();
@@ -354,6 +388,15 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
                    name, (uintmax_t)pid);
        }
 
+       if (daemonized) {
+               hold_lock_file_for_update(&log_lock,
+                                         git_path("gc.log"),
+                                         LOCK_DIE_ON_ERROR);
+               dup2(log_lock.fd, 2);
+               sigchain_push_common(process_log_file_on_signal);
+               atexit(process_log_file_at_exit);
+       }
+
        if (gc_before_repack())
                return -1;