3 test_description
='add -i basic tests'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 TEST_PASSES_SANITIZE_LEAK
=true
9 .
"$TEST_DIRECTORY"/lib-terminal.sh
16 sed -e '/^index/s/[0-9a-f]*[1-9a-f][0-9a-f]*\.\./1234567../' \
17 -e '/^index/s/\.\.[0-9a-f]*[1-9a-f][0-9a-f]*/..9abcdef/' \
18 -e '/^index/s/ 00*\.\./ 0000000../' \
19 -e '/^index/s/\.\.00*$/..0000000/' \
20 -e '/^index/s/\.\.00* /..0000000 /' \
23 test_cmp
"$1.filtered" "$2.filtered"
26 # This function uses a trick to manipulate the interactive add to use color:
27 # the `want_color()` function special-cases the situation where a pager was
28 # spawned and Git now wants to output colored text: to detect that situation,
29 # the environment variable `GIT_PAGER_IN_USE` is set. However, color is
30 # suppressed despite that environment variable if the `TERM` variable
31 # indicates a dumb terminal, so we set that variable, too.
34 # The first element of $@ may be a shell function, as a result POSIX
35 # does not guarantee that "one-shot assignment" will not persist after
36 # the function call. Thus, we prevent these variables from escaping
37 # this function's context with this subshell.
39 GIT_PAGER_IN_USE
=true
&&
41 export GIT_PAGER_IN_USE TERM
&&
46 test_expect_success
'unknown command' '
47 test_when_finished "git reset --hard; rm -f command" &&
50 git diff command >expect &&
51 cat >>expect <<-EOF &&
52 (1/1) Stage addition [y,n,q,a,d,e,p,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help)
53 (1/1) Stage addition [y,n,q,a,d,e,p,?]?$SP
55 git add -p -- command <command >actual 2>&1 &&
56 test_cmp expect actual
59 test_expect_success
'setup (initial)' '
65 test_expect_success
'status works (initial)' '
66 git add -i </dev/null >output &&
67 grep "+1/-0 *+2/-0 file" output
70 test_expect_success
'setup expected' '
73 index 0000000..d95f3ad
81 test_expect_success
'diff works (initial)' '
82 test_write_lines d 1 | git add -i >output &&
83 sed -ne "/new file/,/content/p" <output >diff &&
84 diff_cmp expected diff
86 test_expect_success
'revert works (initial)' '
88 test_write_lines r 1 | git add -i &&
89 git ls-files >output &&
93 test_expect_success
'add untracked (multiple)' '
94 test_when_finished "git reset && rm [1-9]" &&
95 touch $(test_seq 9) &&
96 test_write_lines a "2-5 8-" | git add -i -- [1-9] &&
97 test_write_lines 2 3 4 5 8 9 >expected &&
98 git ls-files [1-9] >output &&
99 test_cmp expected output
102 test_expect_success
'setup (commit)' '
103 echo baseline >file &&
105 git commit -m commit &&
106 echo content >>file &&
111 test_expect_success
'status works (commit)' '
112 git add -i </dev/null >output &&
113 grep "+1/-0 *+2/-0 file" output
116 test_expect_success
'update can stage deletions' '
120 test_write_lines u t "" | git add -i &&
121 git ls-files to-delete >output &&
122 test_must_be_empty output
125 test_expect_success
'setup expected' '
126 cat >expected <<-\EOF
127 index 180b47c..b6f2c08 100644
136 test_expect_success
'diff works (commit)' '
137 test_write_lines d 1 | git add -i >output &&
138 sed -ne "/^index/,/content/p" <output >diff &&
139 diff_cmp expected diff
141 test_expect_success
'revert works (commit)' '
143 test_write_lines r 1 | git add -i &&
144 git add -i </dev/null >output &&
145 grep "unchanged *+3/-0 file" output
148 test_expect_success
'reject multi-key input' '
149 saved=$(git hash-object -w file) &&
150 test_when_finished "git cat-file blob $saved >file" &&
151 echo an extra line >>file &&
152 test_write_lines aa | git add -p >actual &&
153 test_grep "is expected, got ${SQ}aa${SQ}" actual
156 test_expect_success
'setup expected' '
157 cat >expected <<-\EOF
161 test_expect_success
'dummy edit works' '
163 test_write_lines e a | git add -p &&
165 diff_cmp expected diff
168 test_expect_success
'setup patch' '
178 test_expect_success
'setup fake editor' '
179 write_script "fake_editor.sh" <<-\EOF &&
180 mv -f "$1" oldpatch &&
183 test_set_editor "$(pwd)/fake_editor.sh"
186 test_expect_success
'bad edit rejected' '
188 test_write_lines e n d | git add -p >output &&
189 grep "hunk does not apply" output
192 test_expect_success
'setup patch' '
199 test_expect_success
'garbage edit rejected' '
201 test_write_lines e n d | git add -p >output &&
202 grep "hunk does not apply" output
205 test_expect_success
'setup patch' '
215 test_expect_success
'setup expected' '
216 cat >expected <<-\EOF
217 diff --git a/file b/file
218 index b5dd6c9..f910ae9 100644
230 test_expect_success
'real edit works' '
231 test_write_lines e n d | git add -p &&
233 diff_cmp expected output
236 test_expect_success
'setup file' '
237 test_write_lines a "" b "" c >file &&
239 test_write_lines a "" d "" c >file
242 test_expect_success
'setup patch' '
255 test_expect_success
'setup expected' '
257 diff --git a/file b/file
258 index b5dd6c9..f910ae9 100644
271 test_expect_success
'edit can strip spaces from empty context lines' '
272 test_write_lines e n q | git add -p 2>error &&
273 test_must_be_empty error &&
275 diff_cmp expected output
278 test_expect_success
'skip files similarly as commit -a' '
280 echo file >.gitignore &&
281 echo changed >file &&
282 echo y | git add -p file &&
285 git commit -am commit &&
286 git diff >expected &&
287 diff_cmp expected output &&
288 git reset --hard HEAD^
292 test_expect_success FILEMODE
'patch does not affect mode' '
294 echo content >>file &&
296 printf "n\\ny\\n" | git add -p &&
297 git show :file | grep content &&
298 git diff file | grep "new mode"
301 test_expect_success FILEMODE
'stage mode but not hunk' '
303 echo content >>file &&
305 printf "y\\nn\\n" | git add -p &&
306 git diff --cached file | grep "new mode" &&
307 git diff file | grep "+content"
311 test_expect_success FILEMODE
'stage mode and hunk' '
313 echo content >>file &&
315 printf "y\\ny\\n" | git add -p &&
316 git diff --cached file >out &&
317 grep "new mode" out &&
318 grep "+content" out &&
319 git diff file >out &&
320 test_must_be_empty out
323 # end of tests disabled when filemode is not usable
325 test_expect_success
'different prompts for mode change/deleted' '
329 git add --chmod=+x file deleted &&
330 echo changed >file &&
332 test_write_lines n n n |
333 git -c core.filemode=true add -p >actual &&
334 sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
335 cat >expect <<-\EOF &&
336 (1/1) Stage deletion [y,n,q,a,d,p,?]?
337 (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,p,?]?
338 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]?
340 test_cmp expect actual.filtered
343 test_expect_success
'correct message when there is nothing to do' '
346 test_grep "No changes" out &&
347 printf "\\0123" >binary &&
349 printf "\\0abc" >binary &&
351 test_grep "Only binary files changed" out
354 test_expect_success
'setup again' '
356 test_chmod +x file &&
357 echo content >>file &&
358 test_write_lines A B C D>file2 &&
362 # Write the patch file with a new line at the top and bottom
363 test_expect_success
'setup patch' '
365 index 180b47c..b6f2c08 100644
373 \ No newline at end of file
374 diff --git a/file2 b/file2
375 index 8422d40..35b930a 100644
389 # Expected output, diff is similar to the patch but w/ diff at the top
390 test_expect_success
'setup expected' '
391 echo diff --git a/file b/file >expected &&
392 sed -e "/^index 180b47c/s/ 100644/ 100755/" \
394 -e /Y/d patch >>expected &&
395 cat >expected-output <<-\EOF
403 \ No newline at end of file
412 \ No newline at end of file
438 # Test splitting the first patch, then adding both
439 test_expect_success
'add first line works' '
440 git commit -am "clear local changes" &&
442 test_write_lines s y y s y n y | git add -p 2>error >raw-output &&
443 sed -n -e "s/^([1-9]\/[1-9]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
444 -e "/^[-+@ \\\\]"/p raw-output >output &&
445 test_must_be_empty error &&
446 git diff --cached >diff &&
447 diff_cmp expected diff &&
448 test_cmp expected-output output
451 test_expect_success
'setup expected' '
452 cat >expected <<-\EOF
453 diff --git a/non-empty b/non-empty
454 deleted file mode 100644
455 index d95f3ad..0000000
463 test_expect_success
'deleting a non-empty file' '
465 echo content >non-empty &&
467 git commit -m non-empty &&
469 echo y | git add -p non-empty &&
470 git diff --cached >diff &&
471 diff_cmp expected diff
474 test_expect_success
'setup expected' '
475 cat >expected <<-\EOF
476 diff --git a/empty b/empty
477 deleted file mode 100644
478 index e69de29..0000000
482 test_expect_success
'deleting an empty file' '
486 git commit -m empty &&
488 echo y | git add -p empty &&
489 git diff --cached >diff &&
490 diff_cmp expected diff
493 test_expect_success
'adding an empty file' '
497 test_commit initial &&
501 git commit -m empty &&
502 git tag added-file &&
503 git reset --hard HEAD^ &&
504 test_path_is_missing empty &&
506 echo y | git checkout -p added-file -- >actual &&
507 test_path_is_file empty &&
508 test_grep "Apply addition to index and worktree" actual
512 test_expect_success
'split hunk setup' '
514 test_write_lines 10 20 30 40 50 60 >test &&
517 git commit -m test &&
519 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
522 test_expect_success
'goto hunk 1 with "g 1"' '
523 test_when_finished "git reset" &&
524 tr _ " " >expect <<-EOF &&
525 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + 1: -1,2 +1,3 +15
527 go to which hunk? @@ -1,2 +1,3 @@
531 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
533 test_write_lines s y g 1 | git add -p >actual &&
534 tail -n 7 <actual >actual.trimmed &&
535 test_cmp expect actual.trimmed
538 test_expect_success
'goto hunk 1 with "g1"' '
539 test_when_finished "git reset" &&
540 tr _ " " >expect <<-EOF &&
544 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
546 test_write_lines s y g1 | git add -p >actual &&
547 tail -n 4 <actual >actual.trimmed &&
548 test_cmp expect actual.trimmed
551 test_expect_success
'navigate to hunk via regex /pattern' '
552 test_when_finished "git reset" &&
553 tr _ " " >expect <<-EOF &&
554 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -1,2 +1,3 @@
558 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
560 test_write_lines s y /1,2 | git add -p >actual &&
561 tail -n 5 <actual >actual.trimmed &&
562 test_cmp expect actual.trimmed
565 test_expect_success
'navigate to hunk via regex / pattern' '
566 test_when_finished "git reset" &&
567 tr _ " " >expect <<-EOF &&
571 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
573 test_write_lines s y / 1,2 | git add -p >actual &&
574 tail -n 4 <actual >actual.trimmed &&
575 test_cmp expect actual.trimmed
578 test_expect_success
'print again the hunk' '
579 test_when_finished "git reset" &&
580 tr _ " " >expect <<-EOF &&
583 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? @@ -1,2 +1,3 @@
587 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
589 test_write_lines s y g 1 p | git add -p >actual &&
590 tail -n 7 <actual >actual.trimmed &&
591 test_cmp expect actual.trimmed
594 test_expect_success TTY
'print again the hunk (PAGER)' '
595 test_when_finished "git reset" &&
596 cat >expect <<-EOF &&
597 <GREEN>+<RESET><GREEN>15<RESET>
599 <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>
601 PAGER <GREEN>+<RESET><GREEN>15<RESET>
603 <BOLD;BLUE>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
605 test_write_lines s y g 1 P |
607 GIT_PAGER="sed s/^/PAGER\ /" &&
609 test_terminal git add -p >actual
611 tail -n 7 <actual | test_decode_color >actual.trimmed &&
612 test_cmp expect actual.trimmed
615 test_expect_success TTY
'P handles SIGPIPE when writing to pager' '
616 test_when_finished "rm -f huge_file; git reset" &&
617 printf "\n%2500000s" Y >huge_file &&
618 git add -N huge_file &&
619 test_write_lines P q | (
620 GIT_PAGER="head -n 1" &&
622 test_terminal git add -p
626 test_expect_success
'split hunk "add -p (edit)"' '
627 # Split, say Edit and do nothing. Then:
629 # 1. Broken version results in a patch that does not apply and
630 # only takes [y/n] (edit again) so the first q is discarded
631 # and then n attempts to discard the edit. Repeat q enough
634 # 2. Correct version applies the (not)edited version, and asks
635 # about the next hunk, against which we say q and program
637 printf "%s\n" s e q n q q |
638 EDITOR=: git add -p &&
643 test_expect_success
'split hunk "add -p (no, yes, edit)"' '
644 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
646 # test sequence is s(plit), n(o), y(es), e(dit)
647 # q n q q is there to make sure we exit at the end.
648 printf "%s\n" s n y e q n q q |
649 EDITOR=: git add -p 2>error &&
650 test_must_be_empty error &&
655 test_expect_success
'split hunk with incomplete line at end' '
657 printf "missing LF" >>test &&
659 test_write_lines before 10 20 30 40 50 60 70 >test &&
660 git grep --cached missing &&
661 test_write_lines s n y q | git add -p &&
662 test_must_fail git grep --cached missing &&
664 test_must_fail git grep --cached before
667 test_expect_success
'edit, adding lines to the first hunk' '
668 test_write_lines 10 11 20 30 40 50 51 60 >test &&
670 tr _ " " >patch <<-EOF &&
680 # test sequence is s(plit), e(dit), n(o)
681 # q n q q is there to make sure we exit at the end.
682 printf "%s\n" s e n q n q q |
683 EDITOR=./fake_editor.sh git add -p 2>error &&
684 test_must_be_empty error &&
685 git diff --cached >actual &&
689 test_expect_success
'patch mode ignores unmerged entries' '
691 test_commit conflict &&
692 test_commit non-conflict &&
693 git checkout -b side &&
694 test_commit side conflict.t &&
696 test_commit main conflict.t &&
697 test_must_fail git merge side &&
698 echo changed >non-conflict.t &&
699 echo y | git add -p >output &&
700 ! grep a/conflict.t output &&
701 cat >expected <<-\EOF &&
702 * Unmerged path conflict.t
703 diff --git a/non-conflict.t b/non-conflict.t
704 index f766221..5ea2ed4 100644
711 git diff --cached >diff &&
712 diff_cmp expected diff
715 test_expect_success
'index is refreshed after applying patch' '
717 echo content >test &&
718 printf y | git add -p &&
719 git diff-files --exit-code
722 test_expect_success
'diffs can be colorized' '
725 echo content >test &&
727 force_color git add -p >output 2>&1 <y &&
728 git diff-files --exit-code &&
730 # We do not want to depend on the exact coloring scheme
731 # git uses for diffs, so just check that we saw some kind of color.
732 grep "$(printf "\\033")" output
735 test_expect_success
'colors can be overridden' '
737 test_when_finished "git rm -f color-test" &&
738 test_write_lines context old more-context >color-test &&
739 git add color-test &&
740 test_write_lines context new more-context another-one >color-test &&
742 echo trigger an error message >input &&
744 -c color.interactive.error=blue \
745 add -i 2>err.raw <input &&
746 test_decode_color <err.raw >err &&
747 grep "<BLUE>Huh (trigger)?<RESET>" err &&
749 test_write_lines help quit >input &&
751 -c color.interactive.header=red \
752 -c color.interactive.help=green \
753 -c color.interactive.prompt=yellow \
754 add -i >actual.raw <input &&
755 test_decode_color <actual.raw >actual &&
756 cat >expect <<-\EOF &&
757 <RED> staged unstaged path<RESET>
758 1: +3/-0 +2/-1 color-test
760 <RED>*** Commands ***<RESET>
761 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
762 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
763 <YELLOW>What now<RESET>> <GREEN>status - show paths with changes<RESET>
764 <GREEN>update - add working tree state to the staged set of changes<RESET>
765 <GREEN>revert - revert staged set of changes back to the HEAD version<RESET>
766 <GREEN>patch - pick hunks and update selectively<RESET>
767 <GREEN>diff - view diff between HEAD and index<RESET>
768 <GREEN>add untracked - add contents of untracked files to the staged set of changes<RESET>
769 <RED>*** Commands ***<RESET>
770 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
771 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
772 <YELLOW>What now<RESET>> Bye.
774 test_cmp expect actual &&
776 : exercise recolor_hunk by editing and then look at the hunk again &&
777 test_write_lines s e K q >input &&
779 -c color.interactive.prompt=yellow \
780 -c color.diff.meta=italic \
781 -c color.diff.frag=magenta \
782 -c color.diff.context=cyan \
783 -c color.diff.old=bold \
784 -c color.diff.new=blue \
785 -c core.editor=touch \
786 add -p >actual.raw <input &&
787 test_decode_color <actual.raw >actual.decoded &&
788 sed "s/index [0-9a-f]*\\.\\.[0-9a-f]* 100644/<INDEX-LINE>/" <actual.decoded >actual &&
789 cat >expect <<-\EOF &&
790 <ITALIC>diff --git a/color-test b/color-test<RESET>
791 <ITALIC><INDEX-LINE><RESET>
792 <ITALIC>--- a/color-test<RESET>
793 <ITALIC>+++ b/color-test<RESET>
794 <MAGENTA>@@ -1,3 +1,4 @@<RESET>
795 <CYAN> context<RESET>
797 <BLUE>+<RESET><BLUE>new<RESET>
798 <CYAN> more-context<RESET>
799 <BLUE>+<RESET><BLUE>another-one<RESET>
800 <YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,p,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
801 <MAGENTA>@@ -1,3 +1,3 @@<RESET>
802 <CYAN> context<RESET>
804 <BLUE>+<RESET><BLUE>new<RESET>
805 <CYAN> more-context<RESET>
806 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
807 <CYAN> more-context<RESET>
808 <BLUE>+<RESET><BLUE>another-one<RESET>
809 <YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
810 <CYAN> context<RESET>
813 <CYAN> more-context<RESET>
814 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
816 test_cmp expect actual
819 test_expect_success
'brackets appear without color' '
821 test_when_finished "git rm -f bracket-test" &&
822 test_write_lines context old more-context >bracket-test &&
823 git add bracket-test &&
824 test_write_lines context new more-context another-one >bracket-test &&
826 test_write_lines quit >input &&
827 git add -i >actual <input &&
829 sed "s/^|//" >expect <<-\EOF &&
830 | staged unstaged path
831 | 1: +3/-0 +2/-1 bracket-test
834 | 1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
835 | 5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
839 test_cmp expect actual
842 test_expect_success
'colors can be skipped with color.ui=false' '
844 test_when_finished "git rm -f color-test" &&
845 test_write_lines context old more-context >color-test &&
846 git add color-test &&
847 test_write_lines context new more-context another-one >color-test &&
849 test_write_lines help quit >input &&
852 add -i >actual.raw <input &&
853 test_decode_color <actual.raw >actual &&
854 test_cmp actual.raw actual
857 test_expect_success
'colorized diffs respect diff.wsErrorHighlight' '
865 force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
866 test_decode_color <output.raw >output &&
870 test_expect_success
'diffFilter filters diff' '
873 echo content >test &&
874 test_config interactive.diffFilter "sed s/^/foo:/" &&
876 force_color git add -p >output 2>&1 <y &&
878 # avoid depending on the exact coloring or content of the prompts,
879 # and just make sure we saw our diff prefixed
880 grep foo:.*content output
883 test_expect_success
'detect bogus diffFilter output' '
886 echo content >test &&
887 test_config interactive.diffFilter "sed 6d" &&
889 force_color test_must_fail git add -p <y >output 2>&1 &&
890 grep "mismatched output" output
893 test_expect_success
'handle iffy colored hunk headers' '
896 echo content >test &&
898 force_color git -c interactive.diffFilter="sed s/.*@@.*/XX/" \
899 add -p >output 2>&1 <n &&
903 test_expect_success
'handle very large filtered diff' '
905 # The specific number here is not important, but it must
906 # be large enough that the output of "git diff --color"
907 # fills up the pipe buffer. 10,000 results in ~200k of
909 test_seq 10000 >test &&
910 test_config interactive.diffFilter cat &&
912 force_color git add -p >output 2>&1 <y &&
913 git diff-files --exit-code -- test
916 test_expect_success
'diff.algorithm is passed to `git diff-files`' '
921 echo changed >file &&
922 test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
923 test_grep "error: option diff-algorithm accepts " err
926 test_expect_success
'patch-mode via -i prompts for files' '
931 git add -i <<-\EOF &&
940 git diff --cached --name-only >actual &&
941 diff_cmp expect actual
944 test_expect_success
'add -p handles globs' '
949 echo base >subdir/two.c &&
951 git commit -m base &&
953 echo change >one.c &&
954 echo change >subdir/two.c &&
955 git add -p "*.c" <<-\EOF &&
960 cat >expect <<-\EOF &&
964 git diff --cached --name-only >actual &&
965 test_cmp expect actual
968 test_expect_success
'add -p handles relative paths' '
971 echo base >relpath.c &&
973 git commit -m relpath &&
975 echo change >relpath.c &&
977 git -C subdir add -p .. 2>error <<-\EOF &&
981 test_must_be_empty error &&
983 cat >expect <<-\EOF &&
986 git diff --cached --name-only >actual &&
987 test_cmp expect actual
990 test_expect_success
'add -p does not expand argument lists' '
993 echo content >not-changed &&
994 git add not-changed &&
995 git commit -m "add not-changed file" &&
998 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
1002 # we know that "file" must be mentioned since we actually
1003 # update it, but we want to be sure that our "." pathspec
1004 # was not expanded into the argument list of any command.
1005 # So look only for "not-changed".
1006 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
1009 test_expect_success
'hunk-editing handles custom comment char' '
1011 echo change >>file &&
1012 test_config core.commentChar "\$" &&
1013 echo e | GIT_EDITOR=true git add -p &&
1014 git diff --exit-code
1017 test_expect_success
'add -p works even with color.ui=always' '
1019 echo change >>file &&
1020 test_config color.ui always &&
1021 echo y | git add -p &&
1022 echo file >expect &&
1023 git diff --cached --name-only >actual &&
1024 test_cmp expect actual
1027 test_expect_success
'setup different kinds of dirty submodules' '
1028 test_create_repo for-submodules &&
1030 cd for-submodules &&
1031 test_commit initial &&
1032 test_create_repo dirty-head &&
1037 cp -R dirty-head dirty-otherwise &&
1038 cp -R dirty-head dirty-both-ways &&
1039 git add dirty-head &&
1040 git add dirty-otherwise dirty-both-ways &&
1041 git commit -m initial &&
1044 test_commit updated &&
1045 cd ../dirty-both-ways &&
1046 test_commit updated &&
1047 echo dirty >>initial &&
1049 cd ../dirty-otherwise &&
1050 echo dirty >>initial &&
1053 git -C for-submodules diff-files --name-only >actual &&
1054 cat >expected <<-\EOF &&
1058 test_cmp expected actual &&
1059 git -C for-submodules diff-files --name-only --ignore-submodules=none >actual &&
1060 cat >expected <<-\EOF &&
1065 test_cmp expected actual &&
1066 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
1067 cat >expected <<-\EOF &&
1071 test_cmp expected actual
1074 test_expect_success
'status ignores dirty submodules (except HEAD)' '
1075 git -C for-submodules add -i </dev/null >output &&
1076 grep dirty-head output &&
1077 grep dirty-both-ways output &&
1078 ! grep dirty-otherwise output
1081 test_expect_success
'handle submodules' '
1082 echo 123 >>for-submodules/dirty-otherwise/initial.t &&
1084 force_color git -C for-submodules add -p dirty-otherwise >output 2>&1 &&
1085 grep "No changes" output &&
1087 force_color git -C for-submodules add -p dirty-head >output 2>&1 <y &&
1088 git -C for-submodules ls-files --stage dirty-head >actual &&
1089 rev="$(git -C for-submodules/dirty-head rev-parse HEAD)" &&
1093 test_expect_success
'set up pathological context' '
1095 test_write_lines a a a a a a a a a a a >a &&
1098 test_write_lines c b a a a a a a a b a a a a >a &&
1099 test_write_lines a a a a a a a b a a a a >expected-1 &&
1100 test_write_lines b a a a a a a a b a a a a >expected-2 &&
1101 # check editing can cope with missing header and deleted context lines
1102 # as well as changes to other lines
1103 test_write_lines +b " a" >patch
1106 test_expect_success
'add -p works with pathological context lines' '
1110 git cat-file blob :a >actual &&
1111 test_cmp expected-1 actual
1114 test_expect_success
'add -p patch editing works with pathological context lines' '
1116 # n q q below is in case edit fails
1117 printf "%s\n" e y n q q |
1119 git cat-file blob :a >actual &&
1120 test_cmp expected-2 actual
1123 test_expect_success
'checkout -p works with pathological context lines' '
1124 test_write_lines a a a a a a >a &&
1126 test_write_lines a b a b a b a b a b a >a &&
1127 test_write_lines s n n y q | git checkout -p &&
1128 test_write_lines a b a b a a b a b a >expect &&
1132 # This should be called from a subshell as it sets a temporary editor
1134 write_script new-file-editor.sh
<<-\EOF &&
1135 sed /^#/d "$1" >patch &&
1136 sed /^+c/d patch >"$1"
1138 test_set_editor "$(pwd)/new-file-editor.sh" &&
1139 test_write_lines a b c d e f >new-file &&
1140 test_write_lines a b d e f >new-file-expect &&
1141 test_write_lines "@@ -0,0 +1,6 @@" +a +b +c +d +e +f >patch-expect
1144 test_expect_success 'add -N followed by add -p patch editing' '
1148 git add -N new-file &&
1149 test_write_lines e n q | git add -p &&
1150 git cat-file blob :new-file >actual &&
1151 test_cmp new-file-expect actual &&
1152 test_cmp patch-expect patch
1156 test_expect_success 'checkout -p patch editing of added file' '
1161 git commit -m "add new file" &&
1163 git commit -m "remove new file" &&
1164 test_write_lines e n q | git checkout -p HEAD^ &&
1165 test_cmp new-file-expect new-file &&
1166 test_cmp patch-expect patch
1170 test_expect_success 'show help from add--helper' '
1172 cat >expect <<-EOF &&
1174 <BOLD>*** Commands ***<RESET>
1175 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
1176 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1177 <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes
<RESET
>
1178 <BOLD
;RED
>update
- add working tree state to the staged
set of changes
<RESET
>
1179 <BOLD
;RED
>revert
- revert staged
set of changes back to the HEAD version
<RESET
>
1180 <BOLD
;RED
>patch - pick hunks and update selectively
<RESET
>
1181 <BOLD
;RED
>diff - view
diff between HEAD and index
<RESET
>
1182 <BOLD
;RED
>add untracked
- add contents of untracked files to the staged
set of changes
<RESET
>
1183 <BOLD
>*** Commands
***<RESET
>
1184 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
1185 5: <BOLD
;BLUE
>p
<RESET
>atch
6: <BOLD
;BLUE
>d
<RESET
>iff
7: <BOLD
;BLUE
>q
<RESET
>uit
8: <BOLD
;BLUE
>h
<RESET
>elp
1186 <BOLD
;BLUE
>What now
<RESET
>>$SP
1189 test_write_lines h | force_color git add
-i >actual.colored
&&
1190 test_decode_color
<actual.colored
>actual
&&
1191 test_cmp expect actual
1194 test_expect_success 'reset -p with unmerged files
' '
1195 test_when_finished
"git checkout --force main" &&
1196 test_commit one conflict
&&
1197 git checkout
-B side HEAD^
&&
1198 test_commit two conflict
&&
1199 test_must_fail git merge one
&&
1201 # this is a noop with only an unmerged entry
1204 # add files that sort before and after unmerged entry
1209 # confirm that we can reset those files
1210 printf "%s\n" y y | git
reset -p &&
1211 git diff-index
--cached --diff-filter=u HEAD
>staged
&&
1212 test_must_be_empty staged
1215 test_expect_success 'hunk splitting works with
diff.suppressBlankEmpty
' '
1216 test_config
diff.suppressBlankEmpty true
&&
1217 write_script fake-editor.sh
<<-\EOF &&
1218 tr F G <"$1" >"$1.tmp" &&
1222 test_write_lines a b "" c d "" e f "" >file &&
1224 test_write_lines A b "" c D "" e F "" >file &&
1226 test_set_editor "$(pwd)/fake-editor.sh" &&
1227 test_write_lines s n y e q | git add -p file
1229 git cat-file blob :file >actual &&
1230 test_write_lines a b "" c D "" e G "" >expect &&
1231 test_cmp expect actual