1#!/bin/sh
2
3test_description='diff whitespace error detection'
4
5. ./test-lib.sh
6
7test_expect_success setup '
8
9 git config diff.color.whitespace "blue reverse" &&
10 >F &&
11 git add F &&
12 echo " Eight SP indent" >>F &&
13 echo " HT and SP indent" >>F &&
14 echo "With trailing SP " >>F &&
15 echo "Carriage ReturnQ" | tr Q "\015" >>F &&
16 echo "No problem" >>F &&
17 echo >>F
18
19'
20
21blue_grep='7;34m' ;# ESC [ 7 ; 3 4 m
22
23printf "\033[%s" "$blue_grep" >check-grep
24if (grep "$blue_grep" <check-grep | grep "$blue_grep") >/dev/null 2>&1
25then
26 grep_a=grep
27elif (grep -a "$blue_grep" <check-grep | grep -a "$blue_grep") >/dev/null 2>&1
28then
29 grep_a='grep -a'
30else
31 grep_a=grep ;# expected to fail...
32fi
33rm -f check-grep
34
35prepare_output () {
36 git diff --color >output
37 $grep_a "$blue_grep" output >error
38 $grep_a -v "$blue_grep" output >normal
39}
40
41test_expect_success default '
42
43 prepare_output
44
45 grep Eight normal >/dev/null &&
46 grep HT error >/dev/null &&
47 grep With error >/dev/null &&
48 grep Return error >/dev/null &&
49 grep No normal >/dev/null
50
51'
52
53test_expect_success 'without -trail' '
54
55 git config core.whitespace -trail
56 prepare_output
57
58 grep Eight normal >/dev/null &&
59 grep HT error >/dev/null &&
60 grep With normal >/dev/null &&
61 grep Return normal >/dev/null &&
62 grep No normal >/dev/null
63
64'
65
66test_expect_success 'without -trail (attribute)' '
67
68 git config --unset core.whitespace
69 echo "F whitespace=-trail" >.gitattributes
70 prepare_output
71
72 grep Eight normal >/dev/null &&
73 grep HT error >/dev/null &&
74 grep With normal >/dev/null &&
75 grep Return normal >/dev/null &&
76 grep No normal >/dev/null
77
78'
79
80test_expect_success 'without -space' '
81
82 rm -f .gitattributes
83 git config core.whitespace -space
84 prepare_output
85
86 grep Eight normal >/dev/null &&
87 grep HT normal >/dev/null &&
88 grep With error >/dev/null &&
89 grep Return error >/dev/null &&
90 grep No normal >/dev/null
91
92'
93
94test_expect_success 'without -space (attribute)' '
95
96 git config --unset core.whitespace
97 echo "F whitespace=-space" >.gitattributes
98 prepare_output
99
100 grep Eight normal >/dev/null &&
101 grep HT normal >/dev/null &&
102 grep With error >/dev/null &&
103 grep Return error >/dev/null &&
104 grep No normal >/dev/null
105
106'
107
108test_expect_success 'with indent-non-tab only' '
109
110 rm -f .gitattributes
111 git config core.whitespace indent,-trailing,-space
112 prepare_output
113
114 grep Eight error >/dev/null &&
115 grep HT normal >/dev/null &&
116 grep With normal >/dev/null &&
117 grep Return normal >/dev/null &&
118 grep No normal >/dev/null
119
120'
121
122test_expect_success 'with indent-non-tab only (attribute)' '
123
124 git config --unset core.whitespace
125 echo "F whitespace=indent,-trailing,-space" >.gitattributes
126 prepare_output
127
128 grep Eight error >/dev/null &&
129 grep HT normal >/dev/null &&
130 grep With normal >/dev/null &&
131 grep Return normal >/dev/null &&
132 grep No normal >/dev/null
133
134'
135
136test_expect_success 'with cr-at-eol' '
137
138 rm -f .gitattributes
139 git config core.whitespace cr-at-eol
140 prepare_output
141
142 grep Eight normal >/dev/null &&
143 grep HT error >/dev/null &&
144 grep With error >/dev/null &&
145 grep Return normal >/dev/null &&
146 grep No normal >/dev/null
147
148'
149
150test_expect_success 'with cr-at-eol (attribute)' '
151
152 git config --unset core.whitespace
153 echo "F whitespace=trailing,cr-at-eol" >.gitattributes
154 prepare_output
155
156 grep Eight normal >/dev/null &&
157 grep HT error >/dev/null &&
158 grep With error >/dev/null &&
159 grep Return normal >/dev/null &&
160 grep No normal >/dev/null
161
162'
163
164test_expect_success 'trailing empty lines (1)' '
165
166 rm -f .gitattributes &&
167 test_must_fail git diff --check >output &&
168 grep "new blank line at" output &&
169 grep "trailing whitespace" output
170
171'
172
173test_expect_success 'trailing empty lines (2)' '
174
175 echo "F -whitespace" >.gitattributes &&
176 git diff --check >output &&
177 ! test -s output
178
179'
180
181test_expect_success 'do not color trailing cr in context' '
182 git config --unset core.whitespace
183 rm -f .gitattributes &&
184 echo AAAQ | tr Q "\015" >G &&
185 git add G &&
186 echo BBBQ | tr Q "\015" >>G
187 git diff --color G | tr "\015" Q >output &&
188 grep "BBB.*${blue_grep}Q" output &&
189 grep "AAA.*\[mQ" output
190
191'
192
193test_expect_success 'color new trailing blank lines' '
194 { echo a; echo b; echo; echo; } >x &&
195 git add x &&
196 { echo a; echo; echo; echo; echo c; echo; echo; echo; echo; } >x &&
197 git diff --color x >output &&
198 cnt=$($grep_a "${blue_grep}" output | wc -l) &&
199 test $cnt = 2
200'
201
202test_done