Skip to content

Commit 258a618

Browse files
mkiedrowiczgitster
authored andcommitted
git-grep: Bail out when -P is used with -F or -E
This patch makes git-grep die() when -P is used on command line together with -E/--extended-regexp or -F/--fixed-strings. This also makes it bail out when grep.extendedRegexp is enabled. But `git grep -G -P pattern` and `git grep -E -G -P pattern` still work because -G and -E set opts.regflags during parse_options() and there is no way to detect `-G` or `-E -G`. Signed-off-by: Michał Kiedrowicz <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8f852ce commit 258a618

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

builtin/grep.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,9 +925,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
925925

926926
if (!opt.pattern_list)
927927
die(_("no pattern given."));
928+
if (opt.regflags != REG_NEWLINE && opt.pcre)
929+
die(_("cannot mix --extended-regexp and --perl-regexp"));
928930
if (!opt.fixed && opt.ignore_case)
929931
opt.regflags |= REG_ICASE;
930-
if ((opt.regflags != REG_NEWLINE) && opt.fixed)
932+
if ((opt.regflags != REG_NEWLINE || opt.pcre) && opt.fixed)
931933
die(_("cannot mix --fixed-strings and regexp"));
932934

933935
#ifndef NO_PTHREADS

t/t7810-grep.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,4 +637,20 @@ test_expect_success LIBPCRE 'grep -P -w pattern' '
637637
test_cmp expected actual
638638
'
639639

640+
test_expect_success LIBPCRE 'grep -P -F returns error' '
641+
test_expect_code 128 git grep -P -F main
642+
'
643+
644+
test_expect_success LIBPCRE 'grep -P -E returns error' '
645+
test_expect_code 128 git grep -P -E main
646+
'
647+
648+
test_expect_failure LIBPCRE 'grep -P -G returns error' '
649+
test_expect_code 128 git grep -P -G main
650+
'
651+
652+
test_expect_failure LIBPCRE 'grep -P -E -G returns error' '
653+
test_expect_code 128 git grep -P -E -G main
654+
'
655+
640656
test_done

0 commit comments

Comments
 (0)