git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH/RFC] grep: add a perlRegexp configuration option

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 31, 2012, 18:04 UTC
Message-ID
<7vehnrkdrq.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1343753854-66765-1-git-send-email-dark.panda@gmail.com>
J Smith <dark.panda@gmail.com> writes:
> 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.

Show 119 quoted 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(-)
>
> diff --git a/Documentation/config.txt b/Documentation/config.txt
> index a95e5a4..ff3019b 100644
> --- a/Documentation/config.txt
> +++ b/Documentation/config.txt
> @@ -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
> diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
> index 3bec036..8816968 100644
> --- a/Documentation/git-grep.txt
> +++ b/Documentation/git-grep.txt
> @@ -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
>  -------
> diff --git a/builtin/grep.c b/builtin/grep.c
> index 29adb0a..b4475e6 100644
> --- a/builtin/grep.c
> +++ b/builtin/grep.c
> @@ -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;
>  	}
>
> diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
> index 24e9b19..5479dc9 100755
> --- a/t/t7810-grep.sh
> +++ b/t/t7810-grep.sh
> @@ -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
Previous: J SmithNext: J Smith
Message 2 of 7 in “grep: add a perlRegexp configuration option”
  1. grep: add a perlRegexp configuration optionJ Smith, Jul 31, 2012
  2. Junio C HamanoJul 31, 2012
  3. J SmithJul 31, 2012
  4. Junio C HamanoJul 31, 2012
  5. J SmithJul 31, 2012
  6. Junio C HamanoJul 31, 2012
  7. J SmithJul 31, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.