Skip to content

Commit a831c06

Browse files
novalisgitster
authored andcommitted
gc: ignore old gc.log files
A server can end up in a state where there are lots of unreferenced loose objects (say, because many users are doing a bunch of rebasing and pushing their rebased branches). Running "git gc --auto" in this state would cause a gc.log file to be created, preventing future auto gcs, causing pack files to pile up. Since many git operations are O(n) in the number of pack files, this would lead to poor performance. Git should never get itself into a state where it refuses to do any maintenance, just because at some point some piece of the maintenance didn't make progress. Teach Git to ignore gc.log files which are older than (by default) one day old, which can be tweaked via the gc.logExpiry configuration variable. That way, these pack files will get cleaned up, if necessary, at least once per day. And operators who find a need for more-frequent gcs can adjust gc.logExpiry to meet their needs. There is also some cleanup: a successful manual gc, or a warning-free auto gc with an old log file, will remove any old gc.log files. It might still happen that manual intervention is required (e.g. because the repo is corrupt), but at the very least it won't be because Git is too dumb to try again. Signed-off-by: David Turner <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3b9e3c2 commit a831c06

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,12 @@ gc.autoDetach::
13971397
Make `git gc --auto` return immediately and run in background
13981398
if the system supports it. Default is true.
13991399

1400+
gc.logExpiry::
1401+
If the file gc.log exists, then `git gc --auto` won't run
1402+
unless that file is more than 'gc.logExpiry' old. Default is
1403+
"1.day". See `gc.pruneExpire` for more ways to specify its
1404+
value.
1405+
14001406
gc.packRefs::
14011407
Running `git pack-refs` in a repository renders it
14021408
unclonable by Git versions prior to 1.5.1.2 over dumb

builtin/gc.c

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ static int aggressive_window = 250;
3333
static int gc_auto_threshold = 6700;
3434
static int gc_auto_pack_limit = 50;
3535
static int detach_auto = 1;
36+
static unsigned long gc_log_expire_time;
37+
static const char *gc_log_expire = "1.day.ago";
3638
static const char *prune_expire = "2.weeks.ago";
3739
static const char *prune_worktrees_expire = "3.months.ago";
3840

@@ -76,10 +78,28 @@ static void git_config_date_string(const char *key, const char **output)
7678
static void process_log_file(void)
7779
{
7880
struct stat st;
79-
if (!fstat(get_lock_file_fd(&log_lock), &st) && st.st_size)
81+
if (fstat(get_lock_file_fd(&log_lock), &st)) {
82+
/*
83+
* Perhaps there was an i/o error or another
84+
* unlikely situation. Try to make a note of
85+
* this in gc.log along with any existing
86+
* messages.
87+
*/
88+
int saved_errno = errno;
89+
fprintf(stderr, _("Failed to fstat %s: %s"),
90+
get_tempfile_path(&log_lock.tempfile),
91+
strerror(saved_errno));
92+
fflush(stderr);
8093
commit_lock_file(&log_lock);
81-
else
94+
errno = saved_errno;
95+
} else if (st.st_size) {
96+
/* There was some error recorded in the lock file */
97+
commit_lock_file(&log_lock);
98+
} else {
99+
/* No error, clean up any old gc.log */
100+
unlink(git_path("gc.log"));
82101
rollback_lock_file(&log_lock);
102+
}
83103
}
84104

85105
static void process_log_file_at_exit(void)
@@ -113,6 +133,8 @@ static void gc_config(void)
113133
git_config_get_bool("gc.autodetach", &detach_auto);
114134
git_config_date_string("gc.pruneexpire", &prune_expire);
115135
git_config_date_string("gc.worktreepruneexpire", &prune_worktrees_expire);
136+
git_config_date_string("gc.logexpiry", &gc_log_expire);
137+
116138
git_config(git_default_config, NULL);
117139
}
118140

@@ -290,19 +312,34 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
290312
static int report_last_gc_error(void)
291313
{
292314
struct strbuf sb = STRBUF_INIT;
293-
int ret;
315+
int ret = 0;
316+
struct stat st;
317+
char *gc_log_path = git_pathdup("gc.log");
294318

295-
ret = strbuf_read_file(&sb, git_path("gc.log"), 0);
319+
if (stat(gc_log_path, &st)) {
320+
if (errno == ENOENT)
321+
goto done;
322+
323+
ret = error_errno(_("Can't stat %s"), gc_log_path);
324+
goto done;
325+
}
326+
327+
if (st.st_mtime < gc_log_expire_time)
328+
goto done;
329+
330+
ret = strbuf_read_file(&sb, gc_log_path, 0);
296331
if (ret > 0)
297-
return error(_("The last gc run reported the following. "
332+
ret = error(_("The last gc run reported the following. "
298333
"Please correct the root cause\n"
299334
"and remove %s.\n"
300335
"Automatic cleanup will not be performed "
301336
"until the file is removed.\n\n"
302337
"%s"),
303-
git_path("gc.log"), sb.buf);
338+
gc_log_path, sb.buf);
304339
strbuf_release(&sb);
305-
return 0;
340+
done:
341+
free(gc_log_path);
342+
return ret;
306343
}
307344

308345
static int gc_before_repack(void)
@@ -349,7 +386,10 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
349386
argv_array_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
350387
argv_array_pushl(&rerere, "rerere", "gc", NULL);
351388

389+
/* default expiry time, overwritten in gc_config */
352390
gc_config();
391+
if (parse_expiry_date(gc_log_expire, &gc_log_expire_time))
392+
die(_("Failed to parse gc.logexpiry value %s"), gc_log_expire);
353393

354394
if (pack_refs < 0)
355395
pack_refs = !is_bare_repository();
@@ -448,5 +488,8 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
448488
warning(_("There are too many unreachable loose objects; "
449489
"run 'git prune' to remove them."));
450490

491+
if (!daemonized)
492+
unlink(git_path("gc.log"));
493+
451494
return 0;
452495
}

t/t6500-gc.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,20 @@ test_expect_success 'auto gc with too many loose objects does not attempt to cre
6767
test_line_count = 2 new # There is one new pack and its .idx
6868
'
6969

70+
test_expect_success 'background auto gc does not run if gc.log is present and recent but does if it is old' '
71+
test_commit foo &&
72+
test_commit bar &&
73+
git repack &&
74+
test_config gc.autopacklimit 1 &&
75+
test_config gc.autodetach true &&
76+
echo fleem >.git/gc.log &&
77+
test_must_fail git gc --auto 2>err &&
78+
test_i18ngrep "^error:" err &&
79+
test_config gc.logexpiry 5.days &&
80+
test-chmtime =-345600 .git/gc.log &&
81+
test_must_fail git gc --auto &&
82+
test_config gc.logexpiry 2.days &&
83+
git gc --auto
84+
'
7085

7186
test_done

0 commit comments

Comments
 (0)