]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4034-diff-words.sh
Sync with 2.36.3
[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 '
72}
2e5d2003 73
5094d158
JN
74test_expect_success setup '
75 git config diff.color.old red &&
76 git config diff.color.new green &&
77 git config diff.color.func magenta
2e5d2003
JS
78'
79
5094d158
JN
80test_expect_success 'set up pre and post with runs of whitespace' '
81 cp pre.simple pre &&
82 cp post.simple post
882749a0
TR
83'
84
5094d158 85test_expect_success 'word diff with runs of whitespace' '
0253e126 86 cat >expect <<-EOF &&
5094d158 87 <BOLD>diff --git a/pre b/post<RESET>
0253e126 88 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
89 <BOLD>--- a/pre<RESET>
90 <BOLD>+++ b/post<RESET>
91 <CYAN>@@ -1,3 +1,7 @@<RESET>
92 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
93
94 a = b + c<RESET>
95
96 <GREEN>aa = a<RESET>
97
98 <GREEN>aeff = aeff * ( aaa )<RESET>
99 EOF
100 word_diff --color-words &&
101 word_diff --word-diff=color &&
882749a0 102 word_diff --color --word-diff=color
882749a0
TR
103'
104
882749a0 105test_expect_success '--word-diff=porcelain' '
c76b84a1 106 sed "s/#.*$//" >expect <<-EOF &&
5094d158 107 diff --git a/pre b/post
0253e126 108 index $pre..$post 100644
5094d158
JN
109 --- a/pre
110 +++ b/post
111 @@ -1,3 +1,7 @@
112 -h(4)
113 +h(4),hh[44]
114 ~
115 # significant space
116 ~
117 a = b + c
118 ~
119 ~
120 +aa = a
121 ~
122 ~
123 +aeff = aeff * ( aaa )
124 ~
125 EOF
882749a0 126 word_diff --word-diff=porcelain
882749a0
TR
127'
128
882749a0 129test_expect_success '--word-diff=plain' '
0253e126 130 cat >expect <<-EOF &&
5094d158 131 diff --git a/pre b/post
0253e126 132 index $pre..$post 100644
5094d158
JN
133 --- a/pre
134 +++ b/post
135 @@ -1,3 +1,7 @@
136 [-h(4)-]{+h(4),hh[44]+}
882749a0 137
5094d158 138 a = b + c
882749a0 139
5094d158 140 {+aa = a+}
882749a0 141
5094d158
JN
142 {+aeff = aeff * ( aaa )+}
143 EOF
144 word_diff --word-diff=plain &&
882749a0 145 word_diff --word-diff=plain --no-color
882749a0
TR
146'
147
5094d158 148test_expect_success '--word-diff=plain --color' '
0253e126 149 cat >expect <<-EOF &&
5094d158 150 <BOLD>diff --git a/pre b/post<RESET>
0253e126 151 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
152 <BOLD>--- a/pre<RESET>
153 <BOLD>+++ b/post<RESET>
154 <CYAN>@@ -1,3 +1,7 @@<RESET>
155 <RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
882749a0 156
5094d158 157 a = b + c<RESET>
882749a0 158
5094d158 159 <GREEN>{+aa = a+}<RESET>
882749a0 160
5094d158
JN
161 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
162 EOF
882749a0 163 word_diff --word-diff=plain --color
882749a0
TR
164'
165
a4ca1465 166test_expect_success 'word diff without context' '
0253e126 167 cat >expect <<-EOF &&
5094d158 168 <BOLD>diff --git a/pre b/post<RESET>
0253e126 169 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
170 <BOLD>--- a/pre<RESET>
171 <BOLD>+++ b/post<RESET>
172 <CYAN>@@ -1 +1 @@<RESET>
173 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
174 <CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
175
176 <GREEN>aa = a<RESET>
177
178 <GREEN>aeff = aeff * ( aaa )<RESET>
179 EOF
168eff3c 180 word_diff --color-words --unified=0
168eff3c
MH
181'
182
2b6a5417 183test_expect_success 'word diff with a regular expression' '
5094d158 184 cp expect.letter-runs-are-words expect &&
2b6a5417 185 word_diff --color-words="[a-z]+"
2b6a5417
JS
186'
187
0324e8fc
PW
188test_expect_success 'word diff with zero length matches' '
189 cp expect.letter-runs-are-words expect &&
190 word_diff --color-words="[a-z${LF}]*"
191'
192
5094d158 193test_expect_success 'set up a diff driver' '
ae3b970a 194 git config diff.testdriver.wordRegex "[^[:space:]]" &&
5094d158
JN
195 cat <<-\EOF >.gitattributes
196 pre diff=testdriver
197 post diff=testdriver
198 EOF
80c49c3d
TR
199'
200
98a4d87b 201test_expect_success 'option overrides .gitattributes' '
5094d158 202 cp expect.letter-runs-are-words expect &&
80c49c3d 203 word_diff --color-words="[a-z]+"
80c49c3d
TR
204'
205
98a4d87b 206test_expect_success 'use regex supplied by driver' '
5094d158 207 cp expect.non-whitespace-is-word expect &&
80c49c3d 208 word_diff --color-words
80c49c3d
TR
209'
210
5094d158 211test_expect_success 'set up diff.wordRegex option' '
ae3b970a 212 git config diff.wordRegex "[[:alnum:]]+"
98a4d87b
BSSJ
213'
214
98a4d87b 215test_expect_success 'command-line overrides config' '
5094d158 216 cp expect.letter-runs-are-words expect &&
98a4d87b
BSSJ
217 word_diff --color-words="[a-z]+"
218'
219
5094d158 220test_expect_success 'command-line overrides config: --word-diff-regex' '
0253e126 221 cat >expect <<-EOF &&
5094d158 222 <BOLD>diff --git a/pre b/post<RESET>
0253e126 223 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
224 <BOLD>--- a/pre<RESET>
225 <BOLD>+++ b/post<RESET>
226 <CYAN>@@ -1,3 +1,7 @@<RESET>
227 h(4),<GREEN>{+hh+}<RESET>[44]
882749a0 228
5094d158 229 a = b + c<RESET>
882749a0 230
5094d158 231 <GREEN>{+aa = a+}<RESET>
882749a0 232
5094d158
JN
233 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
234 EOF
882749a0
TR
235 word_diff --color --word-diff-regex="[a-z]+"
236'
237
98a4d87b 238test_expect_success '.gitattributes override config' '
5094d158 239 cp expect.non-whitespace-is-word expect &&
98a4d87b
BSSJ
240 word_diff --color-words
241'
242
5094d158 243test_expect_success 'setup: remove diff driver regex' '
ff73aa40 244 test_unconfig diff.testdriver.wordRegex
98a4d87b
BSSJ
245'
246
5094d158 247test_expect_success 'use configured regex' '
0253e126 248 cat >expect <<-EOF &&
5094d158 249 <BOLD>diff --git a/pre b/post<RESET>
0253e126 250 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
251 <BOLD>--- a/pre<RESET>
252 <BOLD>+++ b/post<RESET>
253 <CYAN>@@ -1,3 +1,7 @@<RESET>
254 h(4),<GREEN>hh[44<RESET>]
98a4d87b 255
5094d158 256 a = b + c<RESET>
98a4d87b 257
5094d158 258 <GREEN>aa = a<RESET>
98a4d87b 259
5094d158
JN
260 <GREEN>aeff = aeff * ( aaa<RESET> )
261 EOF
98a4d87b
BSSJ
262 word_diff --color-words
263'
264
2b6a5417 265test_expect_success 'test parsing words for newline' '
5094d158
JN
266 echo "aaa (aaa)" >pre &&
267 echo "aaa (aaa) aaa" >post &&
0253e126 268 pre=$(git rev-parse --short $(git hash-object pre)) &&
269 post=$(git rev-parse --short $(git hash-object post)) &&
270 cat >expect <<-EOF &&
5094d158 271 <BOLD>diff --git a/pre b/post<RESET>
0253e126 272 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
273 <BOLD>--- a/pre<RESET>
274 <BOLD>+++ b/post<RESET>
275 <CYAN>@@ -1 +1 @@<RESET>
276 aaa (aaa) <GREEN>aaa<RESET>
277 EOF
2b6a5417 278 word_diff --color-words="a+"
2b6a5417
JS
279'
280
2b6a5417 281test_expect_success 'test when words are only removed at the end' '
5094d158
JN
282 echo "(:" >pre &&
283 echo "(" >post &&
0253e126 284 pre=$(git rev-parse --short $(git hash-object pre)) &&
285 post=$(git rev-parse --short $(git hash-object post)) &&
286 cat >expect <<-EOF &&
5094d158 287 <BOLD>diff --git a/pre b/post<RESET>
0253e126 288 <BOLD>index $pre..$post 100644<RESET>
5094d158
JN
289 <BOLD>--- a/pre<RESET>
290 <BOLD>+++ b/post<RESET>
291 <CYAN>@@ -1 +1 @@<RESET>
292 (<RED>:<RESET>
293 EOF
2b6a5417 294 word_diff --color-words=.
2b6a5417
JS
295'
296
882749a0 297test_expect_success '--word-diff=none' '
5094d158
JN
298 echo "(:" >pre &&
299 echo "(" >post &&
0253e126 300 pre=$(git rev-parse --short $(git hash-object pre)) &&
301 post=$(git rev-parse --short $(git hash-object post)) &&
302 cat >expect <<-EOF &&
5094d158 303 diff --git a/pre b/post
0253e126 304 index $pre..$post 100644
5094d158
JN
305 --- a/pre
306 +++ b/post
307 @@ -1 +1 @@
308 -(:
309 +(
310 EOF
882749a0 311 word_diff --word-diff=plain --word-diff=none
882749a0
TR
312'
313
62d39359
JS
314test_expect_success 'unset default driver' '
315 test_unconfig diff.wordregex
316'
317
e90d065e 318test_language_driver ada
5094d158
JN
319test_language_driver bibtex
320test_language_driver cpp
321test_language_driver csharp
0719f3ee 322test_language_driver css
3c81760b 323test_language_driver dts
5094d158
JN
324test_language_driver fortran
325test_language_driver html
326test_language_driver java
09188ed9 327test_language_driver kotlin
53b10a14 328test_language_driver matlab
5094d158
JN
329test_language_driver objc
330test_language_driver pascal
5269edf1 331test_language_driver perl
5094d158
JN
332test_language_driver php
333test_language_driver python
334test_language_driver ruby
a4373903 335test_language_driver scheme
5094d158 336test_language_driver tex
8d96e728 337
42536dd9 338test_expect_success 'word-diff with diff.sbe' '
42536dd9
JM
339 cat >pre <<-\EOF &&
340 a
341
342 b
343 EOF
344 cat >post <<-\EOF &&
345 a
346
347 c
348 EOF
0253e126 349 pre=$(git rev-parse --short $(git hash-object pre)) &&
350 post=$(git rev-parse --short $(git hash-object post)) &&
351 cat >expect <<-EOF &&
352 diff --git a/pre b/post
353 index $pre..$post 100644
354 --- a/pre
355 +++ b/post
356 @@ -1,3 +1,3 @@
357 a
358
359 [-b-]{+c+}
360 EOF
ff73aa40 361 test_config diff.suppress-blank-empty true &&
42536dd9
JM
362 word_diff --word-diff=plain
363'
364
c7c2bc0a 365test_expect_success 'word-diff with no newline at EOF' '
0253e126 366 printf "%s" "a a a a a" >pre &&
367 printf "%s" "a a ab a a" >post &&
368 pre=$(git rev-parse --short $(git hash-object pre)) &&
369 post=$(git rev-parse --short $(git hash-object post)) &&
370 cat >expect <<-EOF &&
c7c2bc0a 371 diff --git a/pre b/post
0253e126 372 index $pre..$post 100644
c7c2bc0a
TR
373 --- a/pre
374 +++ b/post
375 @@ -1 +1 @@
376 a a [-a-]{+ab+} a a
377 EOF
c7c2bc0a
TR
378 word_diff --word-diff=plain
379'
380
62d39359
JS
381test_expect_success 'setup history with two files' '
382 echo "a b; c" >a.tex &&
383 echo "a b; c" >z.txt &&
384 git add a.tex z.txt &&
385 git commit -minitial &&
386
387 # modify both
388 echo "a bx; c" >a.tex &&
389 echo "a bx; c" >z.txt &&
390 git commit -mmodified -a
391'
392
6440d341 393test_expect_success 'wordRegex for the first file does not apply to the second' '
62d39359 394 echo "*.tex diff=tex" >.gitattributes &&
ff73aa40 395 test_config diff.tex.wordRegex "[a-z]+|." &&
62d39359
JS
396 cat >expect <<-\EOF &&
397 diff --git a/a.tex b/a.tex
398 --- a/a.tex
399 +++ b/a.tex
400 @@ -1 +1 @@
401 a [-b-]{+bx+}; c
402 diff --git a/z.txt b/z.txt
403 --- a/z.txt
404 +++ b/z.txt
405 @@ -1 +1 @@
406 a [-b;-]{+bx;+} c
407 EOF
408 git diff --word-diff HEAD~ >actual &&
409 compare_diff_patch expect actual
410'
411
2e5d2003 412test_done