diff options
author | Junio C Hamano <gitster@pobox.com> | 2023-11-07 04:26:43 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2023-11-07 04:26:43 +0300 |
commit | 2d2cd0a1bc9ebe9b8df6d7686ad9264b2d7f0f5e (patch) | |
tree | 697b319fa4bea6b47562eff2db38abed71522376 /builtin/grep.c | |
parent | e6bb35d996072ce042700e1fe807a0d04e16f2f8 (diff) | |
parent | 5b2424b658701c16e4fe12babf83fdb15d0f165a (diff) |
Merge branch 'jc/grep-f-relative-to-cwd'
"cd sub && git grep -f patterns" tried to read "patterns" file at
the top level of the working tree; it has been corrected to read
"sub/patterns" instead.
* jc/grep-f-relative-to-cwd:
grep: -f <path> is relative to $cwd
Diffstat (limited to 'builtin/grep.c')
-rw-r--r-- | builtin/grep.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/builtin/grep.c b/builtin/grep.c index b71222330a..fe78d4c98b 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -4,6 +4,7 @@ * Copyright (c) 2006 Junio C Hamano */ #include "builtin.h" +#include "abspath.h" #include "gettext.h" #include "hex.h" #include "repository.h" @@ -812,14 +813,20 @@ static int file_callback(const struct option *opt, const char *arg, int unset) { struct grep_opt *grep_opt = opt->value; int from_stdin; + const char *filename = arg; FILE *patterns; int lno = 0; struct strbuf sb = STRBUF_INIT; BUG_ON_OPT_NEG(unset); - from_stdin = !strcmp(arg, "-"); - patterns = from_stdin ? stdin : fopen(arg, "r"); + if (!*filename) + ; /* leave it as-is */ + else + filename = prefix_filename_except_for_dash(grep_prefix, filename); + + from_stdin = !strcmp(filename, "-"); + patterns = from_stdin ? stdin : fopen(filename, "r"); if (!patterns) die_errno(_("cannot open '%s'"), arg); while (strbuf_getline(&sb, patterns) == 0) { @@ -833,6 +840,8 @@ static int file_callback(const struct option *opt, const char *arg, int unset) if (!from_stdin) fclose(patterns); strbuf_release(&sb); + if (filename != arg) + free((void *)filename); return 0; } |