3 test_description
='word diff colors'
5 TEST_PASSES_SANITIZE_LEAK
=true
7 .
"$TEST_DIRECTORY"/lib-diff.sh
9 cat >pre.simple
<<-\EOF
14 cat >post.simple <<-\
EOF
23 pre=$(git rev-parse --short $(git hash-object pre.simple))
24 post=$(git rev-parse --short $(git hash-object post.simple))
25 cat >expect.letter-runs-are-words <<-EOF
26 <BOLD>diff --git a/pre b/post<RESET>
27 <BOLD>index $pre..$post 100644<RESET>
28 <BOLD>--- a/pre<RESET>
29 <BOLD>+++ b/post<RESET>
30 <CYAN>@@ -1,3 +1,7 @@<RESET>
31 h(4),<GREEN>hh<RESET>[44]
37 <GREEN>aeff = aeff * ( aaa<RESET> )
39 cat >expect.non-whitespace-is-word <<-EOF
40 <BOLD>diff --git a/pre b/post<RESET>
41 <BOLD>index $pre..$post 100644<RESET>
42 <BOLD>--- a/pre<RESET>
43 <BOLD>+++ b/post<RESET>
44 <CYAN>@@ -1,3 +1,7 @@<RESET>
45 h(4)<GREEN>,hh[44]<RESET>
51 <GREEN>aeff = aeff * ( aaa )<RESET>
55 pre=$(git rev-parse --short $(git hash-object pre)) &&
56 post=$(git rev-parse --short $(git hash-object post)) &&
57 test_must_fail git diff --no-index "$@" pre post >output &&
58 test_decode_color <output >output.decrypted &&
59 sed -e "2s/index [^ ]*/index $pre..$post/" expect >expected
60 test_cmp expected output.decrypted
63 test_language_driver () {
65 test_expect_success "diff driver '$lang'" '
66 cp "$TEST_DIRECTORY/t4034/'"$lang"'/pre" \
67 "$TEST_DIRECTORY/t4034/'"$lang"'/post" \
68 "$TEST_DIRECTORY/t4034/'"$lang"'/expect" . &&
69 echo "* diff='"$lang"'" >.gitattributes &&
70 word_diff --color-words
74 test_expect_success setup '
75 git config diff.color.old red &&
76 git config diff.color.new green &&
77 git config diff.color.func magenta
80 test_expect_success 'set up pre and post with runs of whitespace' '
85 test_expect_success 'word diff with runs of whitespace' '
87 <BOLD>diff --git a/pre b/post<RESET>
88 <BOLD>index $pre..$post 100644<RESET>
89 <BOLD>--- a/pre<RESET>
90 <BOLD>+++ b/post<RESET>
91 <CYAN>@@ -1,3 +1,7 @@<RESET>
92 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
98 <GREEN>aeff = aeff * ( aaa )<RESET>
100 word_diff --color-words &&
101 word_diff --word-diff=color &&
102 word_diff --color --word-diff=color
105 test_expect_success '--word-diff=porcelain' '
106 sed "s/#.*$//" >expect <<-EOF &&
107 diff --git a/pre b/post
108 index $pre..$post 100644
123 +aeff = aeff * ( aaa )
126 word_diff --word-diff=porcelain
129 test_expect_success '--word-diff=plain' '
130 cat >expect <<-EOF &&
131 diff --git a/pre b/post
132 index $pre..$post 100644
136 [-h(4)-]{+h
(4),hh
[44]+}
142 {+aeff
= aeff
* ( aaa
)+}
144 word_diff
--word-diff=plain
&&
145 word_diff
--word-diff=plain
--no-color
148 test_expect_success '--word-diff=plain
--color' '
149 cat >expect
<<-EOF &&
150 <BOLD>diff --git a/pre b/post<RESET>
151 <BOLD>index $pre..$post 100644<RESET>
152 <BOLD>--- a/pre<RESET>
153 <BOLD>+++ b/post<RESET>
154 <CYAN>@@ -1,3 +1,7 @@<RESET>
155 <RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
159 <GREEN>{+aa = a+}<RESET>
161 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
163 word_diff
--word-diff=plain
--color
166 test_expect_success 'word
diff without context
' '
167 cat >expect
<<-EOF &&
168 <BOLD>diff --git a/pre b/post<RESET>
169 <BOLD>index $pre..$post 100644<RESET>
170 <BOLD>--- a/pre<RESET>
171 <BOLD>+++ b/post<RESET>
172 <CYAN>@@ -1 +1 @@<RESET>
173 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
174 <CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
178 <GREEN>aeff = aeff * ( aaa )<RESET>
180 word_diff
--color-words --unified=0
183 test_expect_success 'word
diff with a regular expression
' '
184 cp expect.letter-runs-are-words expect
&&
185 word_diff
--color-words="[a-z]+"
188 test_expect_success 'word
diff with zero length matches
' '
189 cp expect.letter-runs-are-words expect
&&
190 word_diff
--color-words="[a-z${LF}]*"
193 test_expect_success 'set up a
diff driver
' '
194 git config
diff.testdriver.wordRegex
"[^[:space:]]" &&
195 cat <<-\EOF >.gitattributes
201 test_expect_success 'option overrides .gitattributes' '
202 cp expect.letter-runs-are-words expect &&
203 word_diff --color-words="[a-z]+"
206 test_expect_success 'use regex supplied by driver' '
207 cp expect.non-whitespace-is-word expect &&
208 word_diff --color-words
211 test_expect_success 'set up diff.wordRegex option' '
212 git config diff.wordRegex "[[:alnum:]]+"
215 test_expect_success 'command-line overrides config' '
216 cp expect.letter-runs-are-words expect &&
217 word_diff --color-words="[a-z]+"
220 test_expect_success 'command-line overrides config: --word-diff-regex' '
221 cat >expect <<-EOF &&
222 <BOLD>diff --git a/pre b/post<RESET>
223 <BOLD>index $pre..$post 100644<RESET>
224 <BOLD>--- a/pre<RESET>
225 <BOLD>+++ b/post<RESET>
226 <CYAN>@@ -1,3 +1,7 @@<RESET>
227 h(4),<GREEN>{+hh+}<RESET>[44]
231 <GREEN>{+aa = a+}<RESET>
233 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
235 word_diff --color --word-diff-regex="[a-z]+"
238 test_expect_success '.gitattributes override config' '
239 cp expect.non-whitespace-is-word expect &&
240 word_diff --color-words
243 test_expect_success 'setup: remove diff driver regex' '
244 test_unconfig diff.testdriver.wordRegex
247 test_expect_success 'use configured regex' '
248 cat >expect <<-EOF &&
249 <BOLD>diff --git a/pre b/post<RESET>
250 <BOLD>index $pre..$post 100644<RESET>
251 <BOLD>--- a/pre<RESET>
252 <BOLD>+++ b/post<RESET>
253 <CYAN>@@ -1,3 +1,7 @@<RESET>
254 h(4),<GREEN>hh[44<RESET>]
260 <GREEN>aeff = aeff * ( aaa<RESET> )
262 word_diff --color-words
265 test_expect_success 'test parsing words for newline' '
266 echo "aaa (aaa)" >pre &&
267 echo "aaa (aaa) aaa" >post &&
268 pre=$(git rev-parse --short $(git hash-object pre)) &&
269 post=$(git rev-parse --short $(git hash-object post)) &&
270 cat >expect <<-EOF &&
271 <BOLD>diff --git a/pre b/post<RESET>
272 <BOLD>index $pre..$post 100644<RESET>
273 <BOLD>--- a/pre<RESET>
274 <BOLD>+++ b/post<RESET>
275 <CYAN>@@ -1 +1 @@<RESET>
276 aaa (aaa) <GREEN>aaa<RESET>
278 word_diff --color-words="a+"
281 test_expect_success 'test when words are only removed at the end' '
284 pre=$(git rev-parse --short $(git hash-object pre)) &&
285 post=$(git rev-parse --short $(git hash-object post)) &&
286 cat >expect <<-EOF &&
287 <BOLD>diff --git a/pre b/post<RESET>
288 <BOLD>index $pre..$post 100644<RESET>
289 <BOLD>--- a/pre<RESET>
290 <BOLD>+++ b/post<RESET>
291 <CYAN>@@ -1 +1 @@<RESET>
294 word_diff --color-words=.
297 test_expect_success '--word-diff=none' '
300 pre=$(git rev-parse --short $(git hash-object pre)) &&
301 post=$(git rev-parse --short $(git hash-object post)) &&
302 cat >expect <<-EOF &&
303 diff --git a/pre b/post
304 index $pre..$post 100644
311 word_diff
--word-diff=plain
--word-diff=none
314 test_expect_success 'unset default driver
' '
315 test_unconfig
diff.wordregex
318 test_language_driver ada
319 test_language_driver bibtex
320 test_language_driver cpp
321 test_language_driver csharp
322 test_language_driver css
323 test_language_driver dts
324 test_language_driver fortran
325 test_language_driver html
326 test_language_driver java
327 test_language_driver kotlin
328 test_language_driver matlab
329 test_language_driver objc
330 test_language_driver pascal
331 test_language_driver perl
332 test_language_driver php
333 test_language_driver python
334 test_language_driver ruby
335 test_language_driver scheme
336 test_language_driver tex
338 test_expect_success 'word-diff with
diff.sbe
' '
349 pre=$(git rev-parse --short $(git hash-object pre)) &&
350 post=$(git rev-parse --short $(git hash-object post)) &&
351 cat >expect <<-EOF &&
352 diff --git a/pre b/post
353 index $pre..$post 100644
361 test_config diff.suppress-blank-empty true &&
362 word_diff --word-diff=plain
365 test_expect_success 'word-diff with no newline at EOF' '
366 printf "%s" "a a a a a" >pre &&
367 printf "%s" "a a ab a a" >post &&
368 pre=$(git rev-parse --short $(git hash-object pre)) &&
369 post=$(git rev-parse --short $(git hash-object post)) &&
370 cat >expect <<-EOF &&
371 diff --git a/pre b/post
372 index $pre..$post 100644
378 word_diff --word-diff=plain
381 test_expect_success 'setup history with two files' '
382 echo "a b; c" >a.tex &&
383 echo "a b; c" >z.txt &&
384 git add a.tex z.txt &&
385 git commit -minitial &&
388 echo "a bx; c" >a.tex &&
389 echo "a bx; c" >z.txt &&
390 git commit -mmodified -a
393 test_expect_success 'wordRegex for the first file does not apply to the second' '
394 echo "*.tex diff=tex" >.gitattributes &&
395 test_config diff.tex.wordRegex "[a-z]+|." &&
396 cat >expect <<-\EOF &&
397 diff --git a/a.tex b/a.tex
402 diff --git a/z.txt b/z.txt
408 git
diff --word-diff HEAD~
>actual
&&
409 compare_diff_patch expect actual