From patchwork Thu Nov 24 20:22:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Allred X-Patchwork-Id: 13055354 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 B9E1CC43219 for ; Thu, 24 Nov 2022 20:22:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229682AbiKXUW4 (ORCPT ); Thu, 24 Nov 2022 15:22:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229670AbiKXUWx (ORCPT ); Thu, 24 Nov 2022 15:22:53 -0500 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 957875D6B5 for ; Thu, 24 Nov 2022 12:22:52 -0800 (PST) Received: by mail-wm1-x32e.google.com with SMTP id r9-20020a1c4409000000b003d02dd48c45so4363617wma.0 for ; Thu, 24 Nov 2022 12:22:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=eeoPzZNAcaYC1zuFX5PF+xFJXa4vS6XRQyzAYX7eilA=; b=MtqS9/gbzvYLSooRdBRYtveCiTa3HlWGnHI0Y/RrkNHI0gIH2s0KDuSmTxe+cy6g2E Q6QnCTLnKgnguBwUt9o5f+ZUeZL6LdjjBqDVqhHkvXWZ+93bVMNNMrk2VUWwV0quE5iN Px6K0JGFNSmCmsSKHw1YuKnXRSXS5D72tHXq8QbAjMiUZedY4mfUchf+Q9W393cu8pK9 W97xweFFZIpagV0HVqY2XpNmLv6ku6p6hT0SJKstJviB4KhjbJWHTZUM6cLAaZKEejb9 rKvVlGA1Q3b1VRDOlCAZawbtxHM6832trzX64s+toIr0obGzxP/IKMO69BzvoE1WKmIB qevg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eeoPzZNAcaYC1zuFX5PF+xFJXa4vS6XRQyzAYX7eilA=; b=lhPmFcF0qRgAYaZINdIG88by0uFgAmhMy+gznrpeXixyoOiurvDsKEX6ACFryc0Sxt V+E4qPv99X6g1YiAfE9iSCh5+u3SMfDgUXsXvjzyVQPgJQl8YxbVVXLOJd4vQIo5Ybal xc++EbXlBeBJFTdmHccPAP3Dq4H5+XYasaFQHEZbp35dXZcDjzFRW+nHaN5rX3HGC1oO 1mpRWEm9ypgvZPolwYI5vl+ANKew2wRMwQzk4YpCDDkY3qbr2ICIDKJO0/fZHo59q9jk q/QDaqKk8F/LCU2sQpt8LaVDhcD2smUa1TKbR+bDmxSlaRS41b1PKroeDBKr+Xs2vdj4 ZzcA== X-Gm-Message-State: ANoB5plej/WogTh21TMUa29LE48r2Ydgk6UPBUp/bk4vYqAsCq0BlhbI axvPpvwdcwJZqYEc0yiha2s5k0LPKO8= X-Google-Smtp-Source: AA0mqf5ENB7wv1c60kNdzcmZNjb7SYNTLIZJpaHP+N+GV854RwJOucpFKqP7zQSIcecrAafPXsBQYg== X-Received: by 2002:a05:600c:54e4:b0:3c6:d39c:fc4b with SMTP id jb4-20020a05600c54e400b003c6d39cfc4bmr13588619wmb.14.1669321370961; Thu, 24 Nov 2022 12:22:50 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id f2-20020a1c6a02000000b003b4868eb71bsm6531588wmc.25.2022.11.24.12.22.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Nov 2022 12:22:50 -0800 (PST) Message-Id: In-Reply-To: References: Date: Thu, 24 Nov 2022 20:22:46 +0000 Subject: [PATCH 1/3] var: do not print usage() with a correct invocation Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Sean Allred , Sean Allred Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Sean Allred From: Sean Allred Before, git-var could print usage() even if the command was invoked correctly with a variable defined in git_vars -- provided that its read() function returned NULL. Now, we only print usage() only if it was called with a logical variable that wasn't defined -- regardless of read(). Since we now know the variable is valid when we call read_var(), we can avoid printing usage() here (and exiting with code 129) and instead exit quietly with code 1. While exiting with a different code can be a breaking change, it's far better than changing the exit status more generally from 'failure' to 'success'. Signed-off-by: Sean Allred --- Documentation/git-var.txt | 3 ++- builtin/var.c | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Documentation/git-var.txt b/Documentation/git-var.txt index 6aa521fab23..0ab5bfa7d72 100644 --- a/Documentation/git-var.txt +++ b/Documentation/git-var.txt @@ -13,7 +13,8 @@ SYNOPSIS DESCRIPTION ----------- -Prints a Git logical variable. +Prints a Git logical variable. Exits with code 1 if the variable has +no value. OPTIONS ------- diff --git a/builtin/var.c b/builtin/var.c index 491db274292..776f1778ae1 100644 --- a/builtin/var.c +++ b/builtin/var.c @@ -56,6 +56,17 @@ static void list_vars(void) printf("%s=%s\n", ptr->name, val); } +static const struct git_var *get_git_var(const char *var) +{ + struct git_var *ptr; + for (ptr = git_vars; ptr->read; ptr++) { + if (strcmp(var, ptr->name) == 0) { + return ptr; + } + } + return NULL; +} + static const char *read_var(const char *var) { struct git_var *ptr; @@ -81,6 +92,7 @@ static int show_config(const char *var, const char *value, void *cb) int cmd_var(int argc, const char **argv, const char *prefix) { + const struct git_var *git_var = NULL; const char *val = NULL; if (argc != 2) usage(var_usage); @@ -91,9 +103,14 @@ int cmd_var(int argc, const char **argv, const char *prefix) return 0; } git_config(git_default_config, NULL); + + git_var = get_git_var(argv[1]); + if (!git_var) + usage(var_usage); + val = read_var(argv[1]); if (!val) - usage(var_usage); + return 1; printf("%s\n", val); From patchwork Thu Nov 24 20:22:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Allred X-Patchwork-Id: 13055355 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 1EAB1C433FE for ; Thu, 24 Nov 2022 20:23:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229688AbiKXUW7 (ORCPT ); Thu, 24 Nov 2022 15:22:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229680AbiKXUWz (ORCPT ); Thu, 24 Nov 2022 15:22:55 -0500 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89F4D5DBB6 for ; Thu, 24 Nov 2022 12:22:53 -0800 (PST) Received: by mail-wm1-x330.google.com with SMTP id 83-20020a1c0256000000b003d03017c6efso4012623wmc.4 for ; Thu, 24 Nov 2022 12:22:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=lFH4VlCTdQoHEmDrDdwcoLZXL+BjE9FIsZo4iptzw2U=; b=qtqpxkVIS6pgFxctfUyGEKJUEDe1XQfiy9vP5u8YBBiQU/njGxrDDRP/3Uimtm5ivI 5Q5r2rbDgSOQxAaR4oGrsF13hy1fcyr5Hew52upL0lkElwh6gQuiqEgTnk5G5N4JtMYD 3wnQYQl3kU5k8cWdMeFl/c8MXnFXrZNxO8e5LtLO+I7oRSHaI2ooPbzeJCImuDDQ2f/o FtCGGJDhSYDiza7mNBptloz8r61MQyfXlHsZAvhtUzlTOnAYz2i8hIwxp7AjUYiZ0lNH ppDBv/lK6r9VVFj5NXhndVSonzp6kDqYAdFihGq1QqTAGHNUZFyBRGRAQiTEeM4OfTfn ecPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lFH4VlCTdQoHEmDrDdwcoLZXL+BjE9FIsZo4iptzw2U=; b=WvY4QTb1D15vX3U4u37buREwGNM2cmt4S+6rf31LpFPXoIDye4d0SndOSP0DzrJvlf IR5q1GcpUMFIzRtLqhjt07teM4zaciIx04nPSBqjIpxFbLHqaQ41jI4ji6DgMsN84H0w tFkc658yO3QFkwX1Ay/ZKyOyggjU+1BhXauhKl/oV+KmWdEE0/8QaEmPB4R3UMGHaaj9 2MuXT4bej44KCaKMsXDTcsxjEQS9M0OiBPozWHtA/NZ+HZsk/opG7GxYvJ23yzV10W7K s9ro4OFHSNz0gdIKhChe5TOh5J7q64RhpNTBHgeiqoyLcKE56pabDAoOM4Qqztwg8x9+ a/vA== X-Gm-Message-State: ANoB5pnoOYsvlvyCZz3ifK/UBtt0/OJQnu2IIJOU+HoyIuRgJkLLA27f 7mdm7IKvQ6LF/R7kARVuC48cugCw/Y4= X-Google-Smtp-Source: AA0mqf70nuE+OwohRuU6Nw3CFRxnI/ksnAECw8wylynXTDi1bR2vc8pSbuQGXPuzhMq8j57aJZ2SPQ== X-Received: by 2002:a05:600c:314f:b0:3cf:9efc:a9b7 with SMTP id h15-20020a05600c314f00b003cf9efca9b7mr25322904wmo.10.1669321371889; Thu, 24 Nov 2022 12:22:51 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id g6-20020adffc86000000b00241c4bd6c09sm2172193wrr.33.2022.11.24.12.22.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Nov 2022 12:22:51 -0800 (PST) Message-Id: <905b109b458e291da04d9879cbc6b032bbd9a302.1669321369.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 24 Nov 2022 20:22:47 +0000 Subject: [PATCH 2/3] var: remove read_var Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Sean Allred , Sean Allred Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Sean Allred From: Sean Allred With our target git_var value now available, we no longer need to call into read_var() to find its read() function again. This does avoid a second loop through git_vars, but mostly it just removes a lot of duplicated logic. Signed-off-by: Sean Allred --- builtin/var.c | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/builtin/var.c b/builtin/var.c index 776f1778ae1..e215cd3b0c0 100644 --- a/builtin/var.c +++ b/builtin/var.c @@ -67,20 +67,6 @@ static const struct git_var *get_git_var(const char *var) return NULL; } -static const char *read_var(const char *var) -{ - struct git_var *ptr; - const char *val; - val = NULL; - for (ptr = git_vars; ptr->read; ptr++) { - if (strcmp(var, ptr->name) == 0) { - val = ptr->read(IDENT_STRICT); - break; - } - } - return val; -} - static int show_config(const char *var, const char *value, void *cb) { if (value) @@ -108,7 +94,7 @@ int cmd_var(int argc, const char **argv, const char *prefix) if (!git_var) usage(var_usage); - val = read_var(argv[1]); + val = git_var->read(IDENT_STRICT); if (!val) return 1; From patchwork Thu Nov 24 20:22:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Allred X-Patchwork-Id: 13055356 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 D6924C43219 for ; Thu, 24 Nov 2022 20:23:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229693AbiKXUXB (ORCPT ); Thu, 24 Nov 2022 15:23:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229681AbiKXUWz (ORCPT ); Thu, 24 Nov 2022 15:22:55 -0500 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C6FD5C0DE for ; Thu, 24 Nov 2022 12:22:54 -0800 (PST) Received: by mail-wm1-x332.google.com with SMTP id m7-20020a05600c090700b003cf8a105d9eso1962029wmp.5 for ; Thu, 24 Nov 2022 12:22:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=+bFiO51UyTE2D3lTycCdsOTBmALDPaRF9AseQ8lhbDI=; b=QE+Q0PQOSJZE6/dk2y9vaMAMPYCNVWV/qEG0x2nwtdCXIJusreDbkippK8J33wE5NF ABzdFhPrfp0GqvPGHzenCWKe8yfYZskrLf8vW8V/VT/maaGQP+AJXewW2Re0G2lFOMI5 k9diaRdUWxmeWaGIkvInl+wYPWW5xFPad9kmZUt6srAeUqRBgGXMBbrtJPvF8O+YTmOs n9T1x4kLIoPovaFhNc3zEQ1Xmt/eE4YhtV0PsqwgkH+yIPcMBdPYqYiTEtHyQhiVDPLk 48UU6xurb8sFiCEf6D4wsEMsDUVbaa7KCG+9xeKll1kD5lPBee45xeTTwAcxogJLq5dd zxwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+bFiO51UyTE2D3lTycCdsOTBmALDPaRF9AseQ8lhbDI=; b=1SXVWisDYN9t7ErNBjduogMJegIQfWihOVJ0csUkXLrr25nLZhK8bT/wWGlz3k+qUd dbPGTcgDCcBZ3j+5AsnWz7GLpD8Nb+wqJbakNcKnH4prIxl5pvRNB/q2S0PAnaJF/eLa 1SKRVyjk4frm5oKTCNqykv9vb6x7HGqhQqgK0Tll7tK+r4ZDfJD6WLSR25J4BXr7t1YR 2yAoFlZ/RlrZ5SNoCFWdJUd6mPqiSCpLsVmGlJHdAyFJ4jYRQNfHTnRq1bs2Ce7vKeoR iwZ1ffF9aogMLLa08kwSm4EPpxWOVgPAu+gl6X6rtVpzC5rk/ggGsHIpWLWOpWjcc7/Y NZ2Q== X-Gm-Message-State: ANoB5pkai4UWlB365NYtOn4NlHZG60HtKlHA5bWyaDlOebKBUwHh8qL2 rDEm6XkZ69VaqEEjwm+0J/yb5YgVGEM= X-Google-Smtp-Source: AA0mqf52PpuKzT+ftv68wUYOxzLZH9g18kyFhMrXGKfdEaYGx3jiCGg6LPgoFbdlXvHuFwqknqAj8g== X-Received: by 2002:a05:600c:4254:b0:3cf:7197:e693 with SMTP id r20-20020a05600c425400b003cf7197e693mr25219371wmm.49.1669321372742; Thu, 24 Nov 2022 12:22:52 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t187-20020a1c46c4000000b003cfa622a18asm6722562wma.3.2022.11.24.12.22.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Nov 2022 12:22:52 -0800 (PST) Message-Id: <8d49a718038c1e7504f512b0d04709b9c2d28df7.1669321369.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 24 Nov 2022 20:22:48 +0000 Subject: [PATCH 3/3] var: allow GIT_EDITOR to return null Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Sean Allred , Sean Allred Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Sean Allred From: Sean Allred The handling to die early when there is no EDITOR is valuable when used in normal code (i.e., editor.c). In git-var, where null/empty-string is a perfectly valid value to return, it doesn't make as much sense. Remove this handling from `git var GIT_EDITOR` so that it does not fail so noisily when there is no defined editor. Signed-off-by: Sean Allred --- builtin/var.c | 7 +---- t/t0007-git-var.sh | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/builtin/var.c b/builtin/var.c index e215cd3b0c0..77e9ef3081a 100644 --- a/builtin/var.c +++ b/builtin/var.c @@ -11,12 +11,7 @@ static const char var_usage[] = "git var (-l | )"; static const char *editor(int flag) { - const char *pgm = git_editor(); - - if (!pgm && flag & IDENT_STRICT) - die("Terminal is dumb, but EDITOR unset"); - - return pgm; + return git_editor(); } static const char *pager(int flag) diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh index e56f4b9ac59..bdef271c92a 100755 --- a/t/t0007-git-var.sh +++ b/t/t0007-git-var.sh @@ -47,6 +47,75 @@ test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' ' ) ' +test_expect_success 'get GIT_EDITOR without configuration' ' + ( + sane_unset GIT_EDITOR && + sane_unset VISUAL && + sane_unset EDITOR && + >expect && + ! git var GIT_EDITOR >actual && + test_cmp expect actual + ) +' + +test_expect_success 'get GIT_EDITOR with configuration' ' + test_config core.editor foo && + ( + sane_unset GIT_EDITOR && + sane_unset VISUAL && + sane_unset EDITOR && + echo foo >expect && + git var GIT_EDITOR >actual && + test_cmp expect actual + ) +' + +test_expect_success 'get GIT_EDITOR with environment variable GIT_EDITOR' ' + ( + sane_unset GIT_EDITOR && + sane_unset VISUAL && + sane_unset EDITOR && + echo bar >expect && + GIT_EDITOR=bar git var GIT_EDITOR >actual && + test_cmp expect actual + ) +' + +test_expect_success 'get GIT_EDITOR with environment variable EDITOR' ' + ( + sane_unset GIT_EDITOR && + sane_unset VISUAL && + sane_unset EDITOR && + echo bar >expect && + EDITOR=bar git var GIT_EDITOR >actual && + test_cmp expect actual + ) +' + +test_expect_success 'get GIT_EDITOR with configuration and environment variable GIT_EDITOR' ' + test_config core.editor foo && + ( + sane_unset GIT_EDITOR && + sane_unset VISUAL && + sane_unset EDITOR && + echo bar >expect && + GIT_EDITOR=bar git var GIT_EDITOR >actual && + test_cmp expect actual + ) +' + +test_expect_success 'get GIT_EDITOR with configuration and environment variable EDITOR' ' + test_config core.editor foo && + ( + sane_unset GIT_EDITOR && + sane_unset VISUAL && + sane_unset EDITOR && + echo foo >expect && + EDITOR=bar git var GIT_EDITOR >actual && + test_cmp expect actual + ) +' + # For git var -l, we check only a representative variable; # testing the whole output would make our test too brittle with # respect to unrelated changes in the test suite's environment.