]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4034-diff-words.sh
The eighth batch
[thirdparty/git.git] / t / t4034-diff-words.sh
CommitLineData
2e5d2003
JS
1#!/bin/sh
2
3test_description='word diff colors'
4
5. ./test-lib.sh
ebd73f50 6. "$TEST_DIRECTORY"/lib-diff.sh
2e5d2003 7
5094d158
JN
8cat >pre.simple <<-\EOF
9 h(4)
2e5d2003 10
5094d158
JN
11 a = b + c
12EOF
13cat >post.simple <<-\EOF
14 h(4),hh[44]
2e5d2003 15
5094d158 16 a = b + c
2e5d2003 17
5094d158 18 aa = a
2e5d2003 19
5094d158 20 aeff = aeff * ( aaa )
2e5d2003 21EOF
0253e126 22pre=$(git rev-parse --short $(git hash-object pre.simple))
23post=$(git rev-parse --short $(git hash-object post.simple))
24cat >expect.letter-runs-are-words <<-EOF
5094d158 25 <BOLD>diff --git a/pre b/post<RESET>
0253e126 26 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
27 <BOLD>--- a/pre<RESET>
28 <BOLD>+++ b/post<RESET>
29 <CYAN>@@ -1,3 +1,7 @@<RESET>
30 h(4),<GREEN>hh<RESET>[44]
2e5d2003 31
5094d158 32 a = b + c<RESET>
2e5d2003 33
5094d158 34 <GREEN>aa = a<RESET>
2e5d2003 35
5094d158 36 <GREEN>aeff = aeff * ( aaa<RESET> )
2e5d2003 37EOF
0253e126 38cat >expect.non-whitespace-is-word <<-EOF
5094d158 39 <BOLD>diff --git a/pre b/post<RESET>
0253e126 40 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
41 <BOLD>--- a/pre<RESET>
42 <BOLD>+++ b/post<RESET>
43 <CYAN>@@ -1,3 +1,7 @@<RESET>
44 h(4)<GREEN>,hh[44]<RESET>
2e5d2003 45
5094d158 46 a = b + c<RESET>
2e5d2003 47
5094d158 48 <GREEN>aa = a<RESET>
2e5d2003 49
5094d158 50 <GREEN>aeff = aeff * ( aaa )<RESET>
2e5d2003
JS
51EOF
52
5094d158 53word_diff () {
0253e126 54 pre=$(git rev-parse --short $(git hash-object pre)) &&
55 post=$(git rev-parse --short $(git hash-object post)) &&
5094d158
JN
56 test_must_fail git diff --no-index "$@" pre post >output &&
57 test_decode_color <output >output.decrypted &&
0253e126 58 sed -e "2s/index [^ ]*/index $pre..$post/" expect >expected
59 test_cmp expected output.decrypted
5094d158 60}
2e5d2003 61
5094d158
JN
62test_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
70 '
be391449 71 test_expect_success "diff driver '$lang' in Islandic" '
5e91056a 72 test_env LANG=is_IS.UTF-8 LANGUAGE=is LC_ALL="$is_IS_locale" \
be391449
RS
73 word_diff --color-words
74 '
5094d158 75}
2e5d2003 76
5094d158
JN
77test_expect_success setup '
78 git config diff.color.old red &&
79 git config diff.color.new green &&
80 git config diff.color.func magenta
2e5d2003
JS
81'
82
5094d158
JN
83test_expect_success 'set up pre and post with runs of whitespace' '
84 cp pre.simple pre &&
85 cp post.simple post
882749a0
TR
86'
87
5094d158 88test_expect_success 'word diff with runs of whitespace' '
0253e126 89 cat >expect <<-EOF &&
5094d158 90 <BOLD>diff --git a/pre b/post<RESET>
0253e126 91 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
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>
96
97 a = b + c<RESET>
98
99 <GREEN>aa = a<RESET>
100
101 <GREEN>aeff = aeff * ( aaa )<RESET>
102 EOF
103 word_diff --color-words &&
104 word_diff --word-diff=color &&
882749a0 105 word_diff --color --word-diff=color
882749a0
TR
106'
107
882749a0 108test_expect_success '--word-diff=porcelain' '
c76b84a1 109 sed "s/#.*$//" >expect <<-EOF &&
5094d158 110 diff --git a/pre b/post
0253e126 111 index $pre..$post 100644
5094d158
JN
112 --- a/pre
113 +++ b/post
114 @@ -1,3 +1,7 @@
115 -h(4)
116 +h(4),hh[44]
117 ~
118 # significant space
119 ~
120 a = b + c
121 ~
122 ~
123 +aa = a
124 ~
125 ~
126 +aeff = aeff * ( aaa )
127 ~
128 EOF
882749a0 129 word_diff --word-diff=porcelain
882749a0
TR
130'
131
882749a0 132test_expect_success '--word-diff=plain' '
0253e126 133 cat >expect <<-EOF &&
5094d158 134 diff --git a/pre b/post
0253e126 135 index $pre..$post 100644
5094d158
JN
136 --- a/pre
137 +++ b/post
138 @@ -1,3 +1,7 @@
139 [-h(4)-]{+h(4),hh[44]+}
882749a0 140
5094d158 141 a = b + c
882749a0 142
5094d158 143 {+aa = a+}
882749a0 144
5094d158
JN
145 {+aeff = aeff * ( aaa )+}
146 EOF
147 word_diff --word-diff=plain &&
882749a0 148 word_diff --word-diff=plain --no-color
882749a0
TR
149'
150
5094d158 151test_expect_success '--word-diff=plain --color' '
0253e126 152 cat >expect <<-EOF &&
5094d158 153 <BOLD>diff --git a/pre b/post<RESET>
0253e126 154 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
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>
882749a0 159
5094d158 160 a = b + c<RESET>
882749a0 161
5094d158 162 <GREEN>{+aa = a+}<RESET>
882749a0 163
5094d158
JN
164 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
165 EOF
882749a0 166 word_diff --word-diff=plain --color
882749a0
TR
167'
168
a4ca1465 169test_expect_success 'word diff without context' '
0253e126 170 cat >expect <<-EOF &&
5094d158 171 <BOLD>diff --git a/pre b/post<RESET>
0253e126 172 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
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>
178
179 <GREEN>aa = a<RESET>
180
181 <GREEN>aeff = aeff * ( aaa )<RESET>
182 EOF
168eff3c 183 word_diff --color-words --unified=0
168eff3c
MH
184'
185
2b6a5417 186test_expect_success 'word diff with a regular expression' '
5094d158 187 cp expect.letter-runs-are-words expect &&
2b6a5417 188 word_diff --color-words="[a-z]+"
2b6a5417
JS
189'
190
0324e8fc
PW
191test_expect_success 'word diff with zero length matches' '
192 cp expect.letter-runs-are-words expect &&
193 word_diff --color-words="[a-z${LF}]*"
194'
195
5094d158 196test_expect_success 'set up a diff driver' '
ae3b970a 197 git config diff.testdriver.wordRegex "[^[:space:]]" &&
5094d158
JN
198 cat <<-\EOF >.gitattributes
199 pre diff=testdriver
200 post diff=testdriver
201 EOF
80c49c3d
TR
202'
203
98a4d87b 204test_expect_success 'option overrides .gitattributes' '
5094d158 205 cp expect.letter-runs-are-words expect &&
80c49c3d 206 word_diff --color-words="[a-z]+"
80c49c3d
TR
207'
208
98a4d87b 209test_expect_success 'use regex supplied by driver' '
5094d158 210 cp expect.non-whitespace-is-word expect &&
80c49c3d 211 word_diff --color-words
80c49c3d
TR
212'
213
5094d158 214test_expect_success 'set up diff.wordRegex option' '
ae3b970a 215 git config diff.wordRegex "[[:alnum:]]+"
98a4d87b
BSSJ
216'
217
98a4d87b 218test_expect_success 'command-line overrides config' '
5094d158 219 cp expect.letter-runs-are-words expect &&
98a4d87b
BSSJ
220 word_diff --color-words="[a-z]+"
221'
222
5094d158 223test_expect_success 'command-line overrides config: --word-diff-regex' '
0253e126 224 cat >expect <<-EOF &&
5094d158 225 <BOLD>diff --git a/pre b/post<RESET>
0253e126 226 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
227 <BOLD>--- a/pre<RESET>
228 <BOLD>+++ b/post<RESET>
229 <CYAN>@@ -1,3 +1,7 @@<RESET>
230 h(4),<GREEN>{+hh+}<RESET>[44]
882749a0 231
5094d158 232 a = b + c<RESET>
882749a0 233
5094d158 234 <GREEN>{+aa = a+}<RESET>
882749a0 235
5094d158
JN
236 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
237 EOF
882749a0
TR
238 word_diff --color --word-diff-regex="[a-z]+"
239'
240
98a4d87b 241test_expect_success '.gitattributes override config' '
5094d158 242 cp expect.non-whitespace-is-word expect &&
98a4d87b
BSSJ
243 word_diff --color-words
244'
245
5094d158 246test_expect_success 'setup: remove diff driver regex' '
ff73aa40 247 test_unconfig diff.testdriver.wordRegex
98a4d87b
BSSJ
248'
249
5094d158 250test_expect_success 'use configured regex' '
0253e126 251 cat >expect <<-EOF &&
5094d158 252 <BOLD>diff --git a/pre b/post<RESET>
0253e126 253 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
254 <BOLD>--- a/pre<RESET>
255 <BOLD>+++ b/post<RESET>
256 <CYAN>@@ -1,3 +1,7 @@<RESET>
257 h(4),<GREEN>hh[44<RESET>]
98a4d87b 258
5094d158 259 a = b + c<RESET>
98a4d87b 260
5094d158 261 <GREEN>aa = a<RESET>
98a4d87b 262
5094d158
JN
263 <GREEN>aeff = aeff * ( aaa<RESET> )
264 EOF
98a4d87b
BSSJ
265 word_diff --color-words
266'
267
2b6a5417 268test_expect_success 'test parsing words for newline' '
5094d158
JN
269 echo "aaa (aaa)" >pre &&
270 echo "aaa (aaa) aaa" >post &&
0253e126 271 pre=$(git rev-parse --short $(git hash-object pre)) &&
272 post=$(git rev-parse --short $(git hash-object post)) &&
273 cat >expect <<-EOF &&
5094d158 274 <BOLD>diff --git a/pre b/post<RESET>
0253e126 275 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
276 <BOLD>--- a/pre<RESET>
277 <BOLD>+++ b/post<RESET>
278 <CYAN>@@ -1 +1 @@<RESET>
279 aaa (aaa) <GREEN>aaa<RESET>
280 EOF
2b6a5417 281 word_diff --color-words="a+"
2b6a5417
JS
282'
283
2b6a5417 284test_expect_success 'test when words are only removed at the end' '
5094d158
JN
285 echo "(:" >pre &&
286 echo "(" >post &&
0253e126 287 pre=$(git rev-parse --short $(git hash-object pre)) &&
288 post=$(git rev-parse --short $(git hash-object post)) &&
289 cat >expect <<-EOF &&
5094d158 290 <BOLD>diff --git a/pre b/post<RESET>
0253e126 291 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
292 <BOLD>--- a/pre<RESET>
293 <BOLD>+++ b/post<RESET>
294 <CYAN>@@ -1 +1 @@<RESET>
295 (<RED>:<RESET>
296 EOF
2b6a5417 297 word_diff --color-words=.
2b6a5417
JS
298'
299
882749a0 300test_expect_success '--word-diff=none' '
5094d158
JN
301 echo "(:" >pre &&
302 echo "(" >post &&
0253e126 303 pre=$(git rev-parse --short $(git hash-object pre)) &&
304 post=$(git rev-parse --short $(git hash-object post)) &&
305 cat >expect <<-EOF &&
5094d158 306 diff --git a/pre b/post
0253e126 307 index $pre..$post 100644
5094d158
JN
308 --- a/pre
309 +++ b/post
310 @@ -1 +1 @@
311 -(:
312 +(
313 EOF
882749a0 314 word_diff --word-diff=plain --word-diff=none
882749a0
TR
315'
316
62d39359
JS
317test_expect_success 'unset default driver' '
318 test_unconfig diff.wordregex
319'
320
e90d065e 321test_language_driver ada
5094d158 322test_language_driver bibtex
ea8a71b4 323test_language_driver bash
5094d158
JN
324test_language_driver cpp
325test_language_driver csharp
0719f3ee 326test_language_driver css
3c81760b 327test_language_driver dts
5094d158
JN
328test_language_driver fortran
329test_language_driver html
330test_language_driver java
09188ed9 331test_language_driver kotlin
53b10a14 332test_language_driver matlab
5094d158
JN
333test_language_driver objc
334test_language_driver pascal
5269edf1 335test_language_driver perl
5094d158
JN
336test_language_driver php
337test_language_driver python
338test_language_driver ruby
a4373903 339test_language_driver scheme
5094d158 340test_language_driver tex
8d96e728 341
42536dd9 342test_expect_success 'word-diff with diff.sbe' '
42536dd9
JM
343 cat >pre <<-\EOF &&
344 a
345
346 b
347 EOF
348 cat >post <<-\EOF &&
349 a
350
351 c
352 EOF
0253e126 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 @@
361 a
362
363 [-b-]{+c+}
364 EOF
ff73aa40 365 test_config diff.suppress-blank-empty true &&
42536dd9
JM
366 word_diff --word-diff=plain
367'
368
c7c2bc0a 369test_expect_success 'word-diff with no newline at EOF' '
0253e126 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 &&
c7c2bc0a 375 diff --git a/pre b/post
0253e126 376 index $pre..$post 100644
c7c2bc0a
TR
377 --- a/pre
378 +++ b/post
379 @@ -1 +1 @@
380 a a [-a-]{+ab+} a a
381 EOF
c7c2bc0a
TR
382 word_diff --word-diff=plain
383'
384
62d39359
JS
385test_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 &&
390
391 # modify both
392 echo "a bx; c" >a.tex &&
393 echo "a bx; c" >z.txt &&
394 git commit -mmodified -a
395'
396
6440d341 397test_expect_success 'wordRegex for the first file does not apply to the second' '
62d39359 398 echo "*.tex diff=tex" >.gitattributes &&
ff73aa40 399 test_config diff.tex.wordRegex "[a-z]+|." &&
62d39359
JS
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
411 EOF
412 git diff --word-diff HEAD~ >actual &&
413 compare_diff_patch expect actual
414'
415
2e5d2003 416test_done