grep: fix multibyte regex handling under macOS

The commit 29de20504e (Makefile: fix default regex settings on Darwin, 2013-05-11) fixed t0070-fundamental.sh under Darwin (macOS) by adopting Git's regex library. However, this library is compiled with NO_MBSUPPORT, which causes git-grep to work incorrectly on multibyte (e.g. UTF-8) files. Current macOS versions pass t0070-fundamental.sh with the native macOS regex library, which also supports multibyte characters. Adjust the Makefile to use the native regex library, and call setlocale(3) to set CTYPE according to the user's preference. The setlocale call is required on all platforms, but in platforms supporting gettext(3), setlocale was called as a side-effect of initializing gettext. Therefore, move the CTYPE setlocale call from gettext.c to common-main.c and the corresponding locale.h include into git-compat-util.h. Thanks to the global initialization of CTYPE setlocale, the test-tool regex command now works correctly with supported multibyte regexes, and is used to set the MB_REGEX test prerequisite by assessing a platform's support for them. Signed-off-by: Diomidis Spinellis <dds@aueb.gr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Diomidis Spinellis committed Aug 26, 2022 at 11:58 UTC 1819ad327b7a1f19540a819813b70a0e8a7f798f
5 files changed +18 -3
Makefile
+1 -1
@@ -1434,7 +1434,6 @@ ifeq ($(uname_S),Darwin)
1434 APPLE_COMMON_CRYPTO = YesPlease
1435 COMPAT_CFLAGS += -DAPPLE_COMMON_CRYPTO
1436 endif
1437 - NO_REGEX = YesPlease
1437 PTHREAD_LIBS =
1438 endif
1439
@@ -2979,6 +2978,7 @@ GIT-BUILD-OPTIONS: FORCE
2978 @echo NO_PERL=\''$(subst ','\'',$(subst ','\'',$(NO_PERL)))'\' >>$@+
2979 @echo NO_PTHREADS=\''$(subst ','\'',$(subst ','\'',$(NO_PTHREADS)))'\' >>$@+
2980 @echo NO_PYTHON=\''$(subst ','\'',$(subst ','\'',$(NO_PYTHON)))'\' >>$@+
2981 + @echo NO_REGEX=\''$(subst ','\'',$(subst ','\'',$(NO_REGEX)))'\' >>$@+
2982 @echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@+
2983 @echo PAGER_ENV=\''$(subst ','\'',$(subst ','\'',$(PAGER_ENV)))'\' >>$@+
2984 @echo DC_SHA1=\''$(subst ','\'',$(subst ','\'',$(DC_SHA1)))'\' >>$@+
common-main.c
+1
@@ -40,6 +40,7 @@ int main(int argc, const char **argv)
40
41 git_resolve_executable_dir(argv[0]);
42
43 + setlocale(LC_CTYPE, "");
44 git_setup_gettext();
45
46 initialize_the_repository();
gettext.c
-2
@@ -10,7 +10,6 @@
10 #include "config.h"
11
12 #ifndef NO_GETTEXT
13 -# include <locale.h>
13 # include <libintl.h>
14 # ifdef GIT_WINDOWS_NATIVE
15
@@ -80,7 +79,6 @@ static int test_vsnprintf(const char *fmt, ...)
79
80 static void init_gettext_charset(const char *domain)
81 {
83 - setlocale(LC_CTYPE, "");
82 charset = locale_charset();
83 bind_textdomain_codeset(domain, charset);
84
git-compat-util.h
+1
@@ -215,6 +215,7 @@ struct strbuf;
215 #endif
216 #include <errno.h>
217 #include <limits.h>
218 +#include <locale.h>
219 #ifdef NEEDS_SYS_PARAM_H
220 #include <sys/param.h>
221 #endif
t/t7810-grep.sh
+15
@@ -18,6 +18,9 @@ test_invalid_grep_expression() {
18 '
19 }
20
21 +LC_ALL=en_US.UTF-8 test-tool regex '^.$' '¿' &&
22 + test_set_prereq MB_REGEX
23 +
24 cat >hello.c <<EOF
25 #include <assert.h>
26 #include <stdio.h>
@@ -88,6 +91,10 @@ test_expect_success setup '
91 echo unusual >"\"unusual\" pathname" &&
92 echo unusual >"t/nested \"unusual\" pathname"
93 fi &&
94 + if test_have_prereq MB_REGEX
95 + then
96 + echo "¿" >reverse-question-mark
97 + fi &&
98 git add . &&
99 test_tick &&
100 git commit -m initial
@@ -569,6 +576,14 @@ do
576 '
577 done
578
579 +test_expect_success MB_REGEX 'grep exactly one char in single-char multibyte file' '
580 + LC_ALL=en_US.UTF-8 git grep "^.$" reverse-question-mark
581 +'
582 +
583 +test_expect_success MB_REGEX 'grep two chars in single-char multibyte file' '
584 + LC_ALL=en_US.UTF-8 test_expect_code 1 git grep ".." reverse-question-mark
585 +'
586 +
587 cat >expected <<EOF
588 file
589 EOF