1#!/bin/sh
2
3test_description='word diff colors'
4
5. ./test-lib.sh
6
7test_expect_success setup '
8
9 git config diff.color.old red &&
10 git config diff.color.new green &&
11 git config diff.color.func magenta
12
13'
14
15word_diff () {
16 test_must_fail git diff --no-index "$@" pre post > output &&
17 test_decode_color <output >output.decrypted &&
18 test_cmp expect output.decrypted
19}
20
21cat > pre <<\EOF
22h(4)
23
24a = b + c
25EOF
26
27cat > post <<\EOF
28h(4),hh[44]
29
30a = b + c
31
32aa = a
33
34aeff = aeff * ( aaa )
35EOF
36
37cat > expect <<\EOF
38<WHITE>diff --git a/pre b/post<RESET>
39<WHITE>index 330b04f..5ed8eff 100644<RESET>
40<WHITE>--- a/pre<RESET>
41<WHITE>+++ b/post<RESET>
42<CYAN>@@ -1,3 +1,7 @@<RESET>
43<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
44
45a = b + c<RESET>
46
47<GREEN>aa = a<RESET>
48
49<GREEN>aeff = aeff * ( aaa )<RESET>
50EOF
51
52test_expect_success 'word diff with runs of whitespace' '
53
54 word_diff --color-words
55
56'
57
58test_expect_success '--word-diff=color' '
59
60 word_diff --word-diff=color
61
62'
63
64test_expect_success '--color --word-diff=color' '
65
66 word_diff --color --word-diff=color
67
68'
69
70sed 's/#.*$//' > expect <<EOF
71diff --git a/pre b/post
72index 330b04f..5ed8eff 100644
73--- a/pre
74+++ b/post
75@@ -1,3 +1,7 @@
76-h(4)
77+h(4),hh[44]
78~
79 # significant space
80~
81 a = b + c
82~
83~
84+aa = a
85~
86~
87+aeff = aeff * ( aaa )
88~
89EOF
90
91test_expect_success '--word-diff=porcelain' '
92
93 word_diff --word-diff=porcelain
94
95'
96
97cat > expect <<EOF
98diff --git a/pre b/post
99index 330b04f..5ed8eff 100644
100--- a/pre
101+++ b/post
102@@ -1,3 +1,7 @@
103[-h(4)-]{+h(4),hh[44]+}
104
105a = b + c
106
107{+aa = a+}
108
109{+aeff = aeff * ( aaa )+}
110EOF
111
112test_expect_success '--word-diff=plain' '
113
114 word_diff --word-diff=plain
115
116'
117
118test_expect_success '--word-diff=plain --no-color' '
119
120 word_diff --word-diff=plain --no-color
121
122'
123
124cat > expect <<EOF
125<WHITE>diff --git a/pre b/post<RESET>
126<WHITE>index 330b04f..5ed8eff 100644<RESET>
127<WHITE>--- a/pre<RESET>
128<WHITE>+++ b/post<RESET>
129<CYAN>@@ -1,3 +1,7 @@<RESET>
130<RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
131
132a = b + c<RESET>
133
134<GREEN>{+aa = a+}<RESET>
135
136<GREEN>{+aeff = aeff * ( aaa )+}<RESET>
137EOF
138
139test_expect_success '--word-diff=plain --color' '
140
141 word_diff --word-diff=plain --color
142
143'
144
145cat > expect <<\EOF
146<WHITE>diff --git a/pre b/post<RESET>
147<WHITE>index 330b04f..5ed8eff 100644<RESET>
148<WHITE>--- a/pre<RESET>
149<WHITE>+++ b/post<RESET>
150<CYAN>@@ -1 +1 @@<RESET>
151<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
152<CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
153
154<GREEN>aa = a<RESET>
155
156<GREEN>aeff = aeff * ( aaa )<RESET>
157EOF
158
159test_expect_success 'word diff without context' '
160
161 word_diff --color-words --unified=0
162
163'
164
165cat > expect <<\EOF
166<WHITE>diff --git a/pre b/post<RESET>
167<WHITE>index 330b04f..5ed8eff 100644<RESET>
168<WHITE>--- a/pre<RESET>
169<WHITE>+++ b/post<RESET>
170<CYAN>@@ -1,3 +1,7 @@<RESET>
171h(4),<GREEN>hh<RESET>[44]
172
173a = b + c<RESET>
174
175<GREEN>aa = a<RESET>
176
177<GREEN>aeff = aeff * ( aaa<RESET> )
178EOF
179cp expect expect.letter-runs-are-words
180
181test_expect_success 'word diff with a regular expression' '
182
183 word_diff --color-words="[a-z]+"
184
185'
186
187test_expect_success 'set a diff driver' '
188 git config diff.testdriver.wordRegex "[^[:space:]]" &&
189 cat <<EOF > .gitattributes
190pre diff=testdriver
191post diff=testdriver
192EOF
193'
194
195test_expect_success 'option overrides .gitattributes' '
196
197 word_diff --color-words="[a-z]+"
198
199'
200
201cat > expect <<\EOF
202<WHITE>diff --git a/pre b/post<RESET>
203<WHITE>index 330b04f..5ed8eff 100644<RESET>
204<WHITE>--- a/pre<RESET>
205<WHITE>+++ b/post<RESET>
206<CYAN>@@ -1,3 +1,7 @@<RESET>
207h(4)<GREEN>,hh[44]<RESET>
208
209a = b + c<RESET>
210
211<GREEN>aa = a<RESET>
212
213<GREEN>aeff = aeff * ( aaa )<RESET>
214EOF
215cp expect expect.non-whitespace-is-word
216
217test_expect_success 'use regex supplied by driver' '
218
219 word_diff --color-words
220
221'
222
223test_expect_success 'set diff.wordRegex option' '
224 git config diff.wordRegex "[[:alnum:]]+"
225'
226
227cp expect.letter-runs-are-words expect
228
229test_expect_success 'command-line overrides config' '
230 word_diff --color-words="[a-z]+"
231'
232
233cat > expect <<\EOF
234<WHITE>diff --git a/pre b/post<RESET>
235<WHITE>index 330b04f..5ed8eff 100644<RESET>
236<WHITE>--- a/pre<RESET>
237<WHITE>+++ b/post<RESET>
238<CYAN>@@ -1,3 +1,7 @@<RESET>
239h(4),<GREEN>{+hh+}<RESET>[44]
240
241a = b + c<RESET>
242
243<GREEN>{+aa = a+}<RESET>
244
245<GREEN>{+aeff = aeff * ( aaa+}<RESET> )
246EOF
247
248test_expect_success 'command-line overrides config: --word-diff-regex' '
249 word_diff --color --word-diff-regex="[a-z]+"
250'
251
252cp expect.non-whitespace-is-word expect
253
254test_expect_success '.gitattributes override config' '
255 word_diff --color-words
256'
257
258test_expect_success 'remove diff driver regex' '
259 git config --unset diff.testdriver.wordRegex
260'
261
262cat > expect <<\EOF
263<WHITE>diff --git a/pre b/post<RESET>
264<WHITE>index 330b04f..5ed8eff 100644<RESET>
265<WHITE>--- a/pre<RESET>
266<WHITE>+++ b/post<RESET>
267<CYAN>@@ -1,3 +1,7 @@<RESET>
268h(4),<GREEN>hh[44<RESET>]
269
270a = b + c<RESET>
271
272<GREEN>aa = a<RESET>
273
274<GREEN>aeff = aeff * ( aaa<RESET> )
275EOF
276
277test_expect_success 'use configured regex' '
278 word_diff --color-words
279'
280
281echo 'aaa (aaa)' > pre
282echo 'aaa (aaa) aaa' > post
283
284cat > expect <<\EOF
285<WHITE>diff --git a/pre b/post<RESET>
286<WHITE>index c29453b..be22f37 100644<RESET>
287<WHITE>--- a/pre<RESET>
288<WHITE>+++ b/post<RESET>
289<CYAN>@@ -1 +1 @@<RESET>
290aaa (aaa) <GREEN>aaa<RESET>
291EOF
292
293test_expect_success 'test parsing words for newline' '
294
295 word_diff --color-words="a+"
296
297
298'
299
300echo '(:' > pre
301echo '(' > post
302
303cat > expect <<\EOF
304<WHITE>diff --git a/pre b/post<RESET>
305<WHITE>index 289cb9d..2d06f37 100644<RESET>
306<WHITE>--- a/pre<RESET>
307<WHITE>+++ b/post<RESET>
308<CYAN>@@ -1 +1 @@<RESET>
309(<RED>:<RESET>
310EOF
311
312test_expect_success 'test when words are only removed at the end' '
313
314 word_diff --color-words=.
315
316'
317
318cat > expect <<\EOF
319diff --git a/pre b/post
320index 289cb9d..2d06f37 100644
321--- a/pre
322+++ b/post
323@@ -1 +1 @@
324-(:
325+(
326EOF
327
328test_expect_success '--word-diff=none' '
329
330 word_diff --word-diff=plain --word-diff=none
331
332'
333
334test_done