Skip to content

Commit e7c2466

Browse files
committed
Merge branch 'jk/grep-double-dash'
* jk/grep-double-dash: accept "git grep -- pattern"
2 parents 318721e + 1123c67 commit e7c2466

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)