usage.c + gc: add and use a die_message_errno()

Change the "error: " output when we exit with 128 due to gc.log errors to use a "fatal: " prefix instead. To do this add a die_message_errno() a sibling function to the die_errno() added in a preceding commit. Before this we'd expect report_last_gc_error() to return -1 from error_errno() in this case. It already treated a status of 0 and 1 specially. Let's just document that anything that's not 0 or 1 should be returned. We could also retain the "ret < 0" behavior here without hardcoding 128 by returning -128, and having the caller do a "return -ret", but I think this makes more sense, and preserves the path from die_message*()'s return value to the "return" without hardcoding "128". Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Dec 7, 2021 at 19:26 UTC 24f6e6d626b0125f741880918fb3b9b85f607a79
3 files changed +20 -6
builtin/gc.c
+7 -6
@@ -470,7 +470,8 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
470 /*
471 * Returns 0 if there was no previous error and gc can proceed, 1 if
472 * gc should not proceed due to an error in the last run. Prints a
473 - * message and returns -1 if an error occurred while reading gc.log
473 + * message and returns with a non-[01] status code if an error occurred
474 + * while reading gc.log
475 */
476 static int report_last_gc_error(void)
477 {
@@ -484,7 +485,7 @@ static int report_last_gc_error(void)
485 if (errno == ENOENT)
486 goto done;
487
487 - ret = error_errno(_("cannot stat '%s'"), gc_log_path);
488 + ret = die_message_errno(_("cannot stat '%s'"), gc_log_path);
489 goto done;
490 }
491
@@ -493,7 +494,7 @@ static int report_last_gc_error(void)
494
495 len = strbuf_read_file(&sb, gc_log_path, 0);
496 if (len < 0)
496 - ret = error_errno(_("cannot read '%s'"), gc_log_path);
497 + ret = die_message_errno(_("cannot read '%s'"), gc_log_path);
498 else if (len > 0) {
499 /*
500 * A previous gc failed. Report the error, and don't
@@ -612,12 +613,12 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
613 if (detach_auto) {
614 int ret = report_last_gc_error();
615
615 - if (ret < 0)
616 - /* an I/O error occurred, already reported */
617 - return 128;
616 if (ret == 1)
617 /* Last gc --auto failed. Skip this one. */
618 return 0;
619 + else if (ret)
620 + /* an I/O error occurred, already reported */
621 + return ret;
622
623 if (lock_repo_for_gc(force, &pid))
624 return 0;
git-compat-util.h
+1
@@ -480,6 +480,7 @@ NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2))
480 NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
481 NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
482 int die_message(const char *err, ...) __attribute__((format (printf, 1, 2)));
483 +int die_message_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
484 int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
485 int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
486 void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
usage.c
+12
@@ -233,6 +233,18 @@ int die_message(const char *err, ...)
233 return 128;
234 }
235
236 +#undef die_message_errno
237 +int die_message_errno(const char *fmt, ...)
238 +{
239 + char buf[1024];
240 + va_list params;
241 +
242 + va_start(params, fmt);
243 + die_message_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
244 + va_end(params);
245 + return 128;
246 +}
247 +
248 #undef error_errno
249 int error_errno(const char *fmt, ...)
250 {