{"thread":{"id":"60351","subject":"Bug: Git grep -f reads the filename relative to the repository root","startedAt":"2023-10-12T12:38:42Z","lastAt":"2023-10-31T00:49:14Z","messageCount":4,"participants":["Erik Cervin Edin","Junio C Hamano","Taylor Blau"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"483147","messageId":"CA+JQ7M_htKUv5=GRQEUqWmJrQmFQNfZkPjr8n12CU6x0Khr4dw@mail.gmail.com","threadId":"60351","inReplyTo":null,"subject":"Bug: Git grep -f reads the filename relative to the repository root","fromName":"Erik Cervin Edin","fromEmail":"erik@cervined.in","sentAt":"2023-10-12T12:38:00Z","receivedAt":"2023-10-12T12:38:42Z","isPatch":false,"sender":{"key":"erik@cervined.in","avatar":null},"body":"Thank you for filling out a Git bug report!\nPlease answer the following questions to help us understand your issue.\n\nWhat did you do before the bug happened? (Steps to reproduce your issue)\n\nIn the Git repository, I ran\n\n    echo tig > pattern-file &&\n        echo git > xdiff/pattern-file &&\n        cd xdfiff &&\n        git grep -f pattern-file\n\nWhat did you expect to happen? (Expected behavior)\n\nGit grep -f to read the pattern-file, in the xdiff directory and\nsearch for lines matching `git` in the xdiff directory.\n\nWhat happened instead? (Actual behavior)\n\nGit grep -f reads the filename, relative to the Git root and searches\nfor lines matching `tig` in the xdiff directory.\n\nWhat's different between what you expected and what actually happened?\n\nThe file that Git grep uses for patterns is read relative to the root\nof the Git repository, and not the current directory.\n\nAnything else you want to add:\n\nPlease review the rest of the bug report below.\nYou can delete any lines you don't wish to share.\n\n\n[System Info]\ngit version:\ngit version 2.42.0\ncpu: x86_64\nno commit associated with this build\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nuname: Linux 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27\n02:56:13 UTC 2023 x86_64\ncompiler info: gnuc: 11.4\nlibc info: glibc: 2.35\n$SHELL (typically, interactive shell): /bin/bash\n"},{"id":"483162","messageId":"xmqqedhzg37z.fsf@gitster.g","threadId":"60351","inReplyTo":"CA+JQ7M_htKUv5=GRQEUqWmJrQmFQNfZkPjr8n12CU6x0Khr4dw@mail.gmail.com","subject":"Re: Bug: Git grep -f reads the filename relative to the repository root","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2023-10-12T17:28:16Z","receivedAt":"2023-10-12T17:35:23Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Erik Cervin Edin <erik@cervined.in> writes:\n\n> In the Git repository, I ran\n>\n>     echo tig > pattern-file &&\n>         echo git > xdiff/pattern-file &&\n>         cd xdfiff &&\n>         git grep -f pattern-file\n>\n> What did you expect to happen? (Expected behavior)\n>\n> Git grep -f to read the pattern-file, in the xdiff directory and\n> search for lines matching `git` in the xdiff directory.\n\nThat does sound like a bug.  It should use the original directory as\nthe base of the relative path computation, similar to the way how\nOPT_FILENAME() options are handled.\n\nPerhaps something along this line, but this is not even compile\ntested yet.\n\n----- >8 --------- >8 --------- >8 --------- >8 -----\nSubject: [PATCH] grep: -f <path> is relative to $cwd\n\nJust like OPT_FILENAME() does, \"git grep -f <path>\" should treat\nthe <path> relative to the original $cwd by paying attention to the\nprefix the command is given.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/grep.c  | 13 +++++++++++--\n t/t7810-grep.sh | 13 +++++++++++++\n 2 files changed, 24 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex b71222330a..fe78d4c98b 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -4,6 +4,7 @@\n  * Copyright (c) 2006 Junio C Hamano\n  */\n #include \"builtin.h\"\n+#include \"abspath.h\"\n #include \"gettext.h\"\n #include \"hex.h\"\n #include \"repository.h\"\n@@ -812,14 +813,20 @@ static int file_callback(const struct option *opt, const char *arg, int unset)\n {\n \tstruct grep_opt *grep_opt = opt->value;\n \tint from_stdin;\n+\tconst char *filename = arg;\n \tFILE *patterns;\n \tint lno = 0;\n \tstruct strbuf sb = STRBUF_INIT;\n \n \tBUG_ON_OPT_NEG(unset);\n \n-\tfrom_stdin = !strcmp(arg, \"-\");\n-\tpatterns = from_stdin ? stdin : fopen(arg, \"r\");\n+\tif (!*filename)\n+\t\t; /* leave it as-is */\n+\telse\n+\t\tfilename = prefix_filename_except_for_dash(grep_prefix, filename);\n+\n+\tfrom_stdin = !strcmp(filename, \"-\");\n+\tpatterns = from_stdin ? stdin : fopen(filename, \"r\");\n \tif (!patterns)\n \t\tdie_errno(_(\"cannot open '%s'\"), arg);\n \twhile (strbuf_getline(&sb, patterns) == 0) {\n@@ -833,6 +840,8 @@ static int file_callback(const struct option *opt, const char *arg, int unset)\n \tif (!from_stdin)\n \t\tfclose(patterns);\n \tstrbuf_release(&sb);\n+\tif (filename != arg)\n+\t\tfree((void *)filename);\n \treturn 0;\n }\n \ndiff --git a/t/t7810-grep.sh b/t/t7810-grep.sh\nindex 39d6d713ec..91ac66935f 100755\n--- a/t/t7810-grep.sh\n+++ b/t/t7810-grep.sh\n@@ -808,6 +808,19 @@ test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '\n \ttest_cmp expected actual\n '\n \n+test_expect_success 'grep -f, use cwd relative file' '\n+\ttest_when_finished \"git rm -f sub/dir/file\" &&\n+\tmkdir -p sub/dir &&\n+\techo hit >sub/dir/file &&\n+\tgit add sub/dir/file &&\n+\techo hit >sub/dir/pattern &&\n+\techo miss >pattern &&\n+\t(\n+\t\tcd sub/dir && git grep -f pattern file\n+\t) &&\n+\tgit -C sub/dir grep -f pattern file\n+'\n+\n cat >expected <<EOF\n y:y yy\n --\n-- \n2.42.0-345-gaab89be2eb\n\n"},{"id":"484159","messageId":"ZUAnEVk65VQQE263@nand.local","threadId":"60351","inReplyTo":"xmqqedhzg37z.fsf@gitster.g","subject":"Re: Bug: Git grep -f reads the filename relative to the repository root","fromName":"Taylor Blau","fromEmail":"me@ttaylorr.com","sentAt":"2023-10-30T21:58:41Z","receivedAt":"2023-10-30T21:58:46Z","isPatch":false,"sender":{"key":"me@ttaylorr.com","avatar":"https://avatars.githubusercontent.com/u/301000140?v=4"},"body":"On Thu, Oct 12, 2023 at 10:28:16AM -0700, Junio C Hamano wrote:\n> Erik Cervin Edin <erik@cervined.in> writes:\n>\n> > In the Git repository, I ran\n> >\n> >     echo tig > pattern-file &&\n> >         echo git > xdiff/pattern-file &&\n> >         cd xdfiff &&\n> >         git grep -f pattern-file\n> >\n> > What did you expect to happen? (Expected behavior)\n> >\n> > Git grep -f to read the pattern-file, in the xdiff directory and\n> > search for lines matching `git` in the xdiff directory.\n>\n> That does sound like a bug.  It should use the original directory as\n> the base of the relative path computation, similar to the way how\n> OPT_FILENAME() options are handled.\n>\n> Perhaps something along this line, but this is not even compile\n> tested yet.\n\nJust going through old mail that I didn't have a chance to respond to,\nthe proposed patch that you included here does compile and pass t7810\nfor me, and the fix looks reasonable as-is. I don't think I see this\npatch on master, but would have no objections to you merging it down.\n\nThanks,\nTaylor\n"},{"id":"484172","messageId":"xmqq4ji77h15.fsf@gitster.g","threadId":"60351","inReplyTo":"ZUAnEVk65VQQE263@nand.local","subject":"Re: Bug: Git grep -f reads the filename relative to the repository root","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2023-10-31T00:49:10Z","receivedAt":"2023-10-31T00:49:14Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Taylor Blau <me@ttaylorr.com> writes:\n\n> Just going through old mail that I didn't have a chance to respond to,\n> the proposed patch that you included here does compile and pass t7810\n> for me, and the fix looks reasonable as-is. I don't think I see this\n> patch on master, but would have no objections to you merging it down.\n\nThanks.\n"}]}