]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4034-diff-words.sh
Start the 2.46 cycle
[thirdparty/git.git] / t / t4034-diff-words.sh
CommitLineData
2e5d2003
JS
1#!/bin/sh
2
3test_description='word diff colors'
4
16d4bd4f 5TEST_PASSES_SANITIZE_LEAK=true
2e5d2003 6. ./test-lib.sh
ebd73f50 7. "$TEST_DIRECTORY"/lib-diff.sh
2e5d2003 8
5094d158
JN
9cat >pre.simple <<-\EOF
10 h(4)
2e5d2003 11
5094d158
JN
12 a = b + c
13EOF
14cat >post.simple <<-\EOF
15 h(4),hh[44]
2e5d2003 16
5094d158 17 a = b + c
2e5d2003 18
5094d158 19 aa = a
2e5d2003 20
5094d158 21 aeff = aeff * ( aaa )
2e5d2003 22EOF
0253e126 23pre=$(git rev-parse --short $(git hash-object pre.simple))
24post=$(git rev-parse --short $(git hash-object post.simple))
25cat >expect.letter-runs-are-words <<-EOF
5094d158 26 <BOLD>diff --git a/pre b/post<RESET>
0253e126 27 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
28 <BOLD>--- a/pre<RESET>
29 <BOLD>+++ b/post<RESET>
30 <CYAN>@@ -1,3 +1,7 @@<RESET>
31 h(4),<GREEN>hh<RESET>[44]
2e5d2003 32
5094d158 33 a = b + c<RESET>
2e5d2003 34
5094d158 35 <GREEN>aa = a<RESET>
2e5d2003 36
5094d158 37 <GREEN>aeff = aeff * ( aaa<RESET> )
2e5d2003 38EOF
0253e126 39cat >expect.non-whitespace-is-word <<-EOF
5094d158 40 <BOLD>diff --git a/pre b/post<RESET>
0253e126 41 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
42 <BOLD>--- a/pre<RESET>
43 <BOLD>+++ b/post<RESET>
44 <CYAN>@@ -1,3 +1,7 @@<RESET>
45 h(4)<GREEN>,hh[44]<RESET>
2e5d2003 46
5094d158 47 a = b + c<RESET>
2e5d2003 48
5094d158 49 <GREEN>aa = a<RESET>
2e5d2003 50
5094d158 51 <GREEN>aeff = aeff * ( aaa )<RESET>
2e5d2003
JS
52EOF
53
5094d158 54word_diff () {
0253e126 55 pre=$(git rev-parse --short $(git hash-object pre)) &&
56 post=$(git rev-parse --short $(git hash-object post)) &&
5094d158
JN
57 test_must_fail git diff --no-index "$@" pre post >output &&
58 test_decode_color <output >output.decrypted &&
0253e126 59 sed -e "2s/index [^ ]*/index $pre..$post/" expect >expected
60 test_cmp expected output.decrypted
5094d158 61}
2e5d2003 62
5094d158
JN
63test_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
71 '
be391449
RS
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
75 '
5094d158 76}
2e5d2003 77
5094d158
JN
78test_expect_success setup '
79 git config diff.color.old red &&
80 git config diff.color.new green &&
81 git config diff.color.func magenta
2e5d2003
JS
82'
83
5094d158
JN
84test_expect_success 'set up pre and post with runs of whitespace' '
85 cp pre.simple pre &&
86 cp post.simple post
882749a0
TR
87'
88
5094d158 89test_expect_success 'word diff with runs of whitespace' '
0253e126 90 cat >expect <<-EOF &&
5094d158 91 <BOLD>diff --git a/pre b/post<RESET>
0253e126 92 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
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>
97
98 a = b + c<RESET>
99
100 <GREEN>aa = a<RESET>
101
102 <GREEN>aeff = aeff * ( aaa )<RESET>
103 EOF
104 word_diff --color-words &&
105 word_diff --word-diff=color &&
882749a0 106 word_diff --color --word-diff=color
882749a0
TR
107'
108
882749a0 109test_expect_success '--word-diff=porcelain' '
c76b84a1 110 sed "s/#.*$//" >expect <<-EOF &&
5094d158 111 diff --git a/pre b/post
0253e126 112 index $pre..$post 100644
5094d158
JN
113 --- a/pre
114 +++ b/post
115 @@ -1,3 +1,7 @@
116 -h(4)
117 +h(4),hh[44]
118 ~
119 # significant space
120 ~
121 a = b + c
122 ~
123 ~
124 +aa = a
125 ~
126 ~
127 +aeff = aeff * ( aaa )
128 ~
129 EOF
882749a0 130 word_diff --word-diff=porcelain
882749a0
TR
131'
132
882749a0 133test_expect_success '--word-diff=plain' '
0253e126 134 cat >expect <<-EOF &&
5094d158 135 diff --git a/pre b/post
0253e126 136 index $pre..$post 100644
5094d158
JN
137 --- a/pre
138 +++ b/post
139 @@ -1,3 +1,7 @@
140 [-h(4)-]{+h(4),hh[44]+}
882749a0 141
5094d158 142 a = b + c
882749a0 143
5094d158 144 {+aa = a+}
882749a0 145
5094d158
JN
146 {+aeff = aeff * ( aaa )+}
147 EOF
148 word_diff --word-diff=plain &&
882749a0 149 word_diff --word-diff=plain --no-color
882749a0
TR
150'
151
5094d158 152test_expect_success '--word-diff=plain --color' '
0253e126 153 cat >expect <<-EOF &&
5094d158 154 <BOLD>diff --git a/pre b/post<RESET>
0253e126 155 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
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>
882749a0 160
5094d158 161 a = b + c<RESET>
882749a0 162
5094d158 163 <GREEN>{+aa = a+}<RESET>
882749a0 164
5094d158
JN
165 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
166 EOF
882749a0 167 word_diff --word-diff=plain --color
882749a0
TR
168'
169
a4ca1465 170test_expect_success 'word diff without context' '
0253e126 171 cat >expect <<-EOF &&
5094d158 172 <BOLD>diff --git a/pre b/post<RESET>
0253e126 173 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
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>
179
180 <GREEN>aa = a<RESET>
181
182 <GREEN>aeff = aeff * ( aaa )<RESET>
183 EOF
168eff3c 184 word_diff --color-words --unified=0
168eff3c
MH
185'
186
2b6a5417 187test_expect_success 'word diff with a regular expression' '
5094d158 188 cp expect.letter-runs-are-words expect &&
2b6a5417 189 word_diff --color-words="[a-z]+"
2b6a5417
JS
190'
191
0324e8fc
PW
192test_expect_success 'word diff with zero length matches' '
193 cp expect.letter-runs-are-words expect &&
194 word_diff --color-words="[a-z${LF}]*"
195'
196
5094d158 197test_expect_success 'set up a diff driver' '
ae3b970a 198 git config diff.testdriver.wordRegex "[^[:space:]]" &&
5094d158
JN
199 cat <<-\EOF >.gitattributes
200 pre diff=testdriver
201 post diff=testdriver
202 EOF
80c49c3d
TR
203'
204
98a4d87b 205test_expect_success 'option overrides .gitattributes' '
5094d158 206 cp expect.letter-runs-are-words expect &&
80c49c3d 207 word_diff --color-words="[a-z]+"
80c49c3d
TR
208'
209
98a4d87b 210test_expect_success 'use regex supplied by driver' '
5094d158 211 cp expect.non-whitespace-is-word expect &&
80c49c3d 212 word_diff --color-words
80c49c3d
TR
213'
214
5094d158 215test_expect_success 'set up diff.wordRegex option' '
ae3b970a 216 git config diff.wordRegex "[[:alnum:]]+"
98a4d87b
BSSJ
217'
218
98a4d87b 219test_expect_success 'command-line overrides config' '
5094d158 220 cp expect.letter-runs-are-words expect &&
98a4d87b
BSSJ
221 word_diff --color-words="[a-z]+"
222'
223
5094d158 224test_expect_success 'command-line overrides config: --word-diff-regex' '
0253e126 225 cat >expect <<-EOF &&
5094d158 226 <BOLD>diff --git a/pre b/post<RESET>
0253e126 227 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
228 <BOLD>--- a/pre<RESET>
229 <BOLD>+++ b/post<RESET>
230 <CYAN>@@ -1,3 +1,7 @@<RESET>
231 h(4),<GREEN>{+hh+}<RESET>[44]
882749a0 232
5094d158 233 a = b + c<RESET>
882749a0 234
5094d158 235 <GREEN>{+aa = a+}<RESET>
882749a0 236
5094d158
JN
237 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
238 EOF
882749a0
TR
239 word_diff --color --word-diff-regex="[a-z]+"
240'
241
98a4d87b 242test_expect_success '.gitattributes override config' '
5094d158 243 cp expect.non-whitespace-is-word expect &&
98a4d87b
BSSJ
244 word_diff --color-words
245'
246
5094d158 247test_expect_success 'setup: remove diff driver regex' '
ff73aa40 248 test_unconfig diff.testdriver.wordRegex
98a4d87b
BSSJ
249'
250
5094d158 251test_expect_success 'use configured regex' '
0253e126 252 cat >expect <<-EOF &&
5094d158 253 <BOLD>diff --git a/pre b/post<RESET>
0253e126 254 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
255 <BOLD>--- a/pre<RESET>
256 <BOLD>+++ b/post<RESET>
257 <CYAN>@@ -1,3 +1,7 @@<RESET>
258 h(4),<GREEN>hh[44<RESET>]
98a4d87b 259
5094d158 260 a = b + c<RESET>
98a4d87b 261
5094d158 262 <GREEN>aa = a<RESET>
98a4d87b 263
5094d158
JN
264 <GREEN>aeff = aeff * ( aaa<RESET> )
265 EOF
98a4d87b
BSSJ
266 word_diff --color-words
267'
268
2b6a5417 269test_expect_success 'test parsing words for newline' '
5094d158
JN
270 echo "aaa (aaa)" >pre &&
271 echo "aaa (aaa) aaa" >post &&
0253e126 272 pre=$(git rev-parse --short $(git hash-object pre)) &&
273 post=$(git rev-parse --short $(git hash-object post)) &&
274 cat >expect <<-EOF &&
5094d158 275 <BOLD>diff --git a/pre b/post<RESET>
0253e126 276 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
277 <BOLD>--- a/pre<RESET>
278 <BOLD>+++ b/post<RESET>
279 <CYAN>@@ -1 +1 @@<RESET>
280 aaa (aaa) <GREEN>aaa<RESET>
281 EOF
2b6a5417 282 word_diff --color-words="a+"
2b6a5417
JS
283'
284
2b6a5417 285test_expect_success 'test when words are only removed at the end' '
5094d158
JN
286 echo "(:" >pre &&
287 echo "(" >post &&
0253e126 288 pre=$(git rev-parse --short $(git hash-object pre)) &&
289 post=$(git rev-parse --short $(git hash-object post)) &&
290 cat >expect <<-EOF &&
5094d158 291 <BOLD>diff --git a/pre b/post<RESET>
0253e126 292 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
293 <BOLD>--- a/pre<RESET>
294 <BOLD>+++ b/post<RESET>
295 <CYAN>@@ -1 +1 @@<RESET>
296 (<RED>:<RESET>
297 EOF
2b6a5417 298 word_diff --color-words=.
2b6a5417
JS
299'
300
882749a0 301test_expect_success '--word-diff=none' '
5094d158
JN
302 echo "(:" >pre &&
303 echo "(" >post &&
0253e126 304 pre=$(git rev-parse --short $(git hash-object pre)) &&
305 post=$(git rev-parse --short $(git hash-object post)) &&
306 cat >expect <<-EOF &&
5094d158 307 diff --git a/pre b/post
0253e126 308 index $pre..$post 100644
5094d158
JN
309 --- a/pre
310 +++ b/post
311 @@ -1 +1 @@
312 -(:
313 +(
314 EOF
882749a0 315 word_diff --word-diff=plain --word-diff=none
882749a0
TR
316'
317
62d39359
JS
318test_expect_success 'unset default driver' '
319 test_unconfig diff.wordregex
320'
321
e90d065e 322test_language_driver ada
5094d158
JN
323test_language_driver bibtex
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