3 test_description
='word diff colors'
6 .
"$TEST_DIRECTORY"/lib
-diff.sh
8 cat >pre.simple
<<-\EOF
13 cat >post.simple
<<-\EOF
22 pre
=$(git rev-parse --short $(git hash-object pre.simple))
23 post
=$(git rev-parse --short $(git hash-object post.simple))
24 cat >expect.letter
-runs-are-words <<-EOF
25 <BOLD>diff --git a/pre b/post<RESET>
26 <BOLD>index $pre..$post 100644<RESET>
27 <BOLD>--- a/pre<RESET>
28 <BOLD>+++ b/post<RESET>
29 <CYAN>@@ -1,3 +1,7 @@<RESET>
30 h(4),<GREEN>hh<RESET>[44]
36 <GREEN>aeff = aeff * ( aaa<RESET> )
38 cat >expect.non
-whitespace-is-word <<-EOF
39 <BOLD>diff --git a/pre b/post<RESET>
40 <BOLD>index $pre..$post 100644<RESET>
41 <BOLD>--- a/pre<RESET>
42 <BOLD>+++ b/post<RESET>
43 <CYAN>@@ -1,3 +1,7 @@<RESET>
44 h(4)<GREEN>,hh[44]<RESET>
50 <GREEN>aeff = aeff * ( aaa )<RESET>
54 pre
=$(git rev-parse --short $(git hash-object pre)) &&
55 post
=$(git rev-parse --short $(git hash-object post)) &&
56 test_must_fail git
diff --no-index "$@" pre post
>output
&&
57 test_decode_color
<output
>output.decrypted
&&
58 sed -e "2s/index [^ ]*/index $pre..$post/" expect
>expected
59 test_cmp expected output.decrypted
62 test_language_driver
() {
64 test_expect_success
"diff driver '$lang'" '
65 cp "$TEST_DIRECTORY/t4034/'"$lang"'/pre" \
66 "$TEST_DIRECTORY/t4034/'"$lang"'/post" \
67 "$TEST_DIRECTORY/t4034/'"$lang"'/expect" . &&
68 echo "* diff='"$lang"'" >.gitattributes &&
69 word_diff --color-words
71 test_expect_success
"diff driver '$lang' in Islandic" '
72 test_env LANG=is_IS.UTF-8 LANGUAGE=is LC_ALL="$is_IS_locale" \
73 word_diff --color-words
77 test_expect_success setup
'
78 git config diff.color.old red &&
79 git config diff.color.new green &&
80 git config diff.color.func magenta
83 test_expect_success
'set up pre and post with runs of whitespace' '
88 test_expect_success
'word diff with runs of whitespace' '
90 <BOLD>diff --git a/pre b/post<RESET>
91 <BOLD>index $pre..$post 100644<RESET>
92 <BOLD>--- a/pre<RESET>
93 <BOLD>+++ b/post<RESET>
94 <CYAN>@@ -1,3 +1,7 @@<RESET>
95 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
101 <GREEN>aeff = aeff * ( aaa )<RESET>
103 word_diff --color-words &&
104 word_diff --word-diff=color &&
105 word_diff --color --word-diff=color
108 test_expect_success
'--word-diff=porcelain' '
109 sed "s/#.*$//" >expect <<-EOF &&
110 diff --git a/pre b/post
111 index $pre..$post 100644
126 +aeff = aeff * ( aaa )
129 word_diff --word-diff=porcelain
132 test_expect_success
'--word-diff=plain' '
133 cat >expect <<-EOF &&
134 diff --git a/pre b/post
135 index $pre..$post 100644
139 [-h(4)-]{+h(4),hh[44]+}
145 {+aeff = aeff * ( aaa )+}
147 word_diff --word-diff=plain &&
148 word_diff --word-diff=plain --no-color
151 test_expect_success
'--word-diff=plain --color' '
152 cat >expect <<-EOF &&
153 <BOLD>diff --git a/pre b/post<RESET>
154 <BOLD>index $pre..$post 100644<RESET>
155 <BOLD>--- a/pre<RESET>
156 <BOLD>+++ b/post<RESET>
157 <CYAN>@@ -1,3 +1,7 @@<RESET>
158 <RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
162 <GREEN>{+aa = a+}<RESET>
164 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
166 word_diff --word-diff=plain --color
169 test_expect_success
'word diff without context' '
170 cat >expect <<-EOF &&
171 <BOLD>diff --git a/pre b/post<RESET>
172 <BOLD>index $pre..$post 100644<RESET>
173 <BOLD>--- a/pre<RESET>
174 <BOLD>+++ b/post<RESET>
175 <CYAN>@@ -1 +1 @@<RESET>
176 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
177 <CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
181 <GREEN>aeff = aeff * ( aaa )<RESET>
183 word_diff --color-words --unified=0
186 test_expect_success
'word diff with a regular expression' '
187 cp expect.letter-runs-are-words expect &&
188 word_diff --color-words="[a-z]+"
191 test_expect_success
'word diff with zero length matches' '
192 cp expect.letter-runs-are-words expect &&
193 word_diff --color-words="[a-z${LF}]*"
196 test_expect_success
'set up a diff driver' '
197 git config diff.testdriver.wordRegex "[^[:space:]]" &&
198 cat <<-\EOF >.gitattributes
204 test_expect_success
'option overrides .gitattributes' '
205 cp expect.letter-runs-are-words expect &&
206 word_diff --color-words="[a-z]+"
209 test_expect_success
'use regex supplied by driver' '
210 cp expect.non-whitespace-is-word expect &&
211 word_diff --color-words
214 test_expect_success
'set up diff.wordRegex option' '
215 git config diff.wordRegex "[[:alnum:]]+"
218 test_expect_success
'command-line overrides config' '
219 cp expect.letter-runs-are-words expect &&
220 word_diff --color-words="[a-z]+"
223 test_expect_success
'command-line overrides config: --word-diff-regex' '
224 cat >expect <<-EOF &&
225 <BOLD>diff --git a/pre b/post<RESET>
226 <BOLD>index $pre..$post 100644<RESET>
227 <BOLD>--- a/pre<RESET>
228 <BOLD>+++ b/post<RESET>
229 <CYAN>@@ -1,3 +1,7 @@<RESET>
230 h(4),<GREEN>{+hh+}<RESET>[44]
234 <GREEN>{+aa = a+}<RESET>
236 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
238 word_diff --color --word-diff-regex="[a-z]+"
241 test_expect_success
'.gitattributes override config' '
242 cp expect.non-whitespace-is-word expect &&
243 word_diff --color-words
246 test_expect_success
'setup: remove diff driver regex' '
247 test_unconfig diff.testdriver.wordRegex
250 test_expect_success
'use configured regex' '
251 cat >expect <<-EOF &&
252 <BOLD>diff --git a/pre b/post<RESET>
253 <BOLD>index $pre..$post 100644<RESET>
254 <BOLD>--- a/pre<RESET>
255 <BOLD>+++ b/post<RESET>
256 <CYAN>@@ -1,3 +1,7 @@<RESET>
257 h(4),<GREEN>hh[44<RESET>]
263 <GREEN>aeff = aeff * ( aaa<RESET> )
265 word_diff --color-words
268 test_expect_success
'test parsing words for newline' '
269 echo "aaa (aaa)" >pre &&
270 echo "aaa (aaa) aaa" >post &&
271 pre=$(git rev-parse --short $(git hash-object pre)) &&
272 post=$(git rev-parse --short $(git hash-object post)) &&
273 cat >expect <<-EOF &&
274 <BOLD>diff --git a/pre b/post<RESET>
275 <BOLD>index $pre..$post 100644<RESET>
276 <BOLD>--- a/pre<RESET>
277 <BOLD>+++ b/post<RESET>
278 <CYAN>@@ -1 +1 @@<RESET>
279 aaa (aaa) <GREEN>aaa<RESET>
281 word_diff --color-words="a+"
284 test_expect_success
'test when words are only removed at the end' '
287 pre=$(git rev-parse --short $(git hash-object pre)) &&
288 post=$(git rev-parse --short $(git hash-object post)) &&
289 cat >expect <<-EOF &&
290 <BOLD>diff --git a/pre b/post<RESET>
291 <BOLD>index $pre..$post 100644<RESET>
292 <BOLD>--- a/pre<RESET>
293 <BOLD>+++ b/post<RESET>
294 <CYAN>@@ -1 +1 @@<RESET>
297 word_diff --color-words=.
300 test_expect_success
'--word-diff=none' '
303 pre=$(git rev-parse --short $(git hash-object pre)) &&
304 post=$(git rev-parse --short $(git hash-object post)) &&
305 cat >expect <<-EOF &&
306 diff --git a/pre b/post
307 index $pre..$post 100644
314 word_diff --word-diff=plain --word-diff=none
317 test_expect_success
'unset default driver' '
318 test_unconfig diff.wordregex
321 test_language_driver ada
322 test_language_driver bibtex
323 test_language_driver bash
324 test_language_driver cpp
325 test_language_driver csharp
326 test_language_driver css
327 test_language_driver dts
328 test_language_driver fortran
329 test_language_driver html
330 test_language_driver java
331 test_language_driver kotlin
332 test_language_driver matlab
333 test_language_driver objc
334 test_language_driver pascal
335 test_language_driver perl
336 test_language_driver php
337 test_language_driver python
338 test_language_driver ruby
339 test_language_driver scheme
340 test_language_driver tex
342 test_expect_success
'word-diff with diff.sbe' '
353 pre=$(git rev-parse --short $(git hash-object pre)) &&
354 post=$(git rev-parse --short $(git hash-object post)) &&
355 cat >expect <<-EOF &&
356 diff --git a/pre b/post
357 index $pre..$post 100644
365 test_config diff.suppress-blank-empty true &&
366 word_diff --word-diff=plain
369 test_expect_success
'word-diff with no newline at EOF' '
370 printf "%s" "a a a a a" >pre &&
371 printf "%s" "a a ab a a" >post &&
372 pre=$(git rev-parse --short $(git hash-object pre)) &&
373 post=$(git rev-parse --short $(git hash-object post)) &&
374 cat >expect <<-EOF &&
375 diff --git a/pre b/post
376 index $pre..$post 100644
382 word_diff --word-diff=plain
385 test_expect_success
'setup history with two files' '
386 echo "a b; c" >a.tex &&
387 echo "a b; c" >z.txt &&
388 git add a.tex z.txt &&
389 git commit -minitial &&
392 echo "a bx; c" >a.tex &&
393 echo "a bx; c" >z.txt &&
394 git commit -mmodified -a
397 test_expect_success
'wordRegex for the first file does not apply to the second' '
398 echo "*.tex diff=tex" >.gitattributes &&
399 test_config diff.tex.wordRegex "[a-z]+|." &&
400 cat >expect <<-\EOF &&
401 diff --git a/a.tex b/a.tex
406 diff --git a/z.txt b/z.txt
412 git diff --word-diff HEAD~ >actual &&
413 compare_diff_patch expect actual