Skip to content

Commit 993fa56

Browse files
committed
Merge branch 'jn/gc-auto'
"gc --auto" ended up calling exit(-1) upon error, which has been corrected to use exit(1). Also the error reporting behaviour when daemonized has been updated to exit with zero status when stopping due to a previously discovered error (which implies there is no point running gc to improve the situation); we used to exit with failure in such a case. * jn/gc-auto: gc: do not return error for prior errors in daemonized mode
2 parents 99913dd + 3029970 commit 993fa56

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

Documentation/config.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,8 @@ gc.writeCommitGraph::
16431643
for details.
16441644

16451645
gc.logExpiry::
1646-
If the file gc.log exists, then `git gc --auto` won't run
1646+
If the file gc.log exists, then `git gc --auto` will print
1647+
its content and exit with status zero instead of running
16471648
unless that file is more than 'gc.logExpiry' old. Default is
16481649
"1.day". See `gc.pruneExpire` for more ways to specify its
16491650
value.

builtin/gc.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,15 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
441441
return NULL;
442442
}
443443

444-
static void report_last_gc_error(void)
444+
/*
445+
* Returns 0 if there was no previous error and gc can proceed, 1 if
446+
* gc should not proceed due to an error in the last run. Prints a
447+
* message and returns -1 if an error occured while reading gc.log
448+
*/
449+
static int report_last_gc_error(void)
445450
{
446451
struct strbuf sb = STRBUF_INIT;
452+
int ret = 0;
447453
ssize_t len;
448454
struct stat st;
449455
char *gc_log_path = git_pathdup("gc.log");
@@ -452,26 +458,35 @@ static void report_last_gc_error(void)
452458
if (errno == ENOENT)
453459
goto done;
454460

455-
die_errno(_("cannot stat '%s'"), gc_log_path);
461+
ret = error_errno(_("cannot stat '%s'"), gc_log_path);
462+
goto done;
456463
}
457464

458465
if (st.st_mtime < gc_log_expire_time)
459466
goto done;
460467

461468
len = strbuf_read_file(&sb, gc_log_path, 0);
462469
if (len < 0)
463-
die_errno(_("cannot read '%s'"), gc_log_path);
464-
else if (len > 0)
465-
die(_("The last gc run reported the following. "
470+
ret = error_errno(_("cannot read '%s'"), gc_log_path);
471+
else if (len > 0) {
472+
/*
473+
* A previous gc failed. Report the error, and don't
474+
* bother with an automatic gc run since it is likely
475+
* to fail in the same way.
476+
*/
477+
warning(_("The last gc run reported the following. "
466478
"Please correct the root cause\n"
467479
"and remove %s.\n"
468480
"Automatic cleanup will not be performed "
469481
"until the file is removed.\n\n"
470482
"%s"),
471483
gc_log_path, sb.buf);
484+
ret = 1;
485+
}
472486
strbuf_release(&sb);
473487
done:
474488
free(gc_log_path);
489+
return ret;
475490
}
476491

477492
static void gc_before_repack(void)
@@ -564,7 +579,13 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
564579
fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
565580
}
566581
if (detach_auto) {
567-
report_last_gc_error(); /* dies on error */
582+
int ret = report_last_gc_error();
583+
if (ret < 0)
584+
/* an I/O error occured, already reported */
585+
exit(128);
586+
if (ret == 1)
587+
/* Last gc --auto failed. Skip this one. */
588+
return 0;
568589

569590
if (lock_repo_for_gc(force, &pid))
570591
return 0;

t/t6500-gc.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ test_expect_success 'background auto gc does not run if gc.log is present and re
137137
test_config gc.autopacklimit 1 &&
138138
test_config gc.autodetach true &&
139139
echo fleem >.git/gc.log &&
140-
test_must_fail git gc --auto 2>err &&
141-
test_i18ngrep "^fatal:" err &&
140+
git gc --auto 2>err &&
141+
test_i18ngrep "^warning:" err &&
142142
test_config gc.logexpiry 5.days &&
143143
test-tool chmtime =-345600 .git/gc.log &&
144-
test_must_fail git gc --auto &&
144+
git gc --auto &&
145145
test_config gc.logexpiry 2.days &&
146146
run_and_wait_for_auto_gc &&
147147
ls .git/objects/pack/pack-*.pack >packs &&

0 commit comments

Comments
 (0)