Skip to content

Commit 031f82f

Browse files
committed
Merge branch 'jk/grep-double-dash' into maint
* jk/grep-double-dash: accept "git grep -- pattern"
2 parents 07cb9a3 + 1123c67 commit 031f82f

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
@@ -868,6 +868,16 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
868868
PARSE_OPT_STOP_AT_NON_OPTION |
869869
PARSE_OPT_NO_INTERNAL_HELP);
870870

871+
/*
872+
* skip a -- separator; we know it cannot be
873+
* separating revisions from pathnames if
874+
* we haven't even had any patterns yet
875+
*/
876+
if (argc > 0 && !opt.pattern_list && !strcmp(argv[0], "--")) {
877+
argv++;
878+
argc--;
879+
}
880+
871881
/* First unrecognized non-option token */
872882
if (argc > 0 && !opt.pattern_list) {
873883
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)