git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Ignacy Gawedzki" <ignacy.gawedzki@green-communications.fr>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [PATCH 3/3] grep: pass PARSE_OPT_NO_INTERNAL_HELP, fix odd -h handling
Date: Fri, 24 Sep 2021 18:51:46 +0200	[thread overview]
Message-ID: <patch-3.3-996f3dae2a7-20210924T164820Z-avarab@gmail.com> (raw)
In-Reply-To: <cover-0.3-00000000000-20210924T164820Z-avarab@gmail.com>

The "grep" command supports both "-h" and "-H" options, along with a
mandatory pattern, but this has been partially usurped by the "-h"
handling in parse_options().

The reason it's just been "odd" instead of a bug is that we'll only
print out "-h" usage with parse_options() if there's no further
non-option arguments, so instead of printing this brief blurb on a
stand-alone -h we'd print out the full usage:

    $ git grep -H
    fatal: no pattern given

But for the aforementioned reason a "git grep -h <pattern>" would
work, we wouldn't take the !PARSE_OPT_NO_INTERNAL_HELP branch in
parse_options_step(), would handle our own custom 'h' option, and
builtin/grep.c itself would know what to do at that point.

Reported-by: Ignacy Gawedzki <ignacy.gawedzki@green-communications.fr>
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/grep.c  | 3 ++-
 t/t0012-help.sh | 4 +++-
 t/t7810-grep.sh | 4 ++++
 3 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/builtin/grep.c b/builtin/grep.c
index 51278b01fa2..a2d6704aa41 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -981,7 +981,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	 */
 	argc = parse_options(argc, argv, prefix, options, grep_usage,
 			     PARSE_OPT_KEEP_DASHDASH |
-			     PARSE_OPT_STOP_AT_NON_OPTION);
+			     PARSE_OPT_STOP_AT_NON_OPTION |
+			     PARSE_OPT_NO_INTERNAL_HELP);
 	grep_commit_pattern_type(pattern_type_arg, &opt);
 
 	if (use_index && !startup_info->have_repository) {
diff --git a/t/t0012-help.sh b/t/t0012-help.sh
index 913f34c8e9d..0b623fe794a 100755
--- a/t/t0012-help.sh
+++ b/t/t0012-help.sh
@@ -90,7 +90,9 @@ test_expect_success 'git help succeeds without git.html' '
 '
 
 test_expect_success 'generate builtin list' '
-	git --list-cmds=builtins >builtins
+	git --list-cmds=builtins >builtins &&
+	grep -v "^grep$" builtins >builtins+ &&
+	mv builtins+ builtins
 '
 
 while read builtin
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 6b6423a07c3..4c2777120b0 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -11,6 +11,10 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
 . ./test-lib.sh
 
+test_expect_success 'usage' '
+	test_expect_code 128 git grep -h
+'
+
 test_invalid_grep_expression() {
 	params="$@" &&
 	test_expect_success "invalid expression: grep $params" '
-- 
2.33.0.1285.g7aff81f6560


  parent reply	other threads:[~2021-09-24 16:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-24 14:19 Wrong option -h in grep, ls-remote, and show-ref Ignacy Gawedzki
2021-09-24 16:51 ` [PATCH 0/3] show-ref, ls-remote, grep: fix -h handling Ævar Arnfjörð Bjarmason
2021-09-24 16:51   ` [PATCH 1/3] show-ref: remove unused custom handling of -h Ævar Arnfjörð Bjarmason
2021-09-24 19:24     ` Junio C Hamano
2021-09-24 20:53       ` René Scharfe
2021-09-24 21:21         ` Junio C Hamano
2021-09-24 16:51   ` [PATCH 2/3] ls-remote: remove documentation for custom -h option Ævar Arnfjörð Bjarmason
2021-09-24 17:11     ` SZEDER Gábor
2021-09-24 17:54       ` Jeff King
2021-09-24 18:17       ` Junio C Hamano
2021-09-24 16:51   ` Ævar Arnfjörð Bjarmason [this message]
2021-09-24 18:12 ` Wrong option -h in grep, ls-remote, and show-ref Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=patch-3.3-996f3dae2a7-20210924T164820Z-avarab@gmail.com \
    --to=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=ignacy.gawedzki@green-communications.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).