]> git.ipfire.org Git - thirdparty/git.git/blob - t/t4034-diff-words.sh
bisect: document command line arguments for "bisect start"
[thirdparty/git.git] / t / t4034-diff-words.sh
1 #!/bin/sh
2
3 test_description='word diff colors'
4
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-diff.sh
8
9 cat >pre.simple <<-\EOF
10 h(4)
11
12 a = b + c
13 EOF
14 cat >post.simple <<-\EOF
15 h(4),hh[44]
16
17 a = b + c
18
19 aa = a
20
21 aeff = aeff * ( aaa )
22 EOF
23 pre=$(git rev-parse --short $(git hash-object pre.simple))
24 post=$(git rev-parse --short $(git hash-object post.simple))
25 cat >expect.letter-runs-are-words <<-EOF
26 <BOLD>diff --git a/pre b/post<RESET>
27 <BOLD>index $pre..$post 100644<RESET>
28 <BOLD>--- a/pre<RESET>
29 <BOLD>+++ b/post<RESET>
30 <CYAN>@@ -1,3 +1,7 @@<RESET>
31 h(4),<GREEN>hh<RESET>[44]
32
33 a = b + c<RESET>
34
35 <GREEN>aa = a<RESET>
36
37 <GREEN>aeff = aeff * ( aaa<RESET> )
38 EOF
39 cat >expect.non-whitespace-is-word <<-EOF
40 <BOLD>diff --git a/pre b/post<RESET>
41 <BOLD>index $pre..$post 100644<RESET>
42 <BOLD>--- a/pre<RESET>
43 <BOLD>+++ b/post<RESET>
44 <CYAN>@@ -1,3 +1,7 @@<RESET>
45 h(4)<GREEN>,hh[44]<RESET>
46
47 a = b + c<RESET>
48
49 <GREEN>aa = a<RESET>
50
51 <GREEN>aeff = aeff * ( aaa )<RESET>
52 EOF
53
54 word_diff () {
55 pre=$(git rev-parse --short $(git hash-object pre)) &&
56 post=$(git rev-parse --short $(git hash-object post)) &&
57 test_must_fail git diff --no-index "$@" pre post >output &&
58 test_decode_color <output >output.decrypted &&
59 sed -e "2s/index [^ ]*/index $pre..$post/" expect >expected
60 test_cmp expected output.decrypted
61 }
62
63 test_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 }
73
74 test_expect_success setup '
75 git config diff.color.old red &&
76 git config diff.color.new green &&
77 git config diff.color.func magenta
78 '
79
80 test_expect_success 'set up pre and post with runs of whitespace' '
81 cp pre.simple pre &&
82 cp post.simple post
83 '
84
85 test_expect_success 'word diff with runs of whitespace' '
86 cat >expect <<-EOF &&
87 <BOLD>diff --git a/pre b/post<RESET>
88 <BOLD>index $pre..$post 100644<RESET>
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 &&
102 word_diff --color --word-diff=color
103 '
104
105 test_expect_success '--word-diff=porcelain' '
106 sed "s/#.*$//" >expect <<-EOF &&
107 diff --git a/pre b/post
108 index $pre..$post 100644
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
126 word_diff --word-diff=porcelain
127 '
128
129 test_expect_success '--word-diff=plain' '
130 cat >expect <<-EOF &&
131 diff --git a/pre b/post
132 index $pre..$post 100644
133 --- a/pre
134 +++ b/post
135 @@ -1,3 +1,7 @@
136 [-h(4)-]{+h(4),hh[44]+}
137
138 a = b + c
139
140 {+aa = a+}
141
142 {+aeff = aeff * ( aaa )+}
143 EOF
144 word_diff --word-diff=plain &&
145 word_diff --word-diff=plain --no-color
146 '
147
148 test_expect_success '--word-diff=plain --color' '
149 cat >expect <<-EOF &&
150 <BOLD>diff --git a/pre b/post<RESET>
151 <BOLD>index $pre..$post 100644<RESET>
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>
156
157 a = b + c<RESET>
158
159 <GREEN>{+aa = a+}<RESET>
160
161 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
162 EOF
163 word_diff --word-diff=plain --color
164 '
165
166 test_expect_success 'word diff without context' '
167 cat >expect <<-EOF &&
168 <BOLD>diff --git a/pre b/post<RESET>
169 <BOLD>index $pre..$post 100644<RESET>
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
180 word_diff --color-words --unified=0
181 '
182
183 test_expect_success 'word diff with a regular expression' '
184 cp expect.letter-runs-are-words expect &&
185 word_diff --color-words="[a-z]+"
186 '
187
188 test_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
193 test_expect_success 'set up a diff driver' '
194 git config diff.testdriver.wordRegex "[^[:space:]]" &&
195 cat <<-\EOF >.gitattributes
196 pre diff=testdriver
197 post diff=testdriver
198 EOF
199 '
200
201 test_expect_success 'option overrides .gitattributes' '
202 cp expect.letter-runs-are-words expect &&
203 word_diff --color-words="[a-z]+"
204 '
205
206 test_expect_success 'use regex supplied by driver' '
207 cp expect.non-whitespace-is-word expect &&
208 word_diff --color-words
209 '
210
211 test_expect_success 'set up diff.wordRegex option' '
212 git config diff.wordRegex "[[:alnum:]]+"
213 '
214
215 test_expect_success 'command-line overrides config' '
216 cp expect.letter-runs-are-words expect &&
217 word_diff --color-words="[a-z]+"
218 '
219
220 test_expect_success 'command-line overrides config: --word-diff-regex' '
221 cat >expect <<-EOF &&
222 <BOLD>diff --git a/pre b/post<RESET>
223 <BOLD>index $pre..$post 100644<RESET>
224 <BOLD>--- a/pre<RESET>
225 <BOLD>+++ b/post<RESET>
226 <CYAN>@@ -1,3 +1,7 @@<RESET>
227 h(4),<GREEN>{+hh+}<RESET>[44]
228
229 a = b + c<RESET>
230
231 <GREEN>{+aa = a+}<RESET>
232
233 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
234 EOF
235 word_diff --color --word-diff-regex="[a-z]+"
236 '
237
238 test_expect_success '.gitattributes override config' '
239 cp expect.non-whitespace-is-word expect &&
240 word_diff --color-words
241 '
242
243 test_expect_success 'setup: remove diff driver regex' '
244 test_unconfig diff.testdriver.wordRegex
245 '
246
247 test_expect_success 'use configured regex' '
248 cat >expect <<-EOF &&
249 <BOLD>diff --git a/pre b/post<RESET>
250 <BOLD>index $pre..$post 100644<RESET>
251 <BOLD>--- a/pre<RESET>
252 <BOLD>+++ b/post<RESET>
253 <CYAN>@@ -1,3 +1,7 @@<RESET>
254 h(4),<GREEN>hh[44<RESET>]
255
256 a = b + c<RESET>
257
258 <GREEN>aa = a<RESET>
259
260 <GREEN>aeff = aeff * ( aaa<RESET> )
261 EOF
262 word_diff --color-words
263 '
264
265 test_expect_success 'test parsing words for newline' '
266 echo "aaa (aaa)" >pre &&
267 echo "aaa (aaa) aaa" >post &&
268 pre=$(git rev-parse --short $(git hash-object pre)) &&
269 post=$(git rev-parse --short $(git hash-object post)) &&
270 cat >expect <<-EOF &&
271 <BOLD>diff --git a/pre b/post<RESET>
272 <BOLD>index $pre..$post 100644<RESET>
273 <BOLD>--- a/pre<RESET>
274 <BOLD>+++ b/post<RESET>
275 <CYAN>@@ -1 +1 @@<RESET>
276 aaa (aaa) <GREEN>aaa<RESET>
277 EOF
278 word_diff --color-words="a+"
279 '
280
281 test_expect_success 'test when words are only removed at the end' '
282 echo "(:" >pre &&
283 echo "(" >post &&
284 pre=$(git rev-parse --short $(git hash-object pre)) &&
285 post=$(git rev-parse --short $(git hash-object post)) &&
286 cat >expect <<-EOF &&
287 <BOLD>diff --git a/pre b/post<RESET>
288 <BOLD>index $pre..$post 100644<RESET>
289 <BOLD>--- a/pre<RESET>
290 <BOLD>+++ b/post<RESET>
291 <CYAN>@@ -1 +1 @@<RESET>
292 (<RED>:<RESET>
293 EOF
294 word_diff --color-words=.
295 '
296
297 test_expect_success '--word-diff=none' '
298 echo "(:" >pre &&
299 echo "(" >post &&
300 pre=$(git rev-parse --short $(git hash-object pre)) &&
301 post=$(git rev-parse --short $(git hash-object post)) &&
302 cat >expect <<-EOF &&
303 diff --git a/pre b/post
304 index $pre..$post 100644
305 --- a/pre
306 +++ b/post
307 @@ -1 +1 @@
308 -(:
309 +(
310 EOF
311 word_diff --word-diff=plain --word-diff=none
312 '
313
314 test_expect_success 'unset default driver' '
315 test_unconfig diff.wordregex
316 '
317
318 test_language_driver ada
319 test_language_driver bibtex
320 test_language_driver cpp
321 test_language_driver csharp
322 test_language_driver css
323 test_language_driver dts
324 test_language_driver fortran
325 test_language_driver html
326 test_language_driver java
327 test_language_driver kotlin
328 test_language_driver matlab
329 test_language_driver objc
330 test_language_driver pascal
331 test_language_driver perl
332 test_language_driver php
333 test_language_driver python
334 test_language_driver ruby
335 test_language_driver scheme
336 test_language_driver tex
337
338 test_expect_success 'word-diff with diff.sbe' '
339 cat >pre <<-\EOF &&
340 a
341
342 b
343 EOF
344 cat >post <<-\EOF &&
345 a
346
347 c
348 EOF
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
361 test_config diff.suppress-blank-empty true &&
362 word_diff --word-diff=plain
363 '
364
365 test_expect_success 'word-diff with no newline at EOF' '
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 &&
371 diff --git a/pre b/post
372 index $pre..$post 100644
373 --- a/pre
374 +++ b/post
375 @@ -1 +1 @@
376 a a [-a-]{+ab+} a a
377 EOF
378 word_diff --word-diff=plain
379 '
380
381 test_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
393 test_expect_success 'wordRegex for the first file does not apply to the second' '
394 echo "*.tex diff=tex" >.gitattributes &&
395 test_config diff.tex.wordRegex "[a-z]+|." &&
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
412 test_done