Merge branch 'jk/repack-leakfix'
[git.git] / t / t4034-diff-words.sh
blob74586f3813c6f44955a5407e0d3382febd9e4e58
1 #!/bin/sh
3 test_description='word diff colors'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-diff.sh
9 cat >pre.simple <<-\EOF
10 h(4)
12 a = b + c
13 EOF
14 cat >post.simple <<-\EOF
15 h(4),hh[44]
17 a = b + c
19 aa = a
21 aeff = aeff * ( aaa )
22 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]
33 a = b + c<RESET>
35 <GREEN>aa = a<RESET>
37 <GREEN>aeff = aeff * ( aaa<RESET> )
38 EOF
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>
47 a = b + c<RESET>
49 <GREEN>aa = a<RESET>
51 <GREEN>aeff = aeff * ( aaa )<RESET>
52 EOF
54 word_diff () {
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 () {
64 lang=$1
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
72 test_expect_success "diff driver '$lang' in Islandic" '
73 LANG=is_IS.UTF-8 LANGUAGE=is LC_ALL="$is_IS_locale" \
74 word_diff --color-words
78 test_expect_success setup '
79 git config diff.color.old red &&
80 git config diff.color.new green &&
81 git config diff.color.func magenta
84 test_expect_success 'set up pre and post with runs of whitespace' '
85 cp pre.simple pre &&
86 cp post.simple post
89 test_expect_success 'word diff with runs of whitespace' '
90 cat >expect <<-EOF &&
91 <BOLD>diff --git a/pre b/post<RESET>
92 <BOLD>index $pre..$post 100644<RESET>
93 <BOLD>--- a/pre<RESET>
94 <BOLD>+++ b/post<RESET>
95 <CYAN>@@ -1,3 +1,7 @@<RESET>
96 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
98 a = b + c<RESET>
100 <GREEN>aa = a<RESET>
102 <GREEN>aeff = aeff * ( aaa )<RESET>
104 word_diff --color-words &&
105 word_diff --word-diff=color &&
106 word_diff --color --word-diff=color
109 test_expect_success '--word-diff=porcelain' '
110 sed "s/#.*$//" >expect <<-EOF &&
111 diff --git a/pre b/post
112 index $pre..$post 100644
113 --- a/pre
114 +++ b/post
115 @@ -1,3 +1,7 @@
116 -h(4)
117 +h(4),hh[44]
119 # significant space
121 a = b + c
124 +aa = a
127 +aeff = aeff * ( aaa )
130 word_diff --word-diff=porcelain
133 test_expect_success '--word-diff=plain' '
134 cat >expect <<-EOF &&
135 diff --git a/pre b/post
136 index $pre..$post 100644
137 --- a/pre
138 +++ b/post
139 @@ -1,3 +1,7 @@
140 [-h(4)-]{+h(4),hh[44]+}
142 a = b + c
144 {+aa = a+}
146 {+aeff = aeff * ( aaa )+}
148 word_diff --word-diff=plain &&
149 word_diff --word-diff=plain --no-color
152 test_expect_success '--word-diff=plain --color' '
153 cat >expect <<-EOF &&
154 <BOLD>diff --git a/pre b/post<RESET>
155 <BOLD>index $pre..$post 100644<RESET>
156 <BOLD>--- a/pre<RESET>
157 <BOLD>+++ b/post<RESET>
158 <CYAN>@@ -1,3 +1,7 @@<RESET>
159 <RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
161 a = b + c<RESET>
163 <GREEN>{+aa = a+}<RESET>
165 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
167 word_diff --word-diff=plain --color
170 test_expect_success 'word diff without context' '
171 cat >expect <<-EOF &&
172 <BOLD>diff --git a/pre b/post<RESET>
173 <BOLD>index $pre..$post 100644<RESET>
174 <BOLD>--- a/pre<RESET>
175 <BOLD>+++ b/post<RESET>
176 <CYAN>@@ -1 +1 @@<RESET>
177 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
178 <CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
180 <GREEN>aa = a<RESET>
182 <GREEN>aeff = aeff * ( aaa )<RESET>
184 word_diff --color-words --unified=0
187 test_expect_success 'word diff with a regular expression' '
188 cp expect.letter-runs-are-words expect &&
189 word_diff --color-words="[a-z]+"
192 test_expect_success 'word diff with zero length matches' '
193 cp expect.letter-runs-are-words expect &&
194 word_diff --color-words="[a-z${LF}]*"
197 test_expect_success 'set up a diff driver' '
198 git config diff.testdriver.wordRegex "[^[:space:]]" &&
199 cat <<-\EOF >.gitattributes
200 pre diff=testdriver
201 post diff=testdriver
205 test_expect_success 'option overrides .gitattributes' '
206 cp expect.letter-runs-are-words expect &&
207 word_diff --color-words="[a-z]+"
210 test_expect_success 'use regex supplied by driver' '
211 cp expect.non-whitespace-is-word expect &&
212 word_diff --color-words
215 test_expect_success 'set up diff.wordRegex option' '
216 git config diff.wordRegex "[[:alnum:]]+"
219 test_expect_success 'command-line overrides config' '
220 cp expect.letter-runs-are-words expect &&
221 word_diff --color-words="[a-z]+"
224 test_expect_success 'command-line overrides config: --word-diff-regex' '
225 cat >expect <<-EOF &&
226 <BOLD>diff --git a/pre b/post<RESET>
227 <BOLD>index $pre..$post 100644<RESET>
228 <BOLD>--- a/pre<RESET>
229 <BOLD>+++ b/post<RESET>
230 <CYAN>@@ -1,3 +1,7 @@<RESET>
231 h(4),<GREEN>{+hh+}<RESET>[44]
233 a = b + c<RESET>
235 <GREEN>{+aa = a+}<RESET>
237 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
239 word_diff --color --word-diff-regex="[a-z]+"
242 test_expect_success '.gitattributes override config' '
243 cp expect.non-whitespace-is-word expect &&
244 word_diff --color-words
247 test_expect_success 'setup: remove diff driver regex' '
248 test_unconfig diff.testdriver.wordRegex
251 test_expect_success 'use configured regex' '
252 cat >expect <<-EOF &&
253 <BOLD>diff --git a/pre b/post<RESET>
254 <BOLD>index $pre..$post 100644<RESET>
255 <BOLD>--- a/pre<RESET>
256 <BOLD>+++ b/post<RESET>
257 <CYAN>@@ -1,3 +1,7 @@<RESET>
258 h(4),<GREEN>hh[44<RESET>]
260 a = b + c<RESET>
262 <GREEN>aa = a<RESET>
264 <GREEN>aeff = aeff * ( aaa<RESET> )
266 word_diff --color-words
269 test_expect_success 'test parsing words for newline' '
270 echo "aaa (aaa)" >pre &&
271 echo "aaa (aaa) aaa" >post &&
272 pre=$(git rev-parse --short $(git hash-object pre)) &&
273 post=$(git rev-parse --short $(git hash-object post)) &&
274 cat >expect <<-EOF &&
275 <BOLD>diff --git a/pre b/post<RESET>
276 <BOLD>index $pre..$post 100644<RESET>
277 <BOLD>--- a/pre<RESET>
278 <BOLD>+++ b/post<RESET>
279 <CYAN>@@ -1 +1 @@<RESET>
280 aaa (aaa) <GREEN>aaa<RESET>
282 word_diff --color-words="a+"
285 test_expect_success 'test when words are only removed at the end' '
286 echo "(:" >pre &&
287 echo "(" >post &&
288 pre=$(git rev-parse --short $(git hash-object pre)) &&
289 post=$(git rev-parse --short $(git hash-object post)) &&
290 cat >expect <<-EOF &&
291 <BOLD>diff --git a/pre b/post<RESET>
292 <BOLD>index $pre..$post 100644<RESET>
293 <BOLD>--- a/pre<RESET>
294 <BOLD>+++ b/post<RESET>
295 <CYAN>@@ -1 +1 @@<RESET>
296 (<RED>:<RESET>
298 word_diff --color-words=.
301 test_expect_success '--word-diff=none' '
302 echo "(:" >pre &&
303 echo "(" >post &&
304 pre=$(git rev-parse --short $(git hash-object pre)) &&
305 post=$(git rev-parse --short $(git hash-object post)) &&
306 cat >expect <<-EOF &&
307 diff --git a/pre b/post
308 index $pre..$post 100644
309 --- a/pre
310 +++ b/post
311 @@ -1 +1 @@
315 word_diff --word-diff=plain --word-diff=none
318 test_expect_success 'unset default driver' '
319 test_unconfig diff.wordregex
322 test_language_driver ada
323 test_language_driver bibtex
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' '
343 cat >pre <<-\EOF &&
348 cat >post <<-\EOF &&
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
358 --- a/pre
359 +++ b/post
360 @@ -1,3 +1,3 @@
363 [-b-]{+c+}
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
377 --- a/pre
378 +++ b/post
379 @@ -1 +1 @@
380 a a [-a-]{+ab+} a a
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 &&
391 # modify both
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
402 --- a/a.tex
403 +++ b/a.tex
404 @@ -1 +1 @@
405 a [-b-]{+bx+}; c
406 diff --git a/z.txt b/z.txt
407 --- a/z.txt
408 +++ b/z.txt
409 @@ -1 +1 @@
410 a [-b;-]{+bx;+} c
412 git diff --word-diff HEAD~ >actual &&
413 compare_diff_patch expect actual
416 test_done