Skip to content

Commit 1123c67

Browse files
peffgitster
authored andcommitted
accept "git grep -- pattern"
Currently the only way to "quote" a grep pattern that might begin with a dash is to use "git grep -e pattern". This works just fine, and is also the way right way to do it on many traditional grep implemenations. Some people prefer to use "git grep -- pattern", however, as "--" is the usual "end of options" marker, and at least GNU grep and Solaris 10 grep support this. This patch makes that syntax work. There is a slight behavior change, in that "git grep -- $X" used to be interpreted as "grep for -- in $X". However, that usage is questionable. "--" is usually the end-of-options marker, so "git grep" was unlike many other greps in treating it as a literal pattern (e.g., both GNU grep and Solaris 10 grep will treat "grep --" as missing a pattern). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c8f6c8 commit 1123c67

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

builtin-grep.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,16 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
861861
PARSE_OPT_STOP_AT_NON_OPTION |
862862
PARSE_OPT_NO_INTERNAL_HELP);
863863

864+
/*
865+
* skip a -- separator; we know it cannot be
866+
* separating revisions from pathnames if
867+
* we haven't even had any patterns yet
868+
*/
869+
if (argc > 0 && !opt.pattern_list && !strcmp(argv[0], "--")) {
870+
argv++;
871+
argc--;
872+
}
873+
864874
/* First unrecognized non-option token */
865875
if (argc > 0 && !opt.pattern_list) {
866876
append_grep_pattern(&opt, argv[0], "command line", 0,

t/t7002-grep.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,37 @@ test_expect_success 'grep -Fi' '
434434
test_cmp expected actual
435435
'
436436

437+
test_expect_success 'setup double-dash tests' '
438+
cat >double-dash <<EOF &&
439+
--
440+
->
441+
other
442+
EOF
443+
git add double-dash
444+
'
445+
446+
cat >expected <<EOF
447+
double-dash:->
448+
EOF
449+
test_expect_success 'grep -- pattern' '
450+
git grep -- "->" >actual &&
451+
test_cmp expected actual
452+
'
453+
test_expect_success 'grep -- pattern -- pathspec' '
454+
git grep -- "->" -- double-dash >actual &&
455+
test_cmp expected actual
456+
'
457+
test_expect_success 'grep -e pattern -- path' '
458+
git grep -e "->" -- double-dash >actual &&
459+
test_cmp expected actual
460+
'
461+
462+
cat >expected <<EOF
463+
double-dash:--
464+
EOF
465+
test_expect_success 'grep -e -- -- path' '
466+
git grep -e -- -- double-dash >actual &&
467+
test_cmp expected actual
468+
'
469+
437470
test_done

0 commit comments

Comments
 (0)