From: René Scharfe Date: Sat, 19 Mar 2011 18:33:15 +0000 (+0100) Subject: grep: read patterns from stdin with -f - X-Git-Tag: v1.7.5-rc0~62 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=c41dd2fd7d3a16e6f0b1629d688bee3240db496c;p=git.git grep: read patterns from stdin with -f - 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 Signed-off-by: Junio C Hamano --- diff --git a/builtin/grep.c b/builtin/grep.c index eaf8560a5..0bf8c0116 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 8a7788dc3..dbc6cd8a7 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 - actual && + test_cmp expected actual +' + cat >expected <