]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7816-grep-binary-pattern.sh
Sync with Git 2.45.1
[thirdparty/git.git] / t / t7816-grep-binary-pattern.sh
CommitLineData
d316af05
ÆAB
1#!/bin/sh
2
3test_description='git grep with a binary pattern files'
4
7ff24785 5TEST_PASSES_SANITIZE_LEAK=true
25754125 6. ./lib-gettext.sh
d316af05 7
25754125 8nul_match_internal () {
d316af05 9 matches=$1
25754125
ÆAB
10 prereqs=$2
11 lc_all=$3
12 extra_flags=$4
13 flags=$5
14 pattern=$6
d316af05
ÆAB
15 pattern_human=$(echo "$pattern" | sed 's/Q/<NUL>/g')
16
17 if test "$matches" = 1
18 then
25754125 19 test_expect_success $prereqs "LC_ALL='$lc_all' git grep $extra_flags -f f $flags '$pattern_human' a" "
d316af05 20 printf '$pattern' | q_to_nul >f &&
25754125 21 LC_ALL='$lc_all' git grep $extra_flags -f f $flags a
d316af05
ÆAB
22 "
23 elif test "$matches" = 0
24 then
25754125
ÆAB
25 test_expect_success $prereqs "LC_ALL='$lc_all' git grep $extra_flags -f f $flags '$pattern_human' a" "
26 >stderr &&
d316af05 27 printf '$pattern' | q_to_nul >f &&
25754125 28 test_must_fail env LC_ALL=\"$lc_all\" git grep $extra_flags -f f $flags a 2>stderr &&
6789275d 29 test_grep ! 'This is only supported with -P under PCRE v2' stderr
d316af05 30 "
25754125 31 elif test "$matches" = P
d316af05 32 then
25754125
ÆAB
33 test_expect_success $prereqs "error, PCRE v2 only: LC_ALL='$lc_all' git grep -f f $flags '$pattern_human' a" "
34 >stderr &&
d316af05 35 printf '$pattern' | q_to_nul >f &&
25754125 36 test_must_fail env LC_ALL=\"$lc_all\" git grep -f f $flags a 2>stderr &&
6789275d 37 test_grep 'This is only supported with -P under PCRE v2' stderr
d316af05
ÆAB
38 "
39 else
40 test_expect_success "PANIC: Test framework error. Unknown matches value $matches" 'false'
41 fi
42}
43
25754125
ÆAB
44nul_match () {
45 matches=$1
46 matches_pcre2=$2
47 matches_pcre2_locale=$3
48 flags=$4
49 pattern=$5
50 pattern_human=$(echo "$pattern" | sed 's/Q/<NUL>/g')
51
52 nul_match_internal "$matches" "" "C" "" "$flags" "$pattern"
53 nul_match_internal "$matches_pcre2" "LIBPCRE2" "C" "-P" "$flags" "$pattern"
54 nul_match_internal "$matches_pcre2_locale" "LIBPCRE2,GETTEXT_LOCALE" "$is_IS_locale" "-P" "$flags" "$pattern"
55}
56
d316af05
ÆAB
57test_expect_success 'setup' "
58 echo 'binaryQfileQm[*]cQ*æQð' | q_to_nul >a &&
59 git add a &&
60 git commit -m.
61"
62
ecbff141 63# Simple fixed-string matching
45d1f37c
ÆAB
64nul_match P P P '-F' 'yQf'
65nul_match P P P '-F' 'yQx'
66nul_match P P P '-Fi' 'YQf'
67nul_match P P P '-Fi' 'YQx'
68nul_match P P 1 '' 'yQf'
69nul_match P P 0 '' 'yQx'
70nul_match P P 1 '' 'æQð'
71nul_match P P P '-F' 'eQm[*]c'
72nul_match P P P '-Fi' 'EQM[*]C'
d316af05
ÆAB
73
74# Regex patterns that would match but shouldn't with -F
45d1f37c
ÆAB
75nul_match P P P '-F' 'yQ[f]'
76nul_match P P P '-F' '[y]Qf'
77nul_match P P P '-Fi' 'YQ[F]'
78nul_match P P P '-Fi' '[Y]QF'
79nul_match P P P '-F' 'æQ[ð]'
80nul_match P P P '-F' '[æ]Qð'
d316af05 81
ecbff141 82# Matching pattern and subject case with -i
25754125 83nul_match P 1 1 '-i' '[æ]Qð'
d316af05 84
25754125
ÆAB
85# ...PCRE v2 only matches non-ASCII with -i casefolding under UTF-8
86# semantics
87nul_match P P P '-Fi' 'ÆQ[Ð]'
88nul_match P 0 1 '-i' 'ÆQ[Ð]'
89nul_match P 0 1 '-i' '[Æ]QÐ'
90nul_match P 0 1 '-i' '[Æ]Qð'
91nul_match P 0 1 '-i' 'ÆQÐ'
d316af05 92
25754125 93# \0 in regexes can only work with -P & PCRE v2
45d1f37c
ÆAB
94nul_match P P 1 '' 'yQ[f]'
95nul_match P P 1 '' '[y]Qf'
96nul_match P P 1 '-i' 'YQ[F]'
97nul_match P P 1 '-i' '[Y]Qf'
98nul_match P P 1 '' 'æQ[ð]'
99nul_match P P 1 '' '[æ]Qð'
100nul_match P P 1 '-i' 'ÆQ[Ð]'
101nul_match P P 1 '' 'eQm.*cQ'
102nul_match P P 1 '-i' 'EQM.*cQ'
103nul_match P P 0 '' 'eQm[*]c'
104nul_match P P 0 '-i' 'EQM[*]C'
d316af05 105
25754125
ÆAB
106# Assert that we're using REG_STARTEND and the pattern doesn't match
107# just because it's cut off at the first \0.
45d1f37c
ÆAB
108nul_match P P 0 '-i' 'NOMATCHQð'
109nul_match P P 0 '-i' '[Æ]QNOMATCH'
110nul_match P P 0 '-i' '[æ]QNOMATCH'
d316af05
ÆAB
111
112# Ensure that the matcher doesn't regress to something that stops at
113# \0
45d1f37c
ÆAB
114nul_match P P P '-F' 'yQ[f]'
115nul_match P P P '-Fi' 'YQ[F]'
116nul_match P P 0 '' 'yQNOMATCH'
117nul_match P P 0 '' 'QNOMATCH'
118nul_match P P 0 '-i' 'YQNOMATCH'
119nul_match P P 0 '-i' 'QNOMATCH'
120nul_match P P P '-F' 'æQ[ð]'
25754125 121nul_match P P P '-Fi' 'ÆQ[Ð]'
45d1f37c
ÆAB
122nul_match P P 1 '-i' 'ÆQ[Ð]'
123nul_match P P 0 '' 'yQNÓMATCH'
124nul_match P P 0 '' 'QNÓMATCH'
125nul_match P P 0 '-i' 'YQNÓMATCH'
126nul_match P P 0 '-i' 'QNÓMATCH'
d316af05
ÆAB
127
128test_done