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
'warn about add.interactive.useBuiltin' '
47 cat >expect <<-\EOF &&
48 warning: the add.interactive.useBuiltin setting has been removed!
49 See its entry in '\''git help config'\'' for details.
51 echo "No changes." >expect.out &&
53 for v in = =true =false
55 git -c "add.interactive.useBuiltin$v" add -p >out 2>actual &&
56 test_cmp expect.out out &&
57 test_cmp expect actual || return 1
61 test_expect_success
'unknown command' '
62 test_when_finished "git reset --hard; rm -f command" &&
65 git diff command >expect &&
66 cat >>expect <<-EOF &&
67 (1/1) Stage addition [y,n,q,a,d,e,p,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help)
68 (1/1) Stage addition [y,n,q,a,d,e,p,?]?$SP
70 git add -p -- command <command >actual 2>&1 &&
71 test_cmp expect actual
74 test_expect_success
'setup (initial)' '
80 test_expect_success
'status works (initial)' '
81 git add -i </dev/null >output &&
82 grep "+1/-0 *+2/-0 file" output
85 test_expect_success
'setup expected' '
88 index 0000000..d95f3ad
96 test_expect_success
'diff works (initial)' '
97 test_write_lines d 1 | git add -i >output &&
98 sed -ne "/new file/,/content/p" <output >diff &&
99 diff_cmp expected diff
101 test_expect_success
'revert works (initial)' '
103 test_write_lines r 1 | git add -i &&
104 git ls-files >output &&
108 test_expect_success
'add untracked (multiple)' '
109 test_when_finished "git reset && rm [1-9]" &&
110 touch $(test_seq 9) &&
111 test_write_lines a "2-5 8-" | git add -i -- [1-9] &&
112 test_write_lines 2 3 4 5 8 9 >expected &&
113 git ls-files [1-9] >output &&
114 test_cmp expected output
117 test_expect_success
'setup (commit)' '
118 echo baseline >file &&
120 git commit -m commit &&
121 echo content >>file &&
126 test_expect_success
'status works (commit)' '
127 git add -i </dev/null >output &&
128 grep "+1/-0 *+2/-0 file" output
131 test_expect_success
'update can stage deletions' '
135 test_write_lines u t "" | git add -i &&
136 git ls-files to-delete >output &&
137 test_must_be_empty output
140 test_expect_success
'setup expected' '
141 cat >expected <<-\EOF
142 index 180b47c..b6f2c08 100644
151 test_expect_success
'diff works (commit)' '
152 test_write_lines d 1 | git add -i >output &&
153 sed -ne "/^index/,/content/p" <output >diff &&
154 diff_cmp expected diff
156 test_expect_success
'revert works (commit)' '
158 test_write_lines r 1 | git add -i &&
159 git add -i </dev/null >output &&
160 grep "unchanged *+3/-0 file" output
163 test_expect_success
'setup expected' '
164 cat >expected <<-\EOF
168 test_expect_success
'dummy edit works' '
170 test_write_lines e a | git add -p &&
172 diff_cmp expected diff
175 test_expect_success
'setup patch' '
185 test_expect_success
'setup fake editor' '
186 write_script "fake_editor.sh" <<-\EOF &&
187 mv -f "$1" oldpatch &&
190 test_set_editor "$(pwd)/fake_editor.sh"
193 test_expect_success
'bad edit rejected' '
195 test_write_lines e n d | git add -p >output &&
196 grep "hunk does not apply" output
199 test_expect_success
'setup patch' '
206 test_expect_success
'garbage edit rejected' '
208 test_write_lines e n d | git add -p >output &&
209 grep "hunk does not apply" output
212 test_expect_success
'setup patch' '
222 test_expect_success
'setup expected' '
223 cat >expected <<-\EOF
224 diff --git a/file b/file
225 index b5dd6c9..f910ae9 100644
237 test_expect_success
'real edit works' '
238 test_write_lines e n d | git add -p &&
240 diff_cmp expected output
243 test_expect_success
'setup file' '
244 test_write_lines a "" b "" c >file &&
246 test_write_lines a "" d "" c >file
249 test_expect_success
'setup patch' '
262 test_expect_success
'setup expected' '
264 diff --git a/file b/file
265 index b5dd6c9..f910ae9 100644
278 test_expect_success
'edit can strip spaces from empty context lines' '
279 test_write_lines e n q | git add -p 2>error &&
280 test_must_be_empty error &&
282 diff_cmp expected output
285 test_expect_success
'skip files similarly as commit -a' '
287 echo file >.gitignore &&
288 echo changed >file &&
289 echo y | git add -p file &&
292 git commit -am commit &&
293 git diff >expected &&
294 diff_cmp expected output &&
295 git reset --hard HEAD^
299 test_expect_success FILEMODE
'patch does not affect mode' '
301 echo content >>file &&
303 printf "n\\ny\\n" | git add -p &&
304 git show :file | grep content &&
305 git diff file | grep "new mode"
308 test_expect_success FILEMODE
'stage mode but not hunk' '
310 echo content >>file &&
312 printf "y\\nn\\n" | git add -p &&
313 git diff --cached file | grep "new mode" &&
314 git diff file | grep "+content"
318 test_expect_success FILEMODE
'stage mode and hunk' '
320 echo content >>file &&
322 printf "y\\ny\\n" | git add -p &&
323 git diff --cached file >out &&
324 grep "new mode" out &&
325 grep "+content" out &&
326 git diff file >out &&
327 test_must_be_empty out
330 # end of tests disabled when filemode is not usable
332 test_expect_success
'different prompts for mode change/deleted' '
336 git add --chmod=+x file deleted &&
337 echo changed >file &&
339 test_write_lines n n n |
340 git -c core.filemode=true add -p >actual &&
341 sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
342 cat >expect <<-\EOF &&
343 (1/1) Stage deletion [y,n,q,a,d,p,?]?
344 (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,p,?]?
345 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]?
347 test_cmp expect actual.filtered
350 test_expect_success
'correct message when there is nothing to do' '
353 test_grep "No changes" out &&
354 printf "\\0123" >binary &&
356 printf "\\0abc" >binary &&
358 test_grep "Only binary files changed" out
361 test_expect_success
'setup again' '
363 test_chmod +x file &&
364 echo content >>file &&
365 test_write_lines A B C D>file2 &&
369 # Write the patch file with a new line at the top and bottom
370 test_expect_success
'setup patch' '
372 index 180b47c..b6f2c08 100644
380 \ No newline at end of file
381 diff --git a/file2 b/file2
382 index 8422d40..35b930a 100644
396 # Expected output, diff is similar to the patch but w/ diff at the top
397 test_expect_success
'setup expected' '
398 echo diff --git a/file b/file >expected &&
399 sed -e "/^index 180b47c/s/ 100644/ 100755/" \
401 -e /Y/d patch >>expected &&
402 cat >expected-output <<-\EOF
410 \ No newline at end of file
419 \ No newline at end of file
445 # Test splitting the first patch, then adding both
446 test_expect_success
'add first line works' '
447 git commit -am "clear local changes" &&
449 test_write_lines s y y s y n y | git add -p 2>error >raw-output &&
450 sed -n -e "s/^([1-9]\/[1-9]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
451 -e "/^[-+@ \\\\]"/p raw-output >output &&
452 test_must_be_empty error &&
453 git diff --cached >diff &&
454 diff_cmp expected diff &&
455 test_cmp expected-output output
458 test_expect_success
'setup expected' '
459 cat >expected <<-\EOF
460 diff --git a/non-empty b/non-empty
461 deleted file mode 100644
462 index d95f3ad..0000000
470 test_expect_success
'deleting a non-empty file' '
472 echo content >non-empty &&
474 git commit -m non-empty &&
476 echo y | git add -p non-empty &&
477 git diff --cached >diff &&
478 diff_cmp expected diff
481 test_expect_success
'setup expected' '
482 cat >expected <<-\EOF
483 diff --git a/empty b/empty
484 deleted file mode 100644
485 index e69de29..0000000
489 test_expect_success
'deleting an empty file' '
493 git commit -m empty &&
495 echo y | git add -p empty &&
496 git diff --cached >diff &&
497 diff_cmp expected diff
500 test_expect_success
'adding an empty file' '
504 test_commit initial &&
508 git commit -m empty &&
509 git tag added-file &&
510 git reset --hard HEAD^ &&
511 test_path_is_missing empty &&
513 echo y | git checkout -p added-file -- >actual &&
514 test_path_is_file empty &&
515 test_grep "Apply addition to index and worktree" actual
519 test_expect_success
'split hunk setup' '
521 test_write_lines 10 20 30 40 50 60 >test &&
524 git commit -m test &&
526 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
529 test_expect_success
'goto hunk' '
530 test_when_finished "git reset" &&
531 tr _ " " >expect <<-EOF &&
532 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + 1: -1,2 +1,3 +15
534 go to which hunk? @@ -1,2 +1,3 @@
538 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
540 test_write_lines s y g 1 | git add -p >actual &&
541 tail -n 7 <actual >actual.trimmed &&
542 test_cmp expect actual.trimmed
545 test_expect_success
'navigate to hunk via regex' '
546 test_when_finished "git reset" &&
547 tr _ " " >expect <<-EOF &&
548 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -1,2 +1,3 @@
552 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
554 test_write_lines s y /1,2 | git add -p >actual &&
555 tail -n 5 <actual >actual.trimmed &&
556 test_cmp expect actual.trimmed
559 test_expect_success
'split hunk "add -p (edit)"' '
560 # Split, say Edit and do nothing. Then:
562 # 1. Broken version results in a patch that does not apply and
563 # only takes [y/n] (edit again) so the first q is discarded
564 # and then n attempts to discard the edit. Repeat q enough
567 # 2. Correct version applies the (not)edited version, and asks
568 # about the next hunk, against which we say q and program
570 printf "%s\n" s e q n q q |
571 EDITOR=: git add -p &&
576 test_expect_success
'split hunk "add -p (no, yes, edit)"' '
577 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
579 # test sequence is s(plit), n(o), y(es), e(dit)
580 # q n q q is there to make sure we exit at the end.
581 printf "%s\n" s n y e q n q q |
582 EDITOR=: git add -p 2>error &&
583 test_must_be_empty error &&
588 test_expect_success
'split hunk with incomplete line at end' '
590 printf "missing LF" >>test &&
592 test_write_lines before 10 20 30 40 50 60 70 >test &&
593 git grep --cached missing &&
594 test_write_lines s n y q | git add -p &&
595 test_must_fail git grep --cached missing &&
597 test_must_fail git grep --cached before
600 test_expect_success
'edit, adding lines to the first hunk' '
601 test_write_lines 10 11 20 30 40 50 51 60 >test &&
603 tr _ " " >patch <<-EOF &&
613 # test sequence is s(plit), e(dit), n(o)
614 # q n q q is there to make sure we exit at the end.
615 printf "%s\n" s e n q n q q |
616 EDITOR=./fake_editor.sh git add -p 2>error &&
617 test_must_be_empty error &&
618 git diff --cached >actual &&
622 test_expect_success
'patch mode ignores unmerged entries' '
624 test_commit conflict &&
625 test_commit non-conflict &&
626 git checkout -b side &&
627 test_commit side conflict.t &&
629 test_commit main conflict.t &&
630 test_must_fail git merge side &&
631 echo changed >non-conflict.t &&
632 echo y | git add -p >output &&
633 ! grep a/conflict.t output &&
634 cat >expected <<-\EOF &&
635 * Unmerged path conflict.t
636 diff --git a/non-conflict.t b/non-conflict.t
637 index f766221..5ea2ed4 100644
644 git diff --cached >diff &&
645 diff_cmp expected diff
648 test_expect_success
'index is refreshed after applying patch' '
650 echo content >test &&
651 printf y | git add -p &&
652 git diff-files --exit-code
655 test_expect_success
'diffs can be colorized' '
658 echo content >test &&
660 force_color git add -p >output 2>&1 <y &&
661 git diff-files --exit-code &&
663 # We do not want to depend on the exact coloring scheme
664 # git uses for diffs, so just check that we saw some kind of color.
665 grep "$(printf "\\033")" output
668 test_expect_success
'colors can be overridden' '
670 test_when_finished "git rm -f color-test" &&
671 test_write_lines context old more-context >color-test &&
672 git add color-test &&
673 test_write_lines context new more-context another-one >color-test &&
675 echo trigger an error message >input &&
677 -c color.interactive.error=blue \
678 add -i 2>err.raw <input &&
679 test_decode_color <err.raw >err &&
680 grep "<BLUE>Huh (trigger)?<RESET>" err &&
682 test_write_lines help quit >input &&
684 -c color.interactive.header=red \
685 -c color.interactive.help=green \
686 -c color.interactive.prompt=yellow \
687 add -i >actual.raw <input &&
688 test_decode_color <actual.raw >actual &&
689 cat >expect <<-\EOF &&
690 <RED> staged unstaged path<RESET>
691 1: +3/-0 +2/-1 color-test
693 <RED>*** Commands ***<RESET>
694 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
695 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
696 <YELLOW>What now<RESET>> <GREEN>status - show paths with changes<RESET>
697 <GREEN>update - add working tree state to the staged set of changes<RESET>
698 <GREEN>revert - revert staged set of changes back to the HEAD version<RESET>
699 <GREEN>patch - pick hunks and update selectively<RESET>
700 <GREEN>diff - view diff between HEAD and index<RESET>
701 <GREEN>add untracked - add contents of untracked files to the staged set of changes<RESET>
702 <RED>*** Commands ***<RESET>
703 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
704 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
705 <YELLOW>What now<RESET>> Bye.
707 test_cmp expect actual &&
709 : exercise recolor_hunk by editing and then look at the hunk again &&
710 test_write_lines s e K q >input &&
712 -c color.interactive.prompt=yellow \
713 -c color.diff.meta=italic \
714 -c color.diff.frag=magenta \
715 -c color.diff.context=cyan \
716 -c color.diff.old=bold \
717 -c color.diff.new=blue \
718 -c core.editor=touch \
719 add -p >actual.raw <input &&
720 test_decode_color <actual.raw >actual.decoded &&
721 sed "s/index [0-9a-f]*\\.\\.[0-9a-f]* 100644/<INDEX-LINE>/" <actual.decoded >actual &&
722 cat >expect <<-\EOF &&
723 <ITALIC>diff --git a/color-test b/color-test<RESET>
724 <ITALIC><INDEX-LINE><RESET>
725 <ITALIC>--- a/color-test<RESET>
726 <ITALIC>+++ b/color-test<RESET>
727 <MAGENTA>@@ -1,3 +1,4 @@<RESET>
728 <CYAN> context<RESET>
730 <BLUE>+<RESET><BLUE>new<RESET>
731 <CYAN> more-context<RESET>
732 <BLUE>+<RESET><BLUE>another-one<RESET>
733 <YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,p,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
734 <MAGENTA>@@ -1,3 +1,3 @@<RESET>
735 <CYAN> context<RESET>
737 <BLUE>+<RESET><BLUE>new<RESET>
738 <CYAN> more-context<RESET>
739 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
740 <CYAN> more-context<RESET>
741 <BLUE>+<RESET><BLUE>another-one<RESET>
742 <YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
743 <CYAN> context<RESET>
746 <CYAN> more-context<RESET>
747 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
749 test_cmp expect actual
752 test_expect_success
'brackets appear without color' '
754 test_when_finished "git rm -f bracket-test" &&
755 test_write_lines context old more-context >bracket-test &&
756 git add bracket-test &&
757 test_write_lines context new more-context another-one >bracket-test &&
759 test_write_lines quit >input &&
760 git add -i >actual <input &&
762 sed "s/^|//" >expect <<-\EOF &&
763 | staged unstaged path
764 | 1: +3/-0 +2/-1 bracket-test
767 | 1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
768 | 5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
772 test_cmp expect actual
775 test_expect_success
'colors can be skipped with color.ui=false' '
777 test_when_finished "git rm -f color-test" &&
778 test_write_lines context old more-context >color-test &&
779 git add color-test &&
780 test_write_lines context new more-context another-one >color-test &&
782 test_write_lines help quit >input &&
785 add -i >actual.raw <input &&
786 test_decode_color <actual.raw >actual &&
787 test_cmp actual.raw actual
790 test_expect_success
'colorized diffs respect diff.wsErrorHighlight' '
798 force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
799 test_decode_color <output.raw >output &&
803 test_expect_success
'diffFilter filters diff' '
806 echo content >test &&
807 test_config interactive.diffFilter "sed s/^/foo:/" &&
809 force_color git add -p >output 2>&1 <y &&
811 # avoid depending on the exact coloring or content of the prompts,
812 # and just make sure we saw our diff prefixed
813 grep foo:.*content output
816 test_expect_success
'detect bogus diffFilter output' '
819 echo content >test &&
820 test_config interactive.diffFilter "sed 6d" &&
822 force_color test_must_fail git add -p <y >output 2>&1 &&
823 grep "mismatched output" output
826 test_expect_success
'handle iffy colored hunk headers' '
829 echo content >test &&
831 force_color git -c interactive.diffFilter="sed s/.*@@.*/XX/" \
832 add -p >output 2>&1 <n &&
836 test_expect_success
'handle very large filtered diff' '
838 # The specific number here is not important, but it must
839 # be large enough that the output of "git diff --color"
840 # fills up the pipe buffer. 10,000 results in ~200k of
842 test_seq 10000 >test &&
843 test_config interactive.diffFilter cat &&
845 force_color git add -p >output 2>&1 <y &&
846 git diff-files --exit-code -- test
849 test_expect_success
'diff.algorithm is passed to `git diff-files`' '
854 echo changed >file &&
855 test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
856 test_grep "error: option diff-algorithm accepts " err
859 test_expect_success
'patch-mode via -i prompts for files' '
864 git add -i <<-\EOF &&
873 git diff --cached --name-only >actual &&
874 diff_cmp expect actual
877 test_expect_success
'add -p handles globs' '
882 echo base >subdir/two.c &&
884 git commit -m base &&
886 echo change >one.c &&
887 echo change >subdir/two.c &&
888 git add -p "*.c" <<-\EOF &&
893 cat >expect <<-\EOF &&
897 git diff --cached --name-only >actual &&
898 test_cmp expect actual
901 test_expect_success
'add -p handles relative paths' '
904 echo base >relpath.c &&
906 git commit -m relpath &&
908 echo change >relpath.c &&
910 git -C subdir add -p .. 2>error <<-\EOF &&
914 test_must_be_empty error &&
916 cat >expect <<-\EOF &&
919 git diff --cached --name-only >actual &&
920 test_cmp expect actual
923 test_expect_success
'add -p does not expand argument lists' '
926 echo content >not-changed &&
927 git add not-changed &&
928 git commit -m "add not-changed file" &&
931 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
935 # we know that "file" must be mentioned since we actually
936 # update it, but we want to be sure that our "." pathspec
937 # was not expanded into the argument list of any command.
938 # So look only for "not-changed".
939 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
942 test_expect_success
'hunk-editing handles custom comment char' '
944 echo change >>file &&
945 test_config core.commentChar "\$" &&
946 echo e | GIT_EDITOR=true git add -p &&
950 test_expect_success
'add -p works even with color.ui=always' '
952 echo change >>file &&
953 test_config color.ui always &&
954 echo y | git add -p &&
956 git diff --cached --name-only >actual &&
957 test_cmp expect actual
960 test_expect_success
'setup different kinds of dirty submodules' '
961 test_create_repo for-submodules &&
964 test_commit initial &&
965 test_create_repo dirty-head &&
970 cp -R dirty-head dirty-otherwise &&
971 cp -R dirty-head dirty-both-ways &&
972 git add dirty-head &&
973 git add dirty-otherwise dirty-both-ways &&
974 git commit -m initial &&
977 test_commit updated &&
978 cd ../dirty-both-ways &&
979 test_commit updated &&
980 echo dirty >>initial &&
982 cd ../dirty-otherwise &&
983 echo dirty >>initial &&
986 git -C for-submodules diff-files --name-only >actual &&
987 cat >expected <<-\EOF &&
991 test_cmp expected actual &&
992 git -C for-submodules diff-files --name-only --ignore-submodules=none >actual &&
993 cat >expected <<-\EOF &&
998 test_cmp expected actual &&
999 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
1000 cat >expected <<-\EOF &&
1004 test_cmp expected actual
1007 test_expect_success
'status ignores dirty submodules (except HEAD)' '
1008 git -C for-submodules add -i </dev/null >output &&
1009 grep dirty-head output &&
1010 grep dirty-both-ways output &&
1011 ! grep dirty-otherwise output
1014 test_expect_success
'handle submodules' '
1015 echo 123 >>for-submodules/dirty-otherwise/initial.t &&
1017 force_color git -C for-submodules add -p dirty-otherwise >output 2>&1 &&
1018 grep "No changes" output &&
1020 force_color git -C for-submodules add -p dirty-head >output 2>&1 <y &&
1021 git -C for-submodules ls-files --stage dirty-head >actual &&
1022 rev="$(git -C for-submodules/dirty-head rev-parse HEAD)" &&
1026 test_expect_success
'set up pathological context' '
1028 test_write_lines a a a a a a a a a a a >a &&
1031 test_write_lines c b a a a a a a a b a a a a >a &&
1032 test_write_lines a a a a a a a b a a a a >expected-1 &&
1033 test_write_lines b a a a a a a a b a a a a >expected-2 &&
1034 # check editing can cope with missing header and deleted context lines
1035 # as well as changes to other lines
1036 test_write_lines +b " a" >patch
1039 test_expect_success
'add -p works with pathological context lines' '
1043 git cat-file blob :a >actual &&
1044 test_cmp expected-1 actual
1047 test_expect_success
'add -p patch editing works with pathological context lines' '
1049 # n q q below is in case edit fails
1050 printf "%s\n" e y n q q |
1052 git cat-file blob :a >actual &&
1053 test_cmp expected-2 actual
1056 test_expect_success
'checkout -p works with pathological context lines' '
1057 test_write_lines a a a a a a >a &&
1059 test_write_lines a b a b a b a b a b a >a &&
1060 test_write_lines s n n y q | git checkout -p &&
1061 test_write_lines a b a b a a b a b a >expect &&
1065 # This should be called from a subshell as it sets a temporary editor
1067 write_script new-file-editor.sh
<<-\EOF &&
1068 sed /^#/d "$1" >patch &&
1069 sed /^+c/d patch >"$1"
1071 test_set_editor "$(pwd)/new-file-editor.sh" &&
1072 test_write_lines a b c d e f >new-file &&
1073 test_write_lines a b d e f >new-file-expect &&
1074 test_write_lines "@@ -0,0 +1,6 @@" +a +b +c +d +e +f >patch-expect
1077 test_expect_success 'add -N followed by add -p patch editing' '
1081 git add -N new-file &&
1082 test_write_lines e n q | git add -p &&
1083 git cat-file blob :new-file >actual &&
1084 test_cmp new-file-expect actual &&
1085 test_cmp patch-expect patch
1089 test_expect_success 'checkout -p patch editing of added file' '
1094 git commit -m "add new file" &&
1096 git commit -m "remove new file" &&
1097 test_write_lines e n q | git checkout -p HEAD^ &&
1098 test_cmp new-file-expect new-file &&
1099 test_cmp patch-expect patch
1103 test_expect_success 'show help from add--helper' '
1105 cat >expect <<-EOF &&
1107 <BOLD>*** Commands ***<RESET>
1108 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
1109 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1110 <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes
<RESET
>
1111 <BOLD
;RED
>update
- add working tree state to the staged
set of changes
<RESET
>
1112 <BOLD
;RED
>revert
- revert staged
set of changes back to the HEAD version
<RESET
>
1113 <BOLD
;RED
>patch - pick hunks and update selectively
<RESET
>
1114 <BOLD
;RED
>diff - view
diff between HEAD and index
<RESET
>
1115 <BOLD
;RED
>add untracked
- add contents of untracked files to the staged
set of changes
<RESET
>
1116 <BOLD
>*** Commands
***<RESET
>
1117 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
1118 5: <BOLD
;BLUE
>p
<RESET
>atch
6: <BOLD
;BLUE
>d
<RESET
>iff
7: <BOLD
;BLUE
>q
<RESET
>uit
8: <BOLD
;BLUE
>h
<RESET
>elp
1119 <BOLD
;BLUE
>What now
<RESET
>>$SP
1122 test_write_lines h | force_color git add
-i >actual.colored
&&
1123 test_decode_color
<actual.colored
>actual
&&
1124 test_cmp expect actual
1127 test_expect_success 'reset -p with unmerged files
' '
1128 test_when_finished
"git checkout --force main" &&
1129 test_commit one conflict
&&
1130 git checkout
-B side HEAD^
&&
1131 test_commit two conflict
&&
1132 test_must_fail git merge one
&&
1134 # this is a noop with only an unmerged entry
1137 # add files that sort before and after unmerged entry
1142 # confirm that we can reset those files
1143 printf "%s\n" y y | git
reset -p &&
1144 git diff-index
--cached --diff-filter=u HEAD
>staged
&&
1145 test_must_be_empty staged