Merge branch 'nd/gc-auto-background-fix' into maint
authorJunio C Hamano <gitster@pobox.com>
Tue, 3 Nov 2015 23:32:33 +0000 (15:32 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 3 Nov 2015 23:32:33 +0000 (15:32 -0800)
When "git gc --auto" is backgrounded, its diagnosis message is
lost. Save it to a file in $GIT_DIR and show it next time the "gc
--auto" is run.

* nd/gc-auto-background-fix:
gc: save log from daemonized gc --auto and print it next time

1  2 
builtin/gc.c
diff --combined builtin/gc.c
index b757d9ae4fdff0d0834cc1c94f9c3595541d54c1,47fc1a6547f1aea81c7c86f62349791a7306a844..9216f7b9e262d62c49fa939377ba6c40254c47e7
@@@ -11,7 -11,6 +11,7 @@@
   */
  
  #include "builtin.h"
 +#include "tempfile.h"
  #include "lockfile.h"
  #include "parse-options.h"
  #include "run-command.h"
@@@ -22,7 -21,7 +22,7 @@@
  #define FAILED_RUN "failed to run %s"
  
  static const char * const builtin_gc_usage[] = {
 -      N_("git gc [options]"),
 +      N_("git gc [<options>]"),
        NULL
  };
  
@@@ -34,28 -33,51 +34,51 @@@ 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 struct lock_file log_lock;
  
 -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);
 +      }
  }
  
 -      if (!fstat(log_lock.fd, &st) && st.st_size)
+ static void process_log_file(void)
+ {
+       struct stat st;
++      if (!fstat(get_lock_file_fd(&log_lock), &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;
        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);
  }
  
@@@ -187,22 -217,20 +210,22 @@@ static const char *lock_repo_for_gc(in
        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 &&
                        if (fd >= 0)
                                rollback_lock_file(&lock);
                        *ret_pid = pid;
 +                      free(pidfile_path);
                        return locking_host;
                }
        }
        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;
  }
  
+ 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))
@@@ -262,6 -310,7 +303,7 @@@ int cmd_gc(int argc, const char **argv
        int force = 0;
        const char *name;
        pid_t pid;
+       int daemonized = 0;
  
        struct option builtin_gc_options[] = {
                OPT__QUIET(&quiet, N_("suppress progress reporting")),
        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();
                        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();
                    name, (uintmax_t)pid);
        }
  
 -              dup2(log_lock.fd, 2);
+       if (daemonized) {
+               hold_lock_file_for_update(&log_lock,
+                                         git_path("gc.log"),
+                                         LOCK_DIE_ON_ERROR);
++              dup2(get_lock_file_fd(&log_lock), 2);
+               sigchain_push_common(process_log_file_on_signal);
+               atexit(process_log_file_at_exit);
+       }
        if (gc_before_repack())
                return -1;
  
 -      if (run_command_v_opt(repack.argv, RUN_GIT_CMD))
 -              return error(FAILED_RUN, repack.argv[0]);
 +      if (!repository_format_precious_objects) {
 +              if (run_command_v_opt(repack.argv, RUN_GIT_CMD))
 +                      return error(FAILED_RUN, repack.argv[0]);
 +
 +              if (prune_expire) {
 +                      argv_array_push(&prune, prune_expire);
 +                      if (quiet)
 +                              argv_array_push(&prune, "--no-progress");
 +                      if (run_command_v_opt(prune.argv, RUN_GIT_CMD))
 +                              return error(FAILED_RUN, prune.argv[0]);
 +              }
 +      }
  
 -      if (prune_expire) {
 -              argv_array_push(&prune, prune_expire);
 -              if (quiet)
 -                      argv_array_push(&prune, "--no-progress");
 -              if (run_command_v_opt(prune.argv, RUN_GIT_CMD))
 -                      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))