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

Re: git bug: Perl compatible regular expressions do not work as expected

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 27, 2023, 21:33 UTC
Message-ID
<xmqqjzz1nalp.fsf@gitster.g>
In-Reply-To
<03fd7ddb-8241-1a0a-3e82-d8083e4ce0f7@web.de>
René Scharfe <l.s.r@web.de> writes:
Show 6 quoted lines
> 54463d32ef only affects basic regular expressions (BRE), but -G and -S
> use extended ones (ERE).  macOS allows both to be "enhanced".
>
> I have a hard time finding a readable reference to the documentation to
> brings us all onto the same page regarding what REG_ENHANCED actually
> does.  [1] is in raw troff format, [2] isn't very pretty and shows ads.

Wow, [2] is also misleading in that it loses the distinction between '+' and '\+'; in [1] at least you can see the differences between \&+ and \e+ ;-)

> Anyway, the rather lengthy section "ENHANCED FEATURES" explains it.

I suspect that 54463d32ef was done in a conservative way to avoid unintended side effects to make ERE "enhanced". I am not 100% certain, but after reading the documentation you pointed at, I do not see a valid expression without ENHANCED flag starting to mean totally different thing with it (well, an extra '?' turning a pattern from greedy to minimal may count as such a change in semantics, but I do not see anybody sensible adding an extra '?' in a pattern in the first place).

Perhaps it is safe to go ahead and loosen what 54463d32ef did, with something like this (this is not even compile tested, of course)?

 Makefile                  | 9 +++++++++
 compat/regcomp_enhanced.c | 2 ++
 git-compat-util.h         | 2 +-
 3 files changed, 12 insertions(+), 1 deletion(-)
diff --git c/Makefile w/Makefile
index 50ee51fde3..1fe0eb399e 100644
--- c/Makefile
+++ w/Makefile
@@ -293,6 +293,10 @@ include shared.mak
 # the flag REG_ENHANCED and you'd like to use it to enable enhanced basic
 # regular expressions.
 #
+# Define USE_ENHANCED_REGULAR_EXPRESSIONS if your C library provides
+# the flag REG_ENHANCED and you'd like to use it to enable enhanced
+# regular expressions for both BRE and ERE.
+#
 # Define HAVE_DEV_TTY if your system can open /dev/tty to interact with the
 # user.
 #
@@ -2041,11 +2045,16 @@ ifdef NO_REGEX
 	COMPAT_CFLAGS += -Icompat/regex
 	COMPAT_OBJS += compat/regex/regex.o
 else
+ifdef USE_ENHANCED_REGULAR_EXPRESSIONS
+	COMPAT_CFLAGS += -DUSE_ENHANCED_REGULAR_EXPRESSIONS
+	COMPAT_OBJS += compat/regcomp_enhanced.o
+else
 ifdef USE_ENHANCED_BASIC_REGULAR_EXPRESSIONS
 	COMPAT_CFLAGS += -DUSE_ENHANCED_BASIC_REGULAR_EXPRESSIONS
 	COMPAT_OBJS += compat/regcomp_enhanced.o
 endif
 endif
+endif
 ifdef NATIVE_CRLF
 	BASIC_CFLAGS += -DNATIVE_CRLF
 endif
diff --git c/compat/regcomp_enhanced.c w/compat/regcomp_enhanced.c
index 84193ce53b..aadbbac6d3 100644
--- c/compat/regcomp_enhanced.c
+++ w/compat/regcomp_enhanced.c
@@ -3,7 +3,9 @@
 
 int git_regcomp(regex_t *preg, const char *pattern, int cflags)
 {
+#ifndef USE_ENHANCED_REGULAR_EXPRESSIONS
 	if (!(cflags & REG_EXTENDED))
+#endif
 		cflags |= REG_ENHANCED;
 	return regcomp(preg, pattern, cflags);
 }
diff --git c/git-compat-util.h w/git-compat-util.h
index 1e6592624d..95d9b75a15 100644
--- c/git-compat-util.h
+++ w/git-compat-util.h
@@ -1377,7 +1377,7 @@ 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
+#if defined(USE_ENHANCED_BASIC_REGULAR_EXPRESSIONS) || defined(USE_ENHANCED_REGULAR_EXPRESSIONS)
 int git_regcomp(regex_t *preg, const char *pattern, int cflags);
 #define regcomp git_regcomp
 #endif
Previous: René ScharfeNext: Junio C Hamano
Message 10 of 18 in “git bug: Perl compatible regular expressions do not work as expected”
  1. Mario GrgicMar 25, 2023
  2. Kristoffer HaugsbakkMar 25, 2023
  3. Mario GrgicMar 25, 2023
  4. demerphqMar 25, 2023
  5. Mario GrgicMar 25, 2023
  6. demerphqMar 25, 2023
  7. René ScharfeMar 25, 2023
  8. Junio C HamanoMar 27, 2023
  9. René ScharfeMar 27, 2023
  10. Junio C HamanoMar 27, 2023
  11. Junio C HamanoMar 28, 2023
  12. René ScharfeMar 28, 2023
  13. Mario GrgicMar 25, 2023
  14. Mario GrgicMar 25, 2023
  15. Junio C HamanoMar 27, 2023
  16. Mario GrgicMar 27, 2023
  17. Junio C HamanoMar 27, 2023
  18. Mario GrgicMar 28, 2023

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.