Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/t4124-apply-ws-rule.sh # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / t4124-apply-ws-rule.sh` 848 lines ยท 21506 bytes Raw Blame History 1 #!/bin/sh 2 3 test_description='core.whitespace rules and git apply' 4 5 . ./test-lib.sh 6 7 prepare_test_file () { 8 9 # A line that has character X is touched iff RULE is in effect: 10 # X RULE 11 # ! trailing-space 12 # @ space-before-tab 13 # # indent-with-non-tab (default tab width 8) 14 # = indent-with-non-tab,tabwidth=16 15 # % tab-in-indent 16 sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF 17 An_SP in an ordinary line>and a HT. 18 >A HT (%). 19 _>A SP and a HT (@%). 20 _>_A SP, a HT and a SP (@%). 21 _______Seven SP. 22 ________Eight SP (#). 23 _______>Seven SP and a HT (@%). 24 ________>Eight SP and a HT (@#%). 25 _______>_Seven SP, a HT and a SP (@%). 26 ________>_Eight SP, a HT and a SP (@#%). 27 _______________Fifteen SP (#). 28 _______________>Fifteen SP and a HT (@#%). 29 ________________Sixteen SP (#=). 30 ________________>Sixteen SP and a HT (@#%=). 31 _____a__Five SP, a non WS, two SP. 32 A line with a (!) trailing SP_ 33 A line with a (!) trailing HT> 34 EOF 35 } 36 37 apply_patch () { 38 cmd_prefix= && 39 if test "x$1" = 'x!' 40 then 41 cmd_prefix=test_must_fail && 42 shift 43 fi && 44 >target && 45 sed -e "s|\([ab]\)/file|\1/target|" //p" >fixed 55 # busybox's diff(1) doesn't output normal format 56 if ! test -s fixed 57 then 58 $DIFF -u file target | 59 grep -v '^+++ target' | 60 sed -ne "/^+/s/+//p" >fixed 61 fi 62 63 # the changed lines are all expected to change 64 fixed_cnt=$(wc -l fixed-patch 80 test -s fixed-patch && return 0 81 82 # Make sure it is complaint-free 83 >target 84 git apply --whitespace=error-all file && 91 git add file && 92 prepare_test_file >file && 93 git diff-files -p >patch && 94 >target && 95 git add target 96 97 ' 98 99 test_expect_success 'whitespace=nowarn, default rule' ' 100 101 apply_patch --whitespace=nowarn && 102 test_cmp file target 103 104 ' 105 106 test_expect_success 'whitespace=warn, default rule' ' 107 108 apply_patch --whitespace=warn && 109 test_cmp file target 110 111 ' 112 113 test_expect_success 'whitespace=error-all, default rule' ' 114 115 apply_patch ! --whitespace=error-all && 116 test_must_be_empty target 117 118 ' 119 120 test_expect_success 'whitespace=error-all, no rule' ' 121 122 git config core.whitespace -trailing,-space-before,-indent && 123 apply_patch --whitespace=error-all && 124 test_cmp file target 125 126 ' 127 128 test_expect_success 'whitespace=error-all, no rule (attribute)' ' 129 130 git config --unset core.whitespace && 131 echo "target -whitespace" >.gitattributes && 132 apply_patch --whitespace=error-all && 133 test_cmp file target 134 135 ' 136 137 test_expect_success 'spaces inserted by tab-in-indent' ' 138 139 git config core.whitespace -trailing,-space,-indent,tab && 140 rm -f .gitattributes && 141 test_fix % && 142 sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF >expect && 143 An_SP in an ordinary line>and a HT. 144 ________A HT (%). 145 ________A SP and a HT (@%). 146 _________A SP, a HT and a SP (@%). 147 _______Seven SP. 148 ________Eight SP (#). 149 ________Seven SP and a HT (@%). 150 ________________Eight SP and a HT (@#%). 151 _________Seven SP, a HT and a SP (@%). 152 _________________Eight SP, a HT and a SP (@#%). 153 _______________Fifteen SP (#). 154 ________________Fifteen SP and a HT (@#%). 155 ________________Sixteen SP (#=). 156 ________________________Sixteen SP and a HT (@#%=). 157 _____a__Five SP, a non WS, two SP. 158 A line with a (!) trailing SP_ 159 A line with a (!) trailing HT> 160 EOF 161 test_cmp expect target 162 163 ' 164 165 for t in - '' 166 do 167 case "$t" in '') tt='!' ;; *) tt= ;; esac 168 for s in - '' 169 do 170 case "$s" in '') ts='@' ;; *) ts= ;; esac 171 for i in - '' 172 do 173 case "$i" in '') ti='#' ti16='=';; *) ti= ti16= ;; esac 174 for h in - '' 175 do 176 [ -z "$h$i" ] && continue 177 case "$h" in '') th='%' ;; *) th= ;; esac 178 rule=${t}trailing,${s}space,${i}indent,${h}tab 179 180 rm -f .gitattributes 181 test_expect_success "rule=$rule" ' 182 git config core.whitespace "$rule" && 183 test_fix "$tt$ts$ti$th" 184 ' 185 186 test_expect_success "rule=$rule,tabwidth=16" ' 187 git config core.whitespace "$rule,tabwidth=16" && 188 test_fix "$tt$ts$ti16$th" 189 ' 190 191 test_expect_success "rule=$rule (attributes)" ' 192 git config --unset core.whitespace && 193 echo "target whitespace=$rule" >.gitattributes && 194 test_fix "$tt$ts$ti$th" 195 ' 196 197 test_expect_success "rule=$rule,tabwidth=16 (attributes)" ' 198 echo "target whitespace=$rule,tabwidth=16" >.gitattributes && 199 test_fix "$tt$ts$ti16$th" 200 ' 201 202 done 203 done 204 done 205 done 206 207 create_patch () { 208 sed -e "s/_/ /" <<-\EOF 209 diff --git a/target b/target 210 index `e69de29`..`8bd6648` 100644 211 --- a/target 212 +++ b/target 213 @@ -0,0 +1,3 @@ 214 +An empty line follows 215 + 216 +A line with trailing whitespace and no newline_ 217 \ No newline at end of file 218 EOF 219 } 220 221 test_expect_success 'trailing whitespace & no newline at the end of file' ' 222 >target && 223 create_patch >patch-file && 224 git apply --whitespace=fix patch-file && 225 test_grep "newline$" target && 226 test_grep "^$" target 227 ' 228 229 test_expect_success 'blank at EOF with --whitespace=fix (1)' ' 230 test_might_fail git config --unset core.whitespace && 231 rm -f .gitattributes && 232 233 test_write_lines a b c >one && 234 git add one && 235 test_write_lines a b c >expect && 236 { cat expect && echo; } >one && 237 git diff -- one >patch && 238 239 git checkout one && 240 git apply --whitespace=fix patch && 241 test_cmp expect one 242 ' 243 244 test_expect_success 'blank at EOF with --whitespace=fix (2)' ' 245 test_write_lines a b c >one && 246 git add one && 247 test_write_lines a b >expect && 248 { cat expect && test_write_lines "" ""; } >one && 249 git diff -- one >patch && 250 251 git checkout one && 252 git apply --whitespace=fix patch && 253 test_cmp expect one 254 ' 255 256 test_expect_success 'blank at EOF with --whitespace=fix (3)' ' 257 test_write_lines a b "" >one && 258 git add one && 259 test_write_lines a c "" >expect && 260 { cat expect && test_write_lines "" ""; } >one && 261 git diff -- one >patch && 262 263 git checkout one && 264 git apply --whitespace=fix patch && 265 test_cmp expect one 266 ' 267 268 test_expect_success 'blank at end of hunk, not at EOF with --whitespace=fix' ' 269 test_write_lines a b "" "" "" "" "" d >one && 270 git add one && 271 test_write_lines a b "" "" "" "" "" "" d >expect && 272 cp expect one && 273 git diff -- one >patch && 274 275 git checkout one && 276 git apply --whitespace=fix patch && 277 test_cmp expect one 278 ' 279 280 test_expect_success 'blank at EOF with --whitespace=warn' ' 281 test_write_lines a b c >one && 282 git add one && 283 echo >>one && 284 cat one >expect && 285 git diff -- one >patch && 286 287 git checkout one && 288 git apply --whitespace=warn patch 2>error && 289 test_cmp expect one && 290 test_grep "new blank line at EOF" error 291 ' 292 293 test_expect_success 'blank at EOF with --whitespace=error' ' 294 test_write_lines a b c >one && 295 git add one && 296 cat one >expect && 297 echo >>one && 298 git diff -- one >patch && 299 300 git checkout one && 301 test_must_fail git apply --whitespace=error patch 2>error && 302 test_cmp expect one && 303 test_grep "new blank line at EOF" error 304 ' 305 306 test_expect_success 'blank but not empty at EOF' ' 307 test_write_lines a b c >one && 308 git add one && 309 echo " " >>one && 310 cat one >expect && 311 git diff -- one >patch && 312 313 git checkout one && 314 git apply --whitespace=warn patch 2>error && 315 test_cmp expect one && 316 test_grep "new blank line at EOF" error 317 ' 318 319 test_expect_success 'applying beyond EOF requires one non-blank context line' ' 320 test_write_lines "" "" "" "" >one && 321 git add one && 322 echo b >>one && 323 git diff -- one >patch && 324 325 git checkout one && 326 test_write_lines a "" >one && 327 cp one expect && 328 test_must_fail git apply --whitespace=fix patch && 329 test_cmp expect one && 330 test_must_fail git apply --ignore-space-change --whitespace=fix patch && 331 test_cmp expect one 332 ' 333 334 test_expect_success 'tons of blanks at EOF should not apply' ' 335 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do 336 test_write_lines "" "" "" "" || return 1 337 done >one && 338 git add one && 339 echo a >>one && 340 git diff -- one >patch && 341 342 >one && 343 test_must_fail git apply --whitespace=fix patch && 344 test_must_fail git apply --ignore-space-change --whitespace=fix patch 345 ' 346 347 test_expect_success 'missing blank line at end with --whitespace=fix' ' 348 echo a >one && 349 echo >>one && 350 git add one && 351 echo b >>one && 352 cp one expect && 353 git diff -- one >patch && 354 echo a >one && 355 cp one saved-one && 356 test_must_fail git apply patch && 357 git apply --whitespace=fix patch && 358 test_cmp expect one && 359 mv saved-one one && 360 git apply --ignore-space-change --whitespace=fix patch && 361 test_cmp expect one 362 ' 363 364 test_expect_success 'two missing blank lines at end with --whitespace=fix' ' 365 test_write_lines a "" b c >one && 366 cp one no-blank-lines && 367 test_write_lines "" "" >>one && 368 git add one && 369 echo d >>one && 370 cp one expect && 371 echo >>one && 372 git diff -- one >patch && 373 cp no-blank-lines one && 374 test_must_fail git apply patch && 375 git apply --whitespace=fix patch && 376 test_cmp expect one && 377 mv no-blank-lines one && 378 test_must_fail git apply patch && 379 git apply --ignore-space-change --whitespace=fix patch && 380 test_cmp expect one 381 ' 382 383 test_expect_success 'missing blank line at end, insert before end, --whitespace=fix' ' 384 test_write_lines a "" >one && 385 git add one && 386 test_write_lines b a "" >one && 387 cp one expect && 388 git diff -- one >patch && 389 echo a >one && 390 test_must_fail git apply patch && 391 git apply --whitespace=fix patch && 392 test_cmp expect one 393 ' 394 395 test_expect_success 'shrink file with tons of missing blanks at end of file' ' 396 test_write_lines a b c >one && 397 cp one no-blank-lines && 398 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do 399 test_write_lines "" "" "" "" || return 1 400 done >>one && 401 git add one && 402 echo a >one && 403 cp one expect && 404 git diff -- one >patch && 405 cp no-blank-lines one && 406 test_must_fail git apply patch && 407 git apply --whitespace=fix patch && 408 test_cmp expect one && 409 mv no-blank-lines one && 410 git apply --ignore-space-change --whitespace=fix patch && 411 test_cmp expect one 412 ' 413 414 test_expect_success 'missing blanks at EOF must only match blank lines' ' 415 test_write_lines a b >one && 416 git add one && 417 test_write_lines c d >>one && 418 git diff -- one >patch && 419 420 echo a >one && 421 test_must_fail git apply patch && 422 test_must_fail git apply --whitespace=fix patch && 423 test_must_fail git apply --ignore-space-change --whitespace=fix patch 424 ' 425 426 sed -e's/Z//' >one <>one && 436 git diff -- one >patch && 437 test_write_lines a b c >one && 438 cp one expect && 439 test_write_lines "" d >>expect && 440 git add one && 441 442 git apply --whitespace=fix patch && 443 test_cmp expect one 444 ' 445 446 sed -e's/Z//' >one <>one && 456 cp one expect && 457 git diff -- one >patch && 458 test_write_lines a b c >one && 459 git add one && 460 461 git checkout-index -f one && 462 git apply --ignore-space-change --whitespace=fix patch && 463 test_cmp expect one 464 ' 465 466 test_expect_success 'same, but with CR-LF line endings && cr-at-eol set' ' 467 git config core.whitespace cr-at-eol && 468 printf "a\r\n" >one && 469 printf "b\r\n" >>one && 470 printf "c\r\n" >>one && 471 cp one save-one && 472 printf " \r\n" >>one && 473 git add one && 474 printf "d\r\n" >>one && 475 cp one expect && 476 git diff -- one >patch && 477 mv save-one one && 478 479 git apply --ignore-space-change --whitespace=fix patch && 480 test_cmp expect one 481 ' 482 483 test_expect_success 'CR-LF line endings && add line && text=auto' ' 484 git config --unset core.whitespace && 485 printf "a\r\n" >one && 486 cp one save-one && 487 git add one && 488 printf "b\r\n" >>one && 489 cp one expect && 490 git diff -- one >patch && 491 mv save-one one && 492 echo "one text=auto" >.gitattributes && 493 git apply patch && 494 test_cmp expect one 495 ' 496 497 test_expect_success 'CR-LF line endings && change line && text=auto' ' 498 printf "a\r\n" >one && 499 cp one save-one && 500 git add one && 501 printf "b\r\n" >one && 502 cp one expect && 503 git diff -- one >patch && 504 mv save-one one && 505 echo "one text=auto" >.gitattributes && 506 git apply patch && 507 test_cmp expect one 508 ' 509 510 test_expect_success 'LF in repo, CRLF in worktree && change line && text=auto' ' 511 printf "a\n" >one && 512 git add one && 513 printf "b\r\n" >one && 514 git diff -- one >patch && 515 printf "a\r\n" >one && 516 echo "one text=auto" >.gitattributes && 517 git -c core.eol=CRLF apply patch && 518 printf "b\r\n" >expect && 519 test_cmp expect one 520 ' 521 522 test_expect_success 'whitespace=fix to expand' ' 523 qz_to_tab_space >preimage <<-\EOF && 524 QQa 525 QQb 526 QQc 527 ZZZZZZZZZZZZZZZZd 528 QQe 529 QQf 530 QQg 531 EOF 532 qz_to_tab_space >patch <<-\EOF && 533 diff --git a/preimage b/preimage 534 --- a/preimage 535 +++ b/preimage 536 @@ -1,7 +1,6 @@ 537 QQa 538 QQb 539 QQc 540 -QQd 541 QQe 542 QQf 543 QQg 544 EOF 545 git -c core.whitespace=tab-in-indent apply --whitespace=fix patch 546 ' 547 548 test_expect_success 'whitespace check skipped for excluded paths' ' 549 git config core.whitespace blank-at-eol && 550 >used && 551 >unused && 552 git add used unused && 553 echo "used" >used && 554 echo "unused " >unused && 555 git diff-files -p used unused >patch && 556 git apply --include=used --stat --whitespace=error sample-i && 567 test_write_lines 1 2 3 "" 0 5 >sample2-i && 568 cat sample-i >target && 569 git add target && 570 cat sample2-i >target && 571 git diff-files -p target >patch && 572 sed -e "s/^ $//" patch-new && 573 574 cat sample-i >target && 575 git apply --whitespace=fix error && 576 test_cmp sample2-i target && 577 test_must_be_empty error 578 ' 579 580 test_expect_success 'incomplete context line (not an error)' ' 581 (test_write_lines 1 2 3 4 5 && printf 6) >sample-i && 582 (test_write_lines 1 2 3 0 5 && printf 6) >sample2-i && 583 cat sample-i >target && 584 git add target && 585 cat sample2-i >target && 586 git diff-files -p target >patch && 587 588 cat sample-i >target && 589 git apply --whitespace=error target && 593 git apply --whitespace=error --check error && 594 test_cmp sample-i target && 595 test_must_be_empty error && 596 597 cat sample2-i >target && 598 git apply --whitespace=error -R target && 602 git apply -R --whitespace=error --check error && 603 test_cmp sample2-i target && 604 test_must_be_empty error 605 ' 606 607 test_expect_success 'last line made incomplete (error)' ' 608 test_write_lines 1 2 3 4 5 6 >sample && 609 (test_write_lines 1 2 3 4 5 && printf 6) >sample-i && 610 cat sample >target && 611 git add target && 612 cat sample-i >target && 613 git diff-files -p target >patch && 614 615 cat sample >target && 616 test_must_fail git apply --whitespace=error error && 617 test_grep "no newline" error && 618 619 cat sample >target && 620 test_must_fail git apply --whitespace=error --check actual && 621 test_cmp sample target && 622 cat >expect <<-\EOF && 623 :10: no newline at the end of file. 624 6 625 error: 1 line adds whitespace errors. 626 EOF 627 test_cmp expect actual && 628 629 cat sample-i >target && 630 git apply --whitespace=error -R target && 634 git apply --whitespace=error --check -R error && 635 test_cmp sample-i target && 636 test_must_be_empty error && 637 638 cat sample >target && 639 git apply --whitespace=fix sample-i && 645 test_write_lines 1 2 3 4 5 6 >sample && 646 cat sample-i >target && 647 git add target && 648 cat sample >target && 649 git diff-files -p target >patch && 650 651 cat sample-i >target && 652 git apply --whitespace=error target && 656 git apply --whitespace=error --check error && 657 test_cmp sample-i target && 658 test_must_be_empty error && 659 660 cat sample >target && 661 test_must_fail git apply --whitespace=error -R error && 662 test_grep "no newline" error && 663 664 cat sample >target && 665 test_must_fail git apply --whitespace=error --check -R actual && 666 test_cmp sample target && 667 cat >expect <<-\EOF && 668 :9: no newline at the end of file. 669 6 670 error: 1 line adds whitespace errors. 671 EOF 672 test_cmp expect actual && 673 674 cat sample >target && 675 git apply --whitespace=fix -R sample-i && 681 test_write_lines 1 2 3 4 5 7 >sample3 && 682 cat sample-i >target && 683 git add target && 684 cat sample3 >target && 685 git diff-files -p target >patch && 686 687 cat sample-i >target && 688 git apply --whitespace=error target && 692 git apply --whitespace=error --check error && 693 test_cmp sample-i target && 694 test_must_be_empty error && 695 696 cat sample3 >target && 697 test_must_fail git apply --whitespace=error -R error && 698 test_grep "no newline" error && 699 700 cat sample3 >target && 701 test_must_fail git apply --whitespace=error -R --check actual && 702 test_cmp sample3 target && 703 cat >expect <<-\EOF && 704 :9: no newline at the end of file. 705 6 706 error: 1 line adds whitespace errors. 707 EOF 708 test_cmp expect actual && 709 710 cat sample3 >target && 711 git apply --whitespace=fix -R sample-i && 717 (test_write_lines 1 2 3 4 5 && printf 7) >sample3-i && 718 test_write_lines 1 2 3 4 5 6 >sample && 719 test_write_lines 1 2 3 4 5 7 >sample3 && 720 cat sample-i >target && 721 git add target && 722 cat sample3-i >target && 723 git diff-files -p target >patch && 724 725 cat sample-i >target && 726 test_must_fail git apply --whitespace=error error && 727 test_grep "no newline" error && 728 729 cat sample-i >target && 730 test_must_fail git apply --whitespace=error --check actual && 731 test_cmp sample-i target && 732 cat >expect <<-\EOF && 733 :11: no newline at the end of file. 734 7 735 error: 1 line adds whitespace errors. 736 EOF 737 test_cmp expect actual && 738 739 cat sample3-i >target && 740 test_must_fail git apply --whitespace=error -R error && 741 test_grep "no newline" error && 742 743 cat sample3-i >target && 744 test_must_fail git apply --whitespace=error --check -R actual && 745 test_cmp sample3-i target && 746 cat >expect <<-\EOF && 747 :9: no newline at the end of file. 748 6 749 error: 1 line adds whitespace errors. 750 EOF 751 test_cmp expect actual && 752 753 cat sample-i >target && 754 git apply --whitespace=fix target && 758 git apply --whitespace=fix -R patch0.txt <<-EOF && 772 diff --git a/mylink b/mylink 773 index $oneshort..$twoshort 120000 774 --- a/mylink 775 +++ b/mylink 776 @@ -1 +1 @@ 777 -one 778 \ No newline at end of file 779 +two 780 \ No newline at end of file 781 EOF 782 783 # the index has the preimage symlink 784 git update-index --add --cacheinfo "120000,$oneblob,mylink" && 785 786 # check the patch going forward and reverse 787 git -c core.whitespace=incomplete apply --cached --check \ 788 --whitespace=error patch0.txt && 789 790 git update-index --add --cacheinfo "120000,$twoblob,mylink" && 791 git -c core.whitespace=incomplete apply --cached --check \ 792 --whitespace=error -R patch0.txt && 793 794 # the patch turns it into the postimage symlink 795 git update-index --add --cacheinfo "120000,$oneblob,mylink" && 796 git -c core.whitespace=incomplete apply --cached --whitespace=error \ 797 patch0.txt && 798 799 # and then back. 800 git -c core.whitespace=incomplete apply --cached -R --whitespace=error \ 801 patch0.txt && 802 803 # a text file turns into a symlink 804 cat >patch1.txt <<-EOF && 805 diff --git a/mylink b/mylink 806 deleted file mode 100644 807 index $oneshort..`0000000` 808 --- a/mylink 809 +++ /dev/null 810 @@ -1 +0,0 @@ 811 -one 812 \ No newline at end of file 813 diff --git a/mylink b/mylink 814 new file mode 120000 815 index `0000000`..$twoshort 816 --- /dev/null 817 +++ b/mylink 818 @@ -0,0 +1 @@ 819 +two 820 \ No newline at end of file 821 EOF 822 823 # the index has the preimage text 824 git update-index --cacheinfo "100644,$oneblob,mylink" && 825 826 # check 827 git -c core.whitespace=incomplete apply --cached \ 828 --check --whitespace=error patch1.txt && 829 830 # reverse, leaving an incomplete text file, should error 831 git update-index --cacheinfo "120000,$twoblob,mylink" && 832 test_must_fail git -c core.whitespace=incomplete \ 833 apply --cached --check --whitespace=error -R patch1.txt && 834 835 # apply to create a symbolic link 836 git update-index --cacheinfo "100644,$oneblob,mylink" && 837 git -c core.whitespace=incomplete apply --cached --whitespace=error \ 838 patch1.txt && 839 840 # turning it back into an incomplete text file is an error 841 test_must_fail git -c core.whitespace=incomplete \ 842 apply --cached --whitespace=error -R patch1.txt 843 844 845 846 ' 847 848 test_done