]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin/gc.c
Merge branch 'aw/gc-lockfile-fscanf-fix' into maint
[thirdparty/git.git] / builtin / gc.c
index 22b35a627e60be8bf15f28624834ac1f913efcd8..53c19be8b2c2e6b5495c879d7ab77a6123e3c1b6 100644 (file)
@@ -11,6 +11,7 @@
  */
 
 #include "builtin.h"
+#include "config.h"
 #include "tempfile.h"
 #include "lockfile.h"
 #include "parse-options.h"
@@ -33,6 +34,8 @@ static int aggressive_window = 250;
 static int gc_auto_threshold = 6700;
 static int gc_auto_pack_limit = 50;
 static int detach_auto = 1;
+static timestamp_t gc_log_expire_time;
+static const char *gc_log_expire = "1.day.ago";
 static const char *prune_expire = "2.weeks.ago";
 static const char *prune_worktrees_expire = "3.months.ago";
 
@@ -62,24 +65,31 @@ static void report_pack_garbage(unsigned seen_bits, const char *path)
                string_list_append(&pack_garbage, path);
 }
 
-static void git_config_date_string(const char *key, const char **output)
-{
-       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 process_log_file(void)
 {
        struct stat st;
-       if (!fstat(get_lock_file_fd(&log_lock), &st) && st.st_size)
+       if (fstat(get_lock_file_fd(&log_lock), &st)) {
+               /*
+                * Perhaps there was an i/o error or another
+                * unlikely situation.  Try to make a note of
+                * this in gc.log along with any existing
+                * messages.
+                */
+               int saved_errno = errno;
+               fprintf(stderr, _("Failed to fstat %s: %s"),
+                       get_tempfile_path(&log_lock.tempfile),
+                       strerror(saved_errno));
+               fflush(stderr);
                commit_lock_file(&log_lock);
-       else
+               errno = saved_errno;
+       } else if (st.st_size) {
+               /* There was some error recorded in the lock file */
+               commit_lock_file(&log_lock);
+       } else {
+               /* No error, clean up any old gc.log */
+               unlink(git_path("gc.log"));
                rollback_lock_file(&log_lock);
+       }
 }
 
 static void process_log_file_at_exit(void)
@@ -111,8 +121,10 @@ 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);
-       git_config_date_string("gc.pruneexpire", &prune_expire);
-       git_config_date_string("gc.worktreepruneexpire", &prune_worktrees_expire);
+       git_config_get_expiry("gc.pruneexpire", &prune_expire);
+       git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
+       git_config_get_expiry("gc.logexpiry", &gc_log_expire);
+
        git_config(git_default_config, NULL);
 }
 
@@ -124,8 +136,6 @@ static int too_many_loose_objects(void)
         * distributed, we can check only one and get a reasonable
         * estimate.
         */
-       char path[PATH_MAX];
-       const char *objdir = get_object_directory();
        DIR *dir;
        struct dirent *ent;
        int auto_threshold;
@@ -135,15 +145,11 @@ static int too_many_loose_objects(void)
        if (gc_auto_threshold <= 0)
                return 0;
 
-       if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) {
-               warning(_("insanely long object directory %.*s"), 50, objdir);
-               return 0;
-       }
-       dir = opendir(path);
+       dir = opendir(git_path("objects/17"));
        if (!dir)
                return 0;
 
-       auto_threshold = (gc_auto_threshold + 255) / 256;
+       auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
        while ((ent = readdir(dir)) != NULL) {
                if (strspn(ent->d_name, "0123456789abcdef") != 38 ||
                    ent->d_name[38] != '\0')
@@ -191,6 +197,11 @@ static void add_repack_all_option(void)
        }
 }
 
+static void add_repack_incremental_option(void)
+{
+       argv_array_push(&repack, "--no-write-bitmap-index");
+}
+
 static int need_to_gc(void)
 {
        /*
@@ -208,7 +219,9 @@ static int need_to_gc(void)
         */
        if (too_many_packs())
                add_repack_all_option();
-       else if (!too_many_loose_objects())
+       else if (too_many_loose_objects())
+               add_repack_incremental_option();
+       else
                return 0;
 
        if (run_hook_le(NULL, "pre-auto-gc", NULL))
@@ -232,7 +245,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
                /* already locked */
                return NULL;
 
-       if (gethostname(my_host, sizeof(my_host)))
+       if (xgethostname(my_host, sizeof(my_host)))
                xsnprintf(my_host, sizeof(my_host), "unknown");
 
        pidfile_path = git_pathdup("gc.pid");
@@ -287,19 +300,34 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
 static int report_last_gc_error(void)
 {
        struct strbuf sb = STRBUF_INIT;
-       int ret;
+       int ret = 0;
+       struct stat st;
+       char *gc_log_path = git_pathdup("gc.log");
+
+       if (stat(gc_log_path, &st)) {
+               if (errno == ENOENT)
+                       goto done;
 
-       ret = strbuf_read_file(&sb, git_path("gc.log"), 0);
+               ret = error_errno(_("Can't stat %s"), gc_log_path);
+               goto done;
+       }
+
+       if (st.st_mtime < gc_log_expire_time)
+               goto done;
+
+       ret = strbuf_read_file(&sb, gc_log_path, 0);
        if (ret > 0)
-               return error(_("The last gc run reported the following. "
+               ret = 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);
+                           gc_log_path, sb.buf);
        strbuf_release(&sb);
-       return 0;
+done:
+       free(gc_log_path);
+       return ret;
 }
 
 static int gc_before_repack(void)
@@ -346,7 +374,10 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
        argv_array_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
        argv_array_pushl(&rerere, "rerere", "gc", NULL);
 
+       /* default expiry time, overwritten in gc_config */
        gc_config();
+       if (parse_expiry_date(gc_log_expire, &gc_log_expire_time))
+               die(_("Failed to parse gc.logexpiry value %s"), gc_log_expire);
 
        if (pack_refs < 0)
                pack_refs = !is_bare_repository();
@@ -383,8 +414,12 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
                        if (report_last_gc_error())
                                return -1;
 
+                       if (lock_repo_for_gc(force, &pid))
+                               return 0;
                        if (gc_before_repack())
                                return -1;
+                       delete_tempfile(&pidfile);
+
                        /*
                         * failure to daemonize is ok, we'll continue
                         * in foreground
@@ -445,5 +480,8 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
                warning(_("There are too many unreachable loose objects; "
                        "run 'git prune' to remove them."));
 
+       if (!daemonized)
+               unlink(git_path("gc.log"));
+
        return 0;
 }