From patchwork Wed Dec 8 12:34:14 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: 12664331 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 AD6EFC433FE for ; Wed, 8 Dec 2021 12:34:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233502AbhLHMiC (ORCPT ); Wed, 8 Dec 2021 07:38:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233484AbhLHMiB (ORCPT ); Wed, 8 Dec 2021 07:38:01 -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 1BCCAC061746 for ; Wed, 8 Dec 2021 04:34:29 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id d24so3944168wra.0 for ; Wed, 08 Dec 2021 04:34:29 -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=w12Aip9Me96PywRbeocM/QqnLvMZIun7edcr0mVNjgI=; b=DBLjHXT1Fy/hyzgHFXltpk7/mJzqi3cg1YL53Kc7oIzj4OnJv4b1K3u+/wo1sSyjJ/ cDFun4JFe8lFkjfDBJV67P4BrfMab2Mz1lH5NqugMBb3x+EI44jTCpIpzoFwnWwmJZ+p h7/WW0ONX7dw83Az2JwL1k7llAF7cjlkwBOW53XcWCbSH5qSPXnYkq943od+0833NmIQ uVvWDyPacctCW1PolbUsD41FfgoWh1SzXfZmSKzdxYL+2yiCa1a+9INunWWzkfctvEg4 AAzcne9WTd8WMVJFTqH+Ds2+rt6rmbdClg9KtUEk+HvNRxJQ/0izkWN5+k+OEie4k2TL w1Cg== 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=w12Aip9Me96PywRbeocM/QqnLvMZIun7edcr0mVNjgI=; b=fSxGw/AlDWGnoaBuJWvNmV4hSrsDq5JFSLZvBbwgtGLd5Btd5Lce3lM6Ght7ufysGl syKXQlvfq7YXvU0JTNNFGdEmp94ddOX8Fu4vg8ssEMlmrlS3m0l+2+HnbN0QkFNN1mnH Z4oNL3rgZ7/zAx583iTmSdckyd8+8tZAz5gNNWelmj2HEITU5/FDuBvgf0JBbjZ24Klz 4vP62RjmcrXkEQyDxiKgcz0dd2cr+RiU+JoTsZ5rxm3Fr8u6oz68nyvxkvXefGfE+rF6 ihYufLepmm2eB375OsE+CmBSOxa0fId+LMG99eovR5z40A3sXfm6lpDWvc87A2qZmqgi 6JyA== X-Gm-Message-State: AOAM533Aa4tVT5w02C6+sMeRqKXBHWlqNGRZop24COjOk0gwEiB6eXAS TNTJQFJtMtbQ68tGbONVPayImXPGAfwCxA== X-Google-Smtp-Source: ABdhPJxjvAEbKrucvFksvERZCryGKCwyV7tC+ibpxGs/oS12Pez7OfHO5SVmt44eJ7s6GFiURlcPag== X-Received: by 2002:a05:6000:1c2:: with SMTP id t2mr56781191wrx.378.1638966867421; Wed, 08 Dec 2021 04:34:27 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id d8sm2540692wrm.76.2021.12.08.04.34.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 04:34:26 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , John Cai , Sergey Organov , Jiang Xin , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= Subject: [PATCH v4 01/10] cat-file tests: test bad usage Date: Wed, 8 Dec 2021 13:34:14 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.926.g895e15e0c0c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Stress test the usage emitted when options are combined in ways that isn't supported. Let's test various option combinations, some of these we buggily allow right now. E.g. this reveals a bug in 321459439e1 (cat-file: support --textconv/--filters in batch mode, 2016-09-09) that we'll fix in a subsequent commit. We're supposed to be emitting a relevant message when --batch-all-objects is combined with --textconv or --filters, but we don't. The cases of needing to assign to opt=2 in the "opt" loop are because on those we do the right thing already, in subsequent commits the "test_expect_failure" cases will be fixed, and the for-loops unified. Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t1006-cat-file.sh | 94 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index 658628375c8..fc9191c1b94 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -4,6 +4,100 @@ test_description='git cat-file' . ./test-lib.sh +test_cmdmode_usage () { + test_expect_code 129 "$@" 2>err && + grep "^error:.*is incompatible with" err +} + +for switches in \ + '-e -p' \ + '-p -t' \ + '-t -s' \ + '-s --textconv' \ + '--textconv --filters' +do + test_expect_success "usage: cmdmode $switches" ' + test_cmdmode_usage git cat-file $switches + ' +done + +test_incompatible_usage () { + test_expect_code 129 "$@" 2>err && + grep -E "^error:.**needs" err +} + +for opt in --batch --batch-check +do + test_expect_success "usage: incompatible options: --path with $opt" ' + test_incompatible_usage git cat-file --path=foo $opt + ' +done + +short_modes="-e -p -t -s" +cw_modes="--textconv --filters" + +for opt in $cw_modes +do + test_expect_success "usage: $opt requires another option" ' + test_expect_code 129 git cat-file $opt + ' + + test_expect_failure "usage: incompatible options: --batch-all-objects with $opt" ' + test_incompatible_usage git cat-file --batch-all-objects $opt + ' +done + +for opt in $short_modes +do + test_expect_success "usage: $opt requires another option" ' + test_expect_code 129 git cat-file $opt + ' + + for opt2 in --batch \ + --batch-check \ + --follow-symlinks + do + test_expect_failure "usage: incompatible options: $opt and $opt2" ' + test_incompatible_usage git cat-file $opt $opt2 + ' + done + + opt2="--path=foo HEAD:some-path.txt" + test_expect_success "usage: incompatible options: $opt and $opt2" ' + test_incompatible_usage git cat-file $opt $opt2 + ' +done + +for opt in $short_modes $cw_modes +do + args="one two three" + test_expect_success "usage: too many arguments: $opt $args" ' + test_expect_code 129 git cat-file $opt $args + ' + + for opt2 in --buffer --follow-symlinks + do + test_expect_success "usage: incompatible arguments: $opt with batch option $opt2" ' + test_expect_code 129 git cat-file $opt $opt2 + ' + done +done + +for opt in --buffer \ + --follow-symlinks \ + --batch-all-objects +do + status=success + if test $opt = "--buffer" + then + status=failure + fi + test_expect_$status "usage: bad option combination: $opt without batch mode" ' + test_expect_code 129 git cat-file $opt && + test_expect_code 129 git cat-file $opt commit HEAD + ' +done + echo_without_newline () { printf '%s' "$*" } From patchwork Wed Dec 8 12:34:15 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: 12664333 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 5CB87C433EF for ; Wed, 8 Dec 2021 12:34:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233528AbhLHMiD (ORCPT ); Wed, 8 Dec 2021 07:38:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233492AbhLHMiB (ORCPT ); Wed, 8 Dec 2021 07:38:01 -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 DFA4DC061746 for ; Wed, 8 Dec 2021 04:34:29 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id d24so3944219wra.0 for ; Wed, 08 Dec 2021 04:34:29 -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=t78MdcMpvZ8iZvOrkJ9Mgso+dzkAHUY7ErUSqkghxHA=; b=dQhzIZTRhVJoTJBtIuxkvmoM2RuBUr7jTfMkEa7dXbxxRwuDqqQhU6CPbUdevnMBfj Jp4J3K/BBHTbn/WVEheKkL1kk98NjwiJV624qR1RsOQPIxC5dEVlLU27VMigndbSILHN 4eAxQx74z+nILweTQ+LCRXynkpUxHTUypsybgsChwDJMZFKuWmxAR6isgC/9Eoq61R4i IhRvrICX/K4Lji2JSIPxi2bJogdkyX5uNeXuRGriontIaVltEfBHRx4FqrIxMONOffyT MmkmdsOE5lmzSEyLNAhBb91RMLeKBx2ZFO9HhBNucGvk5epVLtk3thu/jLs073aQY3X6 EJjg== 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=t78MdcMpvZ8iZvOrkJ9Mgso+dzkAHUY7ErUSqkghxHA=; b=XnYjYduZJv9KlRn/UYmC90DAUo3P5s913zEnVWMfDrzeUbyA2iNiJO2xEPtuo9Rl8V dQcp50Dpx9P4cAJyr5SSWSvOxYspjxBLh+Cfh2MQqqwnM/txfoH42clYbpRyz1m5iUcs DFW/VqrVWX0cZSzLAvouz2XR3TQ+Inyzsko6PVdcKAxSUXjxlhriQpaf6M3dJJsLtv5q 4etnpv8D++RmoGG2zwsKeDsZFnIlfsJk1Y8Tr+6NLh2WHXXRsKwmeGYuQ3pmEbKwWxRs OOQLGQ7ppbp03gjny7zIpsrowZo9pxrdrD8IGdbRHUhJ+Q7xE+nbNjb3udetb5/CxUCn s8Iw== X-Gm-Message-State: AOAM530gS7daLqaraGveMumeEqjywsQSJv42E6MrChW5fydsjBe2xqwS ZtPSFSsswF3SxD+bDDGRrVQ+1i/mb/zgWQ== X-Google-Smtp-Source: ABdhPJzlLFMEkRkoaSqO0RXYvU8pCmbEKCqsjEQioFGiMrbJzPlagWz6BHCj3w67+x+NgKSci8uWFA== X-Received: by 2002:a5d:4889:: with SMTP id g9mr59645015wrq.455.1638966868283; Wed, 08 Dec 2021 04:34:28 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id d8sm2540692wrm.76.2021.12.08.04.34.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 04:34:27 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , John Cai , Sergey Organov , Jiang Xin , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= Subject: [PATCH v4 02/10] cat-file tests: test messaging on bad objects/paths Date: Wed, 8 Dec 2021 13:34:15 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.926.g895e15e0c0c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add tests for the output that's emitted when we disambiguate : in cat-file. This gives us a baseline for improving these messages. For e.g. "git blame" we'll emit: $ git blame HEAD:foo fatal: no such path 'HEAD:foo' in HEAD But cat-file doesn't disambiguate these two cases, and just gives the rather unhelpful: $ git cat-file --textconv HEAD:foo fatal: Not a valid object name HEAD:foo Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t8007-cat-file-textconv.sh | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/t/t8007-cat-file-textconv.sh b/t/t8007-cat-file-textconv.sh index eacd49ade63..71ea2ac987e 100755 --- a/t/t8007-cat-file-textconv.sh +++ b/t/t8007-cat-file-textconv.sh @@ -19,6 +19,48 @@ test_expect_success 'setup ' ' GIT_AUTHOR_NAME=Number2 git commit -a -m Second --date="2010-01-01 20:00:00" ' +test_expect_success 'usage: ' ' + cat >expect <<-\EOF && + fatal: Not a valid object name HEAD2 + EOF + test_must_fail git cat-file --textconv HEAD2 2>actual && + test_cmp expect actual +' + +test_expect_success 'usage: :' ' + cat >expect <<-\EOF && + fatal: Not a valid object name HEAD2:two.bin + EOF + test_must_fail git cat-file --textconv HEAD2:two.bin 2>actual && + test_cmp expect actual +' + +test_expect_success 'usage: :' ' + cat >expect <<-\EOF && + fatal: Not a valid object name HEAD:two.bin + EOF + test_must_fail git cat-file --textconv HEAD:two.bin 2>actual && + test_cmp expect actual +' + + +test_expect_success 'usage: with no ' ' + cat >expect <<-\EOF && + fatal: git cat-file --textconv HEAD: must be + EOF + test_must_fail git cat-file --textconv HEAD 2>actual && + test_cmp expect actual +' + + +test_expect_success 'usage: :' ' + cat >expect <<-\EOF && + fatal: Not a valid object name HEAD2:one.bin + EOF + test_must_fail git cat-file --textconv HEAD2:one.bin 2>actual && + test_cmp expect actual +' + cat >expected < X-Patchwork-Id: 12664337 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 6315AC433F5 for ; Wed, 8 Dec 2021 12:34:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233564AbhLHMiI (ORCPT ); Wed, 8 Dec 2021 07:38:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233512AbhLHMiD (ORCPT ); Wed, 8 Dec 2021 07:38:03 -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 A75F3C061746 for ; Wed, 8 Dec 2021 04:34:30 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id u1so3768574wru.13 for ; Wed, 08 Dec 2021 04:34:30 -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=onxQfEFprRIOizBehiT3izRpZuqKe7yexADBfyvk/Go=; b=Y6q6E1elZ67RCnmQiBsWF4GlefW5RQZJ9dUBu/WUL9762KY6nOD65jmrfEdFew7OUr uNfVg+sXeZI7rehl0h+zEPuSX/aAWzAj+6VqC39y8jfq3ANA4M52KApCqOGcfm/zrX0H AHSMv6IwG/lfYlxT1KUwQFvecBPN79NnuVO5pRPT/H17tgo18vxRdjYxeymhWaeMLZGW ayCxZC9tbMxaAeFL3qWH8oK0uxqvB/2Nm8ft3Au+aMPvM2DAJOGDvaJePDWyhjE6yGC9 tvB8iuUYT91VTpFqoOLoqjLEWbqKbQZczgHZ+LcEgTK2ObY+epb54t+AG3M/9LxDLXhR X2sQ== 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=onxQfEFprRIOizBehiT3izRpZuqKe7yexADBfyvk/Go=; b=C/yBKk5VrcF36GbI3t3lKrwyjBrP+UQeeVTC49MH/1jN3ARdOhw9Nr0uVaU24G+Tei UoBjLtfrdksIGe2R5xAgDtEpXz7ONCt047wODMt+wq3OsVMHfkLpDrqLXRcMy1w5HepQ BVjUS0gY5+HecN3adnQWWbKgXwTPoV587lNiqXPmIQSZ1EWPPVMEz5Q4xeSI3sFgYrx4 4Wk9LTAfIJDCJ3hazH+NcTjOn/7uZGqDGsA97dyg4n4ojr9gg3nMUUYJlJScyPSnjRot n6409865wArWKq/CjNn2s3UF55h67vVgADcSwUEpT8uuQGOUmLzubB/qK7ovxL+LB7EP eXtw== X-Gm-Message-State: AOAM530IlvSMHhgJGo9dnpcrqhHC/q3N6UEs9h+ChZQMJx39wn0FYgnY Mr1Bt0pZ9QCfdjuvuoFSrtHEDaiJ7eQVeg== X-Google-Smtp-Source: ABdhPJyfF5jxtvc0uMnqWLIDDD1DOVxcyKIRiuIM8Wrf4COelCb9VZT+rwhSUiv4YbYhdub4LFiFaw== X-Received: by 2002:a5d:6d88:: with SMTP id l8mr58329966wrs.270.1638966869008; Wed, 08 Dec 2021 04:34:29 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id d8sm2540692wrm.76.2021.12.08.04.34.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 04:34:28 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , John Cai , Sergey Organov , Jiang Xin , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= Subject: [PATCH v4 03/10] parse-options API: add a usage_msg_optf() Date: Wed, 8 Dec 2021 13:34:16 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.926.g895e15e0c0c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a usage_msg_optf() as a shorthand for the sort of usage_msg_opt(xstrfmt(...)) used in builtin/stash.c. I'll make more use of this function in builtin/cat-file.c shortly. The disconnect between the "..." and "fmt" is a bit unusual, but it works just fine and this keeps it consistent with usage_msg_opt(), i.e. a caller of it can be moved to usage_msg_optf() and not have to have its arguments re-arranged. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/stash.c | 4 ++-- parse-options.c | 13 +++++++++++++ parse-options.h | 10 ++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/builtin/stash.c b/builtin/stash.c index 18c812bbe03..c9a09047a6e 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -1811,8 +1811,8 @@ int cmd_stash(int argc, const char **argv, const char *prefix) else if (!strcmp(argv[0], "save")) return !!save_stash(argc, argv, prefix); else if (*argv[0] != '-') - usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), - git_stash_usage, options); + usage_msg_optf(_("unknown subcommand: %s"), + git_stash_usage, options, argv[0]); /* Assume 'stash push' */ strvec_push(&args, "push"); diff --git a/parse-options.c b/parse-options.c index fc5b43ff0b2..5a319dda7d5 100644 --- a/parse-options.c +++ b/parse-options.c @@ -1078,3 +1078,16 @@ void NORETURN usage_msg_opt(const char *msg, fprintf(stderr, "fatal: %s\n\n", msg); usage_with_options(usagestr, options); } + +void NORETURN usage_msg_optf(const char * const fmt, + const char * const *usagestr, + const struct option *options, ...) +{ + struct strbuf msg = STRBUF_INIT; + va_list ap; + va_start(ap, options); + strbuf_vaddf(&msg, fmt, ap); + va_end(ap); + + usage_msg_opt(msg.buf, usagestr, options); +} diff --git a/parse-options.h b/parse-options.h index 275fb440818..4a9fa8a84d7 100644 --- a/parse-options.h +++ b/parse-options.h @@ -225,6 +225,16 @@ NORETURN void usage_msg_opt(const char *msg, const char * const *usagestr, const struct option *options); +/** + * usage_msg_optf() is like usage_msg_opt() except that the first + * argument is a format string, and optional format arguments follow + * after the 3rd option. + */ +__attribute__((format (printf,1,4))) +void NORETURN usage_msg_optf(const char *fmt, + const char * const *usagestr, + const struct option *options, ...); + /* * Use these assertions for callbacks that expect to be called with NONEG and * NOARG respectively, and do not otherwise handle the "unset" and "arg" From patchwork Wed Dec 8 12:34:17 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: 12664335 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 93152C433EF for ; Wed, 8 Dec 2021 12:34:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233544AbhLHMiG (ORCPT ); Wed, 8 Dec 2021 07:38:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233522AbhLHMiD (ORCPT ); Wed, 8 Dec 2021 07:38:03 -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 8783FC061A32 for ; Wed, 8 Dec 2021 04:34:31 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id u17so3863277wrt.3 for ; Wed, 08 Dec 2021 04:34:31 -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=KanFhJYJgnSiPm0LhzXvt+tSGW3FBlu5niqsG7uqiKU=; b=i5xeU58T6SZiBWwNe0mSdiUcy8qaFetXDwDg8RY8JC2MEKDUvOBVtb0QHNhc2rcrs4 yHUlFZwVqxPXhvVp+BlMM7ZHTCAtQzZ64uF1AO+GeHCqYbWg1nnNmtF7s2Wa47gcKW3F v8gzhBERQiDv2S2wGHgzqKGMJzKMxSzsUClEKRrwsam0QIqRp4iVawPGMqf7Y5fXQCjE SL8NM94cQgbVzimu1rhWbiDCCrsUJo1UUCBZWZ6ZTAO1N8wN6eS6IYUW1q07gryO7+WW ViEoEK64t0/eDX41b/narYy1+pNkIEXIIu8UfFL0ocNrgNUmEAl3SfqAOTJS9J7NRKfw IcuQ== 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=KanFhJYJgnSiPm0LhzXvt+tSGW3FBlu5niqsG7uqiKU=; b=ixjZkNs/2yK9TNkQIWopraGg46ukHv+OE3R39N3IZLAhiiK4E131ChYH1eX88MOmSJ c1aZhswygxnBnbew3hyyFSdpJ8gxPR3SgrdQ6Vu+1ri+dNBeWbPv29tv2Dg0JlF2eQed zo8z8RGc7Nnb+4Xit8G+D7wDLfF8HElnI9+e4lpZLh4OGfDoC56XnbxXzkRGKmtqGg0k UoBms7AZcXHHDiLUnredLA3NvRRbRCcwxBUl7nCQYwnSLWiavr7QEcM0C0XQ9UQOIcvw pR+8hRAmuXH6+0g+kKw7HmoK3Jl65ikQpimSt3hSx9/BQvszwT0nDdrEkQEXtf7Ify+i p2IQ== X-Gm-Message-State: AOAM531MT6ckarBVpwUsGdQWyMdz9lLqrUh8siuMCNkO9StV/vZK+JT6 sOUPQWNlgTbRuOxznYufMR1Hzk2CvDYjfQ== X-Google-Smtp-Source: ABdhPJwAx2FOXNa6mhHloMXfzk7i4tnMBCfJuS+9cKZ3Z/uqrupUqSz/mLgKqdUPuBS3xKdB1zh5/w== X-Received: by 2002:adf:fb47:: with SMTP id c7mr59085657wrs.602.1638966869846; Wed, 08 Dec 2021 04:34:29 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id d8sm2540692wrm.76.2021.12.08.04.34.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 04:34:29 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , John Cai , Sergey Organov , Jiang Xin , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= Subject: [PATCH v4 04/10] cat-file docs: fix SYNOPSIS and "-h" output Date: Wed, 8 Dec 2021 13:34:17 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.926.g895e15e0c0c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org There were various inaccuracies in the previous SYNOPSIS output, e.g. "--path" is not something that can optionally go with any options except --textconv or --filters, as the output implied. The opening line of the DESCRIPTION section is also "In its first form[...]", which refers to "git cat-file ", but the SYNOPSIS section wasn't showing that as the first form! That part of the documentation made sense in d83a42f34a6 (Documentation: minor grammatical fixes in git-cat-file.txt, 2009-03-22) when it was introduced, but since then various options that were added have made that intro make no sense in the context it was in. Now the two will match again. The usage output here is not properly aligned on "master" currently, but will be with my in-flight 4631cfc20bd (parse-options: properly align continued usage output, 2021-09-21), so let's indent things correctly in the C code in anticipation of that. Signed-off-by: Ævar Arnfjörð Bjarmason --- Documentation/git-cat-file.txt | 10 ++++++++-- builtin/cat-file.c | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt index 27b27e2b300..73ebbc70ee2 100644 --- a/Documentation/git-cat-file.txt +++ b/Documentation/git-cat-file.txt @@ -9,8 +9,14 @@ git-cat-file - Provide content or type and size information for repository objec SYNOPSIS -------- [verse] -'git cat-file' (-t [--allow-unknown-type]| -s [--allow-unknown-type]| -e | -p | | --textconv | --filters ) [--path=] -'git cat-file' (--batch[=] | --batch-check[=]) [ --textconv | --filters ] [--follow-symlinks] +'git cat-file' +'git cat-file' (-e | -p) +'git cat-file' ( -t | -s ) [--allow-unknown-type] +'git cat-file' (--batch | --batch-check) [--batch-all-objects] + [--buffer] [--follow-symlinks] [--unordered] + [--textconv | --filters] +'git cat-file' (--textconv | --filters ) + [: | --path= ] DESCRIPTION ----------- diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 86fc03242b8..1df7f797cb6 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -619,8 +619,14 @@ static int batch_objects(struct batch_options *opt) } static const char * const cat_file_usage[] = { - N_("git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | | --textconv | --filters) [--path=] "), - N_("git cat-file (--batch[=] | --batch-check[=]) [--follow-symlinks] [--textconv | --filters]"), + N_("git cat-file "), + N_("git cat-file (-e | -p) "), + N_("git cat-file ( -t | -s ) [--allow-unknown-type] "), + N_("git cat-file (--batch | --batch-check) [--batch-all-objects]\n" + " [--buffer] [--follow-symlinks] [--unordered]\n" + " [--textconv | --filters]"), + N_("git cat-file (--textconv | --filters )\n" + " [: | --path= ]"), NULL }; From patchwork Wed Dec 8 12:34:18 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: 12664339 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 E0F34C433FE for ; Wed, 8 Dec 2021 12:34:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233581AbhLHMiJ (ORCPT ); Wed, 8 Dec 2021 07:38:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233492AbhLHMiE (ORCPT ); Wed, 8 Dec 2021 07:38:04 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EB12C0617A1 for ; Wed, 8 Dec 2021 04:34:32 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id i5so3857804wrb.2 for ; Wed, 08 Dec 2021 04:34:32 -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=oxfn007D2gjQgcWCYEZuZmC9qy4tCBUyAcJkasWsuuw=; b=RLMSWrTMMPXzRmyONCoRses5xt83KhkNLDh+0Ipr3l6LQC3P8ltx0nEdP1e8TxgNYC W8Zezk3kSPfeIj0BwC7tFDOJF2noZ0ZBSNLFbJsYFXS57S1MJcxBwcJK+qXTGtJJskcT rBmt0XAFCw1azp79PMSI6iDB1nnbIRGi5wtoqyiqK/tPqN2TmgNryrNMHis0odkDpZVu 6VpR5ywvfRbGpYvr+4ApxRECXUhshz+GFM6ruosxsaPGpKmrpuTML/35soR62VQcaJlc CyzMwMKZE/sn/hS+gsKfIbkBpvf5hJ5z7rjebxZRnaMuS6Vc+SXKWwvNMmAf1SNzydiu y4hg== 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=oxfn007D2gjQgcWCYEZuZmC9qy4tCBUyAcJkasWsuuw=; b=YNx57aB35/xpIV7ViGtcy83AD+JiCHYeGqOA2NcSSMhRlObaBVkssoiddnvSHlGsKD R4yvLncg3omOrh5OX2wkVbrfVtG211F7rU4TvCFFx0GxHT7/Ve9ngVAyZ36CBGUDtJ4F PV49+C+sB5MGTkbKMdvVHW4ZkwVg+O4A6TwKRn6OfZfmtPyAs67RO4xV8d4dzpAYVr0Z HeFdMeXv5ehaq4IImOZAGFfFXOByS+kv3Onr/lFpuOdtMPcPMtZyY63/Vfqoyz/m5Q+9 0ZODucDsHFn+HmdCWmrQFMQheNKgtBP5ovGOp3mygCvDu79huQvXf+x749oHsl4nsHGm oKyg== X-Gm-Message-State: AOAM533K9sSH9DWFx9628HF8glp+wvZF5XA9sIWx1ghrWSgTMwBtbC2p WfzC0z/KfJTJzVzXbbpgGtUqjjz5LdGfZg== X-Google-Smtp-Source: ABdhPJyX7ObbGfZEWdaA8kohBh2p3of6cT/K6ssOBXHlUIzibYMuk9LPrUatb0nV3Pa3JLQiL2jo7Q== X-Received: by 2002:adf:bc89:: with SMTP id g9mr58144937wrh.578.1638966870684; Wed, 08 Dec 2021 04:34:30 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id d8sm2540692wrm.76.2021.12.08.04.34.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 04:34:30 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , John Cai , Sergey Organov , Jiang Xin , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= Subject: [PATCH v4 05/10] cat-file: move "usage" variable to cmd_cat_file() Date: Wed, 8 Dec 2021 13:34:18 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.926.g895e15e0c0c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org There's no benefit to defining this at a distance, and it makes the code harder to read as you've got to scroll up to see the usage that corresponds to the options. In subsequent commits I'll make use of usage_msg_opt(), which will be quite noisy if I have to use the long "cat_file_usage" variable, there's no other command being defined in this file, so let's rename it to just "usage". Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/cat-file.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 1df7f797cb6..6d0f645301b 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -618,18 +618,6 @@ static int batch_objects(struct batch_options *opt) return retval; } -static const char * const cat_file_usage[] = { - N_("git cat-file "), - N_("git cat-file (-e | -p) "), - N_("git cat-file ( -t | -s ) [--allow-unknown-type] "), - N_("git cat-file (--batch | --batch-check) [--batch-all-objects]\n" - " [--buffer] [--follow-symlinks] [--unordered]\n" - " [--textconv | --filters]"), - N_("git cat-file (--textconv | --filters )\n" - " [: | --path= ]"), - NULL -}; - static int git_cat_file_config(const char *var, const char *value, void *cb) { if (userdiff_config(var, value) < 0) @@ -664,6 +652,17 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) struct batch_options batch = {0}; int unknown_type = 0; + const char * const usage[] = { + N_("git cat-file "), + N_("git cat-file (-e | -p) "), + N_("git cat-file ( -t | -s ) [--allow-unknown-type] "), + N_("git cat-file (--batch | --batch-check) [--batch-all-objects]\n" + " [--buffer] [--follow-symlinks] [--unordered]\n" + " [--textconv | --filters]"), + N_("git cat-file (--textconv | --filters )\n" + " [: | --path= ]"), + NULL + }; const struct option options[] = { OPT_GROUP(N_(" can be one of: blob, tree, commit, tag")), OPT_CMDMODE('t', NULL, &opt, N_("show object type"), 't'), @@ -700,7 +699,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) git_config(git_cat_file_config, NULL); batch.buffer_output = -1; - argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0); + argc = parse_options(argc, argv, prefix, options, usage, 0); if (opt) { if (batch.enabled && (opt == 'c' || opt == 'w')) @@ -708,35 +707,35 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) else if (argc == 1) obj_name = argv[0]; else - usage_with_options(cat_file_usage, options); + usage_with_options(usage, options); } if (!opt && !batch.enabled) { if (argc == 2) { exp_type = argv[0]; obj_name = argv[1]; } else - usage_with_options(cat_file_usage, options); + usage_with_options(usage, options); } if (batch.enabled) { if (batch.cmdmode != opt || argc) - usage_with_options(cat_file_usage, options); + usage_with_options(usage, options); if (batch.cmdmode && batch.all_objects) die("--batch-all-objects cannot be combined with " "--textconv nor with --filters"); } if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) { - usage_with_options(cat_file_usage, options); + usage_with_options(usage, options); } if (force_path && opt != 'c' && opt != 'w') { error("--path= needs --textconv or --filters"); - usage_with_options(cat_file_usage, options); + usage_with_options(usage, options); } if (force_path && batch.enabled) { error("--path= incompatible with --batch"); - usage_with_options(cat_file_usage, options); + usage_with_options(usage, options); } if (batch.buffer_output < 0) From patchwork Wed Dec 8 12:34:19 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: 12664341 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 103E8C433EF for ; Wed, 8 Dec 2021 12:34:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233539AbhLHMiK (ORCPT ); Wed, 8 Dec 2021 07:38:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233501AbhLHMiF (ORCPT ); Wed, 8 Dec 2021 07:38:05 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A727C061746 for ; Wed, 8 Dec 2021 04:34:33 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id o13so3773702wrs.12 for ; Wed, 08 Dec 2021 04:34:33 -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=BWtSkRwBdBViZ7bDGNm0+1jaSarsGs9w0Vb2fgahiKM=; b=YbT3lRyeAZ9VraUc0a8VuGMQu3CRqae5balxBItE6ObEbiJcGHiiZs8yWfk/APF+Gp AWZNdWWfVSHr8hq9B/ajSjiyvouajW75SeI9XuisA5QnZmZKGFy8ZH2lA4lCxe79pdiz t7GVHqUfbsP7JsLD0Q3Z/GViv8dMqdOFdKkWm4Z6GEXQP2rqirriPhrzUQSeZ71iVkr+ EbNMX93nj1J92vIQULbmZIifgkcJh3yRt2QHNUClDD6JP1d5AEZDiPPc/8imp+IJiOta jYkJxpZnPVRGXmpcOqZUqXUSKp/IjbcCvj/0Cw43nPXI9I9+tFCEcDEu0KftleQoE0d4 Z7Pg== 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=BWtSkRwBdBViZ7bDGNm0+1jaSarsGs9w0Vb2fgahiKM=; b=ZeygvnlYrlIRW6IYZJCIG5JjuDCMd3t3fkoGoYhqfNzRyVyq714NrRaCkf9Q5lmcD+ rajFh8c/iLULqRoa8iJsv9o6tevi1MbpeOA8+Ja9qN4+nFrCRVSOmRWTO/R+XVBCaUIY 9dq/35Uj35hstFJJ7kNqWkBUW7wMxEtuMT4qCDvXczvk3JT+HuHB62jYujpWkgboyjT8 ALaOM3td3Hy3jJN/tIEedkKYBoGAYHlD7BeaxFq/LZ4W/ulwdFhTBI7U6QIdeivs9pfw /EJbGU6QfXkeMSocrAoLkJhm5/wa75FrSytmZssXL4z3oPaFPKIIT/94ntY2j9FJ9j6i YQ4Q== X-Gm-Message-State: AOAM53312EYfZOItti8GeZ2QeOeMBRc2c4ODld/WJJ6NYfBHvdx6tiX0 cNbVM6kOXikvML2xs/LMJYGrCkQgnaXXzQ== X-Google-Smtp-Source: ABdhPJzavCCk/i7Ugr1qMsG7DNQRHFM4kvA7v3Tg22MO0pqG0taNYvXMab0JPPN9tC4w9NMy9FxMrw== X-Received: by 2002:a5d:598c:: with SMTP id n12mr56740867wri.250.1638966871855; Wed, 08 Dec 2021 04:34:31 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id d8sm2540692wrm.76.2021.12.08.04.34.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 04:34:30 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , John Cai , Sergey Organov , Jiang Xin , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= Subject: [PATCH v4 06/10] cat-file: make --batch-all-objects a CMDMODE Date: Wed, 8 Dec 2021 13:34:19 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.926.g895e15e0c0c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The usage of OPT_CMDMODE() in "cat-file"[1] was added in parallel with the development of[3] the --batch-all-objects option[4], so we've since grown[5] checks that it can't be combined with other command modes, when it should just be made a top-level command-mode instead. It doesn't combine with --filters, --textconv etc. By giving parse_options() information about what options are mutually exclusive with one another we can get the die() message being removed here for free, we didn't even use that removed message in some cases, e.g. for both of: --batch-all-objects --textconv --batch-all-objects --filters We'd take the "goto usage" in the "if (opt)" branch, and never reach the previous message. Now we'll emit e.g.: $ git cat-file --batch-all-objects --filters error: option `filters' is incompatible with --batch-all-objects 1. b48158ac94c (cat-file: make the options mutually exclusive, 2015-05-03) 2. https://lore.kernel.org/git/xmqqtwspgusf.fsf@gitster.dls.corp.google.com/ 3. https://lore.kernel.org/git/20150622104559.GG14475@peff.net/ 4. 6a951937ae1 (cat-file: add --batch-all-objects option, 2015-06-22) 5. 321459439e1 (cat-file: support --textconv/--filters in batch mode, 2016-09-09) Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/cat-file.c | 25 +++++++++++-------------- t/t1006-cat-file.sh | 7 ++----- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 6d0f645301b..87356208134 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -674,6 +674,8 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) N_("for blob objects, run textconv on object's content"), 'c'), OPT_CMDMODE(0, "filters", &opt, N_("for blob objects, run filters on object's content"), 'w'), + OPT_CMDMODE(0, "batch-all-objects", &opt, + N_("show all objects with --batch or --batch-check"), 'b'), OPT_STRING(0, "path", &force_path, N_("blob"), N_("use a specific path for --textconv/--filters")), OPT_BOOL(0, "allow-unknown-type", &unknown_type, @@ -689,8 +691,6 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) batch_option_callback), OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks, N_("follow in-tree symlinks (used with --batch or --batch-check)")), - OPT_BOOL(0, "batch-all-objects", &batch.all_objects, - N_("show all objects with --batch or --batch-check")), OPT_BOOL(0, "unordered", &batch.unordered, N_("do not order --batch-all-objects output")), OPT_END() @@ -699,30 +699,27 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) git_config(git_cat_file_config, NULL); batch.buffer_output = -1; - argc = parse_options(argc, argv, prefix, options, usage, 0); - if (opt) { + argc = parse_options(argc, argv, prefix, options, usage, 0); + if (argc && batch.enabled) + usage_with_options(usage, options); + if (opt == 'b') { + batch.all_objects = 1; + } else if (opt) { if (batch.enabled && (opt == 'c' || opt == 'w')) batch.cmdmode = opt; else if (argc == 1) obj_name = argv[0]; else usage_with_options(usage, options); - } - if (!opt && !batch.enabled) { + } else if (!opt && !batch.enabled) { if (argc == 2) { exp_type = argv[0]; obj_name = argv[1]; } else usage_with_options(usage, options); - } - if (batch.enabled) { - if (batch.cmdmode != opt || argc) - usage_with_options(usage, options); - if (batch.cmdmode && batch.all_objects) - die("--batch-all-objects cannot be combined with " - "--textconv nor with --filters"); - } + } else if (batch.enabled && batch.cmdmode != opt) + usage_with_options(usage, options); if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) { usage_with_options(usage, options); diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index fc9191c1b94..ebec2061d25 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -14,7 +14,8 @@ for switches in \ '-p -t' \ '-t -s' \ '-s --textconv' \ - '--textconv --filters' + '--textconv --filters' \ + '--batch-all-objects -e' do test_expect_success "usage: cmdmode $switches" ' test_cmdmode_usage git cat-file $switches @@ -41,10 +42,6 @@ do test_expect_success "usage: $opt requires another option" ' test_expect_code 129 git cat-file $opt ' - - test_expect_failure "usage: incompatible options: --batch-all-objects with $opt" ' - test_incompatible_usage git cat-file --batch-all-objects $opt - ' done for opt in $short_modes From patchwork Wed Dec 8 12:34:20 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: 12664343 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 AB168C433F5 for ; Wed, 8 Dec 2021 12:34:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233550AbhLHMiL (ORCPT ); Wed, 8 Dec 2021 07:38:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233556AbhLHMiH (ORCPT ); Wed, 8 Dec 2021 07:38:07 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A6DDC061D72 for ; Wed, 8 Dec 2021 04:34:34 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id t18so3785169wrg.11 for ; Wed, 08 Dec 2021 04:34:34 -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=o9XcFndDls6zCia+IwKYnSgkLaxYxzH/ubvx1nlVWpg=; b=QHEGKqYbQ1gb5EMU6wmxW2LhVPcd8ASaiynebmn+cy1KKjsjWirndNLAdPGxPG/GBo nkXTJ9v5kq58NV43gYcQfeWLKbaQ7Jn83bdwBpbBlPIcrBuCqBmr8aHfC6DaLLYhDYAB +gly+7uxyuiqQmcCEBropdK6/EQFcBlzPKeQPuJJB8NqT+jJx0xzh81w47tcSDnByYqw d2jOb7A3vGE64Jfun83Ln32WTPTL7ptZGW5gOQPuMh98Yso30R6mCzAgiFtHtBRX4C4l 35ev8XnfZDInJUrtkql41xydfFEAB3nLKcRjI96Dth/u2foIN05I0RY3SkT12PZstQ+8 Kc/A== 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=o9XcFndDls6zCia+IwKYnSgkLaxYxzH/ubvx1nlVWpg=; b=Sm9JRxoCoifLlyaWnvAWdc4c6VSx/1D+WpqRRMKZZrkD3TcLZY4ds1HeYdR8CI2lKD 2mk1sTBiam96l9VSgNNYWW+4qM4a56ysHSEywubavwdyxYvhHjzqPKjutWpG6g95UsOL wm7/gu1MnDPg/gmDY5LlFrHn9TwLPz+4lQu6KU6mMjjl0LzOyGCHZt/se8WBnUSkJIFT DroGn5/44HcLoOY+nxyr7U4n4PdWle6uMkVT3p7oCvKMA0FjSzK6K38gq40ubgBJbPzx EtauBgRNk5fyK3y9zomKxyZIJg8qAyZGMwTIF+tq2SMfyzLRUr7wPztK4Ud19IO80LtY +pBg== X-Gm-Message-State: AOAM5314FvVW4QFqHsbtb4Tn72JP52CLQGVLqOIoSHzII+p2TC1kqNZ4 ELszIj7zLb44TxPM40jkbkwe6zcKbegWYA== X-Google-Smtp-Source: ABdhPJyj17w5h3qucCFbTo0AFtOPnCkh3WOtjmhZNugs7KKfidL51gJz2o2Dh/BJsXnNd1uxnOE5lw== X-Received: by 2002:adf:fb4f:: with SMTP id c15mr59840219wrs.507.1638966872753; Wed, 08 Dec 2021 04:34:32 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id d8sm2540692wrm.76.2021.12.08.04.34.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 04:34:32 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , John Cai , Sergey Organov , Jiang Xin , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= Subject: [PATCH v4 07/10] cat-file: fix remaining usage bugs Date: Wed, 8 Dec 2021 13:34:20 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.926.g895e15e0c0c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org With the migration of --batch-all-objects to OPT_CMDMODE() in the preceding commit one bug with combining it and other OPT_CMDMODE() options was solved, but we were still left with e.g. --buffer silently being discarded when not in batch mode. Fix all those bugs, and in addition emit errors telling the user specifically what options can't be combined with what other options, before this we'd usually just emit the cryptic usage text and leave the users to work it out by themselves. This change is rather large, because to do so we need to untangle the options processing so that we can not only error out, but emit sensible errors, and e.g. emit errors about options before errors about stray argc elements (as they might become valid if the option were removed). Some of the output changes ("error:" to "fatal:" with usage_msg_opt[f]()), but none of the exit codes change, except in those cases where we silently accepted bad option combinations before, now we'll error out. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/cat-file.c | 95 ++++++++++++++++++++++++++++++--------------- t/t1006-cat-file.sh | 41 +++++++++---------- 2 files changed, 84 insertions(+), 52 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 87356208134..1087f0f4a85 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -648,6 +648,8 @@ static int batch_option_callback(const struct option *opt, int cmd_cat_file(int argc, const char **argv, const char *prefix) { int opt = 0; + int opt_cw = 0; + int opt_epts = 0; const char *exp_type = NULL, *obj_name = NULL; struct batch_options batch = {0}; int unknown_type = 0; @@ -701,45 +703,74 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) batch.buffer_output = -1; argc = parse_options(argc, argv, prefix, options, usage, 0); - if (argc && batch.enabled) - usage_with_options(usage, options); - if (opt == 'b') { - batch.all_objects = 1; - } else if (opt) { - if (batch.enabled && (opt == 'c' || opt == 'w')) - batch.cmdmode = opt; - else if (argc == 1) - obj_name = argv[0]; - else - usage_with_options(usage, options); - } else if (!opt && !batch.enabled) { - if (argc == 2) { - exp_type = argv[0]; - obj_name = argv[1]; - } else - usage_with_options(usage, options); - } else if (batch.enabled && batch.cmdmode != opt) - usage_with_options(usage, options); + opt_cw = (opt == 'c' || opt == 'w'); + opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's'); - if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) { - usage_with_options(usage, options); - } - - if (force_path && opt != 'c' && opt != 'w') { - error("--path= needs --textconv or --filters"); - usage_with_options(usage, options); - } + /* --batch-all-objects? */ + if (opt == 'b') + batch.all_objects = 1; - if (force_path && batch.enabled) { - error("--path= incompatible with --batch"); - usage_with_options(usage, options); - } + /* Option compatibility */ + if (force_path && !opt_cw) + usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"), + usage, options, + "--path", _("path|tree-ish"), "--filters", + "--textconv"); + /* Option compatibility with batch mode */ + if (batch.enabled) + ; + else if (batch.follow_symlinks) + usage_msg_optf(_("'%s' requires a batch mode"), usage, options, + "--follow_symlinks"); + else if (batch.buffer_output >= 0) + usage_msg_optf(_("'%s' requires a batch mode"), usage, options, + "--buffer"); + else if (batch.all_objects) + usage_msg_optf(_("'%s' requires a batch mode"), usage, options, + "--batch-all_objects"); + + /* Batch defaults */ if (batch.buffer_output < 0) batch.buffer_output = batch.all_objects; - if (batch.enabled) + /* Return early if we're in batch mode? */ + if (batch.enabled) { + if (opt_cw) + batch.cmdmode = opt; + else if (opt && opt != 'b') + usage_msg_optf(_("'-%c' is incompatible with batch mode"), + usage, options, opt); + else if (argc) + usage_msg_opt(_("batch modes take no arguments"), usage, + options); + return batch_objects(&batch); + } + + if (opt) { + if (!argc && opt == 'c') + usage_msg_optf(_(" required with '%s'"), + usage, options, "--textconv"); + else if (!argc && opt == 'w') + usage_msg_optf(_(" required with '%s'"), + usage, options, "--filters"); + else if (!argc && opt_epts) + usage_msg_optf(_(" required with '-%c'"), + usage, options, opt); + else if (argc == 1) + obj_name = argv[0]; + else + usage_msg_opt(_("too many arguments"), usage, options); + } else if (!argc) { + usage_with_options(usage, options); + } else if (argc != 2) { + usage_msg_optf(_("only two arguments allowed in mode, not %d"), + usage, options, argc); + } else if (argc) { + exp_type = argv[0]; + obj_name = argv[1]; + } if (unknown_type && opt != 't' && opt != 's') die("git cat-file --allow-unknown-type: use with -s or -t"); diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index ebec2061d25..123801cfe2a 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -24,7 +24,7 @@ done test_incompatible_usage () { test_expect_code 129 "$@" 2>err && - grep -E "^error:.**needs" err + grep -E "^(fatal|error):.*(requires|incompatible with|needs)" err } for opt in --batch --batch-check @@ -34,48 +34,54 @@ do ' done +test_missing_usage() { + test_expect_code 129 "$@" 2>err && + grep -E "^fatal:.*required" err +} + short_modes="-e -p -t -s" cw_modes="--textconv --filters" for opt in $cw_modes do test_expect_success "usage: $opt requires another option" ' - test_expect_code 129 git cat-file $opt + test_missing_usage git cat-file $opt ' done for opt in $short_modes do test_expect_success "usage: $opt requires another option" ' - test_expect_code 129 git cat-file $opt + test_missing_usage git cat-file $opt ' for opt2 in --batch \ --batch-check \ - --follow-symlinks + --follow-symlinks \ + "--path=foo HEAD:some-path.txt" do - test_expect_failure "usage: incompatible options: $opt and $opt2" ' + test_expect_success "usage: incompatible options: $opt and $opt2" ' test_incompatible_usage git cat-file $opt $opt2 ' done - - opt2="--path=foo HEAD:some-path.txt" - test_expect_success "usage: incompatible options: $opt and $opt2" ' - test_incompatible_usage git cat-file $opt $opt2 - ' done +test_too_many_arguments() { + test_expect_code 129 "$@" 2>err && + grep -E "^fatal: too many arguments$" err +} + for opt in $short_modes $cw_modes do args="one two three" test_expect_success "usage: too many arguments: $opt $args" ' - test_expect_code 129 git cat-file $opt $args + test_too_many_arguments git cat-file $opt $args ' for opt2 in --buffer --follow-symlinks do test_expect_success "usage: incompatible arguments: $opt with batch option $opt2" ' - test_expect_code 129 git cat-file $opt $opt2 + test_incompatible_usage git cat-file $opt $opt2 ' done done @@ -84,14 +90,9 @@ for opt in --buffer \ --follow-symlinks \ --batch-all-objects do - status=success - if test $opt = "--buffer" - then - status=failure - fi - test_expect_$status "usage: bad option combination: $opt without batch mode" ' - test_expect_code 129 git cat-file $opt && - test_expect_code 129 git cat-file $opt commit HEAD + test_expect_success "usage: bad option combination: $opt without batch mode" ' + test_incompatible_usage git cat-file $opt && + test_incompatible_usage git cat-file $opt commit HEAD ' done From patchwork Wed Dec 8 12:34:21 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: 12664345 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 13227C433FE for ; Wed, 8 Dec 2021 12:34:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233589AbhLHMiM (ORCPT ); Wed, 8 Dec 2021 07:38:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233561AbhLHMiI (ORCPT ); Wed, 8 Dec 2021 07:38:08 -0500 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35857C0698C4 for ; Wed, 8 Dec 2021 04:34:35 -0800 (PST) Received: by mail-wr1-x42b.google.com with SMTP id d24so3944639wra.0 for ; Wed, 08 Dec 2021 04:34:35 -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=NZA1PhCts4hOG88HZnicUP/knOjgwU2S0en4t3K6VII=; b=kG/y+Fpu3MXltyce56bFSB6hyRQ/tH37WZaqj/aRe2KiLMvXEETSmqLWpfrAK6lTXT g25K5GLC2K8ZOUo2sEtZ9Wu6qSJgCjzeYe1zFmamN7EflKF/MZ3e8V+6Z0d62eAQ1PWO v99FVpK/Bi/aZgLkQb5ihqCzu7AHgoO6/3Xwf0Z0GoSyzS03MkXNIrEni7yW4KJbRENs 402hHtAncNX//wyy+PrieAL7hVUclFxlgt0ZYnwB+NXJGAM/2lmTtHp71G9PD/VytDhx SvpHlhwiDScyzUhhUC69+dSpKlTS811C5fBmJ1B1yhhOgbLqgI3gatgRvP4WyWghQIim rSGQ== 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=NZA1PhCts4hOG88HZnicUP/knOjgwU2S0en4t3K6VII=; b=q8tZvjW7oSOfXR3oMBhoy+RqmbZdrfRfRefoPk/8MkBpjK778ZERqQ+tQBGEErrYAi nDCgOJJ+URub0hTNXqFhLJlpZDjOZ5eV51MF2Gv6dmvu12G7a22d2nqu8xxz9cRDpXKe 2qtWudOZXxmKYDN8WP1g9Fhc1FnOvDmX62bcXYQl9QGppX4stfYs+vVKqDu1pFfDPu2k dvU4EvJPANAOo6P+NMqb160wSH6Xr8ql+2c6wYwk0ck2lUj5Br+Yuoffjvbq7Hh07OXG p93orZoIQIvyVGW0MBUdZFwnn4mA639DyrSSNnaE1ymFtPBt5M2Wa2vkJ8hWDuF6qKwM waqA== X-Gm-Message-State: AOAM531RXwz/J9lR/fq5SkNDrh5b5im0l2/NYJ7ITTSwYbIdsA4V35Vs XNLmXEq8EzQvZ0cVqag8gixFz34Td3TDTg== X-Google-Smtp-Source: ABdhPJxce7B8diGCyTFycQd5Ll1AMgOKJPVAiBGjz0hXczqclTsPIWOvvsHhJe9X6KT4U8JabXjLkA== X-Received: by 2002:a05:6000:1a86:: with SMTP id f6mr58280317wry.230.1638966873531; Wed, 08 Dec 2021 04:34:33 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id d8sm2540692wrm.76.2021.12.08.04.34.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 04:34:33 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , John Cai , Sergey Organov , Jiang Xin , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= Subject: [PATCH v4 08/10] cat-file: correct and improve usage information Date: Wed, 8 Dec 2021 13:34:21 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.926.g895e15e0c0c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the usage output emitted on "git cat-file -h" to group related options, making it clear to users which options go with which other ones. The new output is: Check object existence or emit object contents -e check if exists -p pretty-print content Emit [broken] object attributes -t show object type (one of 'blob', 'tree', 'commit', 'tag', ...) -s show object size --allow-unknown-type allow -s and -t to work with broken/corrupt objects Batch objects requested on stdin (or --batch-all-objects) --batch[=] show full or contents --batch-check[=] like --batch, but don't emit --batch-all-objects with --batch[-check]: ignores stdin, batches all known objects Change or optimize batch output --buffer buffer --batch output --follow-symlinks follow in-tree symlinks --unordered do not order objects before emitting them Emit object (blob or tree) with conversion or filter (stand-alone, or with batch) --textconv run textconv on object's content --filters run filters on object's content --path blob|tree use a for (--textconv | --filters ); Not with 'batch' The old usage was: can be one of: blob, tree, commit, tag -t show object type -s show object size -e exit with zero when there's no error -p pretty-print object's content --textconv for blob objects, run textconv on object's content --filters for blob objects, run filters on object's content --batch-all-objects show all objects with --batch or --batch-check --path use a specific path for --textconv/--filters --allow-unknown-type allow -s and -t to work with broken/corrupt objects --buffer buffer --batch output --batch[=] show info and content of objects fed from the standard input --batch-check[=] show info about objects fed from the standard input --follow-symlinks follow in-tree symlinks (used with --batch or --batch-check) --unordered do not order --batch-all-objects output While shorter, I think the new one is easier to understand, as e.g. "--allow-unknown-type" is grouped with "-t" and "-s", as it can only be combined with those options. The same goes for "--buffer", "--unordered" etc. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/cat-file.c | 49 +++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 1087f0f4a85..e71519739a4 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -666,35 +666,44 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) NULL }; const struct option options[] = { - OPT_GROUP(N_(" can be one of: blob, tree, commit, tag")), - OPT_CMDMODE('t', NULL, &opt, N_("show object type"), 't'), - OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'), + /* Simple queries */ + OPT_GROUP(N_("Check object existence or emit object contents")), OPT_CMDMODE('e', NULL, &opt, - N_("exit with zero when there's no error"), 'e'), - OPT_CMDMODE('p', NULL, &opt, N_("pretty-print object's content"), 'p'), - OPT_CMDMODE(0, "textconv", &opt, - N_("for blob objects, run textconv on object's content"), 'c'), - OPT_CMDMODE(0, "filters", &opt, - N_("for blob objects, run filters on object's content"), 'w'), - OPT_CMDMODE(0, "batch-all-objects", &opt, - N_("show all objects with --batch or --batch-check"), 'b'), - OPT_STRING(0, "path", &force_path, N_("blob"), - N_("use a specific path for --textconv/--filters")), + N_("check if exists"), 'e'), + OPT_CMDMODE('p', NULL, &opt, N_("pretty-print content"), 'p'), + + OPT_GROUP(N_("Emit [broken] object attributes")), + OPT_CMDMODE('t', NULL, &opt, N_("show object type (one of 'blob', 'tree', 'commit', 'tag', ...)"), 't'), + OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'), OPT_BOOL(0, "allow-unknown-type", &unknown_type, N_("allow -s and -t to work with broken/corrupt objects")), - OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")), - OPT_CALLBACK_F(0, "batch", &batch, "format", - N_("show info and content of objects fed from the standard input"), + /* Batch mode */ + OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")), + OPT_CALLBACK_F(0, "batch", &batch, N_("format"), + N_("show full or contents"), PARSE_OPT_OPTARG | PARSE_OPT_NONEG, batch_option_callback), - OPT_CALLBACK_F(0, "batch-check", &batch, "format", - N_("show info about objects fed from the standard input"), + OPT_CALLBACK_F(0, "batch-check", &batch, N_("format"), + N_("like --batch, but don't emit "), PARSE_OPT_OPTARG | PARSE_OPT_NONEG, batch_option_callback), + OPT_CMDMODE(0, "batch-all-objects", &opt, + N_("with --batch[-check]: ignores stdin, batches all known objects"), 'b'), + /* Batch-specific options */ + OPT_GROUP(N_("Change or optimize batch output")), + OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")), OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks, - N_("follow in-tree symlinks (used with --batch or --batch-check)")), + N_("follow in-tree symlinks")), OPT_BOOL(0, "unordered", &batch.unordered, - N_("do not order --batch-all-objects output")), + N_("do not order objects before emitting them")), + /* Textconv options, stand-ole*/ + OPT_GROUP(N_("Emit object (blob or tree) with conversion or filter (stand-alone, or with batch)")), + OPT_CMDMODE(0, "textconv", &opt, + N_("run textconv on object's content"), 'c'), + OPT_CMDMODE(0, "filters", &opt, + N_("run filters on object's content"), 'w'), + OPT_STRING(0, "path", &force_path, N_("blob|tree"), + N_("use a for (--textconv | --filters ); Not with 'batch'")), OPT_END() }; From patchwork Wed Dec 8 12:34:22 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: 12664347 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 B751EC433EF for ; Wed, 8 Dec 2021 12:34:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233621AbhLHMiP (ORCPT ); Wed, 8 Dec 2021 07:38:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233575AbhLHMiI (ORCPT ); Wed, 8 Dec 2021 07:38:08 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EB99C0698CB for ; Wed, 8 Dec 2021 04:34:35 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id q3so3845286wru.5 for ; Wed, 08 Dec 2021 04:34:35 -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=xRNtA+ULfQ5ybKhP2md2ovt6QB7AIICfwiVYWiEVzSw=; b=awQcscKjodJe/9/vtMxql6FRk1ns5rjBjPmF62pYZL6x07Ttux/5EHi8D2FPIiC30L JfGnst9wp28rYeLDuw5LjgmD33jWJ1aWBfx65tCj8SSMCxv+YDWA8jjMelqtYnKyg9Xr NKZ+MrIM8XvIhFCsjkeIX+v4jo4Fnw4UHNj3fg2qwNXsCqv/jN/ZiKd8CEfyRNJ6p7SZ kh6lfnmRmGIXVK51EleSZzrTvBo1COEX0tZy/4nZpJXW2Qma0muNx/jNLjVw6hgL4lsY Sc1juxw5tLDsuDy/Bv1Ce7PYjZCTooZVP0K1H86kgLlKzU9zC+vR3IvlsOD7Q6wDN8jZ Yz8A== 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=xRNtA+ULfQ5ybKhP2md2ovt6QB7AIICfwiVYWiEVzSw=; b=FynFgLtFt8iQJ+XVrD9bpcBHJxKtWiqQ7Zvye6mOCFE+Q1wVP3nOEhBriI12h1VwFD 9VEVCX2DXgy4yNIoliX8t5l0Wfx0ZveKKAsLyoQsbbHBxYwzDU513IXBIMdvdYAzELvK LlGioq4AeakIannCc6ZX9O9LW1kDM9LYYlAavAQ7ho6UfAZJP8uFin/uIOBeBU/C/QvM Kv9TI/ed/OxGvBQQwr7ac5QlStq9MCpdTUfq0nHy0wECGurS29rOiA9EctPro3i3XhTS bexljDuvM7pfjMbo3WMw0HBWlwyHWzqf/yHjem/6fJzXZFVtmVWl+tXuoXzUCvaeRt92 +7Iw== X-Gm-Message-State: AOAM532rstFRCGHetk4swuGzyiKjkL0EuR1MMdoAe/ZUHKJEQ1TTPoKg H8OAuXaWCkum29XsxgDausRpLo9YzJrL4Q== X-Google-Smtp-Source: ABdhPJymXgVw7bvrxAfve55Bwo92vknG82X9w8GVNvFsAzf6PluFu4f0kJpIvYAz2bpFbiWcTXX1xw== X-Received: by 2002:a5d:6a4d:: with SMTP id t13mr58772292wrw.104.1638966874305; Wed, 08 Dec 2021 04:34:34 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id d8sm2540692wrm.76.2021.12.08.04.34.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 04:34:33 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , John Cai , Sergey Organov , Jiang Xin , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= Subject: [PATCH v4 09/10] object-name.c: don't have GET_OID_ONLY_TO_DIE imply *_QUIETLY Date: Wed, 8 Dec 2021 13:34:22 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.926.g895e15e0c0c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Stop having GET_OID_ONLY_TO_DIE imply GET_OID_QUIETLY in get_oid_with_context_1(). The *_DIE flag was added in 33bd598c390 (sha1_name.c: teach lookup context to get_sha1_with_context(), 2012-07-02), and then later tweaked in 7243ffdd78d (get_sha1: avoid repeating ourselves via ONLY_TO_DIE, 2016-09-26). Everything in that commit makes sense, but only for callers that expect to fail in an initial call to get_oid_with_context_1(), e.g. as "git show 0017" does via handle_revision_arg(), and then would like to call get_oid_with_context_1() again via this maybe_die_on_misspelt_object_name() function. In the subsequent commit we'll add a new caller that expects to call this only once, but who would still like to have all the error messaging that GET_OID_ONLY_TO_DIE gives it, in addition to any regular errors. Signed-off-by: Ævar Arnfjörð Bjarmason --- object-name.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/object-name.c b/object-name.c index fdff4601b2c..d44a8f3a7ca 100644 --- a/object-name.c +++ b/object-name.c @@ -1795,9 +1795,6 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo, const char *cp; int only_to_die = flags & GET_OID_ONLY_TO_DIE; - if (only_to_die) - flags |= GET_OID_QUIETLY; - memset(oc, 0, sizeof(*oc)); oc->mode = S_IFINVALID; strbuf_init(&oc->symlink_path, 0); @@ -1932,7 +1929,7 @@ void maybe_die_on_misspelt_object_name(struct repository *r, { struct object_context oc; struct object_id oid; - get_oid_with_context_1(r, name, GET_OID_ONLY_TO_DIE, + get_oid_with_context_1(r, name, GET_OID_ONLY_TO_DIE | GET_OID_QUIETLY, prefix, &oid, &oc); } From patchwork Wed Dec 8 12:34:23 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: 12664349 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 A060BC433FE for ; Wed, 8 Dec 2021 12:34:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233630AbhLHMiQ (ORCPT ); Wed, 8 Dec 2021 07:38:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233533AbhLHMiJ (ORCPT ); Wed, 8 Dec 2021 07:38:09 -0500 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC99EC0698C8 for ; Wed, 8 Dec 2021 04:34:36 -0800 (PST) Received: by mail-wm1-x32a.google.com with SMTP id j140-20020a1c2392000000b003399ae48f58so4036800wmj.5 for ; Wed, 08 Dec 2021 04:34:36 -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=8erU62gnY9z8FpXa21+qVl7ToTIe6xEOPOzmYkn0RQQ=; b=gD/WO9HP2u6HoAUCRbsHKp3GUzx0SsXdRBh7oPsKxS783R7UEE73pZB5+t8VZFliMg c2JhRbmRSIJZTen/q2wKwuiJAw3LfvfakChtC+CCv/rAgbnVdK63t1xFOwy696w+sY7G Tqs/7m+u3ZwS6lRTSxziPO1M5JBetABAercl8BbSnQJg1WNojkPMCJLhW0T9zvNsOXiA +igRTl6pjWlYX/F2LonaK+IhIldrlUd+NiqZCsKSr3lAG39U2eCugoRt7sOm6/CLQgBO izyz9Wq+ENv5jzrEBF5KrzCIBwWkAu8JIXTBEB6cyKxgafZm11xbwKDenX2R+a3uQVmI 3rvw== 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=8erU62gnY9z8FpXa21+qVl7ToTIe6xEOPOzmYkn0RQQ=; b=QjfPLRaVCEXLGlwIVn5NFeYvVu+F+LNiQtEgSOX5jJ5FONUN2cNBaK5H6Zgx1Z4oYm unG0+cIoD5eFVxYl6+4WtpASn0Ng153ultTLSGNNNW9Qg3x91pYC8FO1SPlo4RUstrue OE3IAziR04u1Lv6r4zGYNm6WUSqcWyRwXZG2k/95QLQQP3M33X/Eh6/2FgT5eaA9GW+0 0lthZuCnSjMPsIfHlXjWjhHFKKeRSsVfCHdwaxUD0mePVO60lA/OCFP4usCza337k9Jp GPlnPfJsYZBBxErR30yu0y5B9PMHYXaygulVj0sM6Ca9gySyigBNkXO/6B0OlEl3Qprb 8HGw== X-Gm-Message-State: AOAM533VFtQjj/4TZtUOlJBH64QUJZ6myuJS6/L1wqkFNv1eKPiFaBsG jZRilfDaZ5uarKiGTdS83d5i1PeGLd5ZNw== X-Google-Smtp-Source: ABdhPJyBCLCQDBwfq1Vkj7zKAu6ZYQP6ngE7DA94KlJc+a3Xf5D5yCt9/P20lGwfo87F9964IlKEzQ== X-Received: by 2002:a05:600c:1549:: with SMTP id f9mr15940791wmg.118.1638966875094; Wed, 08 Dec 2021 04:34:35 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id d8sm2540692wrm.76.2021.12.08.04.34.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 04:34:34 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , John Cai , Sergey Organov , Jiang Xin , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= Subject: [PATCH v4 10/10] cat-file: use GET_OID_ONLY_TO_DIE in --(textconv|filters) Date: Wed, 8 Dec 2021 13:34:23 +0100 Message-Id: X-Mailer: git-send-email 2.34.1.926.g895e15e0c0c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the cat_one_file() logic that calls get_oid_with_context() under --textconv and --filters to use the GET_OID_ONLY_TO_DIE flag, thus improving the error messaging emitted when e.g. is missing but is not. To service the "cat-file" use-case we need to introduce a new "GET_OID_REQUIRE_PATH" flag, otherwise it would exit early as soon as a valid "HEAD" was resolved, but in the "cat-file" case being changed we always need a valid revision and path. This arguably makes the ":" and ":" use cases worse, as we won't quote the component at the user anymore, but let's just use the existing logic "git log" et al use for now. We can improve the messaging for those cases as a follow-up for all callers. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/cat-file.c | 16 ++++++---------- cache.h | 1 + object-name.c | 3 +++ t/t8007-cat-file-textconv.sh | 8 ++++---- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index e71519739a4..f5437c2d045 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -73,14 +73,17 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, struct object_info oi = OBJECT_INFO_INIT; struct strbuf sb = STRBUF_INIT; unsigned flags = OBJECT_INFO_LOOKUP_REPLACE; + unsigned get_oid_flags = GET_OID_RECORD_PATH | GET_OID_ONLY_TO_DIE; const char *path = force_path; + const int opt_cw = (opt == 'c' || opt == 'w'); + if (!path && opt_cw) + get_oid_flags |= GET_OID_REQUIRE_PATH; if (unknown_type) flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE; - if (get_oid_with_context(the_repository, obj_name, - GET_OID_RECORD_PATH, - &oid, &obj_context)) + if (get_oid_with_context(the_repository, obj_name, get_oid_flags, &oid, + &obj_context)) die("Not a valid object name %s", obj_name); if (!path) @@ -112,9 +115,6 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, return !has_object_file(&oid); case 'w': - if (!path) - die("git cat-file --filters %s: must be " - "", obj_name); if (filter_object(path, obj_context.mode, &oid, &buf, &size)) @@ -122,10 +122,6 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, break; case 'c': - if (!path) - die("git cat-file --textconv %s: must be ", - obj_name); - if (textconv_object(the_repository, path, obj_context.mode, &oid, 1, &buf, &size)) break; diff --git a/cache.h b/cache.h index eba12487b99..788127a9869 100644 --- a/cache.h +++ b/cache.h @@ -1366,6 +1366,7 @@ struct object_context { #define GET_OID_FOLLOW_SYMLINKS 0100 #define GET_OID_RECORD_PATH 0200 #define GET_OID_ONLY_TO_DIE 04000 +#define GET_OID_REQUIRE_PATH 010000 #define GET_OID_DISAMBIGUATORS \ (GET_OID_COMMIT | GET_OID_COMMITTISH | \ diff --git a/object-name.c b/object-name.c index d44a8f3a7ca..92862eeb1ac 100644 --- a/object-name.c +++ b/object-name.c @@ -1799,6 +1799,9 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo, oc->mode = S_IFINVALID; strbuf_init(&oc->symlink_path, 0); ret = get_oid_1(repo, name, namelen, oid, flags); + if (!ret && flags & GET_OID_REQUIRE_PATH) + die(_(": required, only '%s' given"), + name); if (!ret) return ret; /* diff --git a/t/t8007-cat-file-textconv.sh b/t/t8007-cat-file-textconv.sh index 71ea2ac987e..b067983ba1c 100755 --- a/t/t8007-cat-file-textconv.sh +++ b/t/t8007-cat-file-textconv.sh @@ -29,7 +29,7 @@ test_expect_success 'usage: ' ' test_expect_success 'usage: :' ' cat >expect <<-\EOF && - fatal: Not a valid object name HEAD2:two.bin + fatal: invalid object name '\''HEAD2'\''. EOF test_must_fail git cat-file --textconv HEAD2:two.bin 2>actual && test_cmp expect actual @@ -37,7 +37,7 @@ test_expect_success 'usage: :' ' test_expect_success 'usage: :' ' cat >expect <<-\EOF && - fatal: Not a valid object name HEAD:two.bin + fatal: path '\''two.bin'\'' does not exist in '\''HEAD'\'' EOF test_must_fail git cat-file --textconv HEAD:two.bin 2>actual && test_cmp expect actual @@ -46,7 +46,7 @@ test_expect_success 'usage: :' ' test_expect_success 'usage: with no ' ' cat >expect <<-\EOF && - fatal: git cat-file --textconv HEAD: must be + fatal: : required, only '\''HEAD'\'' given EOF test_must_fail git cat-file --textconv HEAD 2>actual && test_cmp expect actual @@ -55,7 +55,7 @@ test_expect_success 'usage: with no ' ' test_expect_success 'usage: :' ' cat >expect <<-\EOF && - fatal: Not a valid object name HEAD2:one.bin + fatal: invalid object name '\''HEAD2'\''. EOF test_must_fail git cat-file --textconv HEAD2:one.bin 2>actual && test_cmp expect actual