]> git.ipfire.org Git - thirdparty/git.git/blob - t/t4034-diff-words.sh
0be647c2fbc05c3a81fd76647037d91f20dcd79b
[thirdparty/git.git] / t / t4034-diff-words.sh
1 #!/bin/sh
2
3 test_description='word diff colors'
4
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-diff.sh
7
8 cat >pre.simple <<-\EOF
9 h(4)
10
11 a = b + c
12 EOF
13 cat >post.simple <<-\EOF
14 h(4),hh[44]
15
16 a = b + c
17
18 aa = a
19
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]
31
32 a = b + c<RESET>
33
34 <GREEN>aa = a<RESET>
35
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>
45
46 a = b + c<RESET>
47
48 <GREEN>aa = a<RESET>
49
50 <GREEN>aeff = aeff * ( aaa )<RESET>
51 EOF
52
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
60 }
61
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
70 '
71 test_expect_success "diff driver '$lang' in Islandic" '
72 test_env LANG=is_IS.UTF-8 LANGUAGE=is LC_ALL="$is_IS_locale" \
73 word_diff --color-words
74 '
75 }
76
77 test_expect_success setup '
78 git config diff.color.old red &&
79 git config diff.color.new green &&
80 git config diff.color.func magenta
81 '
82
83 test_expect_success 'set up pre and post with runs of whitespace' '
84 cp pre.simple pre &&
85 cp post.simple post
86 '
87
88 test_expect_success 'word diff with runs of whitespace' '
89 cat >expect <<-EOF &&
90 <BOLD>diff --git a/pre b/post<RESET>
91 <BOLD>index $pre..$post 100644<RESET>
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 &&
105 word_diff --color --word-diff=color
106 '
107
108 test_expect_success '--word-diff=porcelain' '
109 sed "s/#.*$//" >expect <<-EOF &&
110 diff --git a/pre b/post
111 index $pre..$post 100644
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
129 word_diff --word-diff=porcelain
130 '
131
132 test_expect_success '--word-diff=plain' '
133 cat >expect <<-EOF &&
134 diff --git a/pre b/post
135 index $pre..$post 100644
136 --- a/pre
137 +++ b/post
138 @@ -1,3 +1,7 @@
139 [-h(4)-]{+h(4),hh[44]+}
140
141 a = b + c
142
143 {+aa = a+}
144
145 {+aeff = aeff * ( aaa )+}
146 EOF
147 word_diff --word-diff=plain &&
148 word_diff --word-diff=plain --no-color
149 '
150
151 test_expect_success '--word-diff=plain --color' '
152 cat >expect <<-EOF &&
153 <BOLD>diff --git a/pre b/post<RESET>
154 <BOLD>index $pre..$post 100644<RESET>
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>
159
160 a = b + c<RESET>
161
162 <GREEN>{+aa = a+}<RESET>
163
164 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
165 EOF
166 word_diff --word-diff=plain --color
167 '
168
169 test_expect_success 'word diff without context' '
170 cat >expect <<-EOF &&
171 <BOLD>diff --git a/pre b/post<RESET>
172 <BOLD>index $pre..$post 100644<RESET>
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
183 word_diff --color-words --unified=0
184 '
185
186 test_expect_success 'word diff with a regular expression' '
187 cp expect.letter-runs-are-words expect &&
188 word_diff --color-words="[a-z]+"
189 '
190
191 test_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
196 test_expect_success 'set up a diff driver' '
197 git config diff.testdriver.wordRegex "[^[:space:]]" &&
198 cat <<-\EOF >.gitattributes
199 pre diff=testdriver
200 post diff=testdriver
201 EOF
202 '
203
204 test_expect_success 'option overrides .gitattributes' '
205 cp expect.letter-runs-are-words expect &&
206 word_diff --color-words="[a-z]+"
207 '
208
209 test_expect_success 'use regex supplied by driver' '
210 cp expect.non-whitespace-is-word expect &&
211 word_diff --color-words
212 '
213
214 test_expect_success 'set up diff.wordRegex option' '
215 git config diff.wordRegex "[[:alnum:]]+"
216 '
217
218 test_expect_success 'command-line overrides config' '
219 cp expect.letter-runs-are-words expect &&
220 word_diff --color-words="[a-z]+"
221 '
222
223 test_expect_success 'command-line overrides config: --word-diff-regex' '
224 cat >expect <<-EOF &&
225 <BOLD>diff --git a/pre b/post<RESET>
226 <BOLD>index $pre..$post 100644<RESET>
227 <BOLD>--- a/pre<RESET>
228 <BOLD>+++ b/post<RESET>
229 <CYAN>@@ -1,3 +1,7 @@<RESET>
230 h(4),<GREEN>{+hh+}<RESET>[44]
231
232 a = b + c<RESET>
233
234 <GREEN>{+aa = a+}<RESET>
235
236 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
237 EOF
238 word_diff --color --word-diff-regex="[a-z]+"
239 '
240
241 test_expect_success '.gitattributes override config' '
242 cp expect.non-whitespace-is-word expect &&
243 word_diff --color-words
244 '
245
246 test_expect_success 'setup: remove diff driver regex' '
247 test_unconfig diff.testdriver.wordRegex
248 '
249
250 test_expect_success 'use configured regex' '
251 cat >expect <<-EOF &&
252 <BOLD>diff --git a/pre b/post<RESET>
253 <BOLD>index $pre..$post 100644<RESET>
254 <BOLD>--- a/pre<RESET>
255 <BOLD>+++ b/post<RESET>
256 <CYAN>@@ -1,3 +1,7 @@<RESET>
257 h(4),<GREEN>hh[44<RESET>]
258
259 a = b + c<RESET>
260
261 <GREEN>aa = a<RESET>
262
263 <GREEN>aeff = aeff * ( aaa<RESET> )
264 EOF
265 word_diff --color-words
266 '
267
268 test_expect_success 'test parsing words for newline' '
269 echo "aaa (aaa)" >pre &&
270 echo "aaa (aaa) aaa" >post &&
271 pre=$(git rev-parse --short $(git hash-object pre)) &&
272 post=$(git rev-parse --short $(git hash-object post)) &&
273 cat >expect <<-EOF &&
274 <BOLD>diff --git a/pre b/post<RESET>
275 <BOLD>index $pre..$post 100644<RESET>
276 <BOLD>--- a/pre<RESET>
277 <BOLD>+++ b/post<RESET>
278 <CYAN>@@ -1 +1 @@<RESET>
279 aaa (aaa) <GREEN>aaa<RESET>
280 EOF
281 word_diff --color-words="a+"
282 '
283
284 test_expect_success 'test when words are only removed at the end' '
285 echo "(:" >pre &&
286 echo "(" >post &&
287 pre=$(git rev-parse --short $(git hash-object pre)) &&
288 post=$(git rev-parse --short $(git hash-object post)) &&
289 cat >expect <<-EOF &&
290 <BOLD>diff --git a/pre b/post<RESET>
291 <BOLD>index $pre..$post 100644<RESET>
292 <BOLD>--- a/pre<RESET>
293 <BOLD>+++ b/post<RESET>
294 <CYAN>@@ -1 +1 @@<RESET>
295 (<RED>:<RESET>
296 EOF
297 word_diff --color-words=.
298 '
299
300 test_expect_success '--word-diff=none' '
301 echo "(:" >pre &&
302 echo "(" >post &&
303 pre=$(git rev-parse --short $(git hash-object pre)) &&
304 post=$(git rev-parse --short $(git hash-object post)) &&
305 cat >expect <<-EOF &&
306 diff --git a/pre b/post
307 index $pre..$post 100644
308 --- a/pre
309 +++ b/post
310 @@ -1 +1 @@
311 -(:
312 +(
313 EOF
314 word_diff --word-diff=plain --word-diff=none
315 '
316
317 test_expect_success 'unset default driver' '
318 test_unconfig diff.wordregex
319 '
320
321 test_language_driver ada
322 test_language_driver bibtex
323 test_language_driver bash
324 test_language_driver cpp
325 test_language_driver csharp
326 test_language_driver css
327 test_language_driver dts
328 test_language_driver fortran
329 test_language_driver html
330 test_language_driver java
331 test_language_driver kotlin
332 test_language_driver matlab
333 test_language_driver objc
334 test_language_driver pascal
335 test_language_driver perl
336 test_language_driver php
337 test_language_driver python
338 test_language_driver ruby
339 test_language_driver scheme
340 test_language_driver tex
341
342 test_expect_success 'word-diff with diff.sbe' '
343 cat >pre <<-\EOF &&
344 a
345
346 b
347 EOF
348 cat >post <<-\EOF &&
349 a
350
351 c
352 EOF
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
365 test_config diff.suppress-blank-empty true &&
366 word_diff --word-diff=plain
367 '
368
369 test_expect_success 'word-diff with no newline at EOF' '
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 &&
375 diff --git a/pre b/post
376 index $pre..$post 100644
377 --- a/pre
378 +++ b/post
379 @@ -1 +1 @@
380 a a [-a-]{+ab+} a a
381 EOF
382 word_diff --word-diff=plain
383 '
384
385 test_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
397 test_expect_success 'wordRegex for the first file does not apply to the second' '
398 echo "*.tex diff=tex" >.gitattributes &&
399 test_config diff.tex.wordRegex "[a-z]+|." &&
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
416 test_done