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
23test_expect_success default '
24
25 git diff --color >output
26 grep "$blue_grep" output >error
27 grep -v "$blue_grep" output >normal
28
29 grep Eight normal >/dev/null &&
30 grep HT error >/dev/null &&
31 grep With error >/dev/null &&
32 grep Return error >/dev/null &&
33 grep No normal >/dev/null
34
35'
36
37test_expect_success 'without -trail' '
38
39 git config core.whitespace -trail
40 git diff --color >output
41 grep "$blue_grep" output >error
42 grep -v "$blue_grep" output >normal
43
44 grep Eight normal >/dev/null &&
45 grep HT error >/dev/null &&
46 grep With normal >/dev/null &&
47 grep Return normal >/dev/null &&
48 grep No normal >/dev/null
49
50'
51
52test_expect_success 'without -trail (attribute)' '
53
54 git config --unset core.whitespace
55 echo "F whitespace=-trail" >.gitattributes
56 git diff --color >output
57 grep "$blue_grep" output >error
58 grep -v "$blue_grep" output >normal
59
60 grep Eight normal >/dev/null &&
61 grep HT error >/dev/null &&
62 grep With normal >/dev/null &&
63 grep Return normal >/dev/null &&
64 grep No normal >/dev/null
65
66'
67
68test_expect_success 'without -space' '
69
70 rm -f .gitattributes
71 git config core.whitespace -space
72 git diff --color >output
73 grep "$blue_grep" output >error
74 grep -v "$blue_grep" output >normal
75
76 grep Eight normal >/dev/null &&
77 grep HT normal >/dev/null &&
78 grep With error >/dev/null &&
79 grep Return error >/dev/null &&
80 grep No normal >/dev/null
81
82'
83
84test_expect_success 'without -space (attribute)' '
85
86 git config --unset core.whitespace
87 echo "F whitespace=-space" >.gitattributes
88 git diff --color >output
89 grep "$blue_grep" output >error
90 grep -v "$blue_grep" output >normal
91
92 grep Eight normal >/dev/null &&
93 grep HT normal >/dev/null &&
94 grep With error >/dev/null &&
95 grep Return error >/dev/null &&
96 grep No normal >/dev/null
97
98'
99
100test_expect_success 'with indent-non-tab only' '
101
102 rm -f .gitattributes
103 git config core.whitespace indent,-trailing,-space
104 git diff --color >output
105 grep "$blue_grep" output >error
106 grep -v "$blue_grep" output >normal
107
108 grep Eight error >/dev/null &&
109 grep HT normal >/dev/null &&
110 grep With normal >/dev/null &&
111 grep Return normal >/dev/null &&
112 grep No normal >/dev/null
113
114'
115
116test_expect_success 'with indent-non-tab only (attribute)' '
117
118 git config --unset core.whitespace
119 echo "F whitespace=indent,-trailing,-space" >.gitattributes
120 git diff --color >output
121 grep "$blue_grep" output >error
122 grep -v "$blue_grep" output >normal
123
124 grep Eight error >/dev/null &&
125 grep HT normal >/dev/null &&
126 grep With normal >/dev/null &&
127 grep Return normal >/dev/null &&
128 grep No normal >/dev/null
129
130'
131
132test_expect_success 'with cr-at-eol' '
133
134 rm -f .gitattributes
135 git config core.whitespace cr-at-eol
136 git diff --color >output
137 grep "$blue_grep" output >error
138 grep -v "$blue_grep" output >normal
139
140 grep Eight normal >/dev/null &&
141 grep HT error >/dev/null &&
142 grep With error >/dev/null &&
143 grep Return normal >/dev/null &&
144 grep No normal >/dev/null
145
146'
147
148test_expect_success 'with cr-at-eol (attribute)' '
149
150 git config --unset core.whitespace
151 echo "F whitespace=trailing,cr-at-eol" >.gitattributes
152 git diff --color >output
153 grep "$blue_grep" output >error
154 grep -v "$blue_grep" output >normal
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 "ends with blank lines." 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_done