Skip to contentSkip to conversation

grep: add a perlRegexp configuration option

PATCH/RFC J Smith 2 participants 7 messages Jun 15, 2016 Documentation

lore
Share & tools
lore

J Smith

author
#0 lore raw

Enables the -P flag for perl regexps by default. When both the perlRegexp and extendedRegexp options are enabled, the last enabled option wins.

---
 Documentation/config.txt   |  6 ++++++
 Documentation/git-grep.txt |  6 ++++++
 builtin/grep.c             | 17 +++++++++++++++--
 t/t7810-grep.sh            | 34 ++++++++++++++++++++++++++++++++++
 4 files changed, 61 insertions(+), 2 deletions(-)
Documentation/config.txt +6 -0
index a95e5a4..ff3019b 100644
@@ -1213,6 +1213,12 @@ grep.lineNumber::
 grep.extendedRegexp::
 	If set to true, enable '--extended-regexp' option by default.
 
+grep.perlRegexp::
+	If set to true, enable '--perl-regexp' option by default.
+
+When both the 'grep.extendedRegexp' and 'grep.perlRegexp' options
+are used, the last enabled option wins.
+
 gpg.program::
 	Use this custom program instead of "gpg" found on $PATH when
 	making or verifying a PGP signature. The program must support the
Documentation/git-grep.txt +6 -0
index 3bec036..8816968 100644
@@ -45,6 +45,12 @@ grep.lineNumber::
 grep.extendedRegexp::
 	If set to true, enable '--extended-regexp' option by default.
 
+grep.perlRegexp::
+	If set to true, enable '--perl-regexp' option by default.
+
+When both the 'grep.extendedRegexp' and 'grep.perlRegexp' options
+are used, the last enabled option wins.
+
 
 OPTIONS
 -------
builtin/grep.c +15 -2
index 29adb0a..b4475e6 100644
@@ -268,11 +268,24 @@ static int grep_config(const char *var, const char *value, void *cb)
 	if (userdiff_config(var, value) < 0)
 		return -1;
 
+	if (!strcmp(var, "grep.perlregexp")) {
+		if (git_config_bool(var, value)) {
+			opt->fixed = 0;
+			opt->pcre = 1;
+		} else {
+			opt->pcre = 0;
+		}
+		return 0;
+	}
+
 	if (!strcmp(var, "grep.extendedregexp")) {
-		if (git_config_bool(var, value))
+		if (git_config_bool(var, value)) {
 			opt->regflags |= REG_EXTENDED;
-		else
+			opt->pcre = 0;
+			opt->fixed = 0;
+		} else {
 			opt->regflags &= ~REG_EXTENDED;
+		}
 		return 0;
 	}
 
t/t7810-grep.sh +34 -0
index 24e9b19..5479dc9 100755
@@ -729,6 +729,40 @@ test_expect_success LIBPCRE 'grep -P pattern' '
 	test_cmp expected actual
 '
 
+test_expect_success LIBPCRE 'grep pattern with grep.perlRegexp=true' '
+	git \
+		-c grep.perlregexp=true \
+		grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success LIBPCRE 'grep pattern with grep.perlRegexp=true and then grep.extendedRegexp=true' '
+	test_must_fail git \
+		-c grep.perlregexp=true \
+		-c grep.extendedregexp=true \
+		grep "\p{Ps}.*?\p{Pe}" hello.c
+'
+
+test_expect_success LIBPCRE 'grep pattern with grep.extendedRegexp=true and then grep.perlRegexp=true' '
+	git \
+		-c grep.extendedregexp=true \
+		-c grep.perlregexp=true \
+		grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success LIBPCRE 'grep -E pattern with grep.perlRegexp=true' '
+	test_must_fail git \
+		-c grep.perlregexp=true \
+		grep -E "\p{Ps}.*?\p{Pe}" hello.c
+'
+
+test_expect_success LIBPCRE 'grep -G pattern with grep.perlRegexp=true' '
+	test_must_fail git \
+		-c grep.perlregexp=true \
+		grep -G "\p{Ps}.*?\p{Pe}" hello.c
+'
+
 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
 	>empty &&
 	test_must_fail git -c grep.extendedregexp=true \

