@matheusbsbs / matheus-bagliano / commits / 1123c67cee
Fork 0

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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 6, 2010 at 23:44 UTC 1123c67ceee2f310b08ab5d67b076ef04ab59bfc
2 files changed +43
builtin-grep.c
+10
@@ -861,6 +861,16 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
861 PARSE_OPT_STOP_AT_NON_OPTION |
862 PARSE_OPT_NO_INTERNAL_HELP);
863
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 +
874 /* First unrecognized non-option token */
875 if (argc > 0 && !opt.pattern_list) {
876 append_grep_pattern(&opt, argv[0], "command line", 0,
t/t7002-grep.sh
+33
@@ -434,4 +434,37 @@ test_expect_success 'grep -Fi' '
434 test_cmp expected actual
435 '
436
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 +
470 test_done