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>maint
parent
43c8a30d15
commit
5b2424b658
|
@ -4,6 +4,7 @@
|
||||||
* Copyright (c) 2006 Junio C Hamano
|
* Copyright (c) 2006 Junio C Hamano
|
||||||
*/
|
*/
|
||||||
#include "builtin.h"
|
#include "builtin.h"
|
||||||
|
#include "abspath.h"
|
||||||
#include "gettext.h"
|
#include "gettext.h"
|
||||||
#include "hex.h"
|
#include "hex.h"
|
||||||
#include "repository.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;
|
struct grep_opt *grep_opt = opt->value;
|
||||||
int from_stdin;
|
int from_stdin;
|
||||||
|
const char *filename = arg;
|
||||||
FILE *patterns;
|
FILE *patterns;
|
||||||
int lno = 0;
|
int lno = 0;
|
||||||
struct strbuf sb = STRBUF_INIT;
|
struct strbuf sb = STRBUF_INIT;
|
||||||
|
|
||||||
BUG_ON_OPT_NEG(unset);
|
BUG_ON_OPT_NEG(unset);
|
||||||
|
|
||||||
from_stdin = !strcmp(arg, "-");
|
if (!*filename)
|
||||||
patterns = from_stdin ? stdin : fopen(arg, "r");
|
; /* 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)
|
if (!patterns)
|
||||||
die_errno(_("cannot open '%s'"), arg);
|
die_errno(_("cannot open '%s'"), arg);
|
||||||
while (strbuf_getline(&sb, patterns) == 0) {
|
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)
|
if (!from_stdin)
|
||||||
fclose(patterns);
|
fclose(patterns);
|
||||||
strbuf_release(&sb);
|
strbuf_release(&sb);
|
||||||
|
if (filename != arg)
|
||||||
|
free((void *)filename);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -808,6 +808,19 @@ test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
|
||||||
test_cmp expected actual
|
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
|
cat >expected <<EOF
|
||||||
y:y yy
|
y:y yy
|
||||||
--
|
--
|
||||||
|
|
Loading…
Reference in New Issue