Skip to content

Commit 7675c7b

Browse files
peffgitster
authored andcommitted
t7810: avoid assumption about invalid regex syntax
A few of the tests want to check that "git grep -P -E" will override -P with -E, and vice versa. To do so, we use a regex with "\x{..}", which is valid in PCRE but not defined by POSIX (for basic or extended regular expressions). However, POSIX declares quite a lot of syntax, including "\x", as "undefined". That leaves implementations free to extend the standard if they choose. At least one, musl libc, implements "\x" in the same way as PCRE. Our tests check that "-E" complains about "\x", which fails with musl. We can fix this by finding some construct which behaves reliably on both PCRE and POSIX, but differently in each system. One such construct is the use of backslash inside brackets. In PCRE, "[\d]" interprets "\d" as it would outside the brackets, matching a digit. Whereas in POSIX, the backslash must be treated literally, and we match either it or a literal "d". Moreover, implementations are not free to change this according to POSIX, so we should be able to rely on it. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0b65a8d commit 7675c7b

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

t/t7810-grep.sh

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ test_expect_success setup '
3737
echo "a+bc"
3838
echo "abc"
3939
} >ab &&
40+
{
41+
echo d &&
42+
echo 0
43+
} >d0 &&
4044
echo vvv >v &&
4145
echo ww w >w &&
4246
echo x x xx x >x &&
@@ -1092,36 +1096,36 @@ test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended
10921096
'
10931097

10941098
test_expect_success 'grep -G -F -P -E pattern' '
1095-
>empty &&
1096-
test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual &&
1097-
test_cmp empty actual
1099+
echo "d0:d" >expected &&
1100+
git grep -G -F -P -E "[\d]" d0 >actual &&
1101+
test_cmp expected actual
10981102
'
10991103

11001104
test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1101-
>empty &&
1102-
test_must_fail git \
1105+
echo "d0:d" >expected &&
1106+
git \
11031107
-c grep.patterntype=fixed \
11041108
-c grep.patterntype=basic \
11051109
-c grep.patterntype=perl \
11061110
-c grep.patterntype=extended \
1107-
grep "a\x{2b}b\x{2a}c" ab >actual &&
1108-
test_cmp empty actual
1111+
grep "[\d]" d0 >actual &&
1112+
test_cmp expected actual
11091113
'
11101114

11111115
test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
1112-
echo "ab:a+b*c" >expected &&
1113-
git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual &&
1116+
echo "d0:0" >expected &&
1117+
git grep -G -F -E -P "[\d]" d0 >actual &&
11141118
test_cmp expected actual
11151119
'
11161120

11171121
test_expect_success LIBPCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1118-
echo "ab:a+b*c" >expected &&
1122+
echo "d0:0" >expected &&
11191123
git \
11201124
-c grep.patterntype=fixed \
11211125
-c grep.patterntype=basic \
11221126
-c grep.patterntype=extended \
11231127
-c grep.patterntype=perl \
1124-
grep "a\x{2b}b\x{2a}c" ab >actual &&
1128+
grep "[\d]" d0 >actual &&
11251129
test_cmp expected actual
11261130
'
11271131

0 commit comments

Comments
 (0)