1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
|
#!/bin/sh
test_description='add -i basic tests'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-terminal.sh
SP=" "
diff_cmp () {
for x
do
sed -e '/^index/s/[0-9a-f]*[1-9a-f][0-9a-f]*\.\./1234567../' \
-e '/^index/s/\.\.[0-9a-f]*[1-9a-f][0-9a-f]*/..9abcdef/' \
-e '/^index/s/ 00*\.\./ 0000000../' \
-e '/^index/s/\.\.00*$/..0000000/' \
-e '/^index/s/\.\.00* /..0000000 /' \
"$x" >"$x.filtered"
done
test_cmp "$1.filtered" "$2.filtered"
}
# This function uses a trick to manipulate the interactive add to use color:
# the `want_color()` function special-cases the situation where a pager was
# spawned and Git now wants to output colored text: to detect that situation,
# the environment variable `GIT_PAGER_IN_USE` is set. However, color is
# suppressed despite that environment variable if the `TERM` variable
# indicates a dumb terminal, so we set that variable, too.
force_color () {
# The first element of $@ may be a shell function, as a result POSIX
# does not guarantee that "one-shot assignment" will not persist after
# the function call. Thus, we prevent these variables from escaping
# this function's context with this subshell.
(
GIT_PAGER_IN_USE=true &&
TERM=vt100 &&
export GIT_PAGER_IN_USE TERM &&
"$@"
)
}
test_expect_success 'unknown command' '
test_when_finished "git reset --hard; rm -f command" &&
echo W >command &&
git add -N command &&
git diff command >expect &&
cat >>expect <<-EOF &&
(1/1) Stage addition [y,n,q,a,d,e,p,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help)
(1/1) Stage addition [y,n,q,a,d,e,p,?]?$SP
EOF
git add -p -- command <command >actual 2>&1 &&
test_cmp expect actual
'
test_expect_success 'setup (initial)' '
echo content >file &&
git add file &&
echo more >>file &&
echo lines >>file
'
test_expect_success 'status works (initial)' '
git add -i </dev/null >output &&
grep "+1/-0 *+2/-0 file" output
'
test_expect_success 'setup expected' '
cat >expected <<-\EOF
new file mode 100644
index 0000000..d95f3ad
--- /dev/null
+++ b/file
@@ -0,0 +1 @@
+content
EOF
'
test_expect_success 'diff works (initial)' '
test_write_lines d 1 | git add -i >output &&
sed -ne "/new file/,/content/p" <output >diff &&
diff_cmp expected diff
'
test_expect_success 'revert works (initial)' '
git add file &&
test_write_lines r 1 | git add -i &&
git ls-files >output &&
! grep . output
'
test_expect_success 'add untracked (multiple)' '
test_when_finished "git reset && rm [1-9]" &&
touch $(test_seq 9) &&
test_write_lines a "2-5 8-" | git add -i -- [1-9] &&
test_write_lines 2 3 4 5 8 9 >expected &&
git ls-files [1-9] >output &&
test_cmp expected output
'
test_expect_success 'setup (commit)' '
echo baseline >file &&
git add file &&
git commit -m commit &&
echo content >>file &&
git add file &&
echo more >>file &&
echo lines >>file
'
test_expect_success 'status works (commit)' '
git add -i </dev/null >output &&
grep "+1/-0 *+2/-0 file" output
'
test_expect_success 'update can stage deletions' '
>to-delete &&
git add to-delete &&
rm to-delete &&
test_write_lines u t "" | git add -i &&
git ls-files to-delete >output &&
test_must_be_empty output
'
test_expect_success 'setup expected' '
cat >expected <<-\EOF
index 180b47c..b6f2c08 100644
--- a/file
+++ b/file
@@ -1 +1,2 @@
baseline
+content
EOF
'
test_expect_success 'diff works (commit)' '
test_write_lines d 1 | git add -i >output &&
sed -ne "/^index/,/content/p" <output >diff &&
diff_cmp expected diff
'
test_expect_success 'revert works (commit)' '
git add file &&
test_write_lines r 1 | git add -i &&
git add -i </dev/null >output &&
grep "unchanged *+3/-0 file" output
'
test_expect_success 'reject multi-key input' '
saved=$(git hash-object -w file) &&
test_when_finished "git cat-file blob $saved >file" &&
echo an extra line >>file &&
test_write_lines aa | git add -p >actual &&
test_grep "is expected, got ${SQ}aa${SQ}" actual
'
test_expect_success 'setup expected' '
cat >expected <<-\EOF
EOF
'
test_expect_success 'dummy edit works' '
test_set_editor : &&
test_write_lines e a | git add -p &&
git diff > diff &&
diff_cmp expected diff
'
test_expect_success 'setup patch' '
cat >patch <<-\EOF
@@ -1,1 +1,4 @@
this
+patch
-does not
apply
EOF
'
test_expect_success 'setup fake editor' '
write_script "fake_editor.sh" <<-\EOF &&
mv -f "$1" oldpatch &&
mv -f patch "$1"
EOF
test_set_editor "$(pwd)/fake_editor.sh"
'
test_expect_success 'bad edit rejected' '
git reset &&
test_write_lines e n d | git add -p >output &&
grep "hunk does not apply" output
'
test_expect_success 'setup patch' '
cat >patch <<-\EOF
this patch
is garbage
EOF
'
test_expect_success 'garbage edit rejected' '
git reset &&
test_write_lines e n d | git add -p >output &&
grep "hunk does not apply" output
'
test_expect_success 'setup patch' '
cat >patch <<-\EOF
@@ -1,0 +1,0 @@
baseline
+content
+newcontent
+lines
EOF
'
test_expect_success 'setup expected' '
cat >expected <<-\EOF
diff --git a/file b/file
index b5dd6c9..f910ae9 100644
--- a/file
+++ b/file
@@ -1,4 +1,4 @@
baseline
content
-newcontent
+more
lines
EOF
'
test_expect_success 'real edit works' '
test_write_lines e n d | git add -p &&
git diff >output &&
diff_cmp expected output
'
test_expect_success 'setup file' '
test_write_lines a "" b "" c >file &&
git add file &&
test_write_lines a "" d "" c >file
'
test_expect_success 'setup patch' '
NULL="" &&
cat >patch <<-EOF
@@ -1,4 +1,4 @@
a
$NULL
-b
+f
$SP
c
EOF
'
test_expect_success 'setup expected' '
cat >expected <<-EOF
diff --git a/file b/file
index b5dd6c9..f910ae9 100644
--- a/file
+++ b/file
@@ -1,5 +1,5 @@
a
$SP
-f
+d
$SP
c
EOF
'
test_expect_success 'edit can strip spaces from empty context lines' '
test_write_lines e n q | git add -p 2>error &&
test_must_be_empty error &&
git diff >output &&
diff_cmp expected output
'
test_expect_success 'skip files similarly as commit -a' '
git reset &&
echo file >.gitignore &&
echo changed >file &&
echo y | git add -p file &&
git diff >output &&
git reset &&
git commit -am commit &&
git diff >expected &&
diff_cmp expected output &&
git reset --hard HEAD^
'
rm -f .gitignore
test_expect_success FILEMODE 'patch does not affect mode' '
git reset --hard &&
echo content >>file &&
chmod +x file &&
printf "n\\ny\\n" | git add -p &&
git show :file | grep content &&
git diff file | grep "new mode"
'
test_expect_success FILEMODE 'stage mode but not hunk' '
git reset --hard &&
echo content >>file &&
chmod +x file &&
printf "y\\nn\\n" | git add -p &&
git diff --cached file | grep "new mode" &&
git diff file | grep "+content"
'
test_expect_success FILEMODE 'stage mode and hunk' '
git reset --hard &&
echo content >>file &&
chmod +x file &&
printf "y\\ny\\n" | git add -p &&
git diff --cached file >out &&
grep "new mode" out &&
grep "+content" out &&
git diff file >out &&
test_must_be_empty out
'
# end of tests disabled when filemode is not usable
test_expect_success 'different prompts for mode change/deleted' '
git reset --hard &&
>file &&
>deleted &&
git add --chmod=+x file deleted &&
echo changed >file &&
rm deleted &&
test_write_lines n n n |
git -c core.filemode=true add -p >actual &&
sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
cat >expect <<-\EOF &&
(1/1) Stage deletion [y,n,q,a,d,p,?]?
(1/2) Stage mode change [y,n,q,a,d,j,J,g,/,p,?]?
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]?
EOF
test_cmp expect actual.filtered
'
test_expect_success 'correct message when there is nothing to do' '
git reset --hard &&
git add -p >out &&
test_grep "No changes" out &&
printf "\\0123" >binary &&
git add binary &&
printf "\\0abc" >binary &&
git add -p >out &&
test_grep "Only binary files changed" out
'
test_expect_success 'setup again' '
git reset --hard &&
test_chmod +x file &&
echo content >>file &&
test_write_lines A B C D>file2 &&
git add file2
'
# Write the patch file with a new line at the top and bottom
test_expect_success 'setup patch' '
cat >patch <<-\EOF
index 180b47c..b6f2c08 100644
--- a/file
+++ b/file
@@ -1,2 +1,4 @@
+firstline
baseline
content
+lastline
\ No newline at end of file
diff --git a/file2 b/file2
index 8422d40..35b930a 100644
--- a/file2
+++ b/file2
@@ -1,4 +1,5 @@
-A
+Z
B
+Y
C
-D
+X
EOF
'
# Expected output, diff is similar to the patch but w/ diff at the top
test_expect_success 'setup expected' '
echo diff --git a/file b/file >expected &&
sed -e "/^index 180b47c/s/ 100644/ 100755/" \
-e /1,5/s//1,4/ \
-e /Y/d patch >>expected &&
cat >expected-output <<-\EOF
--- a/file
+++ b/file
@@ -1,2 +1,4 @@
+firstline
baseline
content
+lastline
\ No newline at end of file
@@ -1,2 +1,3 @@
+firstline
baseline
content
@@ -1,2 +2,3 @@
baseline
content
+lastline
\ No newline at end of file
--- a/file2
+++ b/file2
@@ -1,4 +1,5 @@
-A
+Z
B
+Y
C
-D
+X
@@ -1,2 +1,2 @@
-A
+Z
B
@@ -2,2 +2,3 @@
B
+Y
C
@@ -3,2 +4,2 @@
C
-D
+X
EOF
'
# Test splitting the first patch, then adding both
test_expect_success 'add first line works' '
git commit -am "clear local changes" &&
git apply patch &&
test_write_lines s y y s y n y | git add -p 2>error >raw-output &&
sed -n -e "s/^([1-9]\/[1-9]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
-e "/^[-+@ \\\\]"/p raw-output >output &&
test_must_be_empty error &&
git diff --cached >diff &&
diff_cmp expected diff &&
test_cmp expected-output output
'
test_expect_success 'setup expected' '
cat >expected <<-\EOF
diff --git a/non-empty b/non-empty
deleted file mode 100644
index d95f3ad..0000000
--- a/non-empty
+++ /dev/null
@@ -1 +0,0 @@
-content
EOF
'
test_expect_success 'deleting a non-empty file' '
git reset --hard &&
echo content >non-empty &&
git add non-empty &&
git commit -m non-empty &&
rm non-empty &&
echo y | git add -p non-empty &&
git diff --cached >diff &&
diff_cmp expected diff
'
test_expect_success 'setup expected' '
cat >expected <<-\EOF
diff --git a/empty b/empty
deleted file mode 100644
index e69de29..0000000
EOF
'
test_expect_success 'deleting an empty file' '
git reset --hard &&
> empty &&
git add empty &&
git commit -m empty &&
rm empty &&
echo y | git add -p empty &&
git diff --cached >diff &&
diff_cmp expected diff
'
test_expect_success 'adding an empty file' '
git init added &&
(
cd added &&
test_commit initial &&
>empty &&
git add empty &&
test_tick &&
git commit -m empty &&
git tag added-file &&
git reset --hard HEAD^ &&
test_path_is_missing empty &&
echo y | git checkout -p added-file -- >actual &&
test_path_is_file empty &&
test_grep "Apply addition to index and worktree" actual
)
'
test_expect_success 'split hunk setup' '
git reset --hard &&
test_write_lines 10 20 30 40 50 60 >test &&
git add test &&
test_tick &&
git commit -m test &&
test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
'
test_expect_success 'goto hunk 1 with "g 1"' '
test_when_finished "git reset" &&
tr _ " " >expect <<-EOF &&
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + 1: -1,2 +1,3 +15
_ 2: -2,4 +3,8 +21
go to which hunk? @@ -1,2 +1,3 @@
_10
+15
_20
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
EOF
test_write_lines s y g 1 | git add -p >actual &&
tail -n 7 <actual >actual.trimmed &&
test_cmp expect actual.trimmed
'
test_expect_success 'goto hunk 1 with "g1"' '
test_when_finished "git reset" &&
tr _ " " >expect <<-EOF &&
_10
+15
_20
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
EOF
test_write_lines s y g1 | git add -p >actual &&
tail -n 4 <actual >actual.trimmed &&
test_cmp expect actual.trimmed
'
test_expect_success 'navigate to hunk via regex /pattern' '
test_when_finished "git reset" &&
tr _ " " >expect <<-EOF &&
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -1,2 +1,3 @@
_10
+15
_20
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
EOF
test_write_lines s y /1,2 | git add -p >actual &&
tail -n 5 <actual >actual.trimmed &&
test_cmp expect actual.trimmed
'
test_expect_success 'navigate to hunk via regex / pattern' '
test_when_finished "git reset" &&
tr _ " " >expect <<-EOF &&
_10
+15
_20
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
EOF
test_write_lines s y / 1,2 | git add -p >actual &&
tail -n 4 <actual >actual.trimmed &&
test_cmp expect actual.trimmed
'
test_expect_success 'print again the hunk' '
test_when_finished "git reset" &&
tr _ " " >expect <<-EOF &&
+15
20
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? @@ -1,2 +1,3 @@
10
+15
20
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
EOF
test_write_lines s y g 1 p | git add -p >actual &&
tail -n 7 <actual >actual.trimmed &&
test_cmp expect actual.trimmed
'
test_expect_success TTY 'print again the hunk (PAGER)' '
test_when_finished "git reset" &&
cat >expect <<-EOF &&
<GREEN>+<RESET><GREEN>15<RESET>
20<RESET>
<BOLD;BLUE>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>PAGER <CYAN>@@ -1,2 +1,3 @@<RESET>
PAGER 10<RESET>
PAGER <GREEN>+<RESET><GREEN>15<RESET>
PAGER 20<RESET>
<BOLD;BLUE>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
EOF
test_write_lines s y g 1 P |
(
GIT_PAGER="sed s/^/PAGER\ /" &&
export GIT_PAGER &&
test_terminal git add -p >actual
) &&
tail -n 7 <actual | test_decode_color >actual.trimmed &&
test_cmp expect actual.trimmed
'
test_expect_success TTY 'P handles SIGPIPE when writing to pager' '
test_when_finished "rm -f huge_file; git reset" &&
printf "\n%2500000s" Y >huge_file &&
git add -N huge_file &&
test_write_lines P q | (
GIT_PAGER="head -n 1" &&
export GIT_PAGER &&
test_terminal git add -p
)
'
test_expect_success 'split hunk "add -p (edit)"' '
# Split, say Edit and do nothing. Then:
#
# 1. Broken version results in a patch that does not apply and
# only takes [y/n] (edit again) so the first q is discarded
# and then n attempts to discard the edit. Repeat q enough
# times to get out.
#
# 2. Correct version applies the (not)edited version, and asks
# about the next hunk, against which we say q and program
# exits.
printf "%s\n" s e q n q q |
EDITOR=: git add -p &&
git diff >actual &&
! grep "^+15" actual
'
test_expect_success 'split hunk "add -p (no, yes, edit)"' '
test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
git reset &&
# test sequence is s(plit), n(o), y(es), e(dit)
# q n q q is there to make sure we exit at the end.
printf "%s\n" s n y e q n q q |
EDITOR=: git add -p 2>error &&
test_must_be_empty error &&
git diff >actual &&
! grep "^+31" actual
'
test_expect_success 'split hunk with incomplete line at end' '
git reset --hard &&
printf "missing LF" >>test &&
git add test &&
test_write_lines before 10 20 30 40 50 60 70 >test &&
git grep --cached missing &&
test_write_lines s n y q | git add -p &&
test_must_fail git grep --cached missing &&
git grep before &&
test_must_fail git grep --cached before
'
test_expect_success 'edit, adding lines to the first hunk' '
test_write_lines 10 11 20 30 40 50 51 60 >test &&
git reset &&
tr _ " " >patch <<-EOF &&
@@ -1,5 +1,6 @@
_10
+11
+12
_20
+21
+22
_30
EOF
# test sequence is s(plit), e(dit), n(o)
# q n q q is there to make sure we exit at the end.
printf "%s\n" s e n q n q q |
EDITOR=./fake_editor.sh git add -p 2>error &&
test_must_be_empty error &&
git diff --cached >actual &&
grep "^+22" actual
'
test_expect_success 'patch mode ignores unmerged entries' '
git reset --hard &&
test_commit conflict &&
test_commit non-conflict &&
git checkout -b side &&
test_commit side conflict.t &&
git checkout main &&
test_commit main conflict.t &&
test_must_fail git merge side &&
echo changed >non-conflict.t &&
echo y | git add -p >output &&
! grep a/conflict.t output &&
cat >expected <<-\EOF &&
* Unmerged path conflict.t
diff --git a/non-conflict.t b/non-conflict.t
index f766221..5ea2ed4 100644
--- a/non-conflict.t
+++ b/non-conflict.t
@@ -1 +1 @@
-non-conflict
+changed
EOF
git diff --cached >diff &&
diff_cmp expected diff
'
test_expect_success 'index is refreshed after applying patch' '
git reset --hard &&
echo content >test &&
printf y | git add -p &&
git diff-files --exit-code
'
test_expect_success 'diffs can be colorized' '
git reset --hard &&
echo content >test &&
printf y >y &&
force_color git add -p >output 2>&1 <y &&
git diff-files --exit-code &&
# We do not want to depend on the exact coloring scheme
# git uses for diffs, so just check that we saw some kind of color.
grep "$(printf "\\033")" output
'
test_expect_success 'colors can be overridden' '
git reset --hard &&
test_when_finished "git rm -f color-test" &&
test_write_lines context old more-context >color-test &&
git add color-test &&
test_write_lines context new more-context another-one >color-test &&
echo trigger an error message >input &&
force_color git \
-c color.interactive.error=blue \
add -i 2>err.raw <input &&
test_decode_color <err.raw >err &&
grep "<BLUE>Huh (trigger)?<RESET>" err &&
test_write_lines help quit >input &&
force_color git \
-c color.interactive.header=red \
-c color.interactive.help=green \
-c color.interactive.prompt=yellow \
add -i >actual.raw <input &&
test_decode_color <actual.raw >actual &&
cat >expect <<-\EOF &&
<RED> staged unstaged path<RESET>
1: +3/-0 +2/-1 color-test
<RED>*** Commands ***<RESET>
1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
<YELLOW>What now<RESET>> <GREEN>status - show paths with changes<RESET>
<GREEN>update - add working tree state to the staged set of changes<RESET>
<GREEN>revert - revert staged set of changes back to the HEAD version<RESET>
<GREEN>patch - pick hunks and update selectively<RESET>
<GREEN>diff - view diff between HEAD and index<RESET>
<GREEN>add untracked - add contents of untracked files to the staged set of changes<RESET>
<RED>*** Commands ***<RESET>
1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
<YELLOW>What now<RESET>> Bye.
EOF
test_cmp expect actual &&
: exercise recolor_hunk by editing and then look at the hunk again &&
test_write_lines s e K q >input &&
force_color git \
-c color.interactive.prompt=yellow \
-c color.diff.meta=italic \
-c color.diff.frag=magenta \
-c color.diff.context=cyan \
-c color.diff.old=bold \
-c color.diff.new=blue \
-c core.editor=touch \
add -p >actual.raw <input &&
test_decode_color <actual.raw >actual.decoded &&
sed "s/index [0-9a-f]*\\.\\.[0-9a-f]* 100644/<INDEX-LINE>/" <actual.decoded >actual &&
cat >expect <<-\EOF &&
<ITALIC>diff --git a/color-test b/color-test<RESET>
<ITALIC><INDEX-LINE><RESET>
<ITALIC>--- a/color-test<RESET>
<ITALIC>+++ b/color-test<RESET>
<MAGENTA>@@ -1,3 +1,4 @@<RESET>
<CYAN> context<RESET>
<BOLD>-old<RESET>
<BLUE>+<RESET><BLUE>new<RESET>
<CYAN> more-context<RESET>
<BLUE>+<RESET><BLUE>another-one<RESET>
<YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,p,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
<MAGENTA>@@ -1,3 +1,3 @@<RESET>
<CYAN> context<RESET>
<BOLD>-old<RESET>
<BLUE>+<RESET><BLUE>new<RESET>
<CYAN> more-context<RESET>
<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
<CYAN> more-context<RESET>
<BLUE>+<RESET><BLUE>another-one<RESET>
<YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
<CYAN> context<RESET>
<BOLD>-old<RESET>
<BLUE>+new<RESET>
<CYAN> more-context<RESET>
<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
EOF
test_cmp expect actual
'
test_expect_success 'brackets appear without color' '
git reset --hard &&
test_when_finished "git rm -f bracket-test" &&
test_write_lines context old more-context >bracket-test &&
git add bracket-test &&
test_write_lines context new more-context another-one >bracket-test &&
test_write_lines quit >input &&
git add -i >actual <input &&
sed "s/^|//" >expect <<-\EOF &&
| staged unstaged path
| 1: +3/-0 +2/-1 bracket-test
|
|*** Commands ***
| 1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
| 5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
|What now> Bye.
EOF
test_cmp expect actual
'
test_expect_success 'colors can be skipped with color.ui=false' '
git reset --hard &&
test_when_finished "git rm -f color-test" &&
test_write_lines context old more-context >color-test &&
git add color-test &&
test_write_lines context new more-context another-one >color-test &&
test_write_lines help quit >input &&
force_color git \
-c color.ui=false \
add -i >actual.raw <input &&
test_decode_color <actual.raw >actual &&
test_cmp actual.raw actual
'
test_expect_success 'colorized diffs respect diff.wsErrorHighlight' '
git reset --hard &&
echo "old " >test &&
git add test &&
echo "new " >test &&
printf y >y &&
force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
test_decode_color <output.raw >output &&
grep "old<" output
'
test_expect_success 'diffFilter filters diff' '
git reset --hard &&
echo content >test &&
test_config interactive.diffFilter "sed s/^/foo:/" &&
printf y >y &&
force_color git add -p >output 2>&1 <y &&
# avoid depending on the exact coloring or content of the prompts,
# and just make sure we saw our diff prefixed
grep foo:.*content output
'
test_expect_success 'detect bogus diffFilter output' '
git reset --hard &&
echo content >test &&
test_config interactive.diffFilter "sed 6d" &&
printf y >y &&
force_color test_must_fail git add -p <y >output 2>&1 &&
grep "mismatched output" output
'
test_expect_success 'handle iffy colored hunk headers' '
git reset --hard &&
echo content >test &&
printf n >n &&
force_color git -c interactive.diffFilter="sed s/.*@@.*/XX/" \
add -p >output 2>&1 <n &&
grep "^XX$" output
'
test_expect_success 'handle very large filtered diff' '
git reset --hard &&
# The specific number here is not important, but it must
# be large enough that the output of "git diff --color"
# fills up the pipe buffer. 10,000 results in ~200k of
# colored output.
test_seq 10000 >test &&
test_config interactive.diffFilter cat &&
printf y >y &&
force_color git add -p >output 2>&1 <y &&
git diff-files --exit-code -- test
'
test_expect_success 'diff.algorithm is passed to `git diff-files`' '
git reset --hard &&
>file &&
git add file &&
echo changed >file &&
test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
test_grep "error: option diff-algorithm accepts " err
'
test_expect_success 'patch-mode via -i prompts for files' '
git reset --hard &&
echo one >file &&
echo two >test &&
git add -i <<-\EOF &&
patch
test
y
quit
EOF
echo test >expect &&
git diff --cached --name-only >actual &&
diff_cmp expect actual
'
test_expect_success 'add -p handles globs' '
git reset --hard &&
mkdir -p subdir &&
echo base >one.c &&
echo base >subdir/two.c &&
git add "*.c" &&
git commit -m base &&
echo change >one.c &&
echo change >subdir/two.c &&
git add -p "*.c" <<-\EOF &&
y
y
EOF
cat >expect <<-\EOF &&
one.c
subdir/two.c
EOF
git diff --cached --name-only >actual &&
test_cmp expect actual
'
test_expect_success 'add -p handles relative paths' '
git reset --hard &&
echo base >relpath.c &&
git add "*.c" &&
git commit -m relpath &&
echo change >relpath.c &&
mkdir -p subdir &&
git -C subdir add -p .. 2>error <<-\EOF &&
y
EOF
test_must_be_empty error &&
cat >expect <<-\EOF &&
relpath.c
EOF
git diff --cached --name-only >actual &&
test_cmp expect actual
'
test_expect_success 'add -p does not expand argument lists' '
git reset --hard &&
echo content >not-changed &&
git add not-changed &&
git commit -m "add not-changed file" &&
echo change >file &&
GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
y
EOF
# we know that "file" must be mentioned since we actually
# update it, but we want to be sure that our "." pathspec
# was not expanded into the argument list of any command.
# So look only for "not-changed".
! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
'
test_expect_success 'hunk-editing handles custom comment char' '
git reset --hard &&
echo change >>file &&
test_config core.commentChar "\$" &&
echo e | GIT_EDITOR=true git add -p &&
git diff --exit-code
'
test_expect_success 'add -p works even with color.ui=always' '
git reset --hard &&
echo change >>file &&
test_config color.ui always &&
echo y | git add -p &&
echo file >expect &&
git diff --cached --name-only >actual &&
test_cmp expect actual
'
test_expect_success 'setup different kinds of dirty submodules' '
test_create_repo for-submodules &&
(
cd for-submodules &&
test_commit initial &&
test_create_repo dirty-head &&
(
cd dirty-head &&
test_commit initial
) &&
cp -R dirty-head dirty-otherwise &&
cp -R dirty-head dirty-both-ways &&
git add dirty-head &&
git add dirty-otherwise dirty-both-ways &&
git commit -m initial &&
cd dirty-head &&
test_commit updated &&
cd ../dirty-both-ways &&
test_commit updated &&
echo dirty >>initial &&
: >untracked &&
cd ../dirty-otherwise &&
echo dirty >>initial &&
: >untracked
) &&
git -C for-submodules diff-files --name-only >actual &&
cat >expected <<-\EOF &&
dirty-both-ways
dirty-head
EOF
test_cmp expected actual &&
git -C for-submodules diff-files --name-only --ignore-submodules=none >actual &&
cat >expected <<-\EOF &&
dirty-both-ways
dirty-head
dirty-otherwise
EOF
test_cmp expected actual &&
git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
cat >expected <<-\EOF &&
dirty-both-ways
dirty-head
EOF
test_cmp expected actual
'
test_expect_success 'status ignores dirty submodules (except HEAD)' '
git -C for-submodules add -i </dev/null >output &&
grep dirty-head output &&
grep dirty-both-ways output &&
! grep dirty-otherwise output
'
test_expect_success 'handle submodules' '
echo 123 >>for-submodules/dirty-otherwise/initial.t &&
force_color git -C for-submodules add -p dirty-otherwise >output 2>&1 &&
grep "No changes" output &&
force_color git -C for-submodules add -p dirty-head >output 2>&1 <y &&
git -C for-submodules ls-files --stage dirty-head >actual &&
rev="$(git -C for-submodules/dirty-head rev-parse HEAD)" &&
grep "$rev" actual
'
test_expect_success 'set up pathological context' '
git reset --hard &&
test_write_lines a a a a a a a a a a a >a &&
git add a &&
git commit -m a &&
test_write_lines c b a a a a a a a b a a a a >a &&
test_write_lines a a a a a a a b a a a a >expected-1 &&
test_write_lines b a a a a a a a b a a a a >expected-2 &&
# check editing can cope with missing header and deleted context lines
# as well as changes to other lines
test_write_lines +b " a" >patch
'
test_expect_success 'add -p works with pathological context lines' '
git reset &&
printf "%s\n" n y |
git add -p &&
git cat-file blob :a >actual &&
test_cmp expected-1 actual
'
test_expect_success 'add -p patch editing works with pathological context lines' '
git reset &&
# n q q below is in case edit fails
printf "%s\n" e y n q q |
git add -p &&
git cat-file blob :a >actual &&
test_cmp expected-2 actual
'
test_expect_success 'checkout -p works with pathological context lines' '
test_write_lines a a a a a a >a &&
git add a &&
test_write_lines a b a b a b a b a b a >a &&
test_write_lines s n n y q | git checkout -p &&
test_write_lines a b a b a a b a b a >expect &&
test_cmp expect a
'
# This should be called from a subshell as it sets a temporary editor
setup_new_file() {
write_script new-file-editor.sh <<-\EOF &&
sed /^#/d "$1" >patch &&
sed /^+c/d patch >"$1"
EOF
test_set_editor "$(pwd)/new-file-editor.sh" &&
test_write_lines a b c d e f >new-file &&
test_write_lines a b d e f >new-file-expect &&
test_write_lines "@@ -0,0 +1,6 @@" +a +b +c +d +e +f >patch-expect
}
test_expect_success 'add -N followed by add -p patch editing' '
git reset --hard &&
(
setup_new_file &&
git add -N new-file &&
test_write_lines e n q | git add -p &&
git cat-file blob :new-file >actual &&
test_cmp new-file-expect actual &&
test_cmp patch-expect patch
)
'
test_expect_success 'checkout -p patch editing of added file' '
git reset --hard &&
(
setup_new_file &&
git add new-file &&
git commit -m "add new file" &&
git rm new-file &&
git commit -m "remove new file" &&
test_write_lines e n q | git checkout -p HEAD^ &&
test_cmp new-file-expect new-file &&
test_cmp patch-expect patch
)
'
test_expect_success 'show help from add--helper' '
git reset --hard &&
cat >expect <<-EOF &&
<BOLD>*** Commands ***<RESET>
1: <BOLD;BLUE>s<RESET>tatus 2: <BOLD;BLUE>u<RESET>pdate 3: <BOLD;BLUE>r<RESET>evert 4: <BOLD;BLUE>a<RESET>dd untracked
5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
<BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes<RESET>
<BOLD;RED>update - add working tree state to the staged set of changes<RESET>
<BOLD;RED>revert - revert staged set of changes back to the HEAD version<RESET>
<BOLD;RED>patch - pick hunks and update selectively<RESET>
<BOLD;RED>diff - view diff between HEAD and index<RESET>
<BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
<BOLD>*** Commands ***<RESET>
1: <BOLD;BLUE>s<RESET>tatus 2: <BOLD;BLUE>u<RESET>pdate 3: <BOLD;BLUE>r<RESET>evert 4: <BOLD;BLUE>a<RESET>dd untracked
5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
<BOLD;BLUE>What now<RESET>>$SP
Bye.
EOF
test_write_lines h | force_color git add -i >actual.colored &&
test_decode_color <actual.colored >actual &&
test_cmp expect actual
'
test_expect_success 'reset -p with unmerged files' '
test_when_finished "git checkout --force main" &&
test_commit one conflict &&
git checkout -B side HEAD^ &&
test_commit two conflict &&
test_must_fail git merge one &&
# this is a noop with only an unmerged entry
git reset -p &&
# add files that sort before and after unmerged entry
echo a >a &&
echo z >z &&
git add a z &&
# confirm that we can reset those files
printf "%s\n" y y | git reset -p &&
git diff-index --cached --diff-filter=u HEAD >staged &&
test_must_be_empty staged
'
test_expect_success 'hunk splitting works with diff.suppressBlankEmpty' '
test_config diff.suppressBlankEmpty true &&
write_script fake-editor.sh <<-\EOF &&
tr F G <"$1" >"$1.tmp" &&
mv "$1.tmp" "$1"
EOF
test_write_lines a b "" c d "" e f "" >file &&
git add file &&
test_write_lines A b "" c D "" e F "" >file &&
(
test_set_editor "$(pwd)/fake-editor.sh" &&
test_write_lines s n y e q | git add -p file
) &&
git cat-file blob :file >actual &&
test_write_lines a b "" c D "" e G "" >expect &&
test_cmp expect actual
'
test_done
|