--
1.7.11.3

#1 lore raw
Quoted 3 lines · J Smith

Enables the -P flag for perl regexps by default. When both the perlRegexp and extendedRegexp options are enabled, the last enabled option wins.

Turning "grep.extendedregexp" from boolean to an extended boolean to allow "grep.extendedregexp = perl" might be a better alternative. That way, the user wouldn't have to worry about 7 variants of grep.fooRegexp variables twenty years down the road, even though the set of possible values given to "grep.extendedregexp" may have grown over time by then.

Quoted 119 lines
---
 Documentation/config.txt   |  6 ++++++
 Documentation/git-grep.txt |  6 ++++++
 builtin/grep.c             | 17 +++++++++++++++--
 t/t7810-grep.sh            | 34 ++++++++++++++++++++++++++++++++++
 4 files changed, 61 insertions(+), 2 deletions(-)
Documentation/config.txt +6 -0
index a95e5a4..ff3019b 100644
@@ -1213,6 +1213,12 @@ grep.lineNumber::
 grep.extendedRegexp::
 	If set to true, enable '--extended-regexp' option by default.
 
+grep.perlRegexp::
+	If set to true, enable '--perl-regexp' option by default.
+
+When both the 'grep.extendedRegexp' and 'grep.perlRegexp' options
+are used, the last enabled option wins.
+
 gpg.program::
 	Use this custom program instead of "gpg" found on $PATH when
 	making or verifying a PGP signature. The program must support the
Documentation/git-grep.txt +6 -0
index 3bec036..8816968 100644
@@ -45,6 +45,12 @@ grep.lineNumber::
 grep.extendedRegexp::
 	If set to true, enable '--extended-regexp' option by default.
 
+grep.perlRegexp::
+	If set to true, enable '--perl-regexp' option by default.
+
+When both the 'grep.extendedRegexp' and 'grep.perlRegexp' options
+are used, the last enabled option wins.
+
 
 OPTIONS
 -------
builtin/grep.c +15 -2
index 29adb0a..b4475e6 100644
@@ -268,11 +268,24 @@ static int grep_config(const char *var, const char *value, void *cb)
 	if (userdiff_config(var, value) < 0)
 		return -1;
 
+	if (!strcmp(var, "grep.perlregexp")) {
+		if (git_config_bool(var, value)) {
+			opt->fixed = 0;
+			opt->pcre = 1;
+		} else {
+			opt->pcre = 0;
+		}
+		return 0;
+	}
+
 	if (!strcmp(var, "grep.extendedregexp")) {
-		if (git_config_bool(var, value))
+		if (git_config_bool(var, value)) {
 			opt->regflags |= REG_EXTENDED;
-		else
+			opt->pcre = 0;
+			opt->fixed = 0;
+		} else {
 			opt->regflags &= ~REG_EXTENDED;
+		}
 		return 0;
 	}
 
t/t7810-grep.sh +34 -1
index 24e9b19..5479dc9 100755
@@ -729,6 +729,40 @@ test_expect_success LIBPCRE 'grep -P pattern' '
 	test_cmp expected actual
 '
 
+test_expect_success LIBPCRE 'grep pattern with grep.perlRegexp=true' '
+	git \
+		-c grep.perlregexp=true \
+		grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success LIBPCRE 'grep pattern with grep.perlRegexp=true and then grep.extendedRegexp=true' '
+	test_must_fail git \
+		-c grep.perlregexp=true \
+		-c grep.extendedregexp=true \
+		grep "\p{Ps}.*?\p{Pe}" hello.c
+'
+
+test_expect_success LIBPCRE 'grep pattern with grep.extendedRegexp=true and then grep.perlRegexp=true' '
+	git \
+		-c grep.extendedregexp=true \
+		-c grep.perlregexp=true \
+		grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success LIBPCRE 'grep -E pattern with grep.perlRegexp=true' '
+	test_must_fail git \
+		-c grep.perlregexp=true \
+		grep -E "\p{Ps}.*?\p{Pe}" hello.c
+'
+
+test_expect_success LIBPCRE 'grep -G pattern with grep.perlRegexp=true' '
+	test_must_fail git \
+		-c grep.perlregexp=true \
+		grep -G "\p{Ps}.*?\p{Pe}" hello.c
+'
+
 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
 	>empty &&
 	test_must_fail git -c grep.extendedregexp=true \
