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