summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: af4c62a)
raw | patch | inline | side by side (parent: af4c62a)
author | René Scharfe <rene.scharfe@lsrfire.ath.cx> | |
Sat, 19 Mar 2011 18:33:15 +0000 (19:33 +0100) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Sun, 20 Mar 2011 04:46:52 +0000 (21:46 -0700) |
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
named file if "-" (dash) is specified. GNU grep does the same.
Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/grep.c | patch | blob | history | |
t/t7810-grep.sh | patch | blob | history |
diff --git a/builtin/grep.c b/builtin/grep.c
index eaf8560a520bd8aac4fa62394378b444116d6226..0bf8c0116a6a4c178a2c368f17d4103e91486624 100644 (file)
--- a/builtin/grep.c
+++ b/builtin/grep.c
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) {
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 8a7788dc39f236b15e60912c384d835fd1db5a28..dbc6cd8a775a43efbd8fdf433958ccd090b6e3ab 100755 (executable)
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
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
--