git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "René Scharfe" <rene.scharfe@lsrfire.ath.cx>
To: Git Mailing List <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [PATCH] grep: read patterns from stdin with -f -
Date: Sat, 19 Mar 2011 19:33:15 +0100	[thread overview]
Message-ID: <4D84F6EB.5000009@lsrfire.ath.cx> (raw)

Support the well-know convention of reading standard input instead of a
named file if "-" (dash) is specified.  GNU grep does the same.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
I was trying to look for the string "/pack", but MinGW mangles arguments
that look like paths.  "echo /pack | git grep -f-" was my first try to
circumvent this, as echo's arguments are not mangled.  I ended up with
"git grep '[/]pack'" -- and also this patch.

 builtin/grep.c  |    6 ++++--
 t/t7810-grep.sh |    5 +++++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/builtin/grep.c b/builtin/grep.c
index eaf8560..0bf8c01 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -659,11 +659,12 @@ static int context_callback(const struct option *opt, const char *arg,
 static int file_callback(const struct option *opt, const char *arg, int unset)
 {
 	struct grep_opt *grep_opt = opt->value;
+	int from_stdin = !strcmp(arg, "-");
 	FILE *patterns;
 	int lno = 0;
 	struct strbuf sb = STRBUF_INIT;
 
-	patterns = fopen(arg, "r");
+	patterns = from_stdin ? stdin : fopen(arg, "r");
 	if (!patterns)
 		die_errno("cannot open '%s'", arg);
 	while (strbuf_getline(&sb, patterns, '\n') == 0) {
@@ -677,7 +678,8 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
 		s = strbuf_detach(&sb, &len);
 		append_grep_pat(grep_opt, s, len, arg, ++lno, GREP_PATTERN);
 	}
-	fclose(patterns);
+	if (!from_stdin)
+		fclose(patterns);
 	strbuf_release(&sb);
 	return 0;
 }
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 8a7788d..dbc6cd8 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -303,6 +303,11 @@ test_expect_success 'grep -f, ignore empty lines' '
 	test_cmp expected actual
 '
 
+test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
+	git grep -f - <patterns >actual &&
+	test_cmp expected actual
+'
+
 cat >expected <<EOF
 y:y yy
 --
-- 
1.7.4

                 reply	other threads:[~2011-03-19 18:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=4D84F6EB.5000009@lsrfire.ath.cx \
    --to=rene.scharfe@lsrfire.ath.cx \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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).