From: Junio C Hamano <gitster@pobox.com>
To: Erik Cervin Edin <erik@cervined.in>
Cc: Git Mailing List <git@vger.kernel.org>
Subject: Re: Bug: Git grep -f reads the filename relative to the repository root
Date: Thu, 12 Oct 2023 10:28:16 -0700 [thread overview]
Message-ID: <xmqqedhzg37z.fsf@gitster.g> (raw)
In-Reply-To: <CA+JQ7M_htKUv5=GRQEUqWmJrQmFQNfZkPjr8n12CU6x0Khr4dw@mail.gmail.com> (Erik Cervin Edin's message of "Thu, 12 Oct 2023 14:38:00 +0200")
Erik Cervin Edin <erik@cervined.in> writes:
> In the Git repository, I ran
>
> echo tig > pattern-file &&
> echo git > xdiff/pattern-file &&
> cd xdfiff &&
> git grep -f pattern-file
>
> What did you expect to happen? (Expected behavior)
>
> Git grep -f to read the pattern-file, in the xdiff directory and
> search for lines matching `git` in the xdiff directory.
That does sound like a bug. It should use the original directory as
the base of the relative path computation, similar to the way how
OPT_FILENAME() options are handled.
Perhaps something along this line, but this is not even compile
tested yet.
----- >8 --------- >8 --------- >8 --------- >8 -----
Subject: [PATCH] grep: -f <path> is relative to $cwd
Just like OPT_FILENAME() does, "git grep -f <path>" should treat
the <path> relative to the original $cwd by paying attention to the
prefix the command is given.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
builtin/grep.c | 13 +++++++++++--
t/t7810-grep.sh | 13 +++++++++++++
2 files changed, 24 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;
}
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 39d6d713ec..91ac66935f 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -808,6 +808,19 @@ test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
test_cmp expected actual
'
+test_expect_success 'grep -f, use cwd relative file' '
+ test_when_finished "git rm -f sub/dir/file" &&
+ mkdir -p sub/dir &&
+ echo hit >sub/dir/file &&
+ git add sub/dir/file &&
+ echo hit >sub/dir/pattern &&
+ echo miss >pattern &&
+ (
+ cd sub/dir && git grep -f pattern file
+ ) &&
+ git -C sub/dir grep -f pattern file
+'
+
cat >expected <<EOF
y:y yy
--
--
2.42.0-345-gaab89be2eb
next prev parent reply other threads:[~2023-10-12 17:35 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-12 12:38 Bug: Git grep -f reads the filename relative to the repository root Erik Cervin Edin
2023-10-12 17:28 ` Junio C Hamano [this message]
2023-10-30 21:58 ` Taylor Blau
2023-10-31 0:49 ` 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=xmqqedhzg37z.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=erik@cervined.in \
--cc=git@vger.kernel.org \
/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).