From patchwork Fri Sep 10 15:38:31 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: 12485307 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91BCBC433EF for ; Fri, 10 Sep 2021 15:38:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 79A4261074 for ; Fri, 10 Sep 2021 15:38:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234472AbhIJPj6 (ORCPT ); Fri, 10 Sep 2021 11:39:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234460AbhIJPj5 (ORCPT ); Fri, 10 Sep 2021 11:39:57 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B020C061574 for ; Fri, 10 Sep 2021 08:38:46 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id b6so3198393wrh.10 for ; Fri, 10 Sep 2021 08:38:46 -0700 (PDT) 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=l6WkR4S5ppbcK0cjmDboEU+VUIfjy1zwze+1m0pjX0o=; b=IiSSf63Hn2zYpUihTyuCygokT7+j80qREXJYGYlHaCbW+ESoRNOjJ/YJcQH3Yhc24T ceOIA4uRzA9gZV0JB3tsy1oA7XCkwUF9A5oJPBRNOkF2CeSs7/aOOMmtJr8ii3LPHMj6 4fz19zUUg3N5btU0ym9qCkGQBFqFBm7g8hjAP+c9Juyf6ujjjp9PDYAFrZBZ2g+bpG1l +Ov1spKg/pJjXSq6yWGeR3eRjLRKjAJ0WF/fzEsl6EPOr/GFailXJ0r3zldRbu54LTQn IYMHSfmpbDD+vOCRjTUIXgaSfL5NH4+Xd+PQ/0kJb/7vsBZFsTaOjxykVI0QVuEn2XGJ /NEA== 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=l6WkR4S5ppbcK0cjmDboEU+VUIfjy1zwze+1m0pjX0o=; b=D0PvNCgoQeBRn/TsK7s8ZD2uLqTsmv3loQgCi/TFbEcYx1DQgJw+mGWDcHD6sqGaPg l+e+HiWRBRZwc3HoROawWIl/gc+WRPSJF1ligfcNoJW2F4tcWNrGnQtCj5OnqZLaUoq3 eU5vlNutzbCNVZ4YFsSKHUr3VKpNsDJ0DhKVrVLOJa1LoBB8UOZe2NyRpD7niMmYhpi9 m6XC+iWsvRnrM6AxZ+ShzRpgSrl1Tao6z2eH34iSDDIE5TiUee/KXxiKr0Jt9WZ4oLx+ /EEEWMxF7BhSGax0hFKDk3avjKLfmiLdHiPI60eA5lxl3qjzzjeJAMji3KFiEwXqF/4C dElA== X-Gm-Message-State: AOAM530q6yCsYs2oTu49feMKImk0Tqzc+OC9UY8lvq6LLocD39HQDFK+ yQqiifj53kpiY2FhYoUXr+FP2K5zsdNFRA== X-Google-Smtp-Source: ABdhPJxjp6qBS22+rtBITjEQ3EpjKaM9dHuZ0xTzdWVCUSqYsjL87jjM0+akfsqVV8Hu8SNsA80XGw== X-Received: by 2002:a05:6000:1b02:: with SMTP id f2mr9211595wrz.218.1631288324970; Fri, 10 Sep 2021 08:38:44 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id i5sm4181972wmq.17.2021.09.10.08.38.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 08:38:44 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Carlo Arenas , Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 1/6] test-lib.sh: add a UNIX_SOCKETS prerequisite Date: Fri, 10 Sep 2021 17:38:31 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.876.g423ac861752 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Instead of checking $NO_UNIX_SOCKETS directly in t/t0301-credential-cache.sh, follow the more common pattern of creating a test prerequisite in test-lib.sh. See 6320358e31d (Makefile: unix sockets may not available on some platforms, 2011-12-12) for the original implementation of NO_UNIX_SOCKETS. Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t0301-credential-cache.sh | 5 +++-- t/test-lib.sh | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/t/t0301-credential-cache.sh b/t/t0301-credential-cache.sh index ebd5fa5249c..002de427984 100755 --- a/t/t0301-credential-cache.sh +++ b/t/t0301-credential-cache.sh @@ -4,10 +4,11 @@ test_description='credential-cache tests' . ./test-lib.sh . "$TEST_DIRECTORY"/lib-credential.sh -test -z "$NO_UNIX_SOCKETS" || { +if ! test_have_prereq UNIX_SOCKETS +then skip_all='skipping credential-cache tests, unix sockets not available' test_done -} +fi # don't leave a stale daemon running test_atexit 'git credential-cache exit' diff --git a/t/test-lib.sh b/t/test-lib.sh index abcfbed6d61..583f266b1e8 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -1533,6 +1533,7 @@ test -z "$NO_PYTHON" && test_set_prereq PYTHON test -n "$USE_LIBPCRE2" && test_set_prereq PCRE test -n "$USE_LIBPCRE2" && test_set_prereq LIBPCRE2 test -z "$NO_GETTEXT" && test_set_prereq GETTEXT +test -z "$NO_UNIX_SOCKETS" && test_set_prereq UNIX_SOCKETS if test -z "$GIT_TEST_CHECK_CACHE_TREE" then From patchwork Fri Sep 10 15:38:32 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: 12485309 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 780FEC433FE for ; Fri, 10 Sep 2021 15:38:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 60193611EE for ; Fri, 10 Sep 2021 15:38:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234492AbhIJPj7 (ORCPT ); Fri, 10 Sep 2021 11:39:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234460AbhIJPj6 (ORCPT ); Fri, 10 Sep 2021 11:39:58 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BED3C061574 for ; Fri, 10 Sep 2021 08:38:47 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id n7-20020a05600c3b8700b002f8ca941d89so1683313wms.2 for ; Fri, 10 Sep 2021 08:38:47 -0700 (PDT) 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=kDhwe1YX0ebevQCnZ3yLUGdX8s1+4DzEfGwM7imyfLQ=; b=EJC2js8SVRRKusgHbKrjC6q7ssLIqcr+LHtsSjGTrFDGo0lo512k1Cp3gPCjmhzfpR iqBX3BlWU2KxEwAAvMX1Q/C9xkjbmut3AkuofsWEa9l18VLTxESb8NKPlDXHEVrVi+Tz xHO5QAC/dj0CR+ToHBN7TSl8k35b0wjbf5S4uA4aUigwYtEluGxiGLN7ItfxZixDwuQF x1rsWTuuL5lSrBxW5kyb5seSJ7xYVgTc7uSgBtUPGH2c9ksLNT9lK3KSt33tams8IJho ZJ6RQtJAxemp4OSrIN7QKN5zxu7F1zOcoY3Det/gtorQOJYirTSHzDmM452qJCX+qYex H06A== 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=kDhwe1YX0ebevQCnZ3yLUGdX8s1+4DzEfGwM7imyfLQ=; b=3WxYRqctOWWM6j0uvKWCBxdWXXMtVCobmbTmlOUXhYm+ELN2qjR9pK9F78EwRbcge+ A7un7UA7MnwNcEiZ65Ly3uij+Al8ZkD0N3ip9jntG9J90UQixcU/Un+Wh9MA+tI+zjq4 jxxYPM/hc0JZQY27vjK/48aUnwrlaPw4QJe1q6V/OoV8MnF7fngY5d/oY4sGKxiCbyhW x8HDkrei7FI8Pc6BbbvHw+orLxc6cGWO3c3imcuC1ChgFIKnEcRE2Giza2VSV05NgFqP nsGXsQwqvr5qwp4/ViJ0Ot/d0uK8w7U50Ebb9xyGA8WNypnw5UwNgVVplqverKTLCsix Kk1A== X-Gm-Message-State: AOAM5336YheVq2NIaWzHlvkdyM2Gh3bqhWAHaAwPQ7uLav9Xw/0FkD0H bV1F3EtS/0UkDvYbtgAoR7zMKBvJVJ87Aw== X-Google-Smtp-Source: ABdhPJy/XkcizityChwHJtSuX+93YwKw1Is9ypj4gbopEqvpJoi8mXKe6DRsNBUl1NSyZdaC+0aqKw== X-Received: by 2002:a05:600c:298:: with SMTP id 24mr8894902wmk.172.1631288325869; Fri, 10 Sep 2021 08:38:45 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id i5sm4181972wmq.17.2021.09.10.08.38.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 08:38:45 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Carlo Arenas , Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 2/6] git.c: add a NEED_UNIX_SOCKETS option for built-ins Date: Fri, 10 Sep 2021 17:38:32 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.876.g423ac861752 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the implementation of b5dd96b70ac (make credential helpers builtins, 2020-08-13) to declare in the "struct cmd_struct" that NO_UNIX_SOCKETS can't be set. This is one of two in-tree users for the empty lines in parse_options() usage, getting rid of that is the main motivation for this, but it also doesn't make sense to emit these sorts of usage messages just to appease t0012-help.sh, which seemingly b5dd96b70ac aimed to do. I.e. these commands don't support "[options]", or "" so emitting that at the beginning is incorrect. We should just die right away. The existing code also had the edge case of not emitting the die() message if a "-h" argument was given, since parse_options() will handle the exit() itself in that case. We could feed it PARSE_OPT_NO_INTERNAL_HELP, but this is better. By moving this up to the "struct cmd_struct" we can also skip these in --list-cmds=builtins instead, as noted above we shouldn't be exiting with code 129 in these cases. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin.h | 6 ++++++ builtin/credential-cache--daemon.c | 11 +---------- builtin/credential-cache.c | 11 +---------- git.c | 15 ++++++++++++--- t/t0012-help.sh | 10 ++++++++++ 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/builtin.h b/builtin.h index 16ecd5586f0..66713da6a02 100644 --- a/builtin.h +++ b/builtin.h @@ -63,6 +63,12 @@ * more informed decision, e.g., by ignoring `pager.` for * certain subcommands. * + * `NEED_UNIX_SOCKETS`: + * + * This built-in will not work if NO_UNIX_SOCKETS is defined. It + * will be recognized for emitting error messages, but won't be + * listed in --list-cmds=builtins. + * * . Add `builtin/foo.o` to `BUILTIN_OBJS` in `Makefile`. * * Additionally, if `foo` is a new command, there are 4 more things to do: diff --git a/builtin/credential-cache--daemon.c b/builtin/credential-cache--daemon.c index 4c6c89ab0de..d9863287a4d 100644 --- a/builtin/credential-cache--daemon.c +++ b/builtin/credential-cache--daemon.c @@ -304,16 +304,7 @@ int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix) int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix) { - const char * const usage[] = { - "git credential-cache--daemon [options] ", - "", - "credential-cache--daemon is disabled in this build of Git", - NULL - }; - struct option options[] = { OPT_END() }; - - argc = parse_options(argc, argv, prefix, options, usage, 0); - die(_("credential-cache--daemon unavailable; no unix socket support")); + BUG("should not be called under NO_UNIX_SOCKETS"); } #endif /* NO_UNIX_SOCKET */ diff --git a/builtin/credential-cache.c b/builtin/credential-cache.c index e8a74157471..22b49b265bf 100644 --- a/builtin/credential-cache.c +++ b/builtin/credential-cache.c @@ -142,16 +142,7 @@ int cmd_credential_cache(int argc, const char **argv, const char *prefix) int cmd_credential_cache(int argc, const char **argv, const char *prefix) { - const char * const usage[] = { - "git credential-cache [options] ", - "", - "credential-cache is disabled in this build of Git", - NULL - }; - struct option options[] = { OPT_END() }; - - argc = parse_options(argc, argv, prefix, options, usage, 0); - die(_("credential-cache unavailable; no unix socket support")); + BUG("should not be called under NO_UNIX_SOCKETS"); } #endif /* NO_UNIX_SOCKETS */ diff --git a/git.c b/git.c index 18bed9a9964..6b0248841db 100644 --- a/git.c +++ b/git.c @@ -17,6 +17,7 @@ #define SUPPORT_SUPER_PREFIX (1<<4) #define DELAY_PAGER_CONFIG (1<<5) #define NO_PARSEOPT (1<<6) /* parse-options is not used */ +#define NEED_UNIX_SOCKETS (1<<7) /* Works unless -DNO_UNIX_SOCKETS */ struct cmd_struct { const char *cmd; @@ -66,6 +67,10 @@ static int list_cmds(const char *spec) struct string_list list = STRING_LIST_INIT_DUP; int i; int nongit; + unsigned int exclude_option = 0; +#ifdef NO_UNIX_SOCKETS + exclude_option |= NEED_UNIX_SOCKETS; +#endif /* * Set up the repository so we can pick up any repo-level config (like @@ -78,7 +83,7 @@ static int list_cmds(const char *spec) int len = sep - spec; if (match_token(spec, len, "builtins")) - list_builtins(&list, 0); + list_builtins(&list, exclude_option); else if (match_token(spec, len, "main")) list_all_main_cmds(&list); else if (match_token(spec, len, "others")) @@ -423,6 +428,10 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv) const char *prefix; prefix = NULL; +#ifdef NO_UNIX_SOCKETS + if (p->option & NEED_UNIX_SOCKETS) + die(_("%s is unavailable; there is no UNIX socket support in this build of Git"), p->cmd); +#endif help = argc == 2 && !strcmp(argv[1], "-h"); if (!help) { if (p->option & RUN_SETUP) @@ -513,8 +522,8 @@ static struct cmd_struct commands[] = { { "config", cmd_config, RUN_SETUP_GENTLY | DELAY_PAGER_CONFIG }, { "count-objects", cmd_count_objects, RUN_SETUP }, { "credential", cmd_credential, RUN_SETUP_GENTLY | NO_PARSEOPT }, - { "credential-cache", cmd_credential_cache }, - { "credential-cache--daemon", cmd_credential_cache_daemon }, + { "credential-cache", cmd_credential_cache, NEED_UNIX_SOCKETS }, + { "credential-cache--daemon", cmd_credential_cache_daemon, NEED_UNIX_SOCKETS }, { "credential-store", cmd_credential_store }, { "describe", cmd_describe, RUN_SETUP }, { "diff", cmd_diff, NO_PARSEOPT }, diff --git a/t/t0012-help.sh b/t/t0012-help.sh index 5679e29c624..2d05dde4b90 100755 --- a/t/t0012-help.sh +++ b/t/t0012-help.sh @@ -85,4 +85,14 @@ do ' done X-Patchwork-Id: 12485311 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6BB2EC433EF for ; Fri, 10 Sep 2021 15:38:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5105360295 for ; Fri, 10 Sep 2021 15:38:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234520AbhIJPkC (ORCPT ); Fri, 10 Sep 2021 11:40:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234460AbhIJPkA (ORCPT ); Fri, 10 Sep 2021 11:40:00 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFB2BC061574 for ; Fri, 10 Sep 2021 08:38:48 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id s24so1513803wmh.4 for ; Fri, 10 Sep 2021 08:38:48 -0700 (PDT) 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=DqS7usYgHsZcfanoSrDwIY0ZZqrgvd2N/Aa7zSOkCZE=; b=KG5fn8JrtSR2bjhVMd2z1eSXv/JsVCxlZiNqlnPN1Nhl5w7nufxW8j9rb/cuhAXu3G KA/MoRE8Rna3trN5PLoVIxBoJ2ZRUR/hN1ysLyrvzUGJtVE7yhnJu6kTwk6cZobh1512 pDqgTB2oY+aUvA2N8owAhbS766gqOlQOrF5tlB9MyKx3tsuwzOAiLQVUUAXiErTt2gsp loXGMXUiq1U+dHzSV7ZRLhx/fNw/ES+nSh+DGXrudAJXn9fgqVfz1jk9wDdd2C/JW4gC HxQavPRpEWELTWB6MEJ8eKRRc4VJeoWrhIrLYD4TEpxD9yiy8kNz6lHtHLqYSf86tquJ uzkg== 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=DqS7usYgHsZcfanoSrDwIY0ZZqrgvd2N/Aa7zSOkCZE=; b=3/wsKsJnI50tDoKysIedwELbqQ7VcwGXRFdRNP2amwXZu3bxLm6i6YIYRMEHB4Co95 xTxzjJlWjLTLPJRZ2tx2PQeS8IMMIj9KnUuemkgNEO18xphffPRM439/QPjCtlG9tPuw 2AR2GBcr4s6TLWEixlHmwHd2EpWsxqHScqGbUBDsLKZY6ZGJeeLHiufw0VyVTVpWE5Aa RU+v9wrhP9Qbc+1T1GrglBoqVGubvYbJA2paTB7ezG/iBhxzbepZmg5Gw06xD7mPG+Co REnnADRCZAW5GOxkSW61pzzT13WsFRAlA1ZMHxg4NOaPLgEQ7NsT4GCiEu5mTHPTRave Ch3Q== X-Gm-Message-State: AOAM532IKqzun4EfUNbFIF7DkMPFW4ZCkiWoaa2Qk+PjMKq18HjOftt3 XSMly98sx0weFlYyFXZ2fQG6h1xfA0sooA== X-Google-Smtp-Source: ABdhPJyfzSYpEJ3VTSnQc5u5BIFJkPyZo76n1QKna/eiyXDDM4dvGglqvdbSB+Ii1ZLCy81LYWy/YA== X-Received: by 2002:a05:600c:21c8:: with SMTP id x8mr8883039wmj.163.1631288327122; Fri, 10 Sep 2021 08:38:47 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id i5sm4181972wmq.17.2021.09.10.08.38.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 08:38:46 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Carlo Arenas , Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 3/6] parse-options: stop supporting "" in the usagestr array Date: Fri, 10 Sep 2021 17:38:33 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.876.g423ac861752 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The strings in the the "usagestr" array have special handling for the empty string dating back to f389c808b67 (Rework make_usage to print the usage message immediately, 2007-10-14). We'll prefix all strings after the first one with "or: ". Then if we encountered a "" we'll emit all strings after that point verbatim without any "or: " prefixing. This gets rid of that special case, which was added in f389c808b67 (Rework make_usage to print the usage message immediately, 2007-10-14). It was only used "blame" (the "credential-cache*" use of it was removed in the preceding commit). Before this change we'd emit: $ git blame -h usage: git blame [] [] [] [--] are documented in git-rev-list(1) This changes that output to simply use "[]" instead of "[]". This accomplishes the same, is more consistent as "git bundle" and "git blame" use the same way of referring to these options now. The use of this in "blame" dated back to 5817da01434 (git-blame: migrate to incremental parse-option [1/2], 2008-07-08), and the use in "bundle" to 2e0afafebd8 (Add git-bundle: move objects and references by archive, 2007-02-22). Once we get rid of this special case we can also use usage_msg_opt() to emit the error message we'd get on an invalid "-L " argument, which means we can get rid of the old-style "blame_usage" variable. It's possible that this change introduce breakage somewhere. We'd only catch these cases at runtime, and the "git rev-parse --parseopt" command is used by shellscripts, see bac199b7b17 (Update git-sh-setup(1) to allow transparent use of git-rev-parse --parseopt, 2007-11-04). I've grepped the codebase for "OPTIONS_SPEC", "char.*\*.*usage\[\]" etc. I'm fairly sure there no outstanding users of this functionality. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/blame.c | 9 +++------ builtin/rev-parse.c | 3 +++ parse-options.c | 8 +------- t/helper/test-parse-options.c | 2 -- t/t0040-parse-options.sh | 2 -- t/t1502-rev-parse-parseopt.sh | 34 ++++++++++++++++++---------------- 6 files changed, 25 insertions(+), 33 deletions(-) diff --git a/builtin/blame.c b/builtin/blame.c index 641523ff9af..45d9873a999 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -29,12 +29,8 @@ #include "refs.h" #include "tag.h" -static char blame_usage[] = N_("git blame [] [] [] [--] "); - static const char *blame_opt_usage[] = { - blame_usage, - "", - N_(" are documented in git-rev-list(1)"), + N_("git blame [] [] [] [--] "), NULL }; @@ -1107,7 +1103,8 @@ int cmd_blame(int argc, const char **argv, const char *prefix) nth_line_cb, &sb, lno, anchor, &bottom, &top, sb.path, the_repository->index)) - usage(blame_usage); + usage_msg_opt(_("Invalid -L parameters"), + blame_opt_usage, options); if ((!lno && (top || bottom)) || lno < bottom) die(Q_("file %s has only %lu line", "file %s has only %lu lines", diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index 22c4e1a4ff0..aeebfd52805 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -436,7 +436,10 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) for (;;) { if (strbuf_getline(&sb, stdin) == EOF) die(_("premature end of input")); + if (!sb.len) + die(_("empty lines are not permitted before the `--' separator")); ALLOC_GROW(usage, unb + 1, usz); + if (!strcmp("--", sb.buf)) { if (unb < 1) die(_("no usage string given before the `--' separator")); diff --git a/parse-options.c b/parse-options.c index 2abff136a17..950a8279beb 100644 --- a/parse-options.c +++ b/parse-options.c @@ -924,18 +924,12 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx, fprintf(outfile, "cat <<\\EOF\n"); fprintf_ln(outfile, _("usage: %s"), _(*usagestr++)); - while (*usagestr && **usagestr) + while (*usagestr) { /* * TRANSLATORS: the colon here should align with the * one in "usage: %s" translation. */ fprintf_ln(outfile, _(" or: %s"), _(*usagestr++)); - while (*usagestr) { - if (**usagestr) - fprintf_ln(outfile, _(" %s"), _(*usagestr)); - else - fputc('\n', outfile); - usagestr++; } need_newline = 1; diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c index 2051ce57db7..e00aef073b0 100644 --- a/t/helper/test-parse-options.c +++ b/t/helper/test-parse-options.c @@ -102,8 +102,6 @@ int cmd__parse_options(int argc, const char **argv) const char *prefix = "prefix/"; const char *usage[] = { "test-tool parse-options ", - "", - "A helper function for the parse-options API.", NULL }; struct string_list expect = STRING_LIST_INIT_NODUP; diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh index ad4746d899a..2910874ece5 100755 --- a/t/t0040-parse-options.sh +++ b/t/t0040-parse-options.sh @@ -10,8 +10,6 @@ test_description='our own option parser' cat >expect <<\EOF usage: test-tool parse-options - A helper function for the parse-options API. - --yes get a boolean -D, --no-doubt begins with 'no-' -B, --no-fear be brave diff --git a/t/t1502-rev-parse-parseopt.sh b/t/t1502-rev-parse-parseopt.sh index b29563fc997..6badc650d64 100755 --- a/t/t1502-rev-parse-parseopt.sh +++ b/t/t1502-rev-parse-parseopt.sh @@ -6,8 +6,6 @@ test_description='test git rev-parse --parseopt' test_expect_success 'setup optionspec' ' sed -e "s/^|//" >optionspec <<\EOF |some-command [options] ... -| -|some-command does foo and bar! |-- |h,help show the help | @@ -41,8 +39,6 @@ EOF test_expect_success 'setup optionspec-no-switches' ' sed -e "s/^|//" >optionspec_no_switches <<\EOF |some-command [options] ... -| -|some-command does foo and bar! |-- EOF ' @@ -50,8 +46,6 @@ EOF test_expect_success 'setup optionspec-only-hidden-switches' ' sed -e "s/^|//" >optionspec_only_hidden_switches <<\EOF |some-command [options] ... -| -|some-command does foo and bar! |-- |hidden1* A hidden switch EOF @@ -62,8 +56,6 @@ test_expect_success 'test --parseopt help output' ' |cat <<\EOF |usage: some-command [options] ... | -| some-command does foo and bar! -| | -h, --help show the help | --foo some nifty option --foo | --bar ... some cool option --bar with an argument @@ -103,8 +95,6 @@ test_expect_success 'test --parseopt help output no switches' ' |cat <<\EOF |usage: some-command [options] ... | -| some-command does foo and bar! -| |EOF END_EXPECT test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_no_switches && @@ -116,8 +106,6 @@ test_expect_success 'test --parseopt help output hidden switches' ' |cat <<\EOF |usage: some-command [options] ... | -| some-command does foo and bar! -| |EOF END_EXPECT test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_only_hidden_switches && @@ -129,8 +117,6 @@ test_expect_success 'test --parseopt help-all output hidden switches' ' |cat <<\EOF |usage: some-command [options] ... | -| some-command does foo and bar! -| | --hidden1 A hidden switch | |EOF @@ -144,8 +130,6 @@ test_expect_success 'test --parseopt invalid switch help output' ' |error: unknown option `does-not-exist'\'' |usage: some-command [options] ... | -| some-command does foo and bar! -| | -h, --help show the help | --foo some nifty option --foo | --bar ... some cool option --bar with an argument @@ -282,4 +266,22 @@ test_expect_success 'test --parseopt --stuck-long and short option with unset op test_cmp expect output ' +test_expect_success 'test --parseopt help output hidden switches' ' + sed -e "s/^|//" >optionspec-trailing-line <<-\EOF && + |some-command [options] ... + | + | + |-- + |h,help show the help + EOF + + cat >expect <<-\EOF && + fatal: empty lines are not permitted before the `--'"'"' separator + EOF + + test_must_fail git rev-parse --parseopt -- -h >out < optionspec-trailing-line 2>actual && + test_must_be_empty out && + test_cmp expect actual +' + test_done From patchwork Fri Sep 10 15:38:34 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: 12485313 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4950EC433F5 for ; Fri, 10 Sep 2021 15:38:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 256EB61074 for ; Fri, 10 Sep 2021 15:38:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234536AbhIJPkD (ORCPT ); Fri, 10 Sep 2021 11:40:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234497AbhIJPkB (ORCPT ); Fri, 10 Sep 2021 11:40:01 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E101CC061756 for ; Fri, 10 Sep 2021 08:38:49 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id j17-20020a05600c1c1100b002e754875260so1676599wms.4 for ; Fri, 10 Sep 2021 08:38:49 -0700 (PDT) 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=JExnSC7vwOuoaPaeNKovCm7NpBPsFCbPTGpTf/8Z7tQ=; b=qVOUakxojBTTrWETGRCDeCgVAs4Q4uRirBywxH6kSLG6pAY5Sp4Z3HphUgxC2w0YeX qeQOnqWa6WfiuSKq9TGVFs1IIbq20HC5mo/uqJumDinwOM1GaGwyxExnB/vShzVvcSu7 97f/WAQ0qG9n801c9xZNuONn3e1m+UdYUcyE2SVv8AZgykbBgIsH4xxm8ABfvSJg/TlP VwkTNRBtPRZGN52i4E+RHprSeRhx9o8BUsKjqcBV0beGETDUq+YFcG6gx0nhzaS7AT42 At3ntNj5gRuSJTtmdenDYS8btXXoyt2TpE6p5CnN4PJaEGWnAJsNW2auwkLvDljJtl7p 6wsQ== 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=JExnSC7vwOuoaPaeNKovCm7NpBPsFCbPTGpTf/8Z7tQ=; b=rIv82CFIeRx8CqenpnYCDk6I7qlY1Q09hWt/Q8lSzR5H7z48KhYsVvKBw7+mbdwBpv EU2GR0C2TMpxUCWeeGGvPivnoR83OW/FrMzpgQmgIwL7EsUBGi4LygZ957+6XGtd+svA LnIpnrIjcPCS0T7KEJ3TCF/ZcfTzNmNmrA7odc4djmmlw8Kx1862GiU3B6hmQcZhXvp/ m4Ao+q/bEEur+BeDZGWKR1iMa5qcejqa0FPfSd3WT8JRlfiEc6iQQix3SSzLlYBsOlGD c28kyfnx9awr7Mt/E2u1i0YMoEYAGW3F4EeJDuzGwvyfcoCvOAH7pwuDpO7q67fTLOqV TmBQ== X-Gm-Message-State: AOAM533Cg+STTVak4GwDwuj8HUQQCZSApg5MnnnqaQiJ4QAXShr+83Bp ZmX65HO0Efw9C2WQPkKV/jrFJENWFoQuhw== X-Google-Smtp-Source: ABdhPJxgv4745qgJNYTYl3r8XhNAqteyegxLWrt3/vqfeB3aNfNDYkp193HIQdzlH9S+RwEjuHPozg== X-Received: by 2002:a05:600c:1550:: with SMTP id f16mr9161388wmg.111.1631288328262; Fri, 10 Sep 2021 08:38:48 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id i5sm4181972wmq.17.2021.09.10.08.38.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 08:38:47 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Carlo Arenas , Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 4/6] built-ins: "properly" align continued usage output Date: Fri, 10 Sep 2021 17:38:34 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.876.g423ac861752 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Let's "fix" various "git -h" output by "properly" aligning the output in cases where we continue usage output after a "\n". The "fix" and "properly" scare quotes are because this actually makes things worse in some cases, because e.g. in the case of "git tag -h" the "\t\t" effectively works around how parse-options.c aligns this output. But two wrongs don't make a right, let's "fix" this by making it worse temporarily, in anticipation of improving parse-options.c to handle this alignment. The issue is that we should have whitespace corresponding to the length of the command name here, e.g. in the case of "git ls-remote" it should be 14 characters, or the length of ""git ls-remote ". Instead we had 21 characters in builtin/ls-remote.c, those extra 7 characters are the length of "usage: " (and also " or:"). So in the C locale the resulting output aligns nicely: $ git ls-remote -h usage: git ls-remote [--heads] [--tags] [--refs] [--upload-pack=] [-q | --quiet] [--exit-code] [--get-url] [--symref] [ [...]] But that's fragile, we might not be under the C locale. We really should have parse-options.c itself add this padding. In a subsequent commit I'll make it do that. In the case of "tag" and "show-branch" and "stash save" the output was not properly aligned, although in the "git tag" case it was near-enough (aligned with the "-" in "git tag -l") to look good, assuming C locale & a tab-width of 8. In any case, let's align this in a way that looks obviously correct when looking at the source itself, and then improve parse-options.c itself. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/ls-remote.c | 4 ++-- builtin/show-branch.c | 6 +++--- builtin/stash.c | 2 +- builtin/tag.c | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c index f4fd823af83..318949c3d75 100644 --- a/builtin/ls-remote.c +++ b/builtin/ls-remote.c @@ -7,8 +7,8 @@ static const char * const ls_remote_usage[] = { N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=]\n" - " [-q | --quiet] [--exit-code] [--get-url]\n" - " [--symref] [ [...]]"), + " [-q | --quiet] [--exit-code] [--get-url]\n" + " [--symref] [ [...]]"), NULL }; diff --git a/builtin/show-branch.c b/builtin/show-branch.c index d77ce7aeb38..a82cd1534fc 100644 --- a/builtin/show-branch.c +++ b/builtin/show-branch.c @@ -11,9 +11,9 @@ static const char* show_branch_usage[] = { N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n" - " [--current] [--color[=] | --no-color] [--sparse]\n" - " [--more= | --list | --independent | --merge-base]\n" - " [--no-name | --sha1-name] [--topics] [( | )...]"), + " [--current] [--color[=] | --no-color] [--sparse]\n" + " [--more= | --list | --independent | --merge-base]\n" + " [--no-name | --sha1-name] [--topics] [( | )...]"), N_("git show-branch (-g | --reflog)[=[,]] [--list] []"), NULL }; diff --git a/builtin/stash.c b/builtin/stash.c index 8f42360ca91..45b19007d7c 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -85,7 +85,7 @@ static const char * const git_stash_push_usage[] = { static const char * const git_stash_save_usage[] = { N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" - " [-u|--include-untracked] [-a|--all] []"), + " [-u|--include-untracked] [-a|--all] []"), NULL }; diff --git a/builtin/tag.c b/builtin/tag.c index 065b6bf093e..6535ed27ee9 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -23,10 +23,10 @@ static const char * const git_tag_usage[] = { N_("git tag [-a | -s | -u ] [-f] [-m | -F ]\n" - "\t\t []"), + " []"), N_("git tag -d ..."), N_("git tag -l [-n[]] [--contains ] [--no-contains ] [--points-at ]\n" - "\t\t[--format=] [--merged ] [--no-merged ] [...]"), + " [--format=] [--merged ] [--no-merged ] [...]"), N_("git tag -v [--format=] ..."), NULL }; From patchwork Fri Sep 10 15:38:35 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: 12485315 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57831C433F5 for ; Fri, 10 Sep 2021 15:38:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 34B4E61074 for ; Fri, 10 Sep 2021 15:38:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234579AbhIJPkI (ORCPT ); Fri, 10 Sep 2021 11:40:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234516AbhIJPkC (ORCPT ); Fri, 10 Sep 2021 11:40:02 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE93CC061764 for ; Fri, 10 Sep 2021 08:38:50 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id l18-20020a05600c4f1200b002f8cf606262so1777798wmq.1 for ; Fri, 10 Sep 2021 08:38:50 -0700 (PDT) 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=zvOUNevOhcX3AhGpGhMtQtfRlz4ppjsS5O3y3/LTums=; b=MHJsrDCECvEbaKdzY9BHEQRRmr6nwQHF26ltr7wfuNuiVqmcHbK6aD+EWTUiCBxFWb 7MdkBj0yVJ/1J6zK6eZqF1JqA95wl34ccRqqoN5NBLcneWygfDWFK5cwgrKnUijMd2F6 msW/wjejZSGZvj9lFap9mwo8jGobMcZnjyLSOQTHDm3oPI+4R5MpkqbsgTUc1zXrDdp1 ZhUqYnXkTkaSAVpysuO+7USVaMjRjxptIlhDlBSqyvoY8g2a+LAUB/CWALPVYvzFhuX7 bPxTQN2gnzCsLGjy5oxtUKQDApwnzfNkFqljUEZi+xws2aIBBgMX/pfg6m9cxo2RD3V1 FmHw== 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=zvOUNevOhcX3AhGpGhMtQtfRlz4ppjsS5O3y3/LTums=; b=pPyL50NIkkVJrwWDs8Citp4XpNNLcCYu5cUZlyKyGlFVV297mKxiXVTWnJUXYvnfWs 3ay1NRXrQUfjs/qqDJAoQpUffv3xVBoA2R7aRfExODKQiM0gLAxa5Sebm+Dn23XjknNH HuyPz0K2A5qLk+1XJxh1MTZD4G7g9/iyZ4ucWrBmQaxjWRYW5Z8/ccH7xjUJKnOjwgCH vCXTKeVR62D9Upgf0+RrRtnBNaAqfTj1rp1tJDAeSxQKtr1BGORWb5IT0EDamRNgS7U1 /HPNy29VUeSX6jP+3SDthtLblJn0wkt3/j4EOXT1zYt3nESgFXTce6o2i70y5/B7QUk1 2jAg== X-Gm-Message-State: AOAM530hKE3cpTaXxpSPVgL0z+J8mlDkXV1YYqE9a8GcynXoKXJ8i3vM xYz98escE4LuuuZowmzg+jOinPNcQzlIoA== X-Google-Smtp-Source: ABdhPJyN0vDkvOUWTICIEJGvLjpuRQ20DwzXEWW7I+Z4pwZAd+EiIzxQL8v2Z0rzAgFX2DRKkCJtmg== X-Received: by 2002:a1c:7dc8:: with SMTP id y191mr8927192wmc.6.1631288329112; Fri, 10 Sep 2021 08:38:49 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id i5sm4181972wmq.17.2021.09.10.08.38.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 08:38:48 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Carlo Arenas , Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 5/6] send-pack: properly use parse_options() API for usage string Date: Fri, 10 Sep 2021 17:38:35 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.876.g423ac861752 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When "send-pack" was changed to use the parse_options() API in 068c77a5189 (builtin/send-pack.c: use parse_options API, 2015-08-19) it was made to use one very long line, instead it should split them up with newlines. Furthermore we were including an inline explanation that you couldn't combine "--all" and "", but unlike in the "blame" case this was not preceded by an empty string. Let's instead show that --all and can't be combined in the the usual language of the usage syntax instead. We can make it clear that one of the two options "--foo" and "--bar" is mandatory, but that the two are mutually exclusive by referring to them as "( --foo | --bar )". Signed-off-by: Ævar Arnfjörð Bjarmason --- Documentation/git-send-pack.txt | 4 ++-- builtin/send-pack.c | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Documentation/git-send-pack.txt b/Documentation/git-send-pack.txt index 44fd146b912..be41f119740 100644 --- a/Documentation/git-send-pack.txt +++ b/Documentation/git-send-pack.txt @@ -9,10 +9,10 @@ git-send-pack - Push objects over Git protocol to another repository SYNOPSIS -------- [verse] -'git send-pack' [--all] [--dry-run] [--force] [--receive-pack=] +'git send-pack' [--dry-run] [--force] [--receive-pack=] [--verbose] [--thin] [--atomic] [--[no-]signed|--signed=(true|false|if-asked)] - [:] [...] + [:] (--all | ...) DESCRIPTION ----------- diff --git a/builtin/send-pack.c b/builtin/send-pack.c index 729dea1d255..89321423125 100644 --- a/builtin/send-pack.c +++ b/builtin/send-pack.c @@ -17,10 +17,10 @@ #include "protocol.h" static const char * const send_pack_usage[] = { - N_("git send-pack [--all | --mirror] [--dry-run] [--force] " - "[--receive-pack=] [--verbose] [--thin] [--atomic] " - "[:] [...]\n" - " --all and explicit specification are mutually exclusive."), + N_("git send-pack [--mirror] [--dry-run] [--force]\n" + " [--receive-pack=]\n" + " [--verbose] [--thin] [--atomic]\n" + " [:] (--all | ...)"), NULL, }; From patchwork Fri Sep 10 15:38:36 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: 12485317 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58627C433FE for ; Fri, 10 Sep 2021 15:38:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3BA2660295 for ; Fri, 10 Sep 2021 15:38:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234580AbhIJPkJ (ORCPT ); Fri, 10 Sep 2021 11:40:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234498AbhIJPkD (ORCPT ); Fri, 10 Sep 2021 11:40:03 -0400 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 07E59C061757 for ; Fri, 10 Sep 2021 08:38:52 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id i23so3241711wrb.2 for ; Fri, 10 Sep 2021 08:38:51 -0700 (PDT) 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=+sA+OvfwrX1EtHkxaChQ07bUkL9641HtoWPsFIRHmsE=; b=AASLzy7H2R6JPahbwBLolK54imeGKbBP7l4Nkxg5ZUMuMFCtjiZZeUeCtBdy6vHILW t+S8w/Pxu1KQOPjLIdmWfxE0qJTrI+ctUwYQdn6/Qy/NJiGCez1VZ8kXji38cs7sMXUu bAg8eMQbd70nniagHBDwZhBoObwHOP7YNB6IVWMD2mcXuFL9sImtXeOEmDe/9xInvyUu LpJwHtKIY9ZOq27asGPBc3fNnS5cpu4Cd3QHPvwRsInPx3aVKDdvFIOtmwr09Fhmgw1O fAooDotalhfRPl72PKDkx/xXXPtfnmEtNq6cIVF9n3kCHIMrx5K8CirxgMzDl3NUAPWY +Erw== 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=+sA+OvfwrX1EtHkxaChQ07bUkL9641HtoWPsFIRHmsE=; b=qxX6UYg72tbgdSo18gdkAcxo9sA/ej1IwMjkRhSxQbGhF7H4hLoW0i8IXlJoJJ2Hup 7ICSgbXAByuhAK9h7DPytSVHjqUZr8LRv2uNUWYEFWXvK2vGKAr7mxzPAOwVwyE+CuNz G7hk4T3KUqfovW5K9dxzORChqMRQ+7NmNpfQN122SW4awyWF1hq9Z6qLVXWmWyLnfiuL XTvEcRqBZN1wb3Cmu/paaZjl9NGoNe7TKSH4MsDQcWeYEc14SkaMmN0uFEYvzN5EUM9g aI0tCsBYmErONhPbGDNYoDaoQUa0RwgSb/2I4dcD+zyRmmFMa7vIzc2TIAUs1XWoT/nr pqtQ== X-Gm-Message-State: AOAM531o8T8dpi1WP1OTxAUEnQA87Seu45JeIs41aE4+Ya6FSBozz28f zcBtT63DToTuaJXeQsW+rKG2mn9JAuw5BQ== X-Google-Smtp-Source: ABdhPJxD1RYWi/2M5NniFoI06Bt5w0QZ/ZMCZMf6+oEwNsGmLIRm9v4H1sMmYhWR3h8g5FVKTTJOwg== X-Received: by 2002:adf:e702:: with SMTP id c2mr10631059wrm.397.1631288330272; Fri, 10 Sep 2021 08:38:50 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id i5sm4181972wmq.17.2021.09.10.08.38.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 08:38:49 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Carlo Arenas , Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 6/6] parse-options: properly align continued usage output Date: Fri, 10 Sep 2021 17:38:36 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.876.g423ac861752 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Some commands such as "git stash" emit continued options output with e.g. "git stash -h", because usage_with_options_internal() prefixes with its own whitespace the resulting output wasn't properly aligned. Let's account for the added whitespace, which properly aligns the output. The "git stash" command has usage output with a N_() translation that legitimately stretches across multiple lines; N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" " [-u|--include-untracked] [-a|--all] [-m|--message ]\n" [...] We'd like to have that output aligned with the length of the initial "git stash " output, but since usage_with_options_internal() adds its own whitespace prefixing we fell short, before this change we'd emit: $ git stash -h usage: git stash list [] or: git stash show [] [] [...] or: git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet] [-u|--include-untracked] [-a|--all] [-m|--message ] [...] Now we'll properly emit aligned output. I.e. the last four lines above will instead be (a whitespace-only change to the above): [...] or: git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet] [-u|--include-untracked] [-a|--all] [-m|--message ] [...] We could also go for an approach where we have the caller support no padding of their own, i.e. (same as the first example, except for the padding on the second line): N_("git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" "[-u|--include-untracked] [-a|--all] [-m|--message ]\n" [...] But to do that we'll need to find the length of "git stash". We can discover that from the "cmd" in the "struct cmd_struct", but there might cases with sub-commands or "git" itself taking arguments that would make that non-trivial. Even if it was I still think this approach is better, because this way we'll get the same legible alignment in the C code. The fact that usage_with_options_internal() is adding its own prefix padding is an implementation detail that callers shouldn't need to worry about. Implementation notes: We could skip the string_list_split() with a strchr(str, '\n') check, but we'd then need to duplicate our state machine for strings that do and don't contain a "\n". It's simpler to just always split into a "struct string_list", even though the common case is that that "struct string_list" will contain only one element. This is not performance-sensitive code. This change is relatively more complex since I've accounted for making it future-proof for RTL translation support. Later in usage_with_options_internal() we have some existing padding code dating back to d7a38c54a6c (parse-options: be able to generate usages automatically, 2007-10-15) which isn't RTL-safe, but that code would be easy to fix. Let's not introduce new RTL translation problems here. I'm also adding a check to catch the mistake of needlessly adding a trailing "\n", such as: N_("git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]\n" " [-u|--include-untracked] [-a|--all] []\n"), Or even a mistake like adding just one "\n" in a string with no other newlines: N_("git stash list []\n"), This catches the cases already tested for in cmd_parseopt(), but this covers the purely C API. As noted a preceding commit that added the die() to cmd_parseopt() I'm fairly confident that this will be triggered by no in-tree user I've missed. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 65 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 6 deletions(-) diff --git a/parse-options.c b/parse-options.c index 950a8279beb..ff28869d2c9 100644 --- a/parse-options.c +++ b/parse-options.c @@ -917,19 +917,72 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx, FILE *outfile = err ? stderr : stdout; int need_newline; + const char *usage_prefix = _("usage: %s"); + /* + * The translation could be anything, but we can count on + * msgfmt(1)'s --check option to have asserted that "%s" is in + * the translation. So compute the length of the "usage: " + * part. We are assuming that the translator wasn't overly + * clever and used e.g. "%1$s" instead of "%s", there's only + * one "%s" in "usage_prefix" above, so there's no reason to + * do so even with a RTL language. + */ + size_t usage_len = strlen(usage_prefix) - strlen("%s"); + /* + * TRANSLATORS: the colon here should align with the + * one in "usage: %s" translation. + */ + const char *or_prefix = _(" or: %s"); + + /* + * TRANSLATORS: You should only need to translate this format + * string if your language is a RTL language (e.g. Arabic, + * Hebrew etc.), not if it's a LTR language (e.g. German, + * Russian, Chinese etc.). + * + * When a translated usage string has an embedded "\n" it's + * because options have wrapped to the next line. The line + * after the "\n" will then be padded to align with the + * command name, such as N_("git cmd [opt]\n<8 + * spaces>[opt2]"), where the 8 spaces are the same length as + * "git cmd ". + * + * This format string prints out that already-translated + * line. The "%*s" is whitespace padding to account for the + * padding at the start of the line that we add in this + * function. The "%s" is a line in the (hopefully already + * translated) N_() usage string, which contained embedded + * newlines before we split it up. + */ + const char *usage_continued = _("%*s%s"); + const char *prefix = usage_prefix; + if (!usagestr) return PARSE_OPT_HELP; if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) fprintf(outfile, "cat <<\\EOF\n"); - fprintf_ln(outfile, _("usage: %s"), _(*usagestr++)); while (*usagestr) { - /* - * TRANSLATORS: the colon here should align with the - * one in "usage: %s" translation. - */ - fprintf_ln(outfile, _(" or: %s"), _(*usagestr++)); + struct string_list list = STRING_LIST_INIT_DUP; + unsigned int j; + + string_list_split(&list, _(*usagestr++), '\n', -1); + for (j = 0; j < list.nr; j++) { + const char *line = list.items[j].string; + + if (!*line) + BUG("empty or trailing line in usage string"); + + if (!j) + fprintf_ln(outfile, prefix, line); + else + fprintf_ln(outfile, usage_continued, + (int)usage_len, "", line); + } + string_list_clear(&list, 0); + + prefix = or_prefix; } need_newline = 1;