{"thread":{"id":"45108","subject":"[PATCH v4] gc: ignore old gc.log files","startedAt":"2017-02-10T20:03:07Z","lastAt":"2017-02-10T20:03:07Z","messageCount":1,"participants":["David Turner"],"isPatch":true,"patchVersion":4,"patchTotal":null},"messages":[{"id":"311301","messageId":"20170210200012.24822-1-dturner@twosigma.com","threadId":"45108","inReplyTo":null,"subject":"[PATCH v4] gc: ignore old gc.log files","fromName":"David Turner","fromEmail":"dturner@twosigma.com","sentAt":"2017-02-10T20:00:12Z","receivedAt":"2017-02-10T20:03:07Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"Git should never get itself into a state where it refuses to do any\nmaintenance, just because at some point some piece of the maintenance\ndidn't make progress.\n\nIn this commit, git learns to ignore gc.log files which are older than\n(by default) one day old.  It also learns about a config, gc.logExpiry\nto manage this.  There is also some cleanup: a successful manual gc,\nor a warning-free auto gc, will remove any old gc.log files.\n\nIt might still happen that manual intervention is required\n(e.g. because the repo is corrupt), but at the very least it won't be\nbecause Git is too dumb to try again.\n\nAutomatic gc was intended to make client repositories be\nself-maintaining.  It would be good if automatic gc were also useful\nto server operators.  A server can end up in a state whre there are\nlots of unreferenced loose objects (say, because many users are doing\na bunch of rebasing and pushing their rebased branches). Before this\npatch, this state would cause a gc.log file to be created, preventing\nfuture auto gcs.  Then pack files could pile up.  Since many git\noperations are O(n) in the number of pack files, this would lead to\npoor performance.  Now, the pack files will get cleaned up, if\nnecessary, at least once per day.  And operators who find a need for\nmore-frequent gcs can adjust gc.logExpiry to meet their needs.\n\nSigned-off-by: David Turner <dturner@twosigma.com>\nHelped-by: Jeff King <peff@peff.net>\n---\n Documentation/config.txt |  6 ++++\n builtin/gc.c             | 76 +++++++++++++++++++++++++++++++++++++++++++-----\n t/t6500-gc.sh            | 12 ++++++++\n 3 files changed, 87 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex fc5a28a32..a684b7e3e 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -1402,6 +1402,12 @@ gc.autoDetach::\n \tMake `git gc --auto` return immediately and run in background\n \tif the system supports it. Default is true.\n \n+gc.logExpiry::\n+\tIf the file gc.log exists, then `git gc --auto` won't run\n+\tunless that file is more than 'gc.logExpiry' old.  Default is\n+\t\"1.day\".  See `gc.pruneExpire` for more ways to specify its\n+\tvalue.\n+\n gc.packRefs::\n \tRunning `git pack-refs` in a repository renders it\n \tunclonable by Git versions prior to 1.5.1.2 over dumb\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex 331f21926..55c441115 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -33,6 +33,7 @@ static int aggressive_window = 250;\n static int gc_auto_threshold = 6700;\n static int gc_auto_pack_limit = 50;\n static int detach_auto = 1;\n+static unsigned long gc_log_expire_time;\n static const char *prune_expire = \"2.weeks.ago\";\n static const char *prune_worktrees_expire = \"3.months.ago\";\n \n@@ -76,10 +77,48 @@ static void git_config_date_string(const char *key, const char **output)\n static void process_log_file(void)\n {\n \tstruct stat st;\n-\tif (!fstat(get_lock_file_fd(&log_lock), &st) && st.st_size)\n+\tif (fstat(get_lock_file_fd(&log_lock), &st)) {\n+\t\tif (errno == ENOENT) {\n+\t\t\t/*\n+\t\t\t * The user has probably intentionally deleted\n+\t\t\t * gc.log.lock (perhaps because they're blowing\n+\t\t\t * away the whole repo), so thre's no need to\n+\t\t\t * report anything here.  But we also won't\n+\t\t\t * delete gc.log, because we don't know what\n+\t\t\t * the user's intentions are.\n+\t\t\t */\n+\t\t} else {\n+\t\t\tFILE *fp;\n+\t\t\tint fd;\n+\t\t\tint saved_errno = errno;\n+\t\t\t/*\n+\t\t\t * Perhaps there was an i/o error or another\n+\t\t\t * unlikely situation.  Try to make a note of\n+\t\t\t * this in gc.log.  If this fails again,\n+\t\t\t * give up and leave gc.log as it was.\n+\t\t\t */\n+\t\t\trollback_lock_file(&log_lock);\n+\t\t\tfd = hold_lock_file_for_update(&log_lock,\n+\t\t\t\t\t\t       git_path(\"gc.log\"),\n+\t\t\t\t\t\t       LOCK_DIE_ON_ERROR);\n+\n+\t\t\tfp = fdopen(fd, \"w\");\n+\t\t\tfprintf(fp, _(\"Failed to fstat %s: %s\"),\n+\t\t\t\tget_tempfile_path(&log_lock.tempfile),\n+\t\t\t\tstrerror(saved_errno));\n+\t\t\tfclose(fp);\n+\t\t\tcommit_lock_file(&log_lock);\n+\t\t\terrno = saved_errno;\n+\t\t}\n+\n+\t} else if (st.st_size) {\n+\t\t/* There was some error recorded in the lock file */\n \t\tcommit_lock_file(&log_lock);\n-\telse\n+\t} else {\n+\t\t/* No error, clean up any old gc.log */\n+\t\tunlink(git_path(\"gc.log\"));\n \t\trollback_lock_file(&log_lock);\n+\t}\n }\n \n static void process_log_file_at_exit(void)\n@@ -113,6 +152,9 @@ static void gc_config(void)\n \tgit_config_get_bool(\"gc.autodetach\", &detach_auto);\n \tgit_config_date_string(\"gc.pruneexpire\", &prune_expire);\n \tgit_config_date_string(\"gc.worktreepruneexpire\", &prune_worktrees_expire);\n+\tif (!git_config_get_value(\"gc.logexpiry\", &value))\n+\t\tparse_expiry_date(value, &gc_log_expire_time);\n+\n \tgit_config(git_default_config, NULL);\n }\n \n@@ -290,19 +332,34 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)\n static int report_last_gc_error(void)\n {\n \tstruct strbuf sb = STRBUF_INIT;\n-\tint ret;\n+\tint ret = 0;\n+\tstruct stat st;\n+\tchar *gc_log_path = git_pathdup(\"gc.log\");\n+\n+\tif (stat(gc_log_path, &st)) {\n+\t\tif (errno == ENOENT)\n+\t\t\tgoto done;\n+\n+\t\tret = error_errno(_(\"Can't stat %s\"), gc_log_path);\n+\t\tgoto done;\n+\t}\n+\n+\tif (st.st_mtime < gc_log_expire_time)\n+\t\tgoto done;\n \n-\tret = strbuf_read_file(&sb, git_path(\"gc.log\"), 0);\n+\tret = strbuf_read_file(&sb, gc_log_path, 0);\n \tif (ret > 0)\n-\t\treturn error(_(\"The last gc run reported the following. \"\n+\t\tret = error(_(\"The last gc run reported the following. \"\n \t\t\t       \"Please correct the root cause\\n\"\n \t\t\t       \"and remove %s.\\n\"\n \t\t\t       \"Automatic cleanup will not be performed \"\n \t\t\t       \"until the file is removed.\\n\\n\"\n \t\t\t       \"%s\"),\n-\t\t\t     git_path(\"gc.log\"), sb.buf);\n+\t\t\t    gc_log_path, sb.buf);\n \tstrbuf_release(&sb);\n-\treturn 0;\n+done:\n+\tfree(gc_log_path);\n+\treturn ret;\n }\n \n static int gc_before_repack(void)\n@@ -349,6 +406,8 @@ int cmd_gc(int argc, const char **argv, const char *prefix)\n \targv_array_pushl(&prune_worktrees, \"worktree\", \"prune\", \"--expire\", NULL);\n \targv_array_pushl(&rerere, \"rerere\", \"gc\", NULL);\n \n+\t/* default expiry time, overwritten in gc_config */\n+\tparse_expiry_date(\"1.day\", &gc_log_expire_time);\n \tgc_config();\n \n \tif (pack_refs < 0)\n@@ -448,5 +507,8 @@ int cmd_gc(int argc, const char **argv, const char *prefix)\n \t\twarning(_(\"There are too many unreachable loose objects; \"\n \t\t\t\"run 'git prune' to remove them.\"));\n \n+\tif (!daemonized)\n+\t\tunlink(git_path(\"gc.log\"));\n+\n \treturn 0;\n }\ndiff --git a/t/t6500-gc.sh b/t/t6500-gc.sh\nindex 1762dfa6a..e1fb9b4d5 100755\n--- a/t/t6500-gc.sh\n+++ b/t/t6500-gc.sh\n@@ -67,5 +67,17 @@ test_expect_success 'auto gc with too many loose objects does not attempt to cre\n \ttest_line_count = 2 new # There is one new pack and its .idx\n '\n \n+test_expect_success 'background auto gc does not run if gc.log is present and recent but does if it is old' '\n+\ttest_commit foo &&\n+\ttest_commit bar &&\n+\tgit repack &&\n+\ttest_config gc.autopacklimit 1 &&\n+\ttest_config gc.autodetach true &&\n+\techo fleem >.git/gc.log &&\n+\ttest_must_fail git gc --auto 2>err &&\n+\ttest_i18ngrep \"^error:\" err &&\n+\ttest-chmtime =-86401 .git/gc.log &&\n+\tgit gc --auto\n+'\n \n test_done\n-- \n2.11.GIT\n\n"}]}