--

1.7.11.3

author
#2 lore raw
Quoted 7 lines · Junio C Hamano

Turning "grep.extendedregexp" from boolean to an extended boolean to allow "grep.extendedregexp = perl" might be a better alternative. That way, the user wouldn't have to worry about 7 variants of grep.fooRegexp variables twenty years down the road, even though the set of possible values given to "grep.extendedregexp" may have grown over time by then.

Yeah, that sounds good. I've re-written the patch to accommodate the change allowing for the current boolean settings of true/false as well as "perl". For the sake of completeness (verbosity? pedantry?) I also included a setting for "extended" which is equivalent to true.

With this sort of change, would a more generic "grep.regexpMode", "grep.patternType" or something similar perhaps be more descriptive, with "grep.extendedRegexp" being aliased for backwards compatibility purposes? I could also add that functionality if desired.

#3 lore raw
Quoted 2 lines · J Smith

... For the sake of completeness (verbosity? pedantry?) I also included a setting for "extended" which is equivalent to true.

Good thinking.

Quoted 4 lines

With this sort of change, would a more generic "grep.regexpMode", "grep.patternType" or something similar perhaps be more descriptive, with "grep.extendedRegexp" being aliased for backwards compatibility purposes? I could also add that functionality if desired.

A variable called "extendedRegexp" already reads quite naturally if it can have value to say what kind of extendedness is desired, at least to me. So I do not care too deeply either way.

author
#4 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 2 lines · J Smith

... For the sake of completeness (verbosity? pedantry?) I also included a setting for "extended" which is equivalent to true.

Good thinking.

Quoted 8 lines
Quoted 4 lines

With this sort of change, would a more generic "grep.regexpMode", "grep.patternType" or something similar perhaps be more descriptive, with "grep.extendedRegexp" being aliased for backwards compatibility purposes? I could also add that functionality if desired.

A variable called "extendedRegexp" already reads quite naturally if it can have value to say what kind of extendedness is desired, at least to me. So I do not care too deeply either way.

On the flip side, it might be useful to some to have the option to set the value to "fixed" for the "--fixed-strings" argument, in which case the option becomes less a type of extended regexp and more of a simple search string. Were that to be the case, I think "grep.patternType" would feel the most precise.

I think for completeness at the very least I should work in the "fixed" value as an valid value, option naming aside.

#5 lore raw
Quoted 17 lines · J Smith
Quoted 7 lines · Junio C Hamano
Quoted 2 lines · J Smith

... For the sake of completeness (verbosity? pedantry?) I also included a setting for "extended" which is equivalent to true.

Good thinking.

Quoted 8 lines
Quoted 4 lines

With this sort of change, would a more generic "grep.regexpMode", "grep.patternType" or something similar perhaps be more descriptive, with "grep.extendedRegexp" being aliased for backwards compatibility purposes? I could also add that functionality if desired.

A variable called "extendedRegexp" already reads quite naturally if it can have value to say what kind of extendedness is desired, at least to me. So I do not care too deeply either way.

Quoted 8 lines

On the flip side, it might be useful to some to have the option to set the value to "fixed" for the "--fixed-strings" argument, in which case the option becomes less a type of extended regexp and more of a simple search string. Were that to be the case, I think "grep.patternType" would feel the most precise.

I think for completeness at the very least I should work in the "fixed" value as an valid value, option naming aside.

Ok, then grep.patternType it is.

Thanks.

author
#6 lore raw
Quoted 4 lines · Junio C Hamano

Ok, then grep.patternType it is.

Thanks.

Cool, patches should be on their way. I added options for "basic", "extended", "fixed" and "perl" for completeness along with the name change with a BC alias patch separately for perusal.

Cheers.