gc: exit with status 128 on failure

A value of -1 returned from cmd_gc gets propagated to exit(), resulting in an exit status of 255. Use die instead for a clearer error message and a controlled exit. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Jul 16, 2018 at 23:54 UTC fec2ed21871c73d5212f5c8843d250eb7d5a649c
2 files changed +15 -22
builtin/gc.c
+14 -21
@@ -438,10 +438,9 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
438 return NULL;
439 }
440
441 -static int report_last_gc_error(void)
441 +static void report_last_gc_error(void)
442 {
443 struct strbuf sb = STRBUF_INIT;
444 - int ret = 0;
444 ssize_t len;
445 struct stat st;
446 char *gc_log_path = git_pathdup("gc.log");
@@ -450,8 +449,7 @@ static int report_last_gc_error(void)
449 if (errno == ENOENT)
450 goto done;
451
453 - ret = error_errno(_("cannot stat '%s'"), gc_log_path);
454 - goto done;
452 + die_errno(_("cannot stat '%s'"), gc_log_path);
453 }
454
455 if (st.st_mtime < gc_log_expire_time)
@@ -459,9 +457,9 @@ static int report_last_gc_error(void)
457
458 len = strbuf_read_file(&sb, gc_log_path, 0);
459 if (len < 0)
462 - ret = error_errno(_("cannot read '%s'"), gc_log_path);
460 + die_errno(_("cannot read '%s'"), gc_log_path);
461 else if (len > 0)
464 - ret = error(_("The last gc run reported the following. "
462 + die(_("The last gc run reported the following. "
463 "Please correct the root cause\n"
464 "and remove %s.\n"
465 "Automatic cleanup will not be performed "
@@ -471,20 +469,18 @@ static int report_last_gc_error(void)
469 strbuf_release(&sb);
470 done:
471 free(gc_log_path);
474 - return ret;
472 }
473
477 -static int gc_before_repack(void)
474 +static void gc_before_repack(void)
475 {
476 if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
480 - return error(FAILED_RUN, pack_refs_cmd.argv[0]);
477 + die(FAILED_RUN, pack_refs_cmd.argv[0]);
478
479 if (prune_reflogs && run_command_v_opt(reflog.argv, RUN_GIT_CMD))
483 - return error(FAILED_RUN, reflog.argv[0]);
480 + die(FAILED_RUN, reflog.argv[0]);
481
482 pack_refs = 0;
483 prune_reflogs = 0;
487 - return 0;
484 }
485
486 int cmd_gc(int argc, const char **argv, const char *prefix)
@@ -565,13 +561,11 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
561 fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
562 }
563 if (detach_auto) {
568 - if (report_last_gc_error())
569 - return -1;
564 + report_last_gc_error(); /* dies on error */
565
566 if (lock_repo_for_gc(force, &pid))
567 return 0;
573 - if (gc_before_repack())
574 - return -1;
568 + gc_before_repack(); /* dies on failure */
569 delete_tempfile(&pidfile);
570
571 /*
@@ -611,12 +605,11 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
605 atexit(process_log_file_at_exit);
606 }
607
614 - if (gc_before_repack())
615 - return -1;
608 + gc_before_repack();
609
610 if (!repository_format_precious_objects) {
611 if (run_command_v_opt(repack.argv, RUN_GIT_CMD))
619 - return error(FAILED_RUN, repack.argv[0]);
612 + die(FAILED_RUN, repack.argv[0]);
613
614 if (prune_expire) {
615 argv_array_push(&prune, prune_expire);
@@ -626,18 +619,18 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
619 argv_array_push(&prune,
620 "--exclude-promisor-objects");
621 if (run_command_v_opt(prune.argv, RUN_GIT_CMD))
629 - return error(FAILED_RUN, prune.argv[0]);
622 + die(FAILED_RUN, prune.argv[0]);
623 }
624 }
625
626 if (prune_worktrees_expire) {
627 argv_array_push(&prune_worktrees, prune_worktrees_expire);
628 if (run_command_v_opt(prune_worktrees.argv, RUN_GIT_CMD))
636 - return error(FAILED_RUN, prune_worktrees.argv[0]);
629 + die(FAILED_RUN, prune_worktrees.argv[0]);
630 }
631
632 if (run_command_v_opt(rerere.argv, RUN_GIT_CMD))
640 - return error(FAILED_RUN, rerere.argv[0]);
633 + die(FAILED_RUN, rerere.argv[0]);
634
635 report_garbage = report_pack_garbage;
636 reprepare_packed_git(the_repository);
t/t6500-gc.sh
+1 -1
@@ -117,7 +117,7 @@ test_expect_success 'background auto gc does not run if gc.log is present and re
117 test_config gc.autodetach true &&
118 echo fleem >.git/gc.log &&
119 test_must_fail git gc --auto 2>err &&
120 - test_i18ngrep "^error:" err &&
120 + test_i18ngrep "^fatal:" err &&
121 test_config gc.logexpiry 5.days &&
122 test-tool chmtime =-345600 .git/gc.log &&
123 test_must_fail git gc --auto &&