]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7815-grep-binary.sh
The third batch
[thirdparty/git.git] / t / t7815-grep-binary.sh
1 #!/bin/sh
2
3 test_description='git grep in binary files'
4
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 test_expect_success 'setup' "
9 echo 'binaryQfileQm[*]cQ*æQð' | q_to_nul >a &&
10 git add a &&
11 git commit -m.
12 "
13
14 test_expect_success 'git grep ina a' '
15 echo Binary file a matches >expect &&
16 git grep ina a >actual &&
17 test_cmp expect actual
18 '
19
20 test_expect_success 'git grep -ah ina a' '
21 git grep -ah ina a >actual &&
22 test_cmp a actual
23 '
24
25 test_expect_success 'git grep -I ina a' '
26 test_must_fail git grep -I ina a >actual &&
27 test_must_be_empty actual
28 '
29
30 test_expect_success 'git grep -c ina a' '
31 echo a:1 >expect &&
32 git grep -c ina a >actual &&
33 test_cmp expect actual
34 '
35
36 test_expect_success 'git grep -l ina a' '
37 echo a >expect &&
38 git grep -l ina a >actual &&
39 test_cmp expect actual
40 '
41
42 test_expect_success 'git grep -L bar a' '
43 echo a >expect &&
44 git grep -L bar a >actual &&
45 test_cmp expect actual
46 '
47
48 test_expect_success 'git grep -q ina a' '
49 git grep -q ina a >actual &&
50 test_must_be_empty actual
51 '
52
53 test_expect_success 'git grep -F ile a' '
54 git grep -F ile a
55 '
56
57 test_expect_success 'git grep -Fi iLE a' '
58 git grep -Fi iLE a
59 '
60
61 # This test actually passes on platforms where regexec() supports the
62 # flag REG_STARTEND.
63 test_expect_success 'git grep ile a' '
64 git grep ile a
65 '
66
67 test_expect_failure 'git grep .fi a' '
68 git grep .fi a
69 '
70
71 test_expect_success 'grep respects binary diff attribute' '
72 echo text >t &&
73 git add t &&
74 echo t:text >expect &&
75 git grep text t >actual &&
76 test_cmp expect actual &&
77 echo "t -diff" >.gitattributes &&
78 echo "Binary file t matches" >expect &&
79 git grep text t >actual &&
80 test_cmp expect actual
81 '
82
83 test_expect_success 'grep --cached respects binary diff attribute' '
84 git grep --cached text t >actual &&
85 test_cmp expect actual
86 '
87
88 test_expect_success 'grep --cached respects binary diff attribute (2)' '
89 git add .gitattributes &&
90 rm .gitattributes &&
91 git grep --cached text t >actual &&
92 test_when_finished "git rm --cached .gitattributes" &&
93 test_when_finished "git checkout .gitattributes" &&
94 test_cmp expect actual
95 '
96
97 test_expect_success 'grep revision respects binary diff attribute' '
98 git commit -m new &&
99 echo "Binary file HEAD:t matches" >expect &&
100 git grep text HEAD -- t >actual &&
101 test_when_finished "git reset HEAD^" &&
102 test_cmp expect actual
103 '
104
105 test_expect_success 'grep respects not-binary diff attribute' '
106 echo binQary | q_to_nul >b &&
107 git add b &&
108 echo "Binary file b matches" >expect &&
109 git grep bin b >actual &&
110 test_cmp expect actual &&
111 echo "b diff" >.gitattributes &&
112 echo "b:binQary" >expect &&
113 git grep bin b >actual.raw &&
114 nul_to_q <actual.raw >actual &&
115 test_cmp expect actual
116 '
117
118 cat >nul_to_q_textconv <<'EOF'
119 #!/bin/sh
120 "$PERL_PATH" -pe 'y/\000/Q/' < "$1"
121 EOF
122 chmod +x nul_to_q_textconv
123
124 test_expect_success 'setup textconv filters' '
125 echo a diff=foo >.gitattributes &&
126 git config diff.foo.textconv "\"$(pwd)\""/nul_to_q_textconv
127 '
128
129 test_expect_success 'grep does not honor textconv' '
130 test_must_fail git grep Qfile
131 '
132
133 test_expect_success 'grep --textconv honors textconv' '
134 echo "a:binaryQfileQm[*]cQ*æQð" >expect &&
135 git grep --textconv Qfile >actual &&
136 test_cmp expect actual
137 '
138
139 test_expect_success 'grep --no-textconv does not honor textconv' '
140 test_must_fail git grep --no-textconv Qfile
141 '
142
143 test_expect_success 'grep --textconv blob honors textconv' '
144 echo "HEAD:a:binaryQfileQm[*]cQ*æQð" >expect &&
145 git grep --textconv Qfile HEAD:a >actual &&
146 test_cmp expect actual
147 '
148
149 test_done