From patchwork Mon Dec 6 16:55:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12658939 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5EC1FC433FE for ; Mon, 6 Dec 2021 17:04:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242005AbhLFRHl (ORCPT ); Mon, 6 Dec 2021 12:07:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233709AbhLFQ71 (ORCPT ); Mon, 6 Dec 2021 11:59:27 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72EFDC061746 for ; Mon, 6 Dec 2021 08:55:58 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id j3so23868091wrp.1 for ; Mon, 06 Dec 2021 08:55:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/UX8Fb8OjipW5lAzuijt+PprKezztg8JuRK4aWcSSEk=; b=EihHoQ5oE9PXHmZ3QIraH9MBQUcYpzERci0Bv0fvS5nT7wl8ReZEl1RtB1/0tzoO+i tFjUFu6cZigM1dkbRmAWAOuDbHgMnC5ytZS3ho2ktUt6E5izsGQLNM78NYX0jVSokXii OvAJIvZv2Rhe2ppypGM6mTAQdRWIKIoUbMIXhWuo2TKuPllfwhV8QLdsm3tce+Ui2DoQ Nwyd2NCsC3+kjWul5YUjflcv52Q6Pqext//8jnsZcF/1kOdRUxgA45EzPP1n1PeeNCzF ATduFlvOjRDjWzMOAUXdp/igR2J3qjcmNkLf7/AuSuXSgHxZAHaM6P/3UAoZoU/NH4w7 8Q9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/UX8Fb8OjipW5lAzuijt+PprKezztg8JuRK4aWcSSEk=; b=HMt3tng7tsAfhPZhQsI5xef8My5Vu8Zf/XSOCc+s2Qw94cKxweKQ0B7gHnR4ouLURU vTI66MQqL9YId9U4dCIj+A4ED0aCG2vAR177QijnJi7gazJJ1ma4qUO0iOlODCuPozaH hYGsvpCCHNnpLTx9JdMXcimTZWuULiV3XIEz/cCONOrK8Mi/0qIMw7G57WpObPbZ0Vq+ NRqWDJj91Q2DYTZKFMg1f0NuMOBS5HWl49lWyf1wRW9E4QhLo4777PRUFlixm8g4IqC3 X/qSZtYZs/+/j4ENBE1YQ1lxlTPsUeo6u+7tnRDIz53588jQgjM88TAHCaNFFSnWg8U5 bCzw== X-Gm-Message-State: AOAM532cf4GiNukFPi5ZCpeJpRPlhyJQ4v7kEQ1CMEPkN5WPUpwj6Lvn txvWg4VVjeMH/TQQ7o0E1TSYM8ah2G14cA== X-Google-Smtp-Source: ABdhPJzOCLPN/WYEgSvcHyZYU3Bak82Sj9L7VTUxztyFDx0YMiRh+fNEHUWGdQI+NY0ahKRbpMELjg== X-Received: by 2002:adf:cc91:: with SMTP id p17mr44697472wrj.589.1638809756811; Mon, 06 Dec 2021 08:55:56 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id j40sm11775419wms.16.2021.12.06.08.55.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Dec 2021 08:55:56 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Taylor Blau , Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 1/4] usage.c: add a die_message() routine Date: Mon, 6 Dec 2021 17:55:50 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.898.g5a552c2e5f0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org We have code in various places that would like to call die(), but wants to defer the exit(128) it would invoke, e.g. to print an additional message, or adjust the exit code. Add a die_message() helper routine to bridge this gap in the API. Functionally this behaves just like the error() routine, except it'll print a "fatal: " prefix, and it will return with 128 instead of -1, this is so that caller can pass the return value to "exit()", instead of having to hardcode "exit(128)". A subsequent commit will migrate various callers that benefit from this function over to it. For now we're just adding the routine and making die_builtin() in usage.c itself use it. Signed-off-by: Ævar Arnfjörð Bjarmason --- git-compat-util.h | 1 + usage.c | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/git-compat-util.h b/git-compat-util.h index c6bd2a84e55..a83fbdf6398 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -479,6 +479,7 @@ NORETURN void usage(const char *err); NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2))); NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); +int die_message(const char *err, ...) __attribute__((format (printf, 1, 2))); int error(const char *err, ...) __attribute__((format (printf, 1, 2))); int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); diff --git a/usage.c b/usage.c index c7d233b0de9..74b43c5db6f 100644 --- a/usage.c +++ b/usage.c @@ -55,6 +55,12 @@ static NORETURN void usage_builtin(const char *err, va_list params) exit(129); } +static void die_message_builtin(const char *err, va_list params) +{ + trace2_cmd_error_va(err, params); + vreportf("fatal: ", err, params); +} + /* * We call trace2_cmd_error_va() in the below functions first and * expect it to va_copy 'params' before using it (because an 'ap' can @@ -62,10 +68,7 @@ static NORETURN void usage_builtin(const char *err, va_list params) */ static NORETURN void die_builtin(const char *err, va_list params) { - trace2_cmd_error_va(err, params); - - vreportf("fatal: ", err, params); - + die_message_builtin(err, params); exit(128); } @@ -211,6 +214,17 @@ void NORETURN die_errno(const char *fmt, ...) va_end(params); } +#undef die_message +int die_message(const char *err, ...) +{ + va_list params; + + va_start(params, err); + die_message_builtin(err, params); + va_end(params); + return 128; +} + #undef error_errno int error_errno(const char *fmt, ...) { From patchwork Mon Dec 6 16:55:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12658937 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 735BEC43217 for ; Mon, 6 Dec 2021 17:04:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231172AbhLFRHm (ORCPT ); Mon, 6 Dec 2021 12:07:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347632AbhLFQ72 (ORCPT ); Mon, 6 Dec 2021 11:59:28 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41963C061746 for ; Mon, 6 Dec 2021 08:55:59 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id d24so23934254wra.0 for ; Mon, 06 Dec 2021 08:55:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RS8g8b1m0rSADL1KI1UkqxXJYX3nHi6MXAdizSUwaB4=; b=K1DJAEaaDTKHMg94fdcra454JcRXHGaMNjTQIfEtjojFE86q1YCakNjJmQdDQv51j4 cHbOgYAaxskRJ9kZFVsIWmUpvkb9QnJZkzy+nlUUF4880AYkWC11qkTSDTByjWuwrsP/ Fg9Q05fU0WR/WjdR1pohZnZs/ckDTsImXuH16QV/Xg60plJ2cPK/rfEBnsJ8DI9/Dxk1 9Uu2GyTXJS7tFl9ryHaetXNs/P0e71M+WpBmVWTI9RItkV6hLu2VUZNbumcbdv2lxnGy /v+W9kYiSGBWlYP9w6gLHZsaUsQT8mS8eOOAq2/p+UAugDznWJD6yAzsrR1JfBEB7cBO bwqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RS8g8b1m0rSADL1KI1UkqxXJYX3nHi6MXAdizSUwaB4=; b=aLNOYCvRymMxBkMhNs8SDoFC72m9z4tW8/Fltfmhaufe/T8vVJ57T9YbdrAPoMOrFE uCDXq98UuzJ3nkpGk2huhEVxlgX4YoiBTdIIb/qbqgRPTZrHp3Dm9kdJ/JCMGHTOrWPa 8jLE3kznc1pyVAQ0dCnFQ8XaEsiOkUEt9EdjdR0EvLrF+94NlPMD09XSSE0xmqKgvsS8 MhphW1drvzcUuTVOQSJ2YKyF8aDUvW0uhV+0GCJCz9mgiOWvpEYUQ64VB3G+1xLaJsrh ANlu8yBXa3CQZlJuJxUOzkbX46bYllxLtGe4Z4r3UMxQ5v5Byt6x2yK+W98f1J9aImgB KxRQ== X-Gm-Message-State: AOAM530un0CtlnVfSRdd/DJghOyL38JmrbMHeNrSfMy6oQjyepZU07iV ki0ClrVy8Y1t0SnCDj4DOT9BkSzDoB1P+g== X-Google-Smtp-Source: ABdhPJzZE1j0O3VBeuc1TvboWxKCR/iclksa48MGv9hHfX9gc0DXkQ+rPYvX3KfywucHGjdrdf9BQA== X-Received: by 2002:adf:fb05:: with SMTP id c5mr43920821wrr.497.1638809757592; Mon, 06 Dec 2021 08:55:57 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id j40sm11775419wms.16.2021.12.06.08.55.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Dec 2021 08:55:57 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Taylor Blau , Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 2/4] usage.c API users: use die_message() where appropriate Date: Mon, 6 Dec 2021 17:55:51 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.898.g5a552c2e5f0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change code that either called error() and proceeded to exit with 128, or emitted its own "fatal: " messages to use the die_message() function added in a preceding commit. In order to do that we need to add a get_die_message_routine() function, which works like the other get_*_routine() functions in usage.c. There is no set_die_message_rotine(), as it hasn't been needed yet. We can add it if we ever need it. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/fast-import.c | 12 +++++++----- builtin/notes.c | 9 +++++---- git-compat-util.h | 1 + http-backend.c | 3 ++- parse-options.c | 2 +- run-command.c | 16 +++++----------- usage.c | 12 ++++++++++-- 7 files changed, 31 insertions(+), 24 deletions(-) diff --git a/builtin/fast-import.c b/builtin/fast-import.c index 20406f67754..2b2e28bad79 100644 --- a/builtin/fast-import.c +++ b/builtin/fast-import.c @@ -401,16 +401,18 @@ static void dump_marks(void); static NORETURN void die_nicely(const char *err, va_list params) { + va_list cp; static int zombie; - char message[2 * PATH_MAX]; + report_fn die_message_fn = get_die_message_routine(); - vsnprintf(message, sizeof(message), err, params); - fputs("fatal: ", stderr); - fputs(message, stderr); - fputc('\n', stderr); + va_copy(cp, params); + die_message_fn(err, params); if (!zombie) { + char message[2 * PATH_MAX]; + zombie = 1; + vsnprintf(message, sizeof(message), err, cp); write_crash_report(message); end_packfile(); unkeep_all_packs(); diff --git a/builtin/notes.c b/builtin/notes.c index 71c59583a17..2812d1eac40 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -201,11 +201,12 @@ static void prepare_note_data(const struct object_id *object, struct note_data * static void write_note_data(struct note_data *d, struct object_id *oid) { if (write_object_file(d->buf.buf, d->buf.len, blob_type, oid)) { - error(_("unable to write note object")); + int status = die_message(_("unable to write note object")); + if (d->edit_path) - error(_("the note contents have been left in %s"), - d->edit_path); - exit(128); + die_message(_("the note contents have been left in %s"), + d->edit_path); + exit(status); } } diff --git a/git-compat-util.h b/git-compat-util.h index a83fbdf6398..d5e495529c8 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -514,6 +514,7 @@ static inline int const_error(void) typedef void (*report_fn)(const char *, va_list params); void set_die_routine(NORETURN_PTR report_fn routine); +report_fn get_die_message_routine(void); void set_error_routine(report_fn routine); report_fn get_error_routine(void); void set_warn_routine(report_fn routine); diff --git a/http-backend.c b/http-backend.c index 3d6e2ff17f8..982cb62c7cb 100644 --- a/http-backend.c +++ b/http-backend.c @@ -659,8 +659,9 @@ static NORETURN void die_webcgi(const char *err, va_list params) { if (dead <= 1) { struct strbuf hdr = STRBUF_INIT; + report_fn die_message_fn = get_die_message_routine(); - vreportf("fatal: ", err, params); + die_message_fn(err, params); http_status(&hdr, 500, "Internal Server Error"); hdr_nocache(&hdr); diff --git a/parse-options.c b/parse-options.c index fc5b43ff0b2..8bc0a21f1d7 100644 --- a/parse-options.c +++ b/parse-options.c @@ -1075,6 +1075,6 @@ void NORETURN usage_msg_opt(const char *msg, const char * const *usagestr, const struct option *options) { - fprintf(stderr, "fatal: %s\n\n", msg); + die_message("%s\n", msg); /* The extra \n is intentional */ usage_with_options(usagestr, options); } diff --git a/run-command.c b/run-command.c index f40df01c772..a790fe9799d 100644 --- a/run-command.c +++ b/run-command.c @@ -340,15 +340,6 @@ static void child_close_pair(int fd[2]) child_close(fd[1]); } -/* - * parent will make it look like the child spewed a fatal error and died - * this is needed to prevent changes to t0061. - */ -static void fake_fatal(const char *err, va_list params) -{ - vreportf("fatal: ", err, params); -} - static void child_error_fn(const char *err, va_list params) { const char msg[] = "error() should not be called in child\n"; @@ -372,9 +363,10 @@ static void NORETURN child_die_fn(const char *err, va_list params) static void child_err_spew(struct child_process *cmd, struct child_err *cerr) { static void (*old_errfn)(const char *err, va_list params); + report_fn die_message_routine = get_die_message_routine(); old_errfn = get_error_routine(); - set_error_routine(fake_fatal); + set_error_routine(die_message_routine); errno = cerr->syserr; switch (cerr->err) { @@ -1082,7 +1074,9 @@ static void *run_thread(void *data) static NORETURN void die_async(const char *err, va_list params) { - vreportf("fatal: ", err, params); + report_fn die_message_fn = get_die_message_routine(); + + die_message_fn(err, params); if (in_async()) { struct async *async = pthread_getspecific(async_key); diff --git a/usage.c b/usage.c index 74b43c5db6f..76399ba8409 100644 --- a/usage.c +++ b/usage.c @@ -68,7 +68,9 @@ static void die_message_builtin(const char *err, va_list params) */ static NORETURN void die_builtin(const char *err, va_list params) { - die_message_builtin(err, params); + report_fn die_message_fn = get_die_message_routine(); + + die_message_fn(err, params); exit(128); } @@ -112,6 +114,7 @@ static int die_is_recursing_builtin(void) * (ugh), so keep things static. */ static NORETURN_PTR report_fn usage_routine = usage_builtin; static NORETURN_PTR report_fn die_routine = die_builtin; +static report_fn die_message_routine = die_message_builtin; static report_fn error_routine = error_builtin; static report_fn warn_routine = warn_builtin; static int (*die_is_recursing)(void) = die_is_recursing_builtin; @@ -121,6 +124,11 @@ void set_die_routine(NORETURN_PTR report_fn routine) die_routine = routine; } +report_fn get_die_message_routine(void) +{ + return die_message_routine; +} + void set_error_routine(report_fn routine) { error_routine = routine; @@ -220,7 +228,7 @@ int die_message(const char *err, ...) va_list params; va_start(params, err); - die_message_builtin(err, params); + die_message_routine(err, params); va_end(params); return 128; } From patchwork Mon Dec 6 16:55:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12658943 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 286C0C4321E for ; Mon, 6 Dec 2021 17:04:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241953AbhLFRHn (ORCPT ); Mon, 6 Dec 2021 12:07:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347645AbhLFQ73 (ORCPT ); Mon, 6 Dec 2021 11:59:29 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4AEDCC061746 for ; Mon, 6 Dec 2021 08:56:00 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id i5so23851252wrb.2 for ; Mon, 06 Dec 2021 08:56:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aPoYF8IYShvXyW7r9gnbj2KhuykwuMvSfW25/s3TRZ0=; b=gevC+P3ayT0ZP/mJtCUQ38lWo68SpXg5537pknD8fMy801fumxTE+pG1PL1lycqn0l en6q0vT1XZvFYnpIob/csJ9FrG+HMki7L0vcAVyYvYkItvIkEAJ+fXnCGz/fjnC0AQFq v4qUwJnPKoa5oNbny5wZcR2KIXNeBdm9jM9osy6n3mlWmapfMRsDUtrxe1nE489bDSBJ OkHykOuX1FxgCHLwCwhepuddFXDmpp++f9uvlIsB2nSBnJKoTt4l6qyPvzjqYc8ORIPA 4xMKqvDFoVxH3pa5HqCT30kxhaRWABMFdy1WoYEZKkjnoiqqtbj0lCyONGrNodkziJpf 8u1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aPoYF8IYShvXyW7r9gnbj2KhuykwuMvSfW25/s3TRZ0=; b=KZDkdBWmM32EI2npRQOra96c+/waeFBZNyD9T7iFz27tN+qus9Pe69Um6atXFqQnJB laUNys2EPN6TKcra1+1O03qxK0TfcblWAcwx2uGEKlUm3pl5jwjbPt6H4VidcKSwwDew 4GsrQdQCf5t9IGhb2nt4MaN5mSqCs5nzFjETPt+hk7MAZfcKwCJ607/hrnYKEbeOWYuq VhUz49HM2R5ItmGqS4kNk59mzwLXZELI3bKCHNdpDDIbaUo0hxAd9uQgRcwd98cKP87F NekeIOb9i3cuXfivH2SNL4iEdZ+t0CLHBTxz29lHTN7cJ4Fk85SD0AakYE/X4NR5v7Gk wUEg== X-Gm-Message-State: AOAM5305dPHi1f/+RUyuft7ffOovd2qgpQUbSMQGmxPLCarMw3aoPcJC ckpEflpzNh1HXdGdYDumviASEWC/TkYHPQ== X-Google-Smtp-Source: ABdhPJxMT+D8dosR5iIfXPi4jNX9mDAcjrWZUo4XE0tD1sceexK2rWHIujmvsD7Xqz0nCNAxYMUjuQ== X-Received: by 2002:a5d:6091:: with SMTP id w17mr44872258wrt.65.1638809758532; Mon, 06 Dec 2021 08:55:58 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id j40sm11775419wms.16.2021.12.06.08.55.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Dec 2021 08:55:57 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Taylor Blau , Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 3/4] usage.c + gc: add and use a die_message_errno() Date: Mon, 6 Dec 2021 17:55:52 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.898.g5a552c2e5f0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change code the "error: " output when we exit with 128 due to gc.log errors to use a "fatal: " prefix instead. This adds a sibling function to the die_errno() added in a preceding commit. Since it returns 128 instead of -1 (like die_message()) we'll need to adjust report_last_gc_error(). Let's adjust it while we're at it to not conflate the "should skip" and "exit with this non-zero code" conditions, as the caller is no longer hardcoding "128", but relying on die_errno() to return a nen-zero exit() status. Since we're touching this code let's also use "return ret" in place of an "exit(ret)". This is kinder to any cleanup our our "cmd_main()" in "git.c" might want to do. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/gc.c | 21 ++++++++++++--------- git-compat-util.h | 1 + usage.c | 12 ++++++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/builtin/gc.c b/builtin/gc.c index bcef6a4c8da..7b451dfeefc 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -472,19 +472,20 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid) * gc should not proceed due to an error in the last run. Prints a * message and returns -1 if an error occurred while reading gc.log */ -static int report_last_gc_error(void) +static int report_last_gc_error(int *skip) { struct strbuf sb = STRBUF_INIT; int ret = 0; ssize_t len; struct stat st; char *gc_log_path = git_pathdup("gc.log"); + *skip = 0; if (stat(gc_log_path, &st)) { if (errno == ENOENT) goto done; - ret = error_errno(_("cannot stat '%s'"), gc_log_path); + ret = die_message_errno(_("cannot stat '%s'"), gc_log_path); goto done; } @@ -493,7 +494,7 @@ static int report_last_gc_error(void) len = strbuf_read_file(&sb, gc_log_path, 0); if (len < 0) - ret = error_errno(_("cannot read '%s'"), gc_log_path); + ret = die_message_errno(_("cannot read '%s'"), gc_log_path); else if (len > 0) { /* * A previous gc failed. Report the error, and don't @@ -507,7 +508,7 @@ static int report_last_gc_error(void) "until the file is removed.\n\n" "%s"), gc_log_path, sb.buf); - ret = 1; + *skip = 1; } strbuf_release(&sb); done: @@ -610,13 +611,15 @@ int cmd_gc(int argc, const char **argv, const char *prefix) fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n")); } if (detach_auto) { - int ret = report_last_gc_error(); - if (ret < 0) - /* an I/O error occurred, already reported */ - exit(128); - if (ret == 1) + int skip; + int ret = report_last_gc_error(&skip); + + if (skip) /* Last gc --auto failed. Skip this one. */ return 0; + if (ret) + /* an I/O error occurred, already reported */ + return ret; if (lock_repo_for_gc(force, &pid)) return 0; diff --git a/git-compat-util.h b/git-compat-util.h index d5e495529c8..c6c6f7d6b51 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -480,6 +480,7 @@ NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)) NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); int die_message(const char *err, ...) __attribute__((format (printf, 1, 2))); +int die_message_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); int error(const char *err, ...) __attribute__((format (printf, 1, 2))); int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); diff --git a/usage.c b/usage.c index 76399ba8409..3d09e8eea48 100644 --- a/usage.c +++ b/usage.c @@ -233,6 +233,18 @@ int die_message(const char *err, ...) return 128; } +#undef die_message_errno +int die_message_errno(const char *fmt, ...) +{ + char buf[1024]; + va_list params; + + va_start(params, fmt); + die_message_routine(fmt_with_err(buf, sizeof(buf), fmt), params); + va_end(params); + return 128; +} + #undef error_errno int error_errno(const char *fmt, ...) { From patchwork Mon Dec 6 16:55:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12658941 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3B594C43219 for ; Mon, 6 Dec 2021 17:04:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242007AbhLFRHo (ORCPT ); Mon, 6 Dec 2021 12:07:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347656AbhLFQ7a (ORCPT ); Mon, 6 Dec 2021 11:59:30 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D869C061746 for ; Mon, 6 Dec 2021 08:56:01 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id d9so23814848wrw.4 for ; Mon, 06 Dec 2021 08:56:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fAMmpkIiZ4ClEaSx+sZfIyoO2JF8Bispgn46oBK3fCY=; b=Q9Bmd2p1oT6W6kHrWnKJMW0u6yxtgTEVDM2t/dyhsit8R02OA+mcRQJrPtNREzZZlV H8TMWOlVVoRBAzMCCsyoy6Cgy+Eu9lUNpvLW1P/7RVuBBJaaZjyZh+qKJEF74GaYlXAL fEn/aXJUJLJ41FjEXL1IUMlj/Sy8ETrAzBgxyltbPcoVYVnLqK3TNWGjM/7e2t4CiehQ I0Rj+Zyx14Ms5g4jXdlDP+t7PjIyxxQGODsXlrTws2aKH+7LpapW4fG1QJTdyDOoyGVf d+pErMm6s239gKz9im6zJgDihIxxOAGyTzqYF/75y9ZVvVoFcPZ+nmisu9nAEoBUpgMu uefg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fAMmpkIiZ4ClEaSx+sZfIyoO2JF8Bispgn46oBK3fCY=; b=vWJ+quXLqt5XNdf73h3bWQIfbKG2WG+sjNHP3FGZBwPvMxugwRX7grslKQ0Irdbr14 VZc/X+2OfdmV+MOSti4GKXAYemeuuT4ML7+VZsqx2M7CPCS6uwT5WhW+ETYp11Y3RyKH k3a2TcyjoCXtZxzg50dqH9iJT/7YXCUon9s43+7is9b+cbRd5pvL572xuooXSTAkFqku LaOEmD3sn1eGR+3dN5iBrvLQqm4WM9tA8ktdaGfY8Ik1+KXl9+2UmXyOyyrmlMBPfc4s Ws4nmCbQMUixeWiTAJmz9cjReh2ycMw/5u4UJBvMW5SXjsyW+5xPsIF7uuyvyFtBHXpw mrmQ== X-Gm-Message-State: AOAM530uM+bUnjMSK2O/ExHPwNWcScMA9eDsx2RAtaquh9SSzxnfTEKO Qdo3KD3cWEp8iFLwj3Q+xr3kE/rNFROX8A== X-Google-Smtp-Source: ABdhPJwbJiDV9W51NP9pCdOWSCBFInajrsIMAyL051mf8LwFs6kZyC48wQWRwY74scqzs1N7OUsZxQ== X-Received: by 2002:a5d:5303:: with SMTP id e3mr44302055wrv.73.1638809759823; Mon, 06 Dec 2021 08:55:59 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id j40sm11775419wms.16.2021.12.06.08.55.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Dec 2021 08:55:58 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Taylor Blau , Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 4/4] config API: don't use vreportf(), make it static in usage.c Date: Mon, 6 Dec 2021 17:55:53 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.898.g5a552c2e5f0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In preceding commits the rest of the vreportf() users outside of usage.c have been migrated to die_message(), leaving only the git_die_config() function added in 5a80e97c827 (config: add `git_die_config()` to the config-set API, 2014-08-07). Let's have its callers call error() themselves if they want to emit a message, which is exactly what git_die_config() was doing for them before emitting its own die() message. This means that we can make the vreportf() in usage.c "static", and only expose functions such as usage(), die(), warning() etc. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/fast-import.c | 7 ++++--- builtin/notes.c | 6 ++++-- config.c | 22 +++++++++------------- config.h | 10 ++++++---- git-compat-util.h | 1 - imap-send.c | 3 ++- usage.c | 2 +- 7 files changed, 26 insertions(+), 25 deletions(-) diff --git a/builtin/fast-import.c b/builtin/fast-import.c index 2b2e28bad79..4e2432bb491 100644 --- a/builtin/fast-import.c +++ b/builtin/fast-import.c @@ -3456,9 +3456,10 @@ static void git_pack_config(void) } if (!git_config_get_int("pack.indexversion", &indexversion_value)) { pack_idx_opts.version = indexversion_value; - if (pack_idx_opts.version > 2) - git_die_config("pack.indexversion", - "bad pack.indexversion=%"PRIu32, pack_idx_opts.version); + if (pack_idx_opts.version > 2) { + error("bad pack.indexversion=%"PRIu32, pack_idx_opts.version); + git_die_config("pack.indexversion"); + } } if (!git_config_get_ulong("pack.packsizelimit", &packsizelimit_value)) max_packsize = packsizelimit_value; diff --git a/builtin/notes.c b/builtin/notes.c index 2812d1eac40..60c5dab4122 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -763,8 +763,10 @@ static int git_config_get_notes_strategy(const char *key, if (git_config_get_string(key, &value)) return 1; - if (parse_notes_merge_strategy(value, strategy)) - git_die_config(key, _("unknown notes merge strategy %s"), value); + if (parse_notes_merge_strategy(value, strategy)) { + error(_("unknown notes merge strategy %s"), value); + git_die_config(key); + } free(value); return 0; diff --git a/config.c b/config.c index c5873f3a706..30f7971e0cc 100644 --- a/config.c +++ b/config.c @@ -2323,7 +2323,7 @@ int repo_config_get_string(struct repository *repo, git_config_check_init(repo); ret = git_configset_get_string(repo->config, key, dest); if (ret < 0) - git_die_config(key, NULL); + git_die_config(key); return ret; } @@ -2334,7 +2334,7 @@ int repo_config_get_string_tmp(struct repository *repo, git_config_check_init(repo); ret = git_configset_get_string_tmp(repo->config, key, dest); if (ret < 0) - git_die_config(key, NULL); + git_die_config(key); return ret; } @@ -2380,7 +2380,7 @@ int repo_config_get_pathname(struct repository *repo, git_config_check_init(repo); ret = git_configset_get_pathname(repo->config, key, dest); if (ret < 0) - git_die_config(key, NULL); + git_die_config(key); return ret; } @@ -2452,8 +2452,10 @@ int git_config_get_expiry(const char *key, const char **output) return ret; if (strcmp(*output, "now")) { timestamp_t now = approxidate("now"); - if (approxidate(*output) >= now) - git_die_config(key, _("Invalid %s: '%s'"), key, *output); + if (approxidate(*output) >= now) { + error(_("Invalid %s: '%s'"), key, *output); + git_die_config(key); + } } return ret; } @@ -2550,18 +2552,12 @@ void git_die_config_linenr(const char *key, const char *filename, int linenr) key, filename, linenr); } -NORETURN __attribute__((format(printf, 2, 3))) -void git_die_config(const char *key, const char *err, ...) +NORETURN +void git_die_config(const char *key) { const struct string_list *values; struct key_value_info *kv_info; - if (err) { - va_list params; - va_start(params, err); - vreportf("error: ", err, params); - va_end(params); - } values = git_config_get_value_multi(key); kv_info = values->items[values->nr - 1].util; git_die_config_linenr(key, kv_info->filename, kv_info->linenr); diff --git a/config.h b/config.h index f119de01309..fae585d2005 100644 --- a/config.h +++ b/config.h @@ -626,11 +626,13 @@ struct key_value_info { }; /** - * First prints the error message specified by the caller in `err` and then - * dies printing the line number and the file name of the highest priority - * value for the configuration variable `key`. + * Dies printing the line number and the file name of the highest + * priority value for the configuration variable `key`. + * + * Consider calling error() first with a more specific formatted + * message of your own. */ -NORETURN void git_die_config(const char *key, const char *err, ...) __attribute__((format(printf, 2, 3))); +NORETURN void git_die_config(const char *key); /** * Helper function which formats the die error message according to the diff --git a/git-compat-util.h b/git-compat-util.h index c6c6f7d6b51..bdb3977b9ec 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -474,7 +474,6 @@ static inline int git_has_dir_sep(const char *path) struct strbuf; /* General helper functions */ -void vreportf(const char *prefix, const char *err, va_list params); NORETURN void usage(const char *err); NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2))); NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); diff --git a/imap-send.c b/imap-send.c index e6090a0346a..0fdfe5159eb 100644 --- a/imap-send.c +++ b/imap-send.c @@ -1350,7 +1350,8 @@ static int git_imap_config(const char *var, const char *val, void *cb) server.port = git_config_int(var, val); else if (!strcmp("imap.host", var)) { if (!val) { - git_die_config("imap.host", "Missing value for 'imap.host'"); + error("Missing value for 'imap.host'"); + git_die_config("imap.host"); } else { if (starts_with(val, "imap:")) val += 5; diff --git a/usage.c b/usage.c index 3d09e8eea48..9943dd8742e 100644 --- a/usage.c +++ b/usage.c @@ -6,7 +6,7 @@ #include "git-compat-util.h" #include "cache.h" -void vreportf(const char *prefix, const char *err, va_list params) +static void vreportf(const char *prefix, const char *err, va_list params) { char msg[4096]; char *p, *pend = msg + sizeof(msg);