From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-3.7 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by dcvr.yhbt.net (Postfix) with ESMTP id 087AB1F601 for ; Thu, 15 Dec 2022 08:53:34 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="do7RQQqo"; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229793AbiLOIxX (ORCPT ); Thu, 15 Dec 2022 03:53:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229714AbiLOIw5 (ORCPT ); Thu, 15 Dec 2022 03:52:57 -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 A458A40448 for ; Thu, 15 Dec 2022 00:52:38 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id m14so2286930wrh.7 for ; Thu, 15 Dec 2022 00:52:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bsH0VOQ3xWDuA3bBjbiD1H28OOmyN+4twPZjDwMPgKE=; b=do7RQQqoVjib7z1JuCgyqWKzPvNAeBVBPnptT+diNMGFXAqH7SqaenL71PAhKgRxHZ Sq5iYwLLpq3pNwfGxUgHzs2AUzMuUOA+JxmgLdyZxv7fiHLMRXji1jTiA5wglaijQuza EPgXAeopl6otDC32YlcmBZD79CIyMKMr5EFH2ydUgB1O9cMLStaJXiRoNoQuxy+sR0UR psKUITDMFfHn68ZVZt4xUPrpaa90TuHFAf0Lm3rDXc77KQx8uB9sHLKBQYyPoJO4r4XD TQzbYyrGjYxkKD3tPIuPpMHCKG8hmJucemLDNiwPbeD+/e3SMFXXQpUed2bTv35ebevE SdGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bsH0VOQ3xWDuA3bBjbiD1H28OOmyN+4twPZjDwMPgKE=; b=CtxJJIyhVaDKjztnxw9Zp0avRkEtSXH+iGCcAd0rHdS2Iundl/PW0efE20zAgzLKPM FudQ1dw35U5MmK2B4+sm4FTK/8SBJce0Gf+j+R4vNBwnmEvFmgj4cQzzogfGNp0qInVM JppnwiN9ireWPS9nmxcrn37X7pBj+2c+o9z3Ad7cqjbD1PiyWUB9UaF+HdueQJ5PKVAZ MBEv7/GQXriZX7Dm2SIsJ9mhTgjMC9BNNJUuo/p1BS5svIclUXDU+X8X0VezQ2VDtEIs mgtL5LHS5yDG2GQjZFCWCKYJec52vvu19rrF4EOxRgtPl8UbRfVSckMkpGovMFrnUuZE 5g/g== X-Gm-Message-State: ANoB5pkvpKShlFB/WrFB+2EzrCqQjPfk4c9jWjTM7MzP0yIv/geAUB7w xYgqZe+H8IWsTbRRhmWhOrJ0bnZR/jZZtg== X-Google-Smtp-Source: AA0mqf6EL0asOxSdqPbkoAA3SjCsldeB8Cyi0OGEe9oHrEwyW3k/QeBUH7qlEXYdUrR9hTSQOQNcSw== X-Received: by 2002:adf:f0c3:0:b0:242:12cd:d73b with SMTP id x3-20020adff0c3000000b0024212cdd73bmr17557402wro.33.1671094356404; Thu, 15 Dec 2022 00:52:36 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id r10-20020a0560001b8a00b0024274a5db0asm5464905wru.2.2022.12.15.00.52.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Dec 2022 00:52:35 -0800 (PST) From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= To: git@vger.kernel.org Cc: Taylor Blau , Junio C Hamano , Alban Gruin , Phillip Wood , Elijah Newren , Johannes Schindelin , =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Subject: [PATCH v10 12/12] merge-index: make the argument parsing sensible & simpler Date: Thu, 15 Dec 2022 09:52:16 +0100 Message-Id: X-Mailer: git-send-email 2.39.0.rc2.1048.g0e5493b8d5b In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In a preceding commit when we migrated to parse_options() we took pains to be bug-for-bug compatible with the existing command-line interface, if possible. I.e. we forbade forms like: git merge-index -a git merge-index -a But allowed: git merge-index -a git merge-index -a As the "-a" argument was considered be provided for the "", but not a part of "". We don't really need this strictness, as we don't have two "-a" options. It's much simpler to implement a schema where the first non-option argument is the , and the rest are the "...". We only allow that rest if the "-a" option isn't supplied. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/merge-index.c | 28 ++++++++-------------------- t/t6060-merge-index.sh | 12 +++++++++--- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/builtin/merge-index.c b/builtin/merge-index.c index d679272391b..d8b62e4f663 100644 --- a/builtin/merge-index.c +++ b/builtin/merge-index.c @@ -59,21 +59,14 @@ int cmd_merge_index(int argc, const char **argv, const char *prefix) N_("git merge-index [-o] [-q] (-a | ([--] ...))"), NULL }; -#define OPT__MERGE_INDEX_ALL(v) \ - OPT_BOOL('a', NULL, (v), \ - N_("merge all files in the index that need merging")) struct option options[] = { OPT_BOOL('o', NULL, &one_shot, N_("don't stop at the first failed merge")), OPT__QUIET(&quiet, N_("be quiet")), - OPT__MERGE_INDEX_ALL(&all), /* include "-a" to show it in "-bh" */ + OPT_BOOL('a', NULL, &all, + N_("merge all files in the index that need merging")), OPT_END(), }; - struct option options_prog[] = { - OPT__MERGE_INDEX_ALL(&all), - OPT_END(), - }; -#undef OPT__MERGE_INDEX_ALL struct mofs_data data = { 0 }; /* Without this we cannot rely on waitpid() to tell @@ -81,20 +74,15 @@ int cmd_merge_index(int argc, const char **argv, const char *prefix) */ signal(SIGCHLD, SIG_DFL); - if (argc < 3) - usage_with_options(usage, options); - - /* Option parsing without options */ - argc = parse_options(argc, argv, prefix, options, usage, - PARSE_OPT_STOP_AT_NON_OPTION); - if (all) - usage_msg_optf(_("'%s' option can only be provided after ''"), - usage, options, "-a"); - /* and its options */ + argc = parse_options(argc, argv, prefix, options, usage, 0); if (!argc) usage_msg_opt(_("need a argument"), usage, options); data.program = argv[0]; - argc = parse_options(argc, argv, prefix, options_prog, usage, 0); + argv++; + argc--; + if (!argc && !all) + usage_msg_opt(_("need '-a' or '...'"), + usage, options); if (argc && all) usage_msg_opt(_("'-a' and '...' are mutually exclusive"), usage, options); diff --git a/t/t6060-merge-index.sh b/t/t6060-merge-index.sh index bc201a69552..4ff9ace7f73 100755 --- a/t/t6060-merge-index.sh +++ b/t/t6060-merge-index.sh @@ -22,7 +22,7 @@ test_expect_success 'usage: 2 arguments' ' test_expect_success 'usage: -a before ' ' cat >expect <<-\EOF && - fatal: '\''-a'\'' option can only be provided after '\'''\'' + fatal: '\''-a'\'' and '\''...'\'' are mutually exclusive EOF test_expect_code 129 git merge-index -a b program >out 2>actual.raw && grep "^fatal:" actual.raw >actual && @@ -34,7 +34,7 @@ for opt in -q -o do test_expect_success "usage: $opt after -a" ' cat >expect <<-EOF && - fatal: '\''-a'\'' option can only be provided after '\'''\'' + fatal: need a argument EOF test_expect_code 129 git merge-index -a $opt >out 2>actual.raw && grep "^fatal:" actual.raw >actual && @@ -43,7 +43,13 @@ do ' test_expect_success "usage: $opt program" ' - test_expect_code 0 git merge-index $opt program + cat >expect <<-EOF && + fatal: need '\''-a'\'' or '\''...'\'' + EOF + test_expect_code 129 git merge-index $opt program 2>actual.raw && + grep "^fatal:" actual.raw >actual && + test_must_be_empty out && + test_cmp expect actual ' done -- 2.39.0.rc2.1048.g0e5493b8d5b