Subject: Re: BUG: git grep behave oddly with alternatives

From: Junio C Hamano <[email-masked]>
Date: Sun, 08 Jan 2023 10:27:58 +0900

René Scharfe <[email-masked]> writes:

> diff --git a/Makefile b/Makefile
> index db447d0738..46e30be673 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -289,6 +289,10 @@ include shared.mak
>  # Define NO_REGEX if your C library lacks regex support with REG_STARTEND
>  # feature.
>  #
> +# Define USE_ENHANCED_BASIC_REGULAR_EXPRESSIONS if your C library provides
> +# the flag REG_ENHANCED and you'd like to use it to enable enhanced basic
> +# regular expressions.
> +#

I wondered if we should mention macOS somewhere in the description
to help those users that may be affeced, but it seems that looking
for "REG_ENHANCED BSD" with a search engine finds pages that
indicate this is available on BSD's in general?

> @@ -2037,6 +2041,11 @@ endif
>  ifdef NO_REGEX
>  	COMPAT_CFLAGS += -Icompat/regex
>  	COMPAT_OBJS += compat/regex/regex.o
> +else
> +ifdef USE_ENHANCED_BASIC_REGULAR_EXPRESSIONS
> +	COMPAT_CFLAGS += -DUSE_ENHANCED_BASIC_REGULAR_EXPRESSIONS
> +	COMPAT_OBJS += compat/regcomp_enhanced.o
> +endif

OK.

> diff --git a/compat/regcomp_enhanced.c b/compat/regcomp_enhanced.c
> new file mode 100644
> index 0000000000..84193ce53b
> --- /dev/null
> +++ b/compat/regcomp_enhanced.c
> @@ -0,0 +1,9 @@
> +#include "../git-compat-util.h"
> +#undef regcomp
> +int git_regcomp(regex_t *preg, const char *pattern, int cflags)
> +{
> +	if (!(cflags & REG_EXTENDED))
> +		cflags |= REG_ENHANCED;
> +	return regcomp(preg, pattern, cflags);
> +}

OK.  I like the "we only want to affect BRE" bit, that is carefully
done.

> diff --git a/config.mak.uname b/config.mak.uname
> index d63629fe80..7d25995265 100644
> --- a/config.mak.uname
> +++ b/config.mak.uname
> @@ -147,6 +147,7 @@ ifeq ($(uname_S),Darwin)
>  	FREAD_READS_DIRECTORIES = UnfortunatelyYes
>  	HAVE_NS_GET_EXECUTABLE_PATH = YesPlease
>  	CSPRNG_METHOD = arc4random
> +	USE_ENHANCED_BASIC_REGULAR_EXPRESSIONS = YesPlease

OK.  This would give macOS folks who have already been using the
enhanced mode (without us asking) with their older libraries the
behaviour they are more familiar with.  Good.

> diff --git a/git-compat-util.h b/git-compat-util.h
> index 76e4b11131..1efa834089 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -1338,6 +1338,11 @@ static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size,
>  	return regexec(preg, buf, nmatch, pmatch, eflags | REG_STARTEND);
>  }
>
> +#ifdef USE_ENHANCED_BASIC_REGULAR_EXPRESSIONS
> +int git_regcomp(regex_t *preg, const char *pattern, int cflags);
> +#define regcomp git_regcomp
> +#endif

OK.