]>
Commit | Line | Data |
---|---|---|
2344d47f JS |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2006 Johannes E. Schindelin | |
c9a3e724 | 4 | # Copyright (c) 2023 Google LLC |
2344d47f JS |
5 | |
6 | test_description='Test special whitespace in diff engine. | |
7 | ||
8 | ' | |
77e56d55 ÆAB |
9 | |
10 | TEST_PASSES_SANITIZE_LEAK=true | |
2344d47f | 11 | . ./test-lib.sh |
ebd73f50 | 12 | . "$TEST_DIRECTORY"/lib-diff.sh |
2344d47f | 13 | |
a64f8b25 JH |
14 | for opt_res in --patch --quiet -s --stat --shortstat --dirstat=lines \ |
15 | --raw! --name-only! --name-status! | |
c9a3e724 | 16 | do |
a64f8b25 JH |
17 | opts=${opt_res%!} expect_failure= |
18 | test "$opts" = "$opt_res" || | |
19 | expect_failure="test_expect_code 1" | |
c9a3e724 JH |
20 | |
21 | test_expect_success "status with $opts (different)" ' | |
22 | echo foo >x && | |
23 | git add x && | |
24 | echo bar >x && | |
25 | test_expect_code 1 git diff -w $opts --exit-code x | |
26 | ' | |
27 | ||
28 | test_expect_success POSIXPERM "status with $opts (mode differs)" ' | |
29 | test_when_finished "git update-index --chmod=-x x" && | |
30 | echo foo >x && | |
31 | git add x && | |
32 | git update-index --chmod=+x x && | |
33 | test_expect_code 1 git diff -w $opts --exit-code x | |
34 | ' | |
35 | ||
36 | test_expect_success "status with $opts (removing an empty file)" ' | |
37 | : >x && | |
38 | git add x && | |
39 | rm x && | |
40 | test_expect_code 1 git diff -w $opts --exit-code -- x | |
41 | ' | |
42 | ||
43 | test_expect_success "status with $opts (different but equivalent)" ' | |
44 | echo foo >x && | |
45 | git add x && | |
46 | echo " foo" >x && | |
a64f8b25 | 47 | $expect_failure git diff -w $opts --exit-code x |
c9a3e724 JH |
48 | ' |
49 | done | |
50 | ||
d55ef3e0 JH |
51 | test_expect_success "Ray Lehtiniemi's example" ' |
52 | cat <<-\EOF >x && | |
53 | do { | |
54 | nothing; | |
55 | } while (0); | |
56 | EOF | |
57 | git update-index --add x && | |
946d2353 DL |
58 | old_hash_x=$(git hash-object x) && |
59 | before=$(git rev-parse --short "$old_hash_x") && | |
2344d47f | 60 | |
d55ef3e0 JH |
61 | cat <<-\EOF >x && |
62 | do | |
63 | { | |
64 | nothing; | |
65 | } | |
66 | while (0); | |
67 | EOF | |
946d2353 DL |
68 | new_hash_x=$(git hash-object x) && |
69 | after=$(git rev-parse --short "$new_hash_x") && | |
2344d47f | 70 | |
79b0edc1 | 71 | cat <<-EOF >expect && |
d55ef3e0 | 72 | diff --git a/x b/x |
79b0edc1 | 73 | index $before..$after 100644 |
d55ef3e0 JH |
74 | --- a/x |
75 | +++ b/x | |
76 | @@ -1,3 +1,5 @@ | |
77 | -do { | |
78 | +do | |
79 | +{ | |
80 | nothing; | |
81 | -} while (0); | |
82 | +} | |
83 | +while (0); | |
84 | EOF | |
2344d47f | 85 | |
d55ef3e0 JH |
86 | git diff >out && |
87 | test_cmp expect out && | |
2344d47f | 88 | |
d55ef3e0 JH |
89 | git diff -w >out && |
90 | test_cmp expect out && | |
2344d47f | 91 | |
d55ef3e0 JH |
92 | git diff -b >out && |
93 | test_cmp expect out | |
94 | ' | |
2344d47f | 95 | |
d55ef3e0 JH |
96 | test_expect_success 'another test, without options' ' |
97 | tr Q "\015" <<-\EOF >x && | |
98 | whitespace at beginning | |
99 | whitespace change | |
100 | whitespace in the middle | |
101 | whitespace at end | |
102 | unchanged line | |
103 | CR at endQ | |
104 | EOF | |
2344d47f | 105 | |
d55ef3e0 | 106 | git update-index x && |
946d2353 DL |
107 | old_hash_x=$(git hash-object x) && |
108 | before=$(git rev-parse --short "$old_hash_x") && | |
2344d47f | 109 | |
d55ef3e0 JH |
110 | tr "_" " " <<-\EOF >x && |
111 | _ whitespace at beginning | |
112 | whitespace change | |
113 | white space in the middle | |
114 | whitespace at end__ | |
115 | unchanged line | |
116 | CR at end | |
117 | EOF | |
946d2353 DL |
118 | new_hash_x=$(git hash-object x) && |
119 | after=$(git rev-parse --short "$new_hash_x") && | |
2344d47f | 120 | |
79b0edc1 | 121 | tr "Q_" "\015 " <<-EOF >expect && |
d55ef3e0 | 122 | diff --git a/x b/x |
79b0edc1 | 123 | index $before..$after 100644 |
d55ef3e0 JH |
124 | --- a/x |
125 | +++ b/x | |
126 | @@ -1,6 +1,6 @@ | |
127 | -whitespace at beginning | |
128 | -whitespace change | |
129 | -whitespace in the middle | |
130 | -whitespace at end | |
131 | + whitespace at beginning | |
132 | +whitespace change | |
133 | +white space in the middle | |
134 | +whitespace at end__ | |
135 | unchanged line | |
136 | -CR at endQ | |
137 | +CR at end | |
138 | EOF | |
2344d47f | 139 | |
d55ef3e0 JH |
140 | git diff >out && |
141 | test_cmp expect out && | |
2344d47f | 142 | |
d55ef3e0 | 143 | git diff -w >out && |
d3c6751b | 144 | test_must_be_empty out && |
d55ef3e0 JH |
145 | |
146 | git diff -w -b >out && | |
d3c6751b | 147 | test_must_be_empty out && |
d55ef3e0 JH |
148 | |
149 | git diff -w --ignore-space-at-eol >out && | |
d3c6751b | 150 | test_must_be_empty out && |
d55ef3e0 JH |
151 | |
152 | git diff -w -b --ignore-space-at-eol >out && | |
d3c6751b | 153 | test_must_be_empty out && |
2344d47f | 154 | |
e9282f02 | 155 | git diff -w --ignore-cr-at-eol >out && |
d3c6751b | 156 | test_must_be_empty out && |
d55ef3e0 | 157 | |
79b0edc1 | 158 | tr "Q_" "\015 " <<-EOF >expect && |
d55ef3e0 | 159 | diff --git a/x b/x |
79b0edc1 | 160 | index $before..$after 100644 |
d55ef3e0 JH |
161 | --- a/x |
162 | +++ b/x | |
163 | @@ -1,6 +1,6 @@ | |
164 | -whitespace at beginning | |
165 | +_ whitespace at beginning | |
166 | whitespace change | |
167 | -whitespace in the middle | |
168 | +white space in the middle | |
169 | whitespace at end__ | |
170 | unchanged line | |
171 | CR at end | |
172 | EOF | |
173 | git diff -b >out && | |
174 | test_cmp expect out && | |
175 | ||
176 | git diff -b --ignore-space-at-eol >out && | |
177 | test_cmp expect out && | |
178 | ||
e9282f02 JH |
179 | git diff -b --ignore-cr-at-eol >out && |
180 | test_cmp expect out && | |
181 | ||
79b0edc1 | 182 | tr "Q_" "\015 " <<-EOF >expect && |
d55ef3e0 | 183 | diff --git a/x b/x |
79b0edc1 | 184 | index $before..$after 100644 |
d55ef3e0 JH |
185 | --- a/x |
186 | +++ b/x | |
187 | @@ -1,6 +1,6 @@ | |
188 | -whitespace at beginning | |
189 | -whitespace change | |
190 | -whitespace in the middle | |
191 | +_ whitespace at beginning | |
192 | +whitespace change | |
193 | +white space in the middle | |
194 | whitespace at end__ | |
195 | unchanged line | |
196 | CR at end | |
197 | EOF | |
198 | git diff --ignore-space-at-eol >out && | |
e9282f02 JH |
199 | test_cmp expect out && |
200 | ||
201 | git diff --ignore-space-at-eol --ignore-cr-at-eol >out && | |
202 | test_cmp expect out && | |
203 | ||
79b0edc1 | 204 | tr "Q_" "\015 " <<-EOF >expect && |
e9282f02 | 205 | diff --git a/x b/x |
79b0edc1 | 206 | index_$before..$after 100644 |
e9282f02 JH |
207 | --- a/x |
208 | +++ b/x | |
209 | @@ -1,6 +1,6 @@ | |
210 | -whitespace at beginning | |
211 | -whitespace change | |
212 | -whitespace in the middle | |
213 | -whitespace at end | |
214 | +_ whitespace at beginning | |
215 | +whitespace_ _change | |
216 | +white space in the middle | |
217 | +whitespace at end__ | |
218 | unchanged line | |
219 | CR at end | |
220 | EOF | |
221 | git diff --ignore-cr-at-eol >out && | |
d55ef3e0 JH |
222 | test_cmp expect out |
223 | ' | |
2344d47f | 224 | |
36617af7 AP |
225 | test_expect_success 'ignore-blank-lines: only new lines' ' |
226 | test_seq 5 >x && | |
227 | git update-index x && | |
90a95301 | 228 | test_seq 5 | sed "/3/i\\ |
36617af7 AP |
229 | " >x && |
230 | git diff --ignore-blank-lines >out && | |
d3c6751b | 231 | test_must_be_empty out |
36617af7 AP |
232 | ' |
233 | ||
234 | test_expect_success 'ignore-blank-lines: only new lines with space' ' | |
235 | test_seq 5 >x && | |
236 | git update-index x && | |
90a95301 BW |
237 | test_seq 5 | sed "/3/i\\ |
238 | " >x && | |
36617af7 | 239 | git diff -w --ignore-blank-lines >out && |
d3c6751b | 240 | test_must_be_empty out |
36617af7 AP |
241 | ' |
242 | ||
243 | test_expect_success 'ignore-blank-lines: after change' ' | |
244 | cat <<-\EOF >x && | |
245 | 1 | |
246 | 2 | |
247 | ||
248 | 3 | |
249 | 4 | |
250 | 5 | |
251 | ||
252 | 6 | |
253 | 7 | |
254 | EOF | |
255 | git update-index x && | |
256 | cat <<-\EOF >x && | |
257 | change | |
258 | ||
259 | 1 | |
260 | 2 | |
261 | 3 | |
262 | 4 | |
263 | 5 | |
264 | 6 | |
265 | ||
266 | 7 | |
267 | EOF | |
268 | git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp && | |
269 | cat <<-\EOF >expected && | |
270 | diff --git a/x b/x | |
271 | --- a/x | |
272 | +++ b/x | |
273 | @@ -1,6 +1,7 @@ | |
274 | +change | |
275 | + | |
276 | 1 | |
277 | 2 | |
278 | - | |
279 | 3 | |
280 | 4 | |
281 | 5 | |
282 | EOF | |
283 | compare_diff_patch expected out.tmp | |
284 | ' | |
285 | ||
286 | test_expect_success 'ignore-blank-lines: before change' ' | |
287 | cat <<-\EOF >x && | |
288 | 1 | |
289 | 2 | |
290 | ||
291 | 3 | |
292 | 4 | |
293 | 5 | |
294 | 6 | |
295 | 7 | |
296 | EOF | |
297 | git update-index x && | |
298 | cat <<-\EOF >x && | |
299 | ||
300 | 1 | |
301 | 2 | |
302 | 3 | |
303 | 4 | |
304 | 5 | |
305 | ||
306 | 6 | |
307 | 7 | |
308 | change | |
309 | EOF | |
310 | git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp && | |
311 | cat <<-\EOF >expected && | |
312 | diff --git a/x b/x | |
313 | --- a/x | |
314 | +++ b/x | |
315 | @@ -4,5 +4,7 @@ | |
316 | 3 | |
317 | 4 | |
318 | 5 | |
319 | + | |
320 | 6 | |
321 | 7 | |
322 | +change | |
323 | EOF | |
324 | compare_diff_patch expected out.tmp | |
325 | ' | |
326 | ||
327 | test_expect_success 'ignore-blank-lines: between changes' ' | |
328 | cat <<-\EOF >x && | |
329 | 1 | |
330 | 2 | |
331 | 3 | |
332 | 4 | |
333 | 5 | |
334 | ||
335 | ||
336 | 6 | |
337 | 7 | |
338 | 8 | |
339 | 9 | |
340 | 10 | |
341 | EOF | |
342 | git update-index x && | |
343 | cat <<-\EOF >x && | |
344 | change | |
345 | 1 | |
346 | 2 | |
347 | ||
348 | 3 | |
349 | 4 | |
350 | 5 | |
351 | 6 | |
352 | 7 | |
353 | 8 | |
354 | ||
355 | 9 | |
356 | 10 | |
357 | change | |
358 | EOF | |
359 | git diff --ignore-blank-lines >out.tmp && | |
360 | cat <<-\EOF >expected && | |
361 | diff --git a/x b/x | |
362 | --- a/x | |
363 | +++ b/x | |
364 | @@ -1,5 +1,7 @@ | |
365 | +change | |
366 | 1 | |
367 | 2 | |
368 | + | |
369 | 3 | |
370 | 4 | |
371 | 5 | |
372 | @@ -8,5 +8,7 @@ | |
373 | 6 | |
374 | 7 | |
375 | 8 | |
376 | + | |
377 | 9 | |
378 | 10 | |
379 | +change | |
380 | EOF | |
381 | compare_diff_patch expected out.tmp | |
382 | ' | |
383 | ||
384 | test_expect_success 'ignore-blank-lines: between changes (with interhunkctx)' ' | |
385 | test_seq 10 >x && | |
386 | git update-index x && | |
387 | cat <<-\EOF >x && | |
388 | change | |
389 | 1 | |
390 | 2 | |
391 | ||
392 | 3 | |
393 | 4 | |
394 | 5 | |
395 | ||
396 | 6 | |
397 | 7 | |
398 | 8 | |
399 | 9 | |
400 | ||
401 | 10 | |
402 | change | |
403 | EOF | |
404 | git diff --inter-hunk-context=2 --ignore-blank-lines >out.tmp && | |
405 | cat <<-\EOF >expected && | |
406 | diff --git a/x b/x | |
407 | --- a/x | |
408 | +++ b/x | |
409 | @@ -1,10 +1,15 @@ | |
410 | +change | |
411 | 1 | |
412 | 2 | |
413 | + | |
414 | 3 | |
415 | 4 | |
416 | 5 | |
417 | + | |
418 | 6 | |
419 | 7 | |
420 | 8 | |
421 | 9 | |
422 | + | |
423 | 10 | |
424 | +change | |
425 | EOF | |
426 | compare_diff_patch expected out.tmp | |
427 | ' | |
428 | ||
429 | test_expect_success 'ignore-blank-lines: scattered spaces' ' | |
430 | test_seq 10 >x && | |
431 | git update-index x && | |
432 | cat <<-\EOF >x && | |
433 | change | |
434 | 1 | |
435 | 2 | |
436 | 3 | |
437 | ||
438 | 4 | |
439 | ||
440 | 5 | |
441 | ||
442 | 6 | |
443 | ||
444 | ||
445 | 7 | |
446 | ||
447 | 8 | |
448 | 9 | |
449 | 10 | |
450 | change | |
451 | EOF | |
452 | git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp && | |
453 | cat <<-\EOF >expected && | |
454 | diff --git a/x b/x | |
455 | --- a/x | |
456 | +++ b/x | |
457 | @@ -1,3 +1,4 @@ | |
458 | +change | |
459 | 1 | |
460 | 2 | |
461 | 3 | |
462 | @@ -8,3 +15,4 @@ | |
463 | 8 | |
464 | 9 | |
465 | 10 | |
466 | +change | |
467 | EOF | |
468 | compare_diff_patch expected out.tmp | |
469 | ' | |
470 | ||
471 | test_expect_success 'ignore-blank-lines: spaces coalesce' ' | |
472 | test_seq 6 >x && | |
473 | git update-index x && | |
474 | cat <<-\EOF >x && | |
475 | change | |
476 | 1 | |
477 | 2 | |
478 | 3 | |
479 | ||
480 | 4 | |
481 | ||
482 | 5 | |
483 | ||
484 | 6 | |
485 | change | |
486 | EOF | |
487 | git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp && | |
488 | cat <<-\EOF >expected && | |
489 | diff --git a/x b/x | |
490 | --- a/x | |
491 | +++ b/x | |
492 | @@ -1,6 +1,11 @@ | |
493 | +change | |
494 | 1 | |
495 | 2 | |
496 | 3 | |
497 | + | |
498 | 4 | |
499 | + | |
500 | 5 | |
501 | + | |
502 | 6 | |
503 | +change | |
504 | EOF | |
505 | compare_diff_patch expected out.tmp | |
506 | ' | |
507 | ||
508 | test_expect_success 'ignore-blank-lines: mix changes and blank lines' ' | |
509 | test_seq 16 >x && | |
510 | git update-index x && | |
511 | cat <<-\EOF >x && | |
512 | change | |
513 | 1 | |
514 | 2 | |
515 | ||
516 | 3 | |
517 | 4 | |
518 | 5 | |
519 | change | |
520 | 6 | |
521 | 7 | |
522 | 8 | |
523 | ||
524 | 9 | |
525 | 10 | |
526 | 11 | |
527 | change | |
528 | 12 | |
529 | 13 | |
530 | 14 | |
531 | ||
532 | 15 | |
533 | 16 | |
534 | change | |
535 | EOF | |
536 | git diff --ignore-blank-lines >out.tmp && | |
537 | cat <<-\EOF >expected && | |
538 | diff --git a/x b/x | |
539 | --- a/x | |
540 | +++ b/x | |
541 | @@ -1,8 +1,11 @@ | |
542 | +change | |
543 | 1 | |
544 | 2 | |
545 | + | |
546 | 3 | |
547 | 4 | |
548 | 5 | |
549 | +change | |
550 | 6 | |
551 | 7 | |
552 | 8 | |
553 | @@ -9,8 +13,11 @@ | |
554 | 9 | |
555 | 10 | |
556 | 11 | |
557 | +change | |
558 | 12 | |
559 | 13 | |
560 | 14 | |
561 | + | |
562 | 15 | |
563 | 16 | |
564 | +change | |
565 | EOF | |
566 | compare_diff_patch expected out.tmp | |
567 | ' | |
568 | ||
86f8c236 | 569 | test_expect_success 'check mixed spaces and tabs in indent' ' |
86f8c236 | 570 | # This is indented with SP HT SP. |
d55ef3e0 | 571 | echo " foo();" >x && |
946d2353 DL |
572 | test_must_fail git diff --check >check && |
573 | grep "space before tab in indent" check | |
86f8c236 WC |
574 | ' |
575 | ||
9afa2d4a | 576 | test_expect_success 'check mixed tabs and spaces in indent' ' |
9afa2d4a | 577 | # This is indented with HT SP HT. |
d55ef3e0 | 578 | echo " foo();" >x && |
946d2353 DL |
579 | test_must_fail git diff --check >check && |
580 | grep "space before tab in indent" check | |
9afa2d4a BF |
581 | ' |
582 | ||
62c64895 | 583 | test_expect_success 'check with no whitespace errors' ' |
62c64895 | 584 | git commit -m "snapshot" && |
d55ef3e0 | 585 | echo "foo();" >x && |
62c64895 | 586 | git diff --check |
62c64895 WC |
587 | ' |
588 | ||
f8175466 | 589 | test_expect_success 'check with trailing whitespace' ' |
d55ef3e0 | 590 | echo "foo(); " >x && |
d492b31c | 591 | test_must_fail git diff --check |
62c64895 WC |
592 | ' |
593 | ||
f8175466 | 594 | test_expect_success 'check with space before tab in indent' ' |
62c64895 | 595 | # indent has space followed by hard tab |
d55ef3e0 | 596 | echo " foo();" >x && |
d492b31c | 597 | test_must_fail git diff --check |
62c64895 WC |
598 | ' |
599 | ||
da31b358 | 600 | test_expect_success '--check and --exit-code are not exclusive' ' |
62c64895 WC |
601 | git checkout x && |
602 | git diff --check --exit-code | |
62c64895 WC |
603 | ' |
604 | ||
da31b358 | 605 | test_expect_success '--check and --quiet are not exclusive' ' |
62c64895 | 606 | git diff --check --quiet |
62c64895 WC |
607 | ' |
608 | ||
50f04394 JH |
609 | test_expect_success '-w and --exit-code interact sensibly' ' |
610 | test_when_finished "git checkout x" && | |
611 | { | |
612 | test_seq 15 && | |
613 | echo " 16" | |
614 | } >x && | |
615 | test_must_fail git diff --exit-code && | |
616 | git diff -w >actual && | |
617 | test_must_be_empty actual && | |
618 | git diff -w --exit-code | |
619 | ' | |
620 | ||
621 | test_expect_success '-I and --exit-code interact sensibly' ' | |
622 | test_when_finished "git checkout x" && | |
623 | { | |
624 | test_seq 15 && | |
625 | echo " 16" | |
626 | } >x && | |
627 | test_must_fail git diff --exit-code && | |
628 | git diff -I. >actual && | |
629 | test_must_be_empty actual && | |
630 | git diff -I. --exit-code | |
631 | ' | |
632 | ||
62c64895 | 633 | test_expect_success 'check staged with no whitespace errors' ' |
d55ef3e0 | 634 | echo "foo();" >x && |
62c64895 WC |
635 | git add x && |
636 | git diff --cached --check | |
62c64895 WC |
637 | ' |
638 | ||
f8175466 | 639 | test_expect_success 'check staged with trailing whitespace' ' |
d55ef3e0 | 640 | echo "foo(); " >x && |
62c64895 | 641 | git add x && |
d492b31c | 642 | test_must_fail git diff --cached --check |
62c64895 WC |
643 | ' |
644 | ||
f8175466 | 645 | test_expect_success 'check staged with space before tab in indent' ' |
62c64895 | 646 | # indent has space followed by hard tab |
d55ef3e0 | 647 | echo " foo();" >x && |
62c64895 | 648 | git add x && |
d492b31c | 649 | test_must_fail git diff --cached --check |
62c64895 WC |
650 | ' |
651 | ||
652 | test_expect_success 'check with no whitespace errors (diff-index)' ' | |
d55ef3e0 | 653 | echo "foo();" >x && |
62c64895 WC |
654 | git add x && |
655 | git diff-index --check HEAD | |
62c64895 WC |
656 | ' |
657 | ||
f8175466 | 658 | test_expect_success 'check with trailing whitespace (diff-index)' ' |
d55ef3e0 | 659 | echo "foo(); " >x && |
62c64895 | 660 | git add x && |
d492b31c | 661 | test_must_fail git diff-index --check HEAD |
62c64895 WC |
662 | ' |
663 | ||
f8175466 | 664 | test_expect_success 'check with space before tab in indent (diff-index)' ' |
62c64895 | 665 | # indent has space followed by hard tab |
d55ef3e0 | 666 | echo " foo();" >x && |
62c64895 | 667 | git add x && |
d492b31c | 668 | test_must_fail git diff-index --check HEAD |
62c64895 WC |
669 | ' |
670 | ||
671 | test_expect_success 'check staged with no whitespace errors (diff-index)' ' | |
d55ef3e0 | 672 | echo "foo();" >x && |
62c64895 WC |
673 | git add x && |
674 | git diff-index --cached --check HEAD | |
62c64895 WC |
675 | ' |
676 | ||
f8175466 | 677 | test_expect_success 'check staged with trailing whitespace (diff-index)' ' |
d55ef3e0 | 678 | echo "foo(); " >x && |
62c64895 | 679 | git add x && |
d492b31c | 680 | test_must_fail git diff-index --cached --check HEAD |
62c64895 WC |
681 | ' |
682 | ||
f8175466 | 683 | test_expect_success 'check staged with space before tab in indent (diff-index)' ' |
62c64895 | 684 | # indent has space followed by hard tab |
d55ef3e0 | 685 | echo " foo();" >x && |
62c64895 | 686 | git add x && |
d492b31c | 687 | test_must_fail git diff-index --cached --check HEAD |
62c64895 WC |
688 | ' |
689 | ||
690 | test_expect_success 'check with no whitespace errors (diff-tree)' ' | |
d55ef3e0 | 691 | echo "foo();" >x && |
62c64895 WC |
692 | git commit -m "new commit" x && |
693 | git diff-tree --check HEAD^ HEAD | |
62c64895 WC |
694 | ' |
695 | ||
f8175466 | 696 | test_expect_success 'check with trailing whitespace (diff-tree)' ' |
d55ef3e0 | 697 | echo "foo(); " >x && |
62c64895 | 698 | git commit -m "another commit" x && |
d492b31c | 699 | test_must_fail git diff-tree --check HEAD^ HEAD |
62c64895 WC |
700 | ' |
701 | ||
f8175466 | 702 | test_expect_success 'check with space before tab in indent (diff-tree)' ' |
62c64895 | 703 | # indent has space followed by hard tab |
d55ef3e0 | 704 | echo " foo();" >x && |
62c64895 | 705 | git commit -m "yet another" x && |
d492b31c | 706 | test_must_fail git diff-tree --check HEAD^ HEAD |
f8175466 JH |
707 | ' |
708 | ||
fd66bcc3 BW |
709 | test_expect_success 'check with ignored trailing whitespace attr (diff-tree)' ' |
710 | test_when_finished "git reset --hard HEAD^" && | |
711 | ||
712 | # create a whitespace error that should be ignored | |
713 | echo "* -whitespace" >.gitattributes && | |
714 | git add .gitattributes && | |
715 | echo "foo(); " >x && | |
716 | git add x && | |
717 | git commit -m "add trailing space" && | |
718 | ||
719 | # with a worktree diff-tree ignores the whitespace error | |
720 | git diff-tree --root --check HEAD && | |
721 | ||
722 | # without a worktree diff-tree still ignores the whitespace error | |
723 | git -C .git diff-tree --root --check HEAD | |
724 | ' | |
725 | ||
f8175466 | 726 | test_expect_success 'check trailing whitespace (trailing-space: off)' ' |
f8175466 | 727 | git config core.whitespace "-trailing-space" && |
d55ef3e0 | 728 | echo "foo (); " >x && |
f8175466 | 729 | git diff --check |
f8175466 JH |
730 | ' |
731 | ||
732 | test_expect_success 'check trailing whitespace (trailing-space: on)' ' | |
f8175466 | 733 | git config core.whitespace "trailing-space" && |
d55ef3e0 | 734 | echo "foo (); " >x && |
d492b31c | 735 | test_must_fail git diff --check |
f8175466 JH |
736 | ' |
737 | ||
738 | test_expect_success 'check space before tab in indent (space-before-tab: off)' ' | |
f8175466 JH |
739 | # indent contains space followed by HT |
740 | git config core.whitespace "-space-before-tab" && | |
d55ef3e0 | 741 | echo " foo ();" >x && |
f8175466 | 742 | git diff --check |
f8175466 JH |
743 | ' |
744 | ||
745 | test_expect_success 'check space before tab in indent (space-before-tab: on)' ' | |
f8175466 JH |
746 | # indent contains space followed by HT |
747 | git config core.whitespace "space-before-tab" && | |
d55ef3e0 | 748 | echo " foo (); " >x && |
d492b31c | 749 | test_must_fail git diff --check |
f8175466 JH |
750 | ' |
751 | ||
752 | test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' ' | |
a48fcd83 | 753 | git config core.whitespace "-indent-with-non-tab" && |
d55ef3e0 | 754 | echo " foo ();" >x && |
f8175466 | 755 | git diff --check |
f8175466 JH |
756 | ' |
757 | ||
758 | test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' ' | |
f8175466 | 759 | git config core.whitespace "indent-with-non-tab" && |
d55ef3e0 | 760 | echo " foo ();" >x && |
d492b31c | 761 | test_must_fail git diff --check |
62c64895 WC |
762 | ' |
763 | ||
f4b05a49 | 764 | test_expect_success 'ditto, but tabwidth=9' ' |
f4b05a49 JS |
765 | git config core.whitespace "indent-with-non-tab,tabwidth=9" && |
766 | git diff --check | |
f4b05a49 JS |
767 | ' |
768 | ||
9afa2d4a | 769 | test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' ' |
9afa2d4a | 770 | git config core.whitespace "indent-with-non-tab" && |
d55ef3e0 | 771 | echo " foo ();" >x && |
d492b31c | 772 | test_must_fail git diff --check |
9afa2d4a | 773 | ' |
0ef617f4 | 774 | |
f4b05a49 | 775 | test_expect_success 'ditto, but tabwidth=10' ' |
f4b05a49 JS |
776 | git config core.whitespace "indent-with-non-tab,tabwidth=10" && |
777 | test_must_fail git diff --check | |
f4b05a49 JS |
778 | ' |
779 | ||
780 | test_expect_success 'ditto, but tabwidth=20' ' | |
f4b05a49 JS |
781 | git config core.whitespace "indent-with-non-tab,tabwidth=20" && |
782 | git diff --check | |
f4b05a49 JS |
783 | ' |
784 | ||
b27eb499 | 785 | test_expect_success 'check tabs as indentation (tab-in-indent: off)' ' |
b27eb499 | 786 | git config core.whitespace "-tab-in-indent" && |
d55ef3e0 | 787 | echo " foo ();" >x && |
b27eb499 | 788 | git diff --check |
b27eb499 CW |
789 | ' |
790 | ||
791 | test_expect_success 'check tabs as indentation (tab-in-indent: on)' ' | |
b27eb499 | 792 | git config core.whitespace "tab-in-indent" && |
d55ef3e0 | 793 | echo " foo ();" >x && |
b27eb499 | 794 | test_must_fail git diff --check |
b27eb499 CW |
795 | ' |
796 | ||
797 | test_expect_success 'check tabs and spaces as indentation (tab-in-indent: on)' ' | |
b27eb499 | 798 | git config core.whitespace "tab-in-indent" && |
d55ef3e0 | 799 | echo " foo ();" >x && |
b27eb499 | 800 | test_must_fail git diff --check |
b27eb499 CW |
801 | ' |
802 | ||
f4b05a49 | 803 | test_expect_success 'ditto, but tabwidth=1 (must be irrelevant)' ' |
f4b05a49 JS |
804 | git config core.whitespace "tab-in-indent,tabwidth=1" && |
805 | test_must_fail git diff --check | |
f4b05a49 JS |
806 | ' |
807 | ||
b27eb499 | 808 | test_expect_success 'check tab-in-indent and indent-with-non-tab conflict' ' |
b27eb499 | 809 | git config core.whitespace "tab-in-indent,indent-with-non-tab" && |
d55ef3e0 | 810 | echo "foo ();" >x && |
b27eb499 | 811 | test_must_fail git diff --check |
b27eb499 CW |
812 | ' |
813 | ||
814 | test_expect_success 'check tab-in-indent excluded from wildcard whitespace attribute' ' | |
b27eb499 | 815 | git config --unset core.whitespace && |
d55ef3e0 JH |
816 | echo "x whitespace" >.gitattributes && |
817 | echo " foo ();" >x && | |
b27eb499 CW |
818 | git diff --check && |
819 | rm -f .gitattributes | |
b27eb499 CW |
820 | ' |
821 | ||
0ef617f4 | 822 | test_expect_success 'line numbers in --check output are correct' ' |
d55ef3e0 JH |
823 | echo "" >x && |
824 | echo "foo(); " >>x && | |
946d2353 DL |
825 | test_must_fail git diff --check >check && |
826 | grep "x:2:" check | |
0ef617f4 JH |
827 | ' |
828 | ||
5b5061ef | 829 | test_expect_success 'checkdiff detects new trailing blank lines (1)' ' |
877f23cc JH |
830 | echo "foo();" >x && |
831 | echo "" >>x && | |
946d2353 DL |
832 | test_must_fail git diff --check >check && |
833 | grep "new blank line" check | |
877f23cc JH |
834 | ' |
835 | ||
467babf8 | 836 | test_expect_success 'checkdiff detects new trailing blank lines (2)' ' |
afd43c99 | 837 | test_write_lines a b "" "" >x && |
467babf8 | 838 | git add x && |
afd43c99 | 839 | test_write_lines a "" "" "" "" >x && |
946d2353 DL |
840 | test_must_fail git diff --check >check && |
841 | grep "new blank line" check | |
877f23cc JH |
842 | ' |
843 | ||
c35539eb JH |
844 | test_expect_success 'checkdiff allows new blank lines' ' |
845 | git checkout x && | |
846 | mv x y && | |
847 | ( | |
848 | echo "/* This is new */" && | |
849 | echo "" && | |
850 | cat y | |
851 | ) >x && | |
852 | git diff --check | |
853 | ' | |
854 | ||
1cf3d5db | 855 | test_expect_success 'whitespace-only changes not reported (diff)' ' |
3e97c7c6 GB |
856 | git reset --hard && |
857 | echo >x "hello world" && | |
858 | git add x && | |
859 | git commit -m "hello 1" && | |
860 | echo >x "hello world" && | |
861 | git diff -b >actual && | |
1c5e94f4 | 862 | test_must_be_empty actual |
3e97c7c6 GB |
863 | ' |
864 | ||
1cf3d5db MR |
865 | test_expect_success 'whitespace-only changes not reported (diffstat)' ' |
866 | # reuse state from previous test | |
867 | git diff --stat -b >actual && | |
868 | test_must_be_empty actual | |
869 | ' | |
870 | ||
871 | test_expect_success 'whitespace changes with modification reported (diffstat)' ' | |
872 | git reset --hard && | |
873 | echo >x "hello world" && | |
874 | git update-index --chmod=+x x && | |
875 | git diff --stat --cached -b >actual && | |
876 | cat <<-EOF >expect && | |
877 | x | 0 | |
878 | 1 file changed, 0 insertions(+), 0 deletions(-) | |
879 | EOF | |
880 | test_cmp expect actual | |
881 | ' | |
882 | ||
883 | test_expect_success 'whitespace-only changes reported across renames (diffstat)' ' | |
296c6bb2 | 884 | git reset --hard && |
cbe1d9d6 | 885 | for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i" || return 1; done >x && |
296c6bb2 | 886 | git add x && |
1cf3d5db MR |
887 | git commit -m "base" && |
888 | sed -e "5s/^/ /" x >z && | |
889 | git rm x && | |
890 | git add z && | |
891 | git diff -w -M --cached --stat >actual && | |
892 | cat <<-EOF >expect && | |
893 | x => z | 0 | |
894 | 1 file changed, 0 insertions(+), 0 deletions(-) | |
895 | EOF | |
896 | test_cmp expect actual | |
897 | ' | |
898 | ||
899 | test_expect_success 'whitespace-only changes reported across renames' ' | |
900 | git reset --hard HEAD~1 && | |
cbe1d9d6 | 901 | for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i" || return 1; done >x && |
1cf3d5db | 902 | git add x && |
946d2353 DL |
903 | hash_x=$(git hash-object x) && |
904 | before=$(git rev-parse --short "$hash_x") && | |
296c6bb2 CC |
905 | git commit -m "base" && |
906 | sed -e "5s/^/ /" x >z && | |
907 | git rm x && | |
908 | git add z && | |
946d2353 DL |
909 | hash_z=$(git hash-object z) && |
910 | after=$(git rev-parse --short "$hash_z") && | |
911 | git diff -w -M --cached >actual.raw && | |
912 | sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" actual.raw >actual && | |
79b0edc1 | 913 | cat <<-EOF >expect && |
914 | diff --git a/x b/z | |
915 | similarity index NUM% | |
916 | rename from x | |
917 | rename to z | |
918 | index $before..$after 100644 | |
919 | EOF | |
296c6bb2 CC |
920 | test_cmp expect actual |
921 | ' | |
922 | ||
923 | cat >expected <<\EOF | |
924 | diff --git a/empty b/void | |
925 | similarity index 100% | |
926 | rename from empty | |
927 | rename to void | |
928 | EOF | |
929 | ||
930 | test_expect_success 'rename empty' ' | |
931 | git reset --hard && | |
932 | >empty && | |
933 | git add empty && | |
934 | git commit -m empty && | |
935 | git mv empty void && | |
936 | git diff -w --cached -M >current && | |
937 | test_cmp expected current | |
938 | ' | |
939 | ||
5e568f9e AG |
940 | test_expect_success 'combined diff with autocrlf conversion' ' |
941 | ||
942 | git reset --hard && | |
26d0a6d4 | 943 | test_commit "one side" x hello one-side && |
5e568f9e AG |
944 | git checkout HEAD^ && |
945 | echo >x goodbye && | |
946 | git commit -m "the other side" x && | |
947 | git config core.autocrlf true && | |
26d0a6d4 | 948 | test_must_fail git merge one-side >actual && |
6789275d | 949 | test_grep "Automatic merge failed" actual && |
5e568f9e | 950 | |
946d2353 DL |
951 | git diff >actual.raw && |
952 | sed -e "1,/^@@@/d" actual.raw >actual && | |
5e568f9e AG |
953 | ! grep "^-" actual |
954 | ||
955 | ' | |
956 | ||
cfd1a984 KB |
957 | # Start testing the colored format for whitespace checks |
958 | ||
959 | test_expect_success 'setup diff colors' ' | |
cfd1a984 KB |
960 | git config color.diff.plain normal && |
961 | git config color.diff.meta bold && | |
962 | git config color.diff.frag cyan && | |
963 | git config color.diff.func normal && | |
964 | git config color.diff.old red && | |
965 | git config color.diff.new green && | |
966 | git config color.diff.commit yellow && | |
0ad782f2 | 967 | git config color.diff.whitespace blue && |
cfd1a984 | 968 | |
0ad782f2 JH |
969 | git config core.autocrlf false |
970 | ' | |
971 | ||
972 | test_expect_success 'diff that introduces a line with only tabs' ' | |
973 | git config core.whitespace blank-at-eol && | |
974 | git reset --hard && | |
975 | echo "test" >x && | |
946d2353 DL |
976 | old_hash_x=$(git hash-object x) && |
977 | before=$(git rev-parse --short "$old_hash_x") && | |
0ad782f2 JH |
978 | git commit -m "initial" x && |
979 | echo "{NTN}" | tr "NT" "\n\t" >>x && | |
946d2353 DL |
980 | new_hash_x=$(git hash-object x) && |
981 | after=$(git rev-parse --short "$new_hash_x") && | |
982 | git diff --color >current.raw && | |
983 | test_decode_color <current.raw >current && | |
d55ef3e0 | 984 | |
79b0edc1 | 985 | cat >expected <<-EOF && |
d55ef3e0 | 986 | <BOLD>diff --git a/x b/x<RESET> |
79b0edc1 | 987 | <BOLD>index $before..$after 100644<RESET> |
d55ef3e0 JH |
988 | <BOLD>--- a/x<RESET> |
989 | <BOLD>+++ b/x<RESET> | |
990 | <CYAN>@@ -1 +1,4 @@<RESET> | |
991 | test<RESET> | |
992 | <GREEN>+<RESET><GREEN>{<RESET> | |
0ad782f2 | 993 | <GREEN>+<RESET><BLUE> <RESET> |
d55ef3e0 JH |
994 | <GREEN>+<RESET><GREEN>}<RESET> |
995 | EOF | |
cfd1a984 | 996 | |
cfd1a984 KB |
997 | test_cmp expected current |
998 | ' | |
999 | ||
b8767f79 JH |
1000 | test_expect_success 'diff that introduces and removes ws breakages' ' |
1001 | git reset --hard && | |
1002 | { | |
1003 | echo "0. blank-at-eol " && | |
1004 | echo "1. blank-at-eol " | |
1005 | } >x && | |
946d2353 DL |
1006 | old_hash_x=$(git hash-object x) && |
1007 | before=$(git rev-parse --short "$old_hash_x") && | |
b8767f79 JH |
1008 | git commit -a --allow-empty -m preimage && |
1009 | { | |
1010 | echo "0. blank-at-eol " && | |
1011 | echo "1. still-blank-at-eol " && | |
1012 | echo "2. and a new line " | |
1013 | } >x && | |
946d2353 DL |
1014 | new_hash_x=$(git hash-object x) && |
1015 | after=$(git rev-parse --short "$new_hash_x") && | |
b8767f79 | 1016 | |
946d2353 DL |
1017 | git diff --color >current.raw && |
1018 | test_decode_color <current.raw >current && | |
b8767f79 | 1019 | |
79b0edc1 | 1020 | cat >expected <<-EOF && |
b8767f79 | 1021 | <BOLD>diff --git a/x b/x<RESET> |
79b0edc1 | 1022 | <BOLD>index $before..$after 100644<RESET> |
b8767f79 JH |
1023 | <BOLD>--- a/x<RESET> |
1024 | <BOLD>+++ b/x<RESET> | |
1025 | <CYAN>@@ -1,2 +1,3 @@<RESET> | |
1026 | 0. blank-at-eol <RESET> | |
1027 | <RED>-1. blank-at-eol <RESET> | |
1028 | <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET> | |
1029 | <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET> | |
1030 | EOF | |
1031 | ||
1032 | test_cmp expected current | |
1033 | ' | |
1034 | ||
f3f5c7f5 JH |
1035 | test_expect_success 'ws-error-highlight test setup' ' |
1036 | ||
b8767f79 JH |
1037 | git reset --hard && |
1038 | { | |
1039 | echo "0. blank-at-eol " && | |
1040 | echo "1. blank-at-eol " | |
1041 | } >x && | |
946d2353 DL |
1042 | old_hash_x=$(git hash-object x) && |
1043 | before=$(git rev-parse --short "$old_hash_x") && | |
b8767f79 JH |
1044 | git commit -a --allow-empty -m preimage && |
1045 | { | |
1046 | echo "0. blank-at-eol " && | |
1047 | echo "1. still-blank-at-eol " && | |
1048 | echo "2. and a new line " | |
1049 | } >x && | |
946d2353 DL |
1050 | new_hash_x=$(git hash-object x) && |
1051 | after=$(git rev-parse --short "$new_hash_x") && | |
b8767f79 | 1052 | |
79b0edc1 | 1053 | cat >expect.default-old <<-EOF && |
b8767f79 | 1054 | <BOLD>diff --git a/x b/x<RESET> |
79b0edc1 | 1055 | <BOLD>index $before..$after 100644<RESET> |
b8767f79 JH |
1056 | <BOLD>--- a/x<RESET> |
1057 | <BOLD>+++ b/x<RESET> | |
1058 | <CYAN>@@ -1,2 +1,3 @@<RESET> | |
1059 | 0. blank-at-eol <RESET> | |
1060 | <RED>-<RESET><RED>1. blank-at-eol<RESET><BLUE> <RESET> | |
1061 | <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET> | |
1062 | <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET> | |
1063 | EOF | |
1064 | ||
79b0edc1 | 1065 | cat >expect.all <<-EOF && |
b8767f79 | 1066 | <BOLD>diff --git a/x b/x<RESET> |
79b0edc1 | 1067 | <BOLD>index $before..$after 100644<RESET> |
b8767f79 JH |
1068 | <BOLD>--- a/x<RESET> |
1069 | <BOLD>+++ b/x<RESET> | |
1070 | <CYAN>@@ -1,2 +1,3 @@<RESET> | |
1071 | <RESET>0. blank-at-eol<RESET><BLUE> <RESET> | |
1072 | <RED>-<RESET><RED>1. blank-at-eol<RESET><BLUE> <RESET> | |
1073 | <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET> | |
1074 | <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET> | |
1075 | EOF | |
1076 | ||
79b0edc1 | 1077 | cat >expect.none <<-EOF |
b8767f79 | 1078 | <BOLD>diff --git a/x b/x<RESET> |
79b0edc1 | 1079 | <BOLD>index $before..$after 100644<RESET> |
b8767f79 JH |
1080 | <BOLD>--- a/x<RESET> |
1081 | <BOLD>+++ b/x<RESET> | |
1082 | <CYAN>@@ -1,2 +1,3 @@<RESET> | |
1083 | 0. blank-at-eol <RESET> | |
1084 | <RED>-1. blank-at-eol <RESET> | |
1085 | <GREEN>+1. still-blank-at-eol <RESET> | |
1086 | <GREEN>+2. and a new line <RESET> | |
1087 | EOF | |
1088 | ||
f3f5c7f5 JH |
1089 | ' |
1090 | ||
1091 | test_expect_success 'test --ws-error-highlight option' ' | |
1092 | ||
946d2353 DL |
1093 | git diff --color --ws-error-highlight=default,old >current.raw && |
1094 | test_decode_color <current.raw >current && | |
f3f5c7f5 JH |
1095 | test_cmp expect.default-old current && |
1096 | ||
946d2353 DL |
1097 | git diff --color --ws-error-highlight=all >current.raw && |
1098 | test_decode_color <current.raw >current && | |
f3f5c7f5 JH |
1099 | test_cmp expect.all current && |
1100 | ||
946d2353 DL |
1101 | git diff --color --ws-error-highlight=none >current.raw && |
1102 | test_decode_color <current.raw >current && | |
f3f5c7f5 JH |
1103 | test_cmp expect.none current |
1104 | ||
b8767f79 JH |
1105 | ' |
1106 | ||
a17505f2 JH |
1107 | test_expect_success 'test diff.wsErrorHighlight config' ' |
1108 | ||
946d2353 DL |
1109 | git -c diff.wsErrorHighlight=default,old diff --color >current.raw && |
1110 | test_decode_color <current.raw >current && | |
a17505f2 JH |
1111 | test_cmp expect.default-old current && |
1112 | ||
946d2353 DL |
1113 | git -c diff.wsErrorHighlight=all diff --color >current.raw && |
1114 | test_decode_color <current.raw >current && | |
a17505f2 JH |
1115 | test_cmp expect.all current && |
1116 | ||
946d2353 DL |
1117 | git -c diff.wsErrorHighlight=none diff --color >current.raw && |
1118 | test_decode_color <current.raw >current && | |
a17505f2 JH |
1119 | test_cmp expect.none current |
1120 | ||
1121 | ' | |
1122 | ||
1123 | test_expect_success 'option overrides diff.wsErrorHighlight' ' | |
1124 | ||
a655a595 | 1125 | git -c diff.wsErrorHighlight=none \ |
946d2353 DL |
1126 | diff --color --ws-error-highlight=default,old >current.raw && |
1127 | test_decode_color <current.raw >current && | |
a17505f2 JH |
1128 | test_cmp expect.default-old current && |
1129 | ||
a655a595 | 1130 | git -c diff.wsErrorHighlight=default \ |
946d2353 DL |
1131 | diff --color --ws-error-highlight=all >current.raw && |
1132 | test_decode_color <current.raw >current && | |
a17505f2 JH |
1133 | test_cmp expect.all current && |
1134 | ||
a655a595 | 1135 | git -c diff.wsErrorHighlight=all \ |
946d2353 DL |
1136 | diff --color --ws-error-highlight=none >current.raw && |
1137 | test_decode_color <current.raw >current && | |
a17505f2 JH |
1138 | test_cmp expect.none current |
1139 | ||
1140 | ' | |
1141 | ||
2e2d5ac1 SB |
1142 | test_expect_success 'detect moved code, complete file' ' |
1143 | git reset --hard && | |
1144 | cat <<-\EOF >test.c && | |
1145 | #include<stdio.h> | |
1146 | main() | |
1147 | { | |
1148 | printf("Hello World"); | |
1149 | } | |
1150 | EOF | |
1151 | git add test.c && | |
1152 | git commit -m "add main function" && | |
79b0edc1 | 1153 | file=$(git rev-parse --short HEAD:test.c) && |
2e2d5ac1 SB |
1154 | git mv test.c main.c && |
1155 | test_config color.diff.oldMoved "normal red" && | |
1156 | test_config color.diff.newMoved "normal green" && | |
946d2353 DL |
1157 | git diff HEAD --color-moved=zebra --color --no-renames >actual.raw && |
1158 | test_decode_color <actual.raw >actual && | |
79b0edc1 | 1159 | cat >expected <<-EOF && |
2e2d5ac1 SB |
1160 | <BOLD>diff --git a/main.c b/main.c<RESET> |
1161 | <BOLD>new file mode 100644<RESET> | |
79b0edc1 | 1162 | <BOLD>index 0000000..$file<RESET> |
2e2d5ac1 SB |
1163 | <BOLD>--- /dev/null<RESET> |
1164 | <BOLD>+++ b/main.c<RESET> | |
1165 | <CYAN>@@ -0,0 +1,5 @@<RESET> | |
1166 | <BGREEN>+<RESET><BGREEN>#include<stdio.h><RESET> | |
1167 | <BGREEN>+<RESET><BGREEN>main()<RESET> | |
1168 | <BGREEN>+<RESET><BGREEN>{<RESET> | |
1169 | <BGREEN>+<RESET><BGREEN>printf("Hello World");<RESET> | |
1170 | <BGREEN>+<RESET><BGREEN>}<RESET> | |
1171 | <BOLD>diff --git a/test.c b/test.c<RESET> | |
1172 | <BOLD>deleted file mode 100644<RESET> | |
79b0edc1 | 1173 | <BOLD>index $file..0000000<RESET> |
2e2d5ac1 SB |
1174 | <BOLD>--- a/test.c<RESET> |
1175 | <BOLD>+++ /dev/null<RESET> | |
1176 | <CYAN>@@ -1,5 +0,0 @@<RESET> | |
1177 | <BRED>-#include<stdio.h><RESET> | |
1178 | <BRED>-main()<RESET> | |
1179 | <BRED>-{<RESET> | |
1180 | <BRED>-printf("Hello World");<RESET> | |
1181 | <BRED>-}<RESET> | |
1182 | EOF | |
1183 | ||
1184 | test_cmp expected actual | |
1185 | ' | |
1186 | ||
1187 | test_expect_success 'detect malicious moved code, inside file' ' | |
1188 | test_config color.diff.oldMoved "normal red" && | |
1189 | test_config color.diff.newMoved "normal green" && | |
1190 | test_config color.diff.oldMovedAlternative "blue" && | |
1191 | test_config color.diff.newMovedAlternative "yellow" && | |
1192 | git reset --hard && | |
1193 | cat <<-\EOF >main.c && | |
1194 | #include<stdio.h> | |
1195 | int stuff() | |
1196 | { | |
1197 | printf("Hello "); | |
1198 | printf("World\n"); | |
1199 | } | |
1200 | ||
1201 | int secure_foo(struct user *u) | |
1202 | { | |
1203 | if (!u->is_allowed_foo) | |
1204 | return; | |
1205 | foo(u); | |
1206 | } | |
1207 | ||
1208 | int main() | |
1209 | { | |
1210 | foo(); | |
1211 | } | |
1212 | EOF | |
1213 | cat <<-\EOF >test.c && | |
1214 | #include<stdio.h> | |
1215 | int bar() | |
1216 | { | |
1217 | printf("Hello World, but different\n"); | |
1218 | } | |
1219 | ||
1220 | int another_function() | |
1221 | { | |
1222 | bar(); | |
1223 | } | |
1224 | EOF | |
1225 | git add main.c test.c && | |
1226 | git commit -m "add main and test file" && | |
79b0edc1 | 1227 | before_main=$(git rev-parse --short HEAD:main.c) && |
1228 | before_test=$(git rev-parse --short HEAD:test.c) && | |
2e2d5ac1 SB |
1229 | cat <<-\EOF >main.c && |
1230 | #include<stdio.h> | |
1231 | int stuff() | |
1232 | { | |
1233 | printf("Hello "); | |
1234 | printf("World\n"); | |
1235 | } | |
1236 | ||
1237 | int main() | |
1238 | { | |
1239 | foo(); | |
1240 | } | |
1241 | EOF | |
1242 | cat <<-\EOF >test.c && | |
1243 | #include<stdio.h> | |
1244 | int bar() | |
1245 | { | |
1246 | printf("Hello World, but different\n"); | |
1247 | } | |
1248 | ||
1249 | int secure_foo(struct user *u) | |
1250 | { | |
1251 | foo(u); | |
1252 | if (!u->is_allowed_foo) | |
1253 | return; | |
1254 | } | |
1255 | ||
1256 | int another_function() | |
1257 | { | |
1258 | bar(); | |
1259 | } | |
1260 | EOF | |
946d2353 DL |
1261 | hash_main=$(git hash-object main.c) && |
1262 | after_main=$(git rev-parse --short "$hash_main") && | |
1263 | hash_test=$(git hash-object test.c) && | |
1264 | after_test=$(git rev-parse --short "$hash_test") && | |
1265 | git diff HEAD --no-renames --color-moved=zebra --color >actual.raw && | |
1266 | test_decode_color <actual.raw >actual && | |
79b0edc1 | 1267 | cat <<-EOF >expected && |
2e2d5ac1 | 1268 | <BOLD>diff --git a/main.c b/main.c<RESET> |
79b0edc1 | 1269 | <BOLD>index $before_main..$after_main 100644<RESET> |
2e2d5ac1 SB |
1270 | <BOLD>--- a/main.c<RESET> |
1271 | <BOLD>+++ b/main.c<RESET> | |
1272 | <CYAN>@@ -5,13 +5,6 @@<RESET> <RESET>printf("Hello ");<RESET> | |
1273 | printf("World\n");<RESET> | |
1274 | }<RESET> | |
1275 | <RESET> | |
1276 | <BRED>-int secure_foo(struct user *u)<RESET> | |
1277 | <BRED>-{<RESET> | |
1278 | <BLUE>-if (!u->is_allowed_foo)<RESET> | |
1279 | <BLUE>-return;<RESET> | |
f0b8fb6e JT |
1280 | <RED>-foo(u);<RESET> |
1281 | <RED>-}<RESET> | |
1282 | <RED>-<RESET> | |
2e2d5ac1 SB |
1283 | int main()<RESET> |
1284 | {<RESET> | |
1285 | foo();<RESET> | |
1286 | <BOLD>diff --git a/test.c b/test.c<RESET> | |
79b0edc1 | 1287 | <BOLD>index $before_test..$after_test 100644<RESET> |
2e2d5ac1 SB |
1288 | <BOLD>--- a/test.c<RESET> |
1289 | <BOLD>+++ b/test.c<RESET> | |
1290 | <CYAN>@@ -4,6 +4,13 @@<RESET> <RESET>int bar()<RESET> | |
1291 | printf("Hello World, but different\n");<RESET> | |
1292 | }<RESET> | |
1293 | <RESET> | |
1294 | <BGREEN>+<RESET><BGREEN>int secure_foo(struct user *u)<RESET> | |
1295 | <BGREEN>+<RESET><BGREEN>{<RESET> | |
f0b8fb6e | 1296 | <GREEN>+<RESET><GREEN>foo(u);<RESET> |
2e2d5ac1 SB |
1297 | <BGREEN>+<RESET><BGREEN>if (!u->is_allowed_foo)<RESET> |
1298 | <BGREEN>+<RESET><BGREEN>return;<RESET> | |
f0b8fb6e JT |
1299 | <GREEN>+<RESET><GREEN>}<RESET> |
1300 | <GREEN>+<RESET> | |
2e2d5ac1 SB |
1301 | int another_function()<RESET> |
1302 | {<RESET> | |
1303 | bar();<RESET> | |
1304 | EOF | |
1305 | ||
1306 | test_cmp expected actual | |
1307 | ' | |
1308 | ||
176841f0 SB |
1309 | test_expect_success 'plain moved code, inside file' ' |
1310 | test_config color.diff.oldMoved "normal red" && | |
1311 | test_config color.diff.newMoved "normal green" && | |
1312 | test_config color.diff.oldMovedAlternative "blue" && | |
1313 | test_config color.diff.newMovedAlternative "yellow" && | |
1314 | # needs previous test as setup | |
946d2353 DL |
1315 | git diff HEAD --no-renames --color-moved=plain --color >actual.raw && |
1316 | test_decode_color <actual.raw >actual && | |
79b0edc1 | 1317 | cat <<-EOF >expected && |
176841f0 | 1318 | <BOLD>diff --git a/main.c b/main.c<RESET> |
79b0edc1 | 1319 | <BOLD>index $before_main..$after_main 100644<RESET> |
176841f0 SB |
1320 | <BOLD>--- a/main.c<RESET> |
1321 | <BOLD>+++ b/main.c<RESET> | |
1322 | <CYAN>@@ -5,13 +5,6 @@<RESET> <RESET>printf("Hello ");<RESET> | |
1323 | printf("World\n");<RESET> | |
1324 | }<RESET> | |
1325 | <RESET> | |
1326 | <BRED>-int secure_foo(struct user *u)<RESET> | |
1327 | <BRED>-{<RESET> | |
1328 | <BRED>-if (!u->is_allowed_foo)<RESET> | |
1329 | <BRED>-return;<RESET> | |
1330 | <BRED>-foo(u);<RESET> | |
1331 | <BRED>-}<RESET> | |
1332 | <BRED>-<RESET> | |
1333 | int main()<RESET> | |
1334 | {<RESET> | |
1335 | foo();<RESET> | |
1336 | <BOLD>diff --git a/test.c b/test.c<RESET> | |
79b0edc1 | 1337 | <BOLD>index $before_test..$after_test 100644<RESET> |
176841f0 SB |
1338 | <BOLD>--- a/test.c<RESET> |
1339 | <BOLD>+++ b/test.c<RESET> | |
1340 | <CYAN>@@ -4,6 +4,13 @@<RESET> <RESET>int bar()<RESET> | |
1341 | printf("Hello World, but different\n");<RESET> | |
1342 | }<RESET> | |
1343 | <RESET> | |
1344 | <BGREEN>+<RESET><BGREEN>int secure_foo(struct user *u)<RESET> | |
1345 | <BGREEN>+<RESET><BGREEN>{<RESET> | |
1346 | <BGREEN>+<RESET><BGREEN>foo(u);<RESET> | |
1347 | <BGREEN>+<RESET><BGREEN>if (!u->is_allowed_foo)<RESET> | |
1348 | <BGREEN>+<RESET><BGREEN>return;<RESET> | |
1349 | <BGREEN>+<RESET><BGREEN>}<RESET> | |
1350 | <BGREEN>+<RESET> | |
1351 | int another_function()<RESET> | |
1352 | {<RESET> | |
1353 | bar();<RESET> | |
1354 | EOF | |
1355 | ||
1356 | test_cmp expected actual | |
1357 | ' | |
1358 | ||
51da15eb | 1359 | test_expect_success 'detect blocks of moved code' ' |
86b452e2 SB |
1360 | git reset --hard && |
1361 | cat <<-\EOF >lines.txt && | |
f0b8fb6e JT |
1362 | long line 1 |
1363 | long line 2 | |
1364 | long line 3 | |
86b452e2 SB |
1365 | line 4 |
1366 | line 5 | |
1367 | line 6 | |
1368 | line 7 | |
1369 | line 8 | |
1370 | line 9 | |
1371 | line 10 | |
1372 | line 11 | |
1373 | line 12 | |
1374 | line 13 | |
f0b8fb6e JT |
1375 | long line 14 |
1376 | long line 15 | |
1377 | long line 16 | |
86b452e2 SB |
1378 | EOF |
1379 | git add lines.txt && | |
1380 | git commit -m "add poetry" && | |
1381 | cat <<-\EOF >lines.txt && | |
1382 | line 4 | |
1383 | line 5 | |
1384 | line 6 | |
1385 | line 7 | |
1386 | line 8 | |
1387 | line 9 | |
f0b8fb6e JT |
1388 | long line 1 |
1389 | long line 2 | |
1390 | long line 3 | |
1391 | long line 14 | |
1392 | long line 15 | |
1393 | long line 16 | |
86b452e2 SB |
1394 | line 10 |
1395 | line 11 | |
1396 | line 12 | |
1397 | line 13 | |
1398 | EOF | |
1399 | test_config color.diff.oldMoved "magenta" && | |
1400 | test_config color.diff.newMoved "cyan" && | |
1401 | test_config color.diff.oldMovedAlternative "blue" && | |
1402 | test_config color.diff.newMovedAlternative "yellow" && | |
1403 | test_config color.diff.oldMovedDimmed "normal magenta" && | |
1404 | test_config color.diff.newMovedDimmed "normal cyan" && | |
1405 | test_config color.diff.oldMovedAlternativeDimmed "normal blue" && | |
1406 | test_config color.diff.newMovedAlternativeDimmed "normal yellow" && | |
51da15eb SB |
1407 | git diff HEAD --no-renames --color-moved=blocks --color >actual.raw && |
1408 | grep -v "index" actual.raw | test_decode_color >actual && | |
1409 | cat <<-\EOF >expected && | |
1410 | <BOLD>diff --git a/lines.txt b/lines.txt<RESET> | |
1411 | <BOLD>--- a/lines.txt<RESET> | |
1412 | <BOLD>+++ b/lines.txt<RESET> | |
1413 | <CYAN>@@ -1,16 +1,16 @@<RESET> | |
1414 | <MAGENTA>-long line 1<RESET> | |
1415 | <MAGENTA>-long line 2<RESET> | |
1416 | <MAGENTA>-long line 3<RESET> | |
1417 | line 4<RESET> | |
1418 | line 5<RESET> | |
1419 | line 6<RESET> | |
1420 | line 7<RESET> | |
1421 | line 8<RESET> | |
1422 | line 9<RESET> | |
1423 | <CYAN>+<RESET><CYAN>long line 1<RESET> | |
1424 | <CYAN>+<RESET><CYAN>long line 2<RESET> | |
1425 | <CYAN>+<RESET><CYAN>long line 3<RESET> | |
1426 | <CYAN>+<RESET><CYAN>long line 14<RESET> | |
1427 | <CYAN>+<RESET><CYAN>long line 15<RESET> | |
1428 | <CYAN>+<RESET><CYAN>long line 16<RESET> | |
1429 | line 10<RESET> | |
1430 | line 11<RESET> | |
1431 | line 12<RESET> | |
1432 | line 13<RESET> | |
1433 | <MAGENTA>-long line 14<RESET> | |
1434 | <MAGENTA>-long line 15<RESET> | |
1435 | <MAGENTA>-long line 16<RESET> | |
1436 | EOF | |
1437 | test_cmp expected actual | |
1438 | ||
1439 | ' | |
1440 | ||
706b0b5e | 1441 | test_expect_success 'detect permutations inside moved code -- dimmed-zebra' ' |
51da15eb SB |
1442 | # reuse setup from test before! |
1443 | test_config color.diff.oldMoved "magenta" && | |
1444 | test_config color.diff.newMoved "cyan" && | |
1445 | test_config color.diff.oldMovedAlternative "blue" && | |
1446 | test_config color.diff.newMovedAlternative "yellow" && | |
1447 | test_config color.diff.oldMovedDimmed "normal magenta" && | |
1448 | test_config color.diff.newMovedDimmed "normal cyan" && | |
1449 | test_config color.diff.oldMovedAlternativeDimmed "normal blue" && | |
1450 | test_config color.diff.newMovedAlternativeDimmed "normal yellow" && | |
706b0b5e | 1451 | git diff HEAD --no-renames --color-moved=dimmed-zebra --color >actual.raw && |
74cfa7be | 1452 | grep -v "index" actual.raw | test_decode_color >actual && |
86b452e2 SB |
1453 | cat <<-\EOF >expected && |
1454 | <BOLD>diff --git a/lines.txt b/lines.txt<RESET> | |
86b452e2 SB |
1455 | <BOLD>--- a/lines.txt<RESET> |
1456 | <BOLD>+++ b/lines.txt<RESET> | |
1457 | <CYAN>@@ -1,16 +1,16 @@<RESET> | |
f0b8fb6e JT |
1458 | <BMAGENTA>-long line 1<RESET> |
1459 | <BMAGENTA>-long line 2<RESET> | |
1460 | <BMAGENTA>-long line 3<RESET> | |
86b452e2 SB |
1461 | line 4<RESET> |
1462 | line 5<RESET> | |
1463 | line 6<RESET> | |
1464 | line 7<RESET> | |
1465 | line 8<RESET> | |
1466 | line 9<RESET> | |
f0b8fb6e JT |
1467 | <BCYAN>+<RESET><BCYAN>long line 1<RESET> |
1468 | <BCYAN>+<RESET><BCYAN>long line 2<RESET> | |
1469 | <CYAN>+<RESET><CYAN>long line 3<RESET> | |
1470 | <YELLOW>+<RESET><YELLOW>long line 14<RESET> | |
1471 | <BYELLOW>+<RESET><BYELLOW>long line 15<RESET> | |
1472 | <BYELLOW>+<RESET><BYELLOW>long line 16<RESET> | |
86b452e2 SB |
1473 | line 10<RESET> |
1474 | line 11<RESET> | |
1475 | line 12<RESET> | |
1476 | line 13<RESET> | |
f0b8fb6e JT |
1477 | <BMAGENTA>-long line 14<RESET> |
1478 | <BMAGENTA>-long line 15<RESET> | |
1479 | <BMAGENTA>-long line 16<RESET> | |
86b452e2 SB |
1480 | EOF |
1481 | test_cmp expected actual | |
1482 | ' | |
1483 | ||
eb315457 PW |
1484 | test_expect_success 'zebra alternate color is only used when necessary' ' |
1485 | cat >old.txt <<-\EOF && | |
1486 | line 1A should be marked as oldMoved newMovedAlternate | |
1487 | line 1B should be marked as oldMoved newMovedAlternate | |
1488 | unchanged | |
1489 | line 2A should be marked as oldMoved newMovedAlternate | |
1490 | line 2B should be marked as oldMoved newMovedAlternate | |
1491 | line 3A should be marked as oldMovedAlternate newMoved | |
1492 | line 3B should be marked as oldMovedAlternate newMoved | |
1493 | unchanged | |
1494 | line 4A should be marked as oldMoved newMovedAlternate | |
1495 | line 4B should be marked as oldMoved newMovedAlternate | |
1496 | line 5A should be marked as oldMovedAlternate newMoved | |
1497 | line 5B should be marked as oldMovedAlternate newMoved | |
1498 | line 6A should be marked as oldMoved newMoved | |
1499 | line 6B should be marked as oldMoved newMoved | |
1500 | EOF | |
1501 | cat >new.txt <<-\EOF && | |
1502 | line 1A should be marked as oldMoved newMovedAlternate | |
1503 | line 1B should be marked as oldMoved newMovedAlternate | |
1504 | unchanged | |
1505 | line 3A should be marked as oldMovedAlternate newMoved | |
1506 | line 3B should be marked as oldMovedAlternate newMoved | |
1507 | line 2A should be marked as oldMoved newMovedAlternate | |
1508 | line 2B should be marked as oldMoved newMovedAlternate | |
1509 | unchanged | |
1510 | line 6A should be marked as oldMoved newMoved | |
1511 | line 6B should be marked as oldMoved newMoved | |
1512 | line 4A should be marked as oldMoved newMovedAlternate | |
1513 | line 4B should be marked as oldMoved newMovedAlternate | |
1514 | line 5A should be marked as oldMovedAlternate newMoved | |
1515 | line 5B should be marked as oldMovedAlternate newMoved | |
1516 | EOF | |
1517 | test_expect_code 1 git diff --no-index --color --color-moved=zebra \ | |
1518 | --color-moved-ws=allow-indentation-change \ | |
1519 | old.txt new.txt >output && | |
1520 | grep -v index output | test_decode_color >actual && | |
1521 | cat >expected <<-\EOF && | |
1522 | <BOLD>diff --git a/old.txt b/new.txt<RESET> | |
1523 | <BOLD>--- a/old.txt<RESET> | |
1524 | <BOLD>+++ b/new.txt<RESET> | |
1525 | <CYAN>@@ -1,14 +1,14 @@<RESET> | |
1526 | <BOLD;MAGENTA>-line 1A should be marked as oldMoved newMovedAlternate<RESET> | |
1527 | <BOLD;MAGENTA>-line 1B should be marked as oldMoved newMovedAlternate<RESET> | |
1528 | <BOLD;CYAN>+<RESET><BOLD;CYAN> line 1A should be marked as oldMoved newMovedAlternate<RESET> | |
1529 | <BOLD;CYAN>+<RESET><BOLD;CYAN> line 1B should be marked as oldMoved newMovedAlternate<RESET> | |
1530 | unchanged<RESET> | |
1531 | <BOLD;MAGENTA>-line 2A should be marked as oldMoved newMovedAlternate<RESET> | |
1532 | <BOLD;MAGENTA>-line 2B should be marked as oldMoved newMovedAlternate<RESET> | |
1533 | <BOLD;BLUE>-line 3A should be marked as oldMovedAlternate newMoved<RESET> | |
1534 | <BOLD;BLUE>-line 3B should be marked as oldMovedAlternate newMoved<RESET> | |
1535 | <BOLD;CYAN>+<RESET><BOLD;CYAN> line 3A should be marked as oldMovedAlternate newMoved<RESET> | |
1536 | <BOLD;CYAN>+<RESET><BOLD;CYAN> line 3B should be marked as oldMovedAlternate newMoved<RESET> | |
1537 | <BOLD;YELLOW>+<RESET><BOLD;YELLOW> line 2A should be marked as oldMoved newMovedAlternate<RESET> | |
1538 | <BOLD;YELLOW>+<RESET><BOLD;YELLOW> line 2B should be marked as oldMoved newMovedAlternate<RESET> | |
1539 | unchanged<RESET> | |
1540 | <BOLD;MAGENTA>-line 4A should be marked as oldMoved newMovedAlternate<RESET> | |
1541 | <BOLD;MAGENTA>-line 4B should be marked as oldMoved newMovedAlternate<RESET> | |
1542 | <BOLD;BLUE>-line 5A should be marked as oldMovedAlternate newMoved<RESET> | |
1543 | <BOLD;BLUE>-line 5B should be marked as oldMovedAlternate newMoved<RESET> | |
1544 | <BOLD;MAGENTA>-line 6A should be marked as oldMoved newMoved<RESET> | |
1545 | <BOLD;MAGENTA>-line 6B should be marked as oldMoved newMoved<RESET> | |
1546 | <BOLD;CYAN>+<RESET><BOLD;CYAN> line 6A should be marked as oldMoved newMoved<RESET> | |
1547 | <BOLD;CYAN>+<RESET><BOLD;CYAN> line 6B should be marked as oldMoved newMoved<RESET> | |
1548 | <BOLD;YELLOW>+<RESET><BOLD;YELLOW> line 4A should be marked as oldMoved newMovedAlternate<RESET> | |
1549 | <BOLD;YELLOW>+<RESET><BOLD;YELLOW> line 4B should be marked as oldMoved newMovedAlternate<RESET> | |
1550 | <BOLD;CYAN>+<RESET><BOLD;CYAN> line 5A should be marked as oldMovedAlternate newMoved<RESET> | |
1551 | <BOLD;CYAN>+<RESET><BOLD;CYAN> line 5B should be marked as oldMovedAlternate newMoved<RESET> | |
1552 | EOF | |
1553 | test_cmp expected actual | |
1554 | ' | |
1555 | ||
eb893525 PW |
1556 | test_expect_success 'short lines of opposite sign do not get marked as moved' ' |
1557 | cat >old.txt <<-\EOF && | |
1558 | this line should be marked as moved | |
1559 | unchanged | |
1560 | unchanged | |
1561 | unchanged | |
1562 | unchanged | |
1563 | too short | |
1564 | this line should be marked as oldMoved newMoved | |
1565 | this line should be marked as oldMovedAlternate newMoved | |
1566 | unchanged 1 | |
1567 | unchanged 2 | |
1568 | unchanged 3 | |
1569 | unchanged 4 | |
1570 | this line should be marked as oldMoved newMoved/newMovedAlternate | |
1571 | EOF | |
1572 | cat >new.txt <<-\EOF && | |
1573 | too short | |
1574 | unchanged | |
1575 | unchanged | |
1576 | this line should be marked as moved | |
1577 | too short | |
1578 | unchanged | |
1579 | unchanged | |
1580 | this line should be marked as oldMoved newMoved/newMovedAlternate | |
1581 | unchanged 1 | |
1582 | unchanged 2 | |
1583 | this line should be marked as oldMovedAlternate newMoved | |
1584 | this line should be marked as oldMoved newMoved/newMovedAlternate | |
1585 | unchanged 3 | |
1586 | this line should be marked as oldMoved newMoved | |
1587 | unchanged 4 | |
1588 | EOF | |
1589 | test_expect_code 1 git diff --no-index --color --color-moved=zebra \ | |
1590 | old.txt new.txt >output && cat output && | |
1591 | grep -v index output | test_decode_color >actual && | |
1592 | cat >expect <<-\EOF && | |
1593 | <BOLD>diff --git a/old.txt b/new.txt<RESET> | |
1594 | <BOLD>--- a/old.txt<RESET> | |
1595 | <BOLD>+++ b/new.txt<RESET> | |
1596 | <CYAN>@@ -1,13 +1,15 @@<RESET> | |
1597 | <BOLD;MAGENTA>-this line should be marked as moved<RESET> | |
1598 | <GREEN>+<RESET><GREEN>too short<RESET> | |
1599 | unchanged<RESET> | |
1600 | unchanged<RESET> | |
1601 | <BOLD;CYAN>+<RESET><BOLD;CYAN>this line should be marked as moved<RESET> | |
1602 | <GREEN>+<RESET><GREEN>too short<RESET> | |
1603 | unchanged<RESET> | |
1604 | unchanged<RESET> | |
1605 | <RED>-too short<RESET> | |
1606 | <BOLD;MAGENTA>-this line should be marked as oldMoved newMoved<RESET> | |
1607 | <BOLD;BLUE>-this line should be marked as oldMovedAlternate newMoved<RESET> | |
1608 | <BOLD;CYAN>+<RESET><BOLD;CYAN>this line should be marked as oldMoved newMoved/newMovedAlternate<RESET> | |
1609 | unchanged 1<RESET> | |
1610 | unchanged 2<RESET> | |
1611 | <BOLD;CYAN>+<RESET><BOLD;CYAN>this line should be marked as oldMovedAlternate newMoved<RESET> | |
1612 | <BOLD;YELLOW>+<RESET><BOLD;YELLOW>this line should be marked as oldMoved newMoved/newMovedAlternate<RESET> | |
1613 | unchanged 3<RESET> | |
1614 | <BOLD;CYAN>+<RESET><BOLD;CYAN>this line should be marked as oldMoved newMoved<RESET> | |
1615 | unchanged 4<RESET> | |
1616 | <BOLD;MAGENTA>-this line should be marked as oldMoved newMoved/newMovedAlternate<RESET> | |
1617 | EOF | |
1618 | test_cmp expect actual | |
1619 | ' | |
1620 | ||
86b452e2 SB |
1621 | test_expect_success 'cmd option assumes configured colored-moved' ' |
1622 | test_config color.diff.oldMoved "magenta" && | |
1623 | test_config color.diff.newMoved "cyan" && | |
1624 | test_config color.diff.oldMovedAlternative "blue" && | |
1625 | test_config color.diff.newMovedAlternative "yellow" && | |
1626 | test_config color.diff.oldMovedDimmed "normal magenta" && | |
1627 | test_config color.diff.newMovedDimmed "normal cyan" && | |
1628 | test_config color.diff.oldMovedAlternativeDimmed "normal blue" && | |
1629 | test_config color.diff.newMovedAlternativeDimmed "normal yellow" && | |
1630 | test_config diff.colorMoved zebra && | |
74cfa7be SB |
1631 | git diff HEAD --no-renames --color-moved --color >actual.raw && |
1632 | grep -v "index" actual.raw | test_decode_color >actual && | |
86b452e2 SB |
1633 | cat <<-\EOF >expected && |
1634 | <BOLD>diff --git a/lines.txt b/lines.txt<RESET> | |
86b452e2 SB |
1635 | <BOLD>--- a/lines.txt<RESET> |
1636 | <BOLD>+++ b/lines.txt<RESET> | |
1637 | <CYAN>@@ -1,16 +1,16 @@<RESET> | |
f0b8fb6e JT |
1638 | <MAGENTA>-long line 1<RESET> |
1639 | <MAGENTA>-long line 2<RESET> | |
1640 | <MAGENTA>-long line 3<RESET> | |
86b452e2 SB |
1641 | line 4<RESET> |
1642 | line 5<RESET> | |
1643 | line 6<RESET> | |
1644 | line 7<RESET> | |
1645 | line 8<RESET> | |
1646 | line 9<RESET> | |
f0b8fb6e JT |
1647 | <CYAN>+<RESET><CYAN>long line 1<RESET> |
1648 | <CYAN>+<RESET><CYAN>long line 2<RESET> | |
1649 | <CYAN>+<RESET><CYAN>long line 3<RESET> | |
1650 | <YELLOW>+<RESET><YELLOW>long line 14<RESET> | |
1651 | <YELLOW>+<RESET><YELLOW>long line 15<RESET> | |
1652 | <YELLOW>+<RESET><YELLOW>long line 16<RESET> | |
86b452e2 SB |
1653 | line 10<RESET> |
1654 | line 11<RESET> | |
1655 | line 12<RESET> | |
1656 | line 13<RESET> | |
f0b8fb6e JT |
1657 | <MAGENTA>-long line 14<RESET> |
1658 | <MAGENTA>-long line 15<RESET> | |
1659 | <MAGENTA>-long line 16<RESET> | |
86b452e2 SB |
1660 | EOF |
1661 | test_cmp expected actual | |
1662 | ' | |
1663 | ||
b59ec03c | 1664 | test_expect_success 'no effect on diff from --color-moved with --word-diff' ' |
2e2d5ac1 SB |
1665 | cat <<-\EOF >text.txt && |
1666 | Lorem Ipsum is simply dummy text of the printing and typesetting industry. | |
1667 | EOF | |
1668 | git add text.txt && | |
1669 | git commit -a -m "clean state" && | |
1670 | cat <<-\EOF >text.txt && | |
1671 | simply Lorem Ipsum dummy is text of the typesetting and printing industry. | |
1672 | EOF | |
1673 | git diff --color-moved --word-diff >actual && | |
1674 | git diff --word-diff >expect && | |
1675 | test_cmp expect actual | |
1676 | ' | |
1677 | ||
e5e37517 | 1678 | test_expect_success 'no effect on show from --color-moved with --word-diff' ' |
b59ec03c MG |
1679 | git show --color-moved --word-diff >actual && |
1680 | git show --word-diff >expect && | |
1681 | test_cmp expect actual | |
1682 | ' | |
1683 | ||
ecd51258 | 1684 | test_expect_success 'set up whitespace tests' ' |
2e2d5ac1 | 1685 | git reset --hard && |
ecd51258 JK |
1686 | # Note that these lines have no leading or trailing whitespace. |
1687 | cat <<-\EOF >lines.txt && | |
1688 | line 1 | |
1689 | line 2 | |
1690 | line 3 | |
1691 | line 4 | |
83de23cf | 1692 | line 5 |
f0b8fb6e JT |
1693 | long line 6 |
1694 | long line 7 | |
83de23cf JK |
1695 | long line 8 |
1696 | long line 9 | |
ecd51258 JK |
1697 | EOF |
1698 | git add lines.txt && | |
1699 | git commit -m "add poetry" && | |
1700 | git config color.diff.oldMoved "magenta" && | |
1701 | git config color.diff.newMoved "cyan" | |
1702 | ' | |
1703 | ||
1704 | test_expect_success 'move detection ignoring whitespace ' ' | |
1705 | q_to_tab <<-\EOF >lines.txt && | |
ecd51258 JK |
1706 | Qlong line 6 |
1707 | Qlong line 7 | |
83de23cf JK |
1708 | Qlong line 8 |
1709 | Qchanged long line 9 | |
ecd51258 JK |
1710 | line 1 |
1711 | line 2 | |
1712 | line 3 | |
1713 | line 4 | |
83de23cf | 1714 | line 5 |
ecd51258 | 1715 | EOF |
74cfa7be SB |
1716 | git diff HEAD --no-renames --color-moved --color >actual.raw && |
1717 | grep -v "index" actual.raw | test_decode_color >actual && | |
2e2d5ac1 SB |
1718 | cat <<-\EOF >expected && |
1719 | <BOLD>diff --git a/lines.txt b/lines.txt<RESET> | |
2e2d5ac1 SB |
1720 | <BOLD>--- a/lines.txt<RESET> |
1721 | <BOLD>+++ b/lines.txt<RESET> | |
83de23cf | 1722 | <CYAN>@@ -1,9 +1,9 @@<RESET> |
f0b8fb6e JT |
1723 | <GREEN>+<RESET> <GREEN>long line 6<RESET> |
1724 | <GREEN>+<RESET> <GREEN>long line 7<RESET> | |
83de23cf JK |
1725 | <GREEN>+<RESET> <GREEN>long line 8<RESET> |
1726 | <GREEN>+<RESET> <GREEN>changed long line 9<RESET> | |
2e2d5ac1 SB |
1727 | line 1<RESET> |
1728 | line 2<RESET> | |
1729 | line 3<RESET> | |
1730 | line 4<RESET> | |
83de23cf | 1731 | line 5<RESET> |
f0b8fb6e JT |
1732 | <RED>-long line 6<RESET> |
1733 | <RED>-long line 7<RESET> | |
83de23cf JK |
1734 | <RED>-long line 8<RESET> |
1735 | <RED>-long line 9<RESET> | |
2e2d5ac1 SB |
1736 | EOF |
1737 | test_cmp expected actual && | |
1738 | ||
b3095712 SB |
1739 | git diff HEAD --no-renames --color-moved --color \ |
1740 | --color-moved-ws=ignore-all-space >actual.raw && | |
74cfa7be | 1741 | grep -v "index" actual.raw | test_decode_color >actual && |
2e2d5ac1 SB |
1742 | cat <<-\EOF >expected && |
1743 | <BOLD>diff --git a/lines.txt b/lines.txt<RESET> | |
2e2d5ac1 SB |
1744 | <BOLD>--- a/lines.txt<RESET> |
1745 | <BOLD>+++ b/lines.txt<RESET> | |
83de23cf | 1746 | <CYAN>@@ -1,9 +1,9 @@<RESET> |
f0b8fb6e JT |
1747 | <CYAN>+<RESET> <CYAN>long line 6<RESET> |
1748 | <CYAN>+<RESET> <CYAN>long line 7<RESET> | |
83de23cf JK |
1749 | <CYAN>+<RESET> <CYAN>long line 8<RESET> |
1750 | <GREEN>+<RESET> <GREEN>changed long line 9<RESET> | |
2e2d5ac1 SB |
1751 | line 1<RESET> |
1752 | line 2<RESET> | |
1753 | line 3<RESET> | |
1754 | line 4<RESET> | |
83de23cf | 1755 | line 5<RESET> |
f0b8fb6e JT |
1756 | <MAGENTA>-long line 6<RESET> |
1757 | <MAGENTA>-long line 7<RESET> | |
83de23cf JK |
1758 | <MAGENTA>-long line 8<RESET> |
1759 | <RED>-long line 9<RESET> | |
2e2d5ac1 SB |
1760 | EOF |
1761 | test_cmp expected actual | |
1762 | ' | |
1763 | ||
d5aae1f7 JK |
1764 | test_expect_success 'move detection ignoring whitespace changes' ' |
1765 | git reset --hard && | |
1766 | # Lines 6-8 have a space change, but 9 is new whitespace | |
1767 | q_to_tab <<-\EOF >lines.txt && | |
1768 | longQline 6 | |
1769 | longQline 7 | |
1770 | longQline 8 | |
1771 | long liQne 9 | |
1772 | line 1 | |
1773 | line 2 | |
1774 | line 3 | |
1775 | line 4 | |
1776 | line 5 | |
1777 | EOF | |
1778 | ||
74cfa7be SB |
1779 | git diff HEAD --no-renames --color-moved --color >actual.raw && |
1780 | grep -v "index" actual.raw | test_decode_color >actual && | |
d5aae1f7 JK |
1781 | cat <<-\EOF >expected && |
1782 | <BOLD>diff --git a/lines.txt b/lines.txt<RESET> | |
1783 | <BOLD>--- a/lines.txt<RESET> | |
1784 | <BOLD>+++ b/lines.txt<RESET> | |
1785 | <CYAN>@@ -1,9 +1,9 @@<RESET> | |
1786 | <GREEN>+<RESET><GREEN>long line 6<RESET> | |
1787 | <GREEN>+<RESET><GREEN>long line 7<RESET> | |
1788 | <GREEN>+<RESET><GREEN>long line 8<RESET> | |
1789 | <GREEN>+<RESET><GREEN>long li ne 9<RESET> | |
1790 | line 1<RESET> | |
1791 | line 2<RESET> | |
1792 | line 3<RESET> | |
1793 | line 4<RESET> | |
1794 | line 5<RESET> | |
1795 | <RED>-long line 6<RESET> | |
1796 | <RED>-long line 7<RESET> | |
1797 | <RED>-long line 8<RESET> | |
1798 | <RED>-long line 9<RESET> | |
1799 | EOF | |
1800 | test_cmp expected actual && | |
1801 | ||
b3095712 SB |
1802 | git diff HEAD --no-renames --color-moved --color \ |
1803 | --color-moved-ws=ignore-space-change >actual.raw && | |
74cfa7be | 1804 | grep -v "index" actual.raw | test_decode_color >actual && |
d5aae1f7 JK |
1805 | cat <<-\EOF >expected && |
1806 | <BOLD>diff --git a/lines.txt b/lines.txt<RESET> | |
1807 | <BOLD>--- a/lines.txt<RESET> | |
1808 | <BOLD>+++ b/lines.txt<RESET> | |
1809 | <CYAN>@@ -1,9 +1,9 @@<RESET> | |
1810 | <CYAN>+<RESET><CYAN>long line 6<RESET> | |
1811 | <CYAN>+<RESET><CYAN>long line 7<RESET> | |
1812 | <CYAN>+<RESET><CYAN>long line 8<RESET> | |
1813 | <GREEN>+<RESET><GREEN>long li ne 9<RESET> | |
1814 | line 1<RESET> | |
1815 | line 2<RESET> | |
1816 | line 3<RESET> | |
1817 | line 4<RESET> | |
1818 | line 5<RESET> | |
1819 | <MAGENTA>-long line 6<RESET> | |
1820 | <MAGENTA>-long line 7<RESET> | |
1821 | <MAGENTA>-long line 8<RESET> | |
1822 | <RED>-long line 9<RESET> | |
1823 | EOF | |
1824 | test_cmp expected actual | |
1825 | ' | |
1826 | ||
da58318e JK |
1827 | test_expect_success 'move detection ignoring whitespace at eol' ' |
1828 | git reset --hard && | |
1829 | # Lines 6-9 have new eol whitespace, but 9 also has it in the middle | |
1830 | q_to_tab <<-\EOF >lines.txt && | |
1831 | long line 6Q | |
1832 | long line 7Q | |
1833 | long line 8Q | |
1834 | longQline 9Q | |
1835 | line 1 | |
1836 | line 2 | |
1837 | line 3 | |
1838 | line 4 | |
1839 | line 5 | |
1840 | EOF | |
1841 | ||
1842 | # avoid cluttering the output with complaints about our eol whitespace | |
1843 | test_config core.whitespace -blank-at-eol && | |
1844 | ||
74cfa7be SB |
1845 | git diff HEAD --no-renames --color-moved --color >actual.raw && |
1846 | grep -v "index" actual.raw | test_decode_color >actual && | |
da58318e JK |
1847 | cat <<-\EOF >expected && |
1848 | <BOLD>diff --git a/lines.txt b/lines.txt<RESET> | |
1849 | <BOLD>--- a/lines.txt<RESET> | |
1850 | <BOLD>+++ b/lines.txt<RESET> | |
1851 | <CYAN>@@ -1,9 +1,9 @@<RESET> | |
1852 | <GREEN>+<RESET><GREEN>long line 6 <RESET> | |
1853 | <GREEN>+<RESET><GREEN>long line 7 <RESET> | |
1854 | <GREEN>+<RESET><GREEN>long line 8 <RESET> | |
1855 | <GREEN>+<RESET><GREEN>long line 9 <RESET> | |
1856 | line 1<RESET> | |
1857 | line 2<RESET> | |
1858 | line 3<RESET> | |
1859 | line 4<RESET> | |
1860 | line 5<RESET> | |
1861 | <RED>-long line 6<RESET> | |
1862 | <RED>-long line 7<RESET> | |
1863 | <RED>-long line 8<RESET> | |
1864 | <RED>-long line 9<RESET> | |
1865 | EOF | |
1866 | test_cmp expected actual && | |
1867 | ||
b3095712 SB |
1868 | git diff HEAD --no-renames --color-moved --color \ |
1869 | --color-moved-ws=ignore-space-at-eol >actual.raw && | |
74cfa7be | 1870 | grep -v "index" actual.raw | test_decode_color >actual && |
da58318e JK |
1871 | cat <<-\EOF >expected && |
1872 | <BOLD>diff --git a/lines.txt b/lines.txt<RESET> | |
1873 | <BOLD>--- a/lines.txt<RESET> | |
1874 | <BOLD>+++ b/lines.txt<RESET> | |
1875 | <CYAN>@@ -1,9 +1,9 @@<RESET> | |
1876 | <CYAN>+<RESET><CYAN>long line 6 <RESET> | |
1877 | <CYAN>+<RESET><CYAN>long line 7 <RESET> | |
1878 | <CYAN>+<RESET><CYAN>long line 8 <RESET> | |
1879 | <GREEN>+<RESET><GREEN>long line 9 <RESET> | |
1880 | line 1<RESET> | |
1881 | line 2<RESET> | |
1882 | line 3<RESET> | |
1883 | line 4<RESET> | |
1884 | line 5<RESET> | |
1885 | <MAGENTA>-long line 6<RESET> | |
1886 | <MAGENTA>-long line 7<RESET> | |
1887 | <MAGENTA>-long line 8<RESET> | |
1888 | <RED>-long line 9<RESET> | |
1889 | EOF | |
1890 | test_cmp expected actual | |
1891 | ' | |
1892 | ||
ecd51258 JK |
1893 | test_expect_success 'clean up whitespace-test colors' ' |
1894 | git config --unset color.diff.oldMoved && | |
1895 | git config --unset color.diff.newMoved | |
1896 | ' | |
1897 | ||
f0b8fb6e | 1898 | test_expect_success '--color-moved block at end of diff output respects MIN_ALNUM_COUNT' ' |
09153277 JT |
1899 | git reset --hard && |
1900 | >bar && | |
1901 | cat <<-\EOF >foo && | |
1902 | irrelevant_line | |
1903 | line1 | |
1904 | EOF | |
1905 | git add foo bar && | |
1906 | git commit -m x && | |
1907 | ||
1908 | cat <<-\EOF >bar && | |
1909 | line1 | |
1910 | EOF | |
1911 | cat <<-\EOF >foo && | |
1912 | irrelevant_line | |
1913 | EOF | |
1914 | ||
74cfa7be SB |
1915 | git diff HEAD --color-moved=zebra --color --no-renames >actual.raw && |
1916 | grep -v "index" actual.raw | test_decode_color >actual && | |
09153277 JT |
1917 | cat >expected <<-\EOF && |
1918 | <BOLD>diff --git a/bar b/bar<RESET> | |
1919 | <BOLD>--- a/bar<RESET> | |
1920 | <BOLD>+++ b/bar<RESET> | |
1921 | <CYAN>@@ -0,0 +1 @@<RESET> | |
1922 | <GREEN>+<RESET><GREEN>line1<RESET> | |
1923 | <BOLD>diff --git a/foo b/foo<RESET> | |
1924 | <BOLD>--- a/foo<RESET> | |
1925 | <BOLD>+++ b/foo<RESET> | |
1926 | <CYAN>@@ -1,2 +1 @@<RESET> | |
1927 | irrelevant_line<RESET> | |
1928 | <RED>-line1<RESET> | |
1929 | EOF | |
1930 | ||
1931 | test_cmp expected actual | |
1932 | ' | |
1933 | ||
f0b8fb6e JT |
1934 | test_expect_success '--color-moved respects MIN_ALNUM_COUNT' ' |
1935 | git reset --hard && | |
1936 | cat <<-\EOF >foo && | |
1937 | nineteen chars 456789 | |
1938 | irrelevant_line | |
1939 | twenty chars 234567890 | |
1940 | EOF | |
1941 | >bar && | |
1942 | git add foo bar && | |
1943 | git commit -m x && | |
1944 | ||
1945 | cat <<-\EOF >foo && | |
1946 | irrelevant_line | |
1947 | EOF | |
1948 | cat <<-\EOF >bar && | |
1949 | twenty chars 234567890 | |
1950 | nineteen chars 456789 | |
1951 | EOF | |
1952 | ||
74cfa7be SB |
1953 | git diff HEAD --color-moved=zebra --color --no-renames >actual.raw && |
1954 | grep -v "index" actual.raw | test_decode_color >actual && | |
f0b8fb6e JT |
1955 | cat >expected <<-\EOF && |
1956 | <BOLD>diff --git a/bar b/bar<RESET> | |
1957 | <BOLD>--- a/bar<RESET> | |
1958 | <BOLD>+++ b/bar<RESET> | |
1959 | <CYAN>@@ -0,0 +1,2 @@<RESET> | |
1960 | <BOLD;CYAN>+<RESET><BOLD;CYAN>twenty chars 234567890<RESET> | |
1961 | <GREEN>+<RESET><GREEN>nineteen chars 456789<RESET> | |
1962 | <BOLD>diff --git a/foo b/foo<RESET> | |
1963 | <BOLD>--- a/foo<RESET> | |
1964 | <BOLD>+++ b/foo<RESET> | |
1965 | <CYAN>@@ -1,3 +1 @@<RESET> | |
1966 | <RED>-nineteen chars 456789<RESET> | |
1967 | irrelevant_line<RESET> | |
1968 | <BOLD;MAGENTA>-twenty chars 234567890<RESET> | |
1969 | EOF | |
1970 | ||
1971 | test_cmp expected actual | |
1972 | ' | |
1973 | ||
1974 | test_expect_success '--color-moved treats adjacent blocks as separate for MIN_ALNUM_COUNT' ' | |
1975 | git reset --hard && | |
1976 | cat <<-\EOF >foo && | |
1977 | 7charsA | |
1978 | irrelevant_line | |
1979 | 7charsB | |
1980 | 7charsC | |
1981 | EOF | |
1982 | >bar && | |
1983 | git add foo bar && | |
1984 | git commit -m x && | |
1985 | ||
1986 | cat <<-\EOF >foo && | |
1987 | irrelevant_line | |
1988 | EOF | |
1989 | cat <<-\EOF >bar && | |
1990 | 7charsB | |
1991 | 7charsC | |
1992 | 7charsA | |
1993 | EOF | |
1994 | ||
51da15eb SB |
1995 | git diff HEAD --color-moved=zebra --color --no-renames >actual.raw && |
1996 | grep -v "index" actual.raw | test_decode_color >actual && | |
f0b8fb6e JT |
1997 | cat >expected <<-\EOF && |
1998 | <BOLD>diff --git a/bar b/bar<RESET> | |
1999 | <BOLD>--- a/bar<RESET> | |
2000 | <BOLD>+++ b/bar<RESET> | |
2001 | <CYAN>@@ -0,0 +1,3 @@<RESET> | |
2002 | <GREEN>+<RESET><GREEN>7charsB<RESET> | |
2003 | <GREEN>+<RESET><GREEN>7charsC<RESET> | |
2004 | <GREEN>+<RESET><GREEN>7charsA<RESET> | |
2005 | <BOLD>diff --git a/foo b/foo<RESET> | |
2006 | <BOLD>--- a/foo<RESET> | |
2007 | <BOLD>+++ b/foo<RESET> | |
2008 | <CYAN>@@ -1,4 +1 @@<RESET> | |
2009 | <RED>-7charsA<RESET> | |
2010 | irrelevant_line<RESET> | |
2011 | <RED>-7charsB<RESET> | |
2012 | <RED>-7charsC<RESET> | |
2013 | EOF | |
2014 | ||
2015 | test_cmp expected actual | |
2016 | ' | |
2017 | ||
0990658b PW |
2018 | test_expect_success '--color-moved rewinds for MIN_ALNUM_COUNT' ' |
2019 | git reset --hard && | |
2020 | test_write_lines >file \ | |
2021 | A B C one two three four five six seven D E F G H I J && | |
2022 | git add file && | |
2023 | test_write_lines >file \ | |
2024 | one two A B C D E F G H I J two three four five six seven && | |
2025 | git diff --color-moved=zebra -- file && | |
2026 | ||
2027 | git diff --color-moved=zebra --color -- file >actual.raw && | |
2028 | grep -v "index" actual.raw | test_decode_color >actual && | |
2029 | cat >expected <<-\EOF && | |
2030 | <BOLD>diff --git a/file b/file<RESET> | |
2031 | <BOLD>--- a/file<RESET> | |
2032 | <BOLD>+++ b/file<RESET> | |
2033 | <CYAN>@@ -1,13 +1,8 @@<RESET> | |
2034 | <GREEN>+<RESET><GREEN>one<RESET> | |
2035 | <GREEN>+<RESET><GREEN>two<RESET> | |
2036 | A<RESET> | |
2037 | B<RESET> | |
2038 | C<RESET> | |
2039 | <RED>-one<RESET> | |
2040 | <BOLD;MAGENTA>-two<RESET> | |
2041 | <BOLD;MAGENTA>-three<RESET> | |
2042 | <BOLD;MAGENTA>-four<RESET> | |
2043 | <BOLD;MAGENTA>-five<RESET> | |
2044 | <BOLD;MAGENTA>-six<RESET> | |
2045 | <BOLD;MAGENTA>-seven<RESET> | |
2046 | D<RESET> | |
2047 | E<RESET> | |
2048 | F<RESET> | |
2049 | <CYAN>@@ -15,3 +10,9 @@<RESET> <RESET>G<RESET> | |
2050 | H<RESET> | |
2051 | I<RESET> | |
2052 | J<RESET> | |
2053 | <BOLD;CYAN>+<RESET><BOLD;CYAN>two<RESET> | |
2054 | <BOLD;CYAN>+<RESET><BOLD;CYAN>three<RESET> | |
2055 | <BOLD;CYAN>+<RESET><BOLD;CYAN>four<RESET> | |
2056 | <BOLD;CYAN>+<RESET><BOLD;CYAN>five<RESET> | |
2057 | <BOLD;CYAN>+<RESET><BOLD;CYAN>six<RESET> | |
2058 | <BOLD;CYAN>+<RESET><BOLD;CYAN>seven<RESET> | |
2059 | EOF | |
2060 | ||
2061 | test_cmp expected actual | |
2062 | ' | |
2063 | ||
e5e37517 | 2064 | test_expect_success 'move detection with submodules' ' |
2e2d5ac1 SB |
2065 | test_create_repo bananas && |
2066 | echo ripe >bananas/recipe && | |
2067 | git -C bananas add recipe && | |
2068 | test_commit fruit && | |
2069 | test_commit -C bananas recipe && | |
2070 | git submodule add ./bananas && | |
2071 | git add bananas && | |
2072 | git commit -a -m "bananas are like a heavy library?" && | |
2073 | echo foul >bananas/recipe && | |
2074 | echo ripe >fruit.t && | |
2075 | ||
269c73e8 | 2076 | git diff --submodule=diff --color-moved --color >actual && |
2e2d5ac1 SB |
2077 | |
2078 | # no move detection as the moved line is across repository boundaries. | |
2079 | test_decode_color <actual >decoded_actual && | |
2080 | ! grep BGREEN decoded_actual && | |
2081 | ! grep BRED decoded_actual && | |
2082 | ||
2083 | # nor did we mess with it another way | |
946d2353 DL |
2084 | git diff --submodule=diff --color >expect.raw && |
2085 | test_decode_color <expect.raw >expect && | |
b3095712 SB |
2086 | test_cmp expect decoded_actual && |
2087 | rm -rf bananas && | |
2088 | git submodule deinit bananas | |
2089 | ' | |
2090 | ||
2091 | test_expect_success 'only move detection ignores white spaces' ' | |
2092 | git reset --hard && | |
2093 | q_to_tab <<-\EOF >text.txt && | |
2094 | a long line to exceed per-line minimum | |
2095 | another long line to exceed per-line minimum | |
2096 | original file | |
2097 | EOF | |
2098 | git add text.txt && | |
2099 | git commit -m "add text" && | |
2100 | q_to_tab <<-\EOF >text.txt && | |
2101 | Qa long line to exceed per-line minimum | |
2102 | Qanother long line to exceed per-line minimum | |
2103 | new file | |
2104 | EOF | |
2105 | ||
2106 | # Make sure we get a different diff using -w | |
2107 | git diff --color --color-moved -w >actual.raw && | |
2108 | grep -v "index" actual.raw | test_decode_color >actual && | |
2109 | q_to_tab <<-\EOF >expected && | |
2110 | <BOLD>diff --git a/text.txt b/text.txt<RESET> | |
2111 | <BOLD>--- a/text.txt<RESET> | |
2112 | <BOLD>+++ b/text.txt<RESET> | |
2113 | <CYAN>@@ -1,3 +1,3 @@<RESET> | |
2114 | Qa long line to exceed per-line minimum<RESET> | |
2115 | Qanother long line to exceed per-line minimum<RESET> | |
2116 | <RED>-original file<RESET> | |
2117 | <GREEN>+<RESET><GREEN>new file<RESET> | |
2118 | EOF | |
2119 | test_cmp expected actual && | |
2120 | ||
2121 | # And now ignoring white space only in the move detection | |
2122 | git diff --color --color-moved \ | |
2123 | --color-moved-ws=ignore-all-space,ignore-space-change,ignore-space-at-eol >actual.raw && | |
2124 | grep -v "index" actual.raw | test_decode_color >actual && | |
2125 | q_to_tab <<-\EOF >expected && | |
2126 | <BOLD>diff --git a/text.txt b/text.txt<RESET> | |
2127 | <BOLD>--- a/text.txt<RESET> | |
2128 | <BOLD>+++ b/text.txt<RESET> | |
2129 | <CYAN>@@ -1,3 +1,3 @@<RESET> | |
2130 | <BOLD;MAGENTA>-a long line to exceed per-line minimum<RESET> | |
2131 | <BOLD;MAGENTA>-another long line to exceed per-line minimum<RESET> | |
2132 | <RED>-original file<RESET> | |
b0a2ba47 PW |
2133 | <BOLD;CYAN>+<RESET>Q<BOLD;CYAN>a long line to exceed per-line minimum<RESET> |
2134 | <BOLD;CYAN>+<RESET>Q<BOLD;CYAN>another long line to exceed per-line minimum<RESET> | |
b3095712 SB |
2135 | <GREEN>+<RESET><GREEN>new file<RESET> |
2136 | EOF | |
2137 | test_cmp expected actual | |
2e2d5ac1 SB |
2138 | ' |
2139 | ||
ca1f4ae4 SB |
2140 | test_expect_success 'compare whitespace delta across moved blocks' ' |
2141 | ||
2142 | git reset --hard && | |
2143 | q_to_tab <<-\EOF >text.txt && | |
2144 | QIndented | |
2145 | QText across | |
2146 | Qsome lines | |
2147 | QBut! <- this stands out | |
2148 | QAdjusting with | |
2149 | QQdifferent starting | |
2150 | Qwhite spaces | |
2151 | QAnother outlier | |
2152 | QQQIndented | |
2153 | QQQText across | |
2154 | QQQfive lines | |
2155 | QQQthat has similar lines | |
2156 | QQQto previous blocks, but with different indent | |
2157 | QQQYetQAnotherQoutlierQ | |
10acc5f7 | 2158 | QLine with internal w h i t e s p a c e change |
ca1f4ae4 SB |
2159 | EOF |
2160 | ||
2161 | git add text.txt && | |
2162 | git commit -m "add text.txt" && | |
2163 | ||
2164 | q_to_tab <<-\EOF >text.txt && | |
2165 | QQIndented | |
2166 | QQText across | |
2167 | QQsome lines | |
2168 | QQQBut! <- this stands out | |
2169 | Adjusting with | |
2170 | Qdifferent starting | |
2171 | white spaces | |
2172 | AnotherQoutlier | |
2173 | QQIndented | |
2174 | QQText across | |
2175 | QQfive lines | |
2176 | QQthat has similar lines | |
2177 | QQto previous blocks, but with different indent | |
2178 | QQYetQAnotherQoutlier | |
10acc5f7 | 2179 | QLine with internal whitespace change |
ca1f4ae4 SB |
2180 | EOF |
2181 | ||
2182 | git diff --color --color-moved --color-moved-ws=allow-indentation-change >actual.raw && | |
2183 | grep -v "index" actual.raw | test_decode_color >actual && | |
2184 | ||
2185 | q_to_tab <<-\EOF >expected && | |
2186 | <BOLD>diff --git a/text.txt b/text.txt<RESET> | |
2187 | <BOLD>--- a/text.txt<RESET> | |
2188 | <BOLD>+++ b/text.txt<RESET> | |
10acc5f7 | 2189 | <CYAN>@@ -1,15 +1,15 @@<RESET> |
ca1f4ae4 SB |
2190 | <BOLD;MAGENTA>-QIndented<RESET> |
2191 | <BOLD;MAGENTA>-QText across<RESET> | |
2192 | <BOLD;MAGENTA>-Qsome lines<RESET> | |
2193 | <RED>-QBut! <- this stands out<RESET> | |
2194 | <BOLD;MAGENTA>-QAdjusting with<RESET> | |
2195 | <BOLD;MAGENTA>-QQdifferent starting<RESET> | |
2196 | <BOLD;MAGENTA>-Qwhite spaces<RESET> | |
2197 | <RED>-QAnother outlier<RESET> | |
2198 | <BOLD;MAGENTA>-QQQIndented<RESET> | |
2199 | <BOLD;MAGENTA>-QQQText across<RESET> | |
2200 | <BOLD;MAGENTA>-QQQfive lines<RESET> | |
2201 | <BOLD;MAGENTA>-QQQthat has similar lines<RESET> | |
2202 | <BOLD;MAGENTA>-QQQto previous blocks, but with different indent<RESET> | |
2203 | <RED>-QQQYetQAnotherQoutlierQ<RESET> | |
10acc5f7 | 2204 | <RED>-QLine with internal w h i t e s p a c e change<RESET> |
ca1f4ae4 SB |
2205 | <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>Indented<RESET> |
2206 | <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>Text across<RESET> | |
2207 | <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>some lines<RESET> | |
2208 | <GREEN>+<RESET>QQQ<GREEN>But! <- this stands out<RESET> | |
2209 | <BOLD;CYAN>+<RESET><BOLD;CYAN>Adjusting with<RESET> | |
2210 | <BOLD;CYAN>+<RESET>Q<BOLD;CYAN>different starting<RESET> | |
2211 | <BOLD;CYAN>+<RESET><BOLD;CYAN>white spaces<RESET> | |
2212 | <GREEN>+<RESET><GREEN>AnotherQoutlier<RESET> | |
2213 | <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>Indented<RESET> | |
2214 | <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>Text across<RESET> | |
2215 | <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>five lines<RESET> | |
2216 | <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>that has similar lines<RESET> | |
2217 | <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>to previous blocks, but with different indent<RESET> | |
2218 | <GREEN>+<RESET>QQ<GREEN>YetQAnotherQoutlier<RESET> | |
10acc5f7 | 2219 | <GREEN>+<RESET>Q<GREEN>Line with internal whitespace change<RESET> |
ca1f4ae4 SB |
2220 | EOF |
2221 | ||
2222 | test_cmp expected actual | |
2223 | ' | |
2224 | ||
d173e799 SB |
2225 | test_expect_success 'bogus settings in move detection erroring out' ' |
2226 | test_must_fail git diff --color-moved=bogus 2>err && | |
6789275d JH |
2227 | test_grep "must be one of" err && |
2228 | test_grep bogus err && | |
d173e799 SB |
2229 | |
2230 | test_must_fail git -c diff.colormoved=bogus diff 2>err && | |
6789275d JH |
2231 | test_grep "must be one of" err && |
2232 | test_grep "from command-line config" err && | |
d173e799 SB |
2233 | |
2234 | test_must_fail git diff --color-moved-ws=bogus 2>err && | |
6789275d JH |
2235 | test_grep "possible values" err && |
2236 | test_grep bogus err && | |
d173e799 SB |
2237 | |
2238 | test_must_fail git -c diff.colormovedws=bogus diff 2>err && | |
6789275d JH |
2239 | test_grep "possible values" err && |
2240 | test_grep "from command-line config" err | |
d173e799 SB |
2241 | ' |
2242 | ||
ca1f4ae4 SB |
2243 | test_expect_success 'compare whitespace delta incompatible with other space options' ' |
2244 | test_must_fail git diff \ | |
2245 | --color-moved-ws=allow-indentation-change,ignore-all-space \ | |
2246 | 2>err && | |
6789275d | 2247 | test_grep allow-indentation-change err |
ca1f4ae4 SB |
2248 | ' |
2249 | ||
0cd51e9d | 2250 | EMPTY='' |
21536d07 PW |
2251 | test_expect_success 'compare mixed whitespace delta across moved blocks' ' |
2252 | ||
2253 | git reset --hard && | |
25e61909 PW |
2254 | tr "^|Q_" "\f\v\t " <<-EOF >text.txt && |
2255 | ^__ | |
2256 | |____too short without | |
2257 | ^ | |
0cd51e9d PW |
2258 | ___being grouped across blank line |
2259 | ${EMPTY} | |
2260 | context | |
2261 | lines | |
2262 | to | |
2263 | anchor | |
21536d07 PW |
2264 | ____Indented text to |
2265 | _Q____be further indented by four spaces across | |
2266 | ____Qseveral lines | |
2267 | QQ____These two lines have had their | |
2268 | ____indentation reduced by four spaces | |
2269 | Qdifferent indentation change | |
2270 | ____too short | |
2271 | EOF | |
2272 | ||
2273 | git add text.txt && | |
2274 | git commit -m "add text.txt" && | |
2275 | ||
25e61909 | 2276 | tr "^|Q_" "\f\v\t " <<-EOF >text.txt && |
0cd51e9d PW |
2277 | context |
2278 | lines | |
2279 | to | |
2280 | anchor | |
21536d07 PW |
2281 | QIndented text to |
2282 | QQbe further indented by four spaces across | |
2283 | Q____several lines | |
0cd51e9d PW |
2284 | ${EMPTY} |
2285 | QQtoo short without | |
2286 | ${EMPTY} | |
25e61909 | 2287 | ^Q_______being grouped across blank line |
0cd51e9d | 2288 | ${EMPTY} |
21536d07 PW |
2289 | Q_QThese two lines have had their |
2290 | indentation reduced by four spaces | |
2291 | QQdifferent indentation change | |
2292 | __Qtoo short | |
2293 | EOF | |
2294 | ||
2295 | git -c color.diff.whitespace="normal red" \ | |
2296 | -c core.whitespace=space-before-tab \ | |
2297 | diff --color --color-moved --ws-error-highlight=all \ | |
2298 | --color-moved-ws=allow-indentation-change >actual.raw && | |
25e61909 | 2299 | grep -v "index" actual.raw | tr "\f\v" "^|" | test_decode_color >actual && |
21536d07 PW |
2300 | |
2301 | cat <<-\EOF >expected && | |
2302 | <BOLD>diff --git a/text.txt b/text.txt<RESET> | |
2303 | <BOLD>--- a/text.txt<RESET> | |
2304 | <BOLD>+++ b/text.txt<RESET> | |
0cd51e9d | 2305 | <CYAN>@@ -1,16 +1,16 @@<RESET> |
25e61909 PW |
2306 | <BOLD;MAGENTA>-<RESET><BOLD;MAGENTA>^<RESET><BRED> <RESET> |
2307 | <BOLD;MAGENTA>-<RESET><BOLD;MAGENTA>| too short without<RESET> | |
2308 | <BOLD;MAGENTA>-<RESET><BOLD;MAGENTA>^<RESET> | |
0cd51e9d PW |
2309 | <BOLD;MAGENTA>-<RESET><BOLD;MAGENTA> being grouped across blank line<RESET> |
2310 | <BOLD;MAGENTA>-<RESET> | |
2311 | <RESET>context<RESET> | |
2312 | <RESET>lines<RESET> | |
2313 | <RESET>to<RESET> | |
2314 | <RESET>anchor<RESET> | |
21536d07 PW |
2315 | <BOLD;MAGENTA>-<RESET><BOLD;MAGENTA> Indented text to<RESET> |
2316 | <BOLD;MAGENTA>-<RESET><BRED> <RESET> <BOLD;MAGENTA> be further indented by four spaces across<RESET> | |
2317 | <BOLD;MAGENTA>-<RESET><BRED> <RESET> <BOLD;MAGENTA>several lines<RESET> | |
2318 | <BOLD;BLUE>-<RESET> <BOLD;BLUE> These two lines have had their<RESET> | |
2319 | <BOLD;BLUE>-<RESET><BOLD;BLUE> indentation reduced by four spaces<RESET> | |
2320 | <BOLD;MAGENTA>-<RESET> <BOLD;MAGENTA>different indentation change<RESET> | |
2321 | <RED>-<RESET><RED> too short<RESET> | |
2322 | <BOLD;CYAN>+<RESET> <BOLD;CYAN>Indented text to<RESET> | |
2323 | <BOLD;CYAN>+<RESET> <BOLD;CYAN>be further indented by four spaces across<RESET> | |
2324 | <BOLD;CYAN>+<RESET> <BOLD;CYAN> several lines<RESET> | |
0cd51e9d PW |
2325 | <BOLD;YELLOW>+<RESET> |
2326 | <BOLD;YELLOW>+<RESET> <BOLD;YELLOW>too short without<RESET> | |
2327 | <BOLD;YELLOW>+<RESET> | |
25e61909 | 2328 | <BOLD;YELLOW>+<RESET><BOLD;YELLOW>^ being grouped across blank line<RESET> |
0cd51e9d PW |
2329 | <BOLD;YELLOW>+<RESET> |
2330 | <BOLD;CYAN>+<RESET> <BRED> <RESET> <BOLD;CYAN>These two lines have had their<RESET> | |
2331 | <BOLD;CYAN>+<RESET><BOLD;CYAN>indentation reduced by four spaces<RESET> | |
2332 | <BOLD;YELLOW>+<RESET> <BOLD;YELLOW>different indentation change<RESET> | |
21536d07 PW |
2333 | <GREEN>+<RESET><BRED> <RESET> <GREEN>too short<RESET> |
2334 | EOF | |
2335 | ||
2336 | test_cmp expected actual | |
2337 | ' | |
2338 | ||
b777f3fd JK |
2339 | test_expect_success 'combine --ignore-blank-lines with --function-context' ' |
2340 | test_write_lines 1 "" 2 3 4 5 >a && | |
2341 | test_write_lines 1 2 3 4 >b && | |
2342 | test_must_fail git diff --no-index \ | |
2343 | --ignore-blank-lines --function-context a b >actual.raw && | |
2344 | sed -n "/@@/,\$p" <actual.raw >actual && | |
2345 | cat <<-\EOF >expect && | |
2346 | @@ -1,6 +1,4 @@ | |
2347 | 1 | |
0bb313a5 | 2348 | - |
b777f3fd JK |
2349 | 2 |
2350 | 3 | |
2351 | 4 | |
2352 | -5 | |
2353 | EOF | |
2354 | test_cmp expect actual | |
2355 | ' | |
2356 | ||
124a8958 RS |
2357 | test_expect_success 'combine --ignore-blank-lines with --function-context 2' ' |
2358 | test_write_lines a b c "" function 1 2 3 4 5 "" 6 7 8 9 >a && | |
2359 | test_write_lines "" a b c "" function 1 2 3 4 5 6 7 8 >b && | |
2360 | test_must_fail git diff --no-index \ | |
2361 | --ignore-blank-lines --function-context a b >actual.raw && | |
2362 | sed -n "/@@/,\$p" <actual.raw >actual && | |
2363 | cat <<-\EOF >expect && | |
2364 | @@ -5,11 +6,9 @@ c | |
2365 | function | |
2366 | 1 | |
2367 | 2 | |
2368 | 3 | |
2369 | 4 | |
2370 | 5 | |
2371 | - | |
2372 | 6 | |
2373 | 7 | |
2374 | 8 | |
2375 | -9 | |
2376 | EOF | |
2377 | test_cmp expect actual | |
2378 | ' | |
2379 | ||
2344d47f | 2380 | test_done |