The third batch
[git/debian.git] / t / t4034-diff-words.sh
blob561c582d161551147d8e4805aa35ba4178285833
1 #!/bin/sh
3 test_description='word diff colors'
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-diff.sh
8 cat >pre.simple <<-\EOF
9 h(4)
11 a = b + c
12 EOF
13 cat >post.simple <<-\EOF
14 h(4),hh[44]
16 a = b + c
18 aa = a
20 aeff = aeff * ( aaa )
21 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]
32 a = b + c<RESET>
34 <GREEN>aa = a<RESET>
36 <GREEN>aeff = aeff * ( aaa<RESET> )
37 EOF
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>
46 a = b + c<RESET>
48 <GREEN>aa = a<RESET>
50 <GREEN>aeff = aeff * ( aaa )<RESET>
51 EOF
53 word_diff () {
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 () {
63 lang=$1
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
73 test_expect_success setup '
74 git config diff.color.old red &&
75 git config diff.color.new green &&
76 git config diff.color.func magenta
79 test_expect_success 'set up pre and post with runs of whitespace' '
80 cp pre.simple pre &&
81 cp post.simple post
84 test_expect_success 'word diff with runs of whitespace' '
85 cat >expect <<-EOF &&
86 <BOLD>diff --git a/pre b/post<RESET>
87 <BOLD>index $pre..$post 100644<RESET>
88 <BOLD>--- a/pre<RESET>
89 <BOLD>+++ b/post<RESET>
90 <CYAN>@@ -1,3 +1,7 @@<RESET>
91 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
93 a = b + c<RESET>
95 <GREEN>aa = a<RESET>
97 <GREEN>aeff = aeff * ( aaa )<RESET>
98 EOF
99 word_diff --color-words &&
100 word_diff --word-diff=color &&
101 word_diff --color --word-diff=color
104 test_expect_success '--word-diff=porcelain' '
105 sed "s/#.*$//" >expect <<-EOF &&
106 diff --git a/pre b/post
107 index $pre..$post 100644
108 --- a/pre
109 +++ b/post
110 @@ -1,3 +1,7 @@
111 -h(4)
112 +h(4),hh[44]
114 # significant space
116 a = b + c
119 +aa = a
122 +aeff = aeff * ( aaa )
125 word_diff --word-diff=porcelain
128 test_expect_success '--word-diff=plain' '
129 cat >expect <<-EOF &&
130 diff --git a/pre b/post
131 index $pre..$post 100644
132 --- a/pre
133 +++ b/post
134 @@ -1,3 +1,7 @@
135 [-h(4)-]{+h(4),hh[44]+}
137 a = b + c
139 {+aa = a+}
141 {+aeff = aeff * ( aaa )+}
143 word_diff --word-diff=plain &&
144 word_diff --word-diff=plain --no-color
147 test_expect_success '--word-diff=plain --color' '
148 cat >expect <<-EOF &&
149 <BOLD>diff --git a/pre b/post<RESET>
150 <BOLD>index $pre..$post 100644<RESET>
151 <BOLD>--- a/pre<RESET>
152 <BOLD>+++ b/post<RESET>
153 <CYAN>@@ -1,3 +1,7 @@<RESET>
154 <RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
156 a = b + c<RESET>
158 <GREEN>{+aa = a+}<RESET>
160 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
162 word_diff --word-diff=plain --color
165 test_expect_success 'word diff without context' '
166 cat >expect <<-EOF &&
167 <BOLD>diff --git a/pre b/post<RESET>
168 <BOLD>index $pre..$post 100644<RESET>
169 <BOLD>--- a/pre<RESET>
170 <BOLD>+++ b/post<RESET>
171 <CYAN>@@ -1 +1 @@<RESET>
172 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
173 <CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
175 <GREEN>aa = a<RESET>
177 <GREEN>aeff = aeff * ( aaa )<RESET>
179 word_diff --color-words --unified=0
182 test_expect_success 'word diff with a regular expression' '
183 cp expect.letter-runs-are-words expect &&
184 word_diff --color-words="[a-z]+"
187 test_expect_success 'word diff with zero length matches' '
188 cp expect.letter-runs-are-words expect &&
189 word_diff --color-words="[a-z${LF}]*"
192 test_expect_success 'set up a diff driver' '
193 git config diff.testdriver.wordRegex "[^[:space:]]" &&
194 cat <<-\EOF >.gitattributes
195 pre diff=testdriver
196 post diff=testdriver
200 test_expect_success 'option overrides .gitattributes' '
201 cp expect.letter-runs-are-words expect &&
202 word_diff --color-words="[a-z]+"
205 test_expect_success 'use regex supplied by driver' '
206 cp expect.non-whitespace-is-word expect &&
207 word_diff --color-words
210 test_expect_success 'set up diff.wordRegex option' '
211 git config diff.wordRegex "[[:alnum:]]+"
214 test_expect_success 'command-line overrides config' '
215 cp expect.letter-runs-are-words expect &&
216 word_diff --color-words="[a-z]+"
219 test_expect_success 'command-line overrides config: --word-diff-regex' '
220 cat >expect <<-EOF &&
221 <BOLD>diff --git a/pre b/post<RESET>
222 <BOLD>index $pre..$post 100644<RESET>
223 <BOLD>--- a/pre<RESET>
224 <BOLD>+++ b/post<RESET>
225 <CYAN>@@ -1,3 +1,7 @@<RESET>
226 h(4),<GREEN>{+hh+}<RESET>[44]
228 a = b + c<RESET>
230 <GREEN>{+aa = a+}<RESET>
232 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
234 word_diff --color --word-diff-regex="[a-z]+"
237 test_expect_success '.gitattributes override config' '
238 cp expect.non-whitespace-is-word expect &&
239 word_diff --color-words
242 test_expect_success 'setup: remove diff driver regex' '
243 test_unconfig diff.testdriver.wordRegex
246 test_expect_success 'use configured regex' '
247 cat >expect <<-EOF &&
248 <BOLD>diff --git a/pre b/post<RESET>
249 <BOLD>index $pre..$post 100644<RESET>
250 <BOLD>--- a/pre<RESET>
251 <BOLD>+++ b/post<RESET>
252 <CYAN>@@ -1,3 +1,7 @@<RESET>
253 h(4),<GREEN>hh[44<RESET>]
255 a = b + c<RESET>
257 <GREEN>aa = a<RESET>
259 <GREEN>aeff = aeff * ( aaa<RESET> )
261 word_diff --color-words
264 test_expect_success 'test parsing words for newline' '
265 echo "aaa (aaa)" >pre &&
266 echo "aaa (aaa) aaa" >post &&
267 pre=$(git rev-parse --short $(git hash-object pre)) &&
268 post=$(git rev-parse --short $(git hash-object post)) &&
269 cat >expect <<-EOF &&
270 <BOLD>diff --git a/pre b/post<RESET>
271 <BOLD>index $pre..$post 100644<RESET>
272 <BOLD>--- a/pre<RESET>
273 <BOLD>+++ b/post<RESET>
274 <CYAN>@@ -1 +1 @@<RESET>
275 aaa (aaa) <GREEN>aaa<RESET>
277 word_diff --color-words="a+"
280 test_expect_success 'test when words are only removed at the end' '
281 echo "(:" >pre &&
282 echo "(" >post &&
283 pre=$(git rev-parse --short $(git hash-object pre)) &&
284 post=$(git rev-parse --short $(git hash-object post)) &&
285 cat >expect <<-EOF &&
286 <BOLD>diff --git a/pre b/post<RESET>
287 <BOLD>index $pre..$post 100644<RESET>
288 <BOLD>--- a/pre<RESET>
289 <BOLD>+++ b/post<RESET>
290 <CYAN>@@ -1 +1 @@<RESET>
291 (<RED>:<RESET>
293 word_diff --color-words=.
296 test_expect_success '--word-diff=none' '
297 echo "(:" >pre &&
298 echo "(" >post &&
299 pre=$(git rev-parse --short $(git hash-object pre)) &&
300 post=$(git rev-parse --short $(git hash-object post)) &&
301 cat >expect <<-EOF &&
302 diff --git a/pre b/post
303 index $pre..$post 100644
304 --- a/pre
305 +++ b/post
306 @@ -1 +1 @@
310 word_diff --word-diff=plain --word-diff=none
313 test_expect_success 'unset default driver' '
314 test_unconfig diff.wordregex
317 test_language_driver ada
318 test_language_driver bibtex
319 test_language_driver cpp
320 test_language_driver csharp
321 test_language_driver css
322 test_language_driver dts
323 test_language_driver fortran
324 test_language_driver html
325 test_language_driver java
326 test_language_driver matlab
327 test_language_driver objc
328 test_language_driver pascal
329 test_language_driver perl
330 test_language_driver php
331 test_language_driver python
332 test_language_driver ruby
333 test_language_driver scheme
334 test_language_driver tex
336 test_expect_success 'word-diff with diff.sbe' '
337 cat >pre <<-\EOF &&
342 cat >post <<-\EOF &&
347 pre=$(git rev-parse --short $(git hash-object pre)) &&
348 post=$(git rev-parse --short $(git hash-object post)) &&
349 cat >expect <<-EOF &&
350 diff --git a/pre b/post
351 index $pre..$post 100644
352 --- a/pre
353 +++ b/post
354 @@ -1,3 +1,3 @@
357 [-b-]{+c+}
359 test_config diff.suppress-blank-empty true &&
360 word_diff --word-diff=plain
363 test_expect_success 'word-diff with no newline at EOF' '
364 printf "%s" "a a a a a" >pre &&
365 printf "%s" "a a ab a a" >post &&
366 pre=$(git rev-parse --short $(git hash-object pre)) &&
367 post=$(git rev-parse --short $(git hash-object post)) &&
368 cat >expect <<-EOF &&
369 diff --git a/pre b/post
370 index $pre..$post 100644
371 --- a/pre
372 +++ b/post
373 @@ -1 +1 @@
374 a a [-a-]{+ab+} a a
376 word_diff --word-diff=plain
379 test_expect_success 'setup history with two files' '
380 echo "a b; c" >a.tex &&
381 echo "a b; c" >z.txt &&
382 git add a.tex z.txt &&
383 git commit -minitial &&
385 # modify both
386 echo "a bx; c" >a.tex &&
387 echo "a bx; c" >z.txt &&
388 git commit -mmodified -a
391 test_expect_success 'wordRegex for the first file does not apply to the second' '
392 echo "*.tex diff=tex" >.gitattributes &&
393 test_config diff.tex.wordRegex "[a-z]+|." &&
394 cat >expect <<-\EOF &&
395 diff --git a/a.tex b/a.tex
396 --- a/a.tex
397 +++ b/a.tex
398 @@ -1 +1 @@
399 a [-b-]{+bx+}; c
400 diff --git a/z.txt b/z.txt
401 --- a/z.txt
402 +++ b/z.txt
403 @@ -1 +1 @@
404 a [-b;-]{+bx;+} c
406 git diff --word-diff HEAD~ >actual &&
407 compare_diff_patch expect actual
410 test_done