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
14 sed -e '/^index/s/[0-9a-f]*[1-9a-f][0-9a-f]*\.\./1234567../' \
15 -e '/^index/s/\.\.[0-9a-f]*[1-9a-f][0-9a-f]*/..9abcdef/' \
16 -e '/^index/s/ 00*\.\./ 0000000../' \
17 -e '/^index/s/\.\.00*$/..0000000/' \
18 -e '/^index/s/\.\.00* /..0000000 /' \
21 test_cmp
"$1.filtered" "$2.filtered"
24 # This function uses a trick to manipulate the interactive add to use color:
25 # the `want_color()` function special-cases the situation where a pager was
26 # spawned and Git now wants to output colored text: to detect that situation,
27 # the environment variable `GIT_PAGER_IN_USE` is set. However, color is
28 # suppressed despite that environment variable if the `TERM` variable
29 # indicates a dumb terminal, so we set that variable, too.
32 # The first element of $@ may be a shell function, as a result POSIX
33 # does not guarantee that "one-shot assignment" will not persist after
34 # the function call. Thus, we prevent these variables from escaping
35 # this function's context with this subshell.
37 GIT_PAGER_IN_USE
=true
&&
39 export GIT_PAGER_IN_USE TERM
&&
44 test_expect_success
'warn about add.interactive.useBuiltin' '
45 cat >expect <<-\EOF &&
46 warning: the add.interactive.useBuiltin setting has been removed!
47 See its entry in '\''git help config'\'' for details.
51 for v in = =true =false
53 git -c "add.interactive.useBuiltin$v" add -p >out 2>actual &&
54 test_must_be_empty out &&
55 test_cmp expect actual || return 1
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
'setup expected' '
149 cat >expected <<-\EOF
153 test_expect_success
'dummy edit works' '
155 test_write_lines e a | git add -p &&
157 diff_cmp expected diff
160 test_expect_success
'setup patch' '
170 test_expect_success
'setup fake editor' '
171 write_script "fake_editor.sh" <<-\EOF &&
172 mv -f "$1" oldpatch &&
175 test_set_editor "$(pwd)/fake_editor.sh"
178 test_expect_success
'bad edit rejected' '
180 test_write_lines e n d | git add -p >output &&
181 grep "hunk does not apply" output
184 test_expect_success
'setup patch' '
191 test_expect_success
'garbage edit rejected' '
193 test_write_lines e n d | git add -p >output &&
194 grep "hunk does not apply" output
197 test_expect_success
'setup patch' '
207 test_expect_success
'setup expected' '
208 cat >expected <<-\EOF
209 diff --git a/file b/file
210 index b5dd6c9..f910ae9 100644
222 test_expect_success
'real edit works' '
223 test_write_lines e n d | git add -p &&
225 diff_cmp expected output
228 test_expect_success
'setup file' '
229 test_write_lines a "" b "" c >file &&
231 test_write_lines a "" d "" c >file
234 test_expect_success
'setup patch' '
248 test_expect_success
'setup expected' '
250 diff --git a/file b/file
251 index b5dd6c9..f910ae9 100644
264 test_expect_success
'edit can strip spaces from empty context lines' '
265 test_write_lines e n q | git add -p 2>error &&
266 test_must_be_empty error &&
268 diff_cmp expected output
271 test_expect_success
'skip files similarly as commit -a' '
273 echo file >.gitignore &&
274 echo changed >file &&
275 echo y | git add -p file &&
278 git commit -am commit &&
279 git diff >expected &&
280 diff_cmp expected output &&
281 git reset --hard HEAD^
285 test_expect_success FILEMODE
'patch does not affect mode' '
287 echo content >>file &&
289 printf "n\\ny\\n" | git add -p &&
290 git show :file | grep content &&
291 git diff file | grep "new mode"
294 test_expect_success FILEMODE
'stage mode but not hunk' '
296 echo content >>file &&
298 printf "y\\nn\\n" | git add -p &&
299 git diff --cached file | grep "new mode" &&
300 git diff file | grep "+content"
304 test_expect_success FILEMODE
'stage mode and hunk' '
306 echo content >>file &&
308 printf "y\\ny\\n" | git add -p &&
309 git diff --cached file >out &&
310 grep "new mode" out &&
311 grep "+content" out &&
312 git diff file >out &&
313 test_must_be_empty out
316 # end of tests disabled when filemode is not usable
318 test_expect_success
'different prompts for mode change/deleted' '
322 git add --chmod=+x file deleted &&
323 echo changed >file &&
325 test_write_lines n n n |
326 git -c core.filemode=true add -p >actual &&
327 sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
328 cat >expect <<-\EOF &&
329 (1/1) Stage deletion [y,n,q,a,d,p,?]?
330 (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,p,?]?
331 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]?
333 test_cmp expect actual.filtered
336 test_expect_success
'correct message when there is nothing to do' '
339 test_grep "No changes" err &&
340 printf "\\0123" >binary &&
342 printf "\\0abc" >binary &&
344 test_grep "Only binary files changed" err
347 test_expect_success
'setup again' '
349 test_chmod +x file &&
350 echo content >>file &&
351 test_write_lines A B C D>file2 &&
355 # Write the patch file with a new line at the top and bottom
356 test_expect_success
'setup patch' '
358 index 180b47c..b6f2c08 100644
366 \ No newline at end of file
367 diff --git a/file2 b/file2
368 index 8422d40..35b930a 100644
382 # Expected output, diff is similar to the patch but w/ diff at the top
383 test_expect_success
'setup expected' '
384 echo diff --git a/file b/file >expected &&
385 sed -e "/^index 180b47c/s/ 100644/ 100755/" \
387 -e /Y/d patch >>expected &&
388 cat >expected-output <<-\EOF
396 \ No newline at end of file
405 \ No newline at end of file
431 # Test splitting the first patch, then adding both
432 test_expect_success
'add first line works' '
433 git commit -am "clear local changes" &&
435 test_write_lines s y y s y n y | git add -p 2>error >raw-output &&
436 sed -n -e "s/^([1-9]\/[1-9]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
437 -e "/^[-+@ \\\\]"/p raw-output >output &&
438 test_must_be_empty error &&
439 git diff --cached >diff &&
440 diff_cmp expected diff &&
441 test_cmp expected-output output
444 test_expect_success
'setup expected' '
445 cat >expected <<-\EOF
446 diff --git a/non-empty b/non-empty
447 deleted file mode 100644
448 index d95f3ad..0000000
456 test_expect_success
'deleting a non-empty file' '
458 echo content >non-empty &&
460 git commit -m non-empty &&
462 echo y | git add -p non-empty &&
463 git diff --cached >diff &&
464 diff_cmp expected diff
467 test_expect_success
'setup expected' '
468 cat >expected <<-\EOF
469 diff --git a/empty b/empty
470 deleted file mode 100644
471 index e69de29..0000000
475 test_expect_success
'deleting an empty file' '
479 git commit -m empty &&
481 echo y | git add -p empty &&
482 git diff --cached >diff &&
483 diff_cmp expected diff
486 test_expect_success
'adding an empty file' '
490 test_commit initial &&
494 git commit -m empty &&
495 git tag added-file &&
496 git reset --hard HEAD^ &&
497 test_path_is_missing empty &&
499 echo y | git checkout -p added-file -- >actual &&
500 test_path_is_file empty &&
501 test_grep "Apply addition to index and worktree" actual
505 test_expect_success
'split hunk setup' '
507 test_write_lines 10 20 30 40 50 60 >test &&
510 git commit -m test &&
512 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
515 test_expect_success
'goto hunk' '
516 test_when_finished "git reset" &&
517 tr _ " " >expect <<-EOF &&
518 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + 1: -1,2 +1,3 +15
520 go to which hunk? @@ -1,2 +1,3 @@
524 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
526 test_write_lines s y g 1 | git add -p >actual &&
527 tail -n 7 <actual >actual.trimmed &&
528 test_cmp expect actual.trimmed
531 test_expect_success
'navigate to hunk via regex' '
532 test_when_finished "git reset" &&
533 tr _ " " >expect <<-EOF &&
534 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -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 /1,2 | git add -p >actual &&
541 tail -n 5 <actual >actual.trimmed &&
542 test_cmp expect actual.trimmed
545 test_expect_success
'split hunk "add -p (edit)"' '
546 # Split, say Edit and do nothing. Then:
548 # 1. Broken version results in a patch that does not apply and
549 # only takes [y/n] (edit again) so the first q is discarded
550 # and then n attempts to discard the edit. Repeat q enough
553 # 2. Correct version applies the (not)edited version, and asks
554 # about the next hunk, against which we say q and program
556 printf "%s\n" s e q n q q |
557 EDITOR=: git add -p &&
562 test_expect_success
'split hunk "add -p (no, yes, edit)"' '
563 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
565 # test sequence is s(plit), n(o), y(es), e(dit)
566 # q n q q is there to make sure we exit at the end.
567 printf "%s\n" s n y e q n q q |
568 EDITOR=: git add -p 2>error &&
569 test_must_be_empty error &&
574 test_expect_success
'split hunk with incomplete line at end' '
576 printf "missing LF" >>test &&
578 test_write_lines before 10 20 30 40 50 60 70 >test &&
579 git grep --cached missing &&
580 test_write_lines s n y q | git add -p &&
581 test_must_fail git grep --cached missing &&
583 test_must_fail git grep --cached before
586 test_expect_success
'edit, adding lines to the first hunk' '
587 test_write_lines 10 11 20 30 40 50 51 60 >test &&
589 tr _ " " >patch <<-EOF &&
599 # test sequence is s(plit), e(dit), n(o)
600 # q n q q is there to make sure we exit at the end.
601 printf "%s\n" s e n q n q q |
602 EDITOR=./fake_editor.sh git add -p 2>error &&
603 test_must_be_empty error &&
604 git diff --cached >actual &&
608 test_expect_success
'patch mode ignores unmerged entries' '
610 test_commit conflict &&
611 test_commit non-conflict &&
612 git checkout -b side &&
613 test_commit side conflict.t &&
615 test_commit main conflict.t &&
616 test_must_fail git merge side &&
617 echo changed >non-conflict.t &&
618 echo y | git add -p >output &&
619 ! grep a/conflict.t output &&
620 cat >expected <<-\EOF &&
621 * Unmerged path conflict.t
622 diff --git a/non-conflict.t b/non-conflict.t
623 index f766221..5ea2ed4 100644
630 git diff --cached >diff &&
631 diff_cmp expected diff
634 test_expect_success
'index is refreshed after applying patch' '
636 echo content >test &&
637 printf y | git add -p &&
638 git diff-files --exit-code
641 test_expect_success
'diffs can be colorized' '
644 echo content >test &&
646 force_color git add -p >output 2>&1 <y &&
647 git diff-files --exit-code &&
649 # We do not want to depend on the exact coloring scheme
650 # git uses for diffs, so just check that we saw some kind of color.
651 grep "$(printf "\\033")" output
654 test_expect_success
'colors can be overridden' '
656 test_when_finished "git rm -f color-test" &&
657 test_write_lines context old more-context >color-test &&
658 git add color-test &&
659 test_write_lines context new more-context another-one >color-test &&
661 echo trigger an error message >input &&
663 -c color.interactive.error=blue \
664 add -i 2>err.raw <input &&
665 test_decode_color <err.raw >err &&
666 grep "<BLUE>Huh (trigger)?<RESET>" err &&
668 test_write_lines help quit >input &&
670 -c color.interactive.header=red \
671 -c color.interactive.help=green \
672 -c color.interactive.prompt=yellow \
673 add -i >actual.raw <input &&
674 test_decode_color <actual.raw >actual &&
675 cat >expect <<-\EOF &&
676 <RED> staged unstaged path<RESET>
677 1: +3/-0 +2/-1 color-test
679 <RED>*** Commands ***<RESET>
680 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
681 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
682 <YELLOW>What now<RESET>> <GREEN>status - show paths with changes<RESET>
683 <GREEN>update - add working tree state to the staged set of changes<RESET>
684 <GREEN>revert - revert staged set of changes back to the HEAD version<RESET>
685 <GREEN>patch - pick hunks and update selectively<RESET>
686 <GREEN>diff - view diff between HEAD and index<RESET>
687 <GREEN>add untracked - add contents of untracked files to the staged set of changes<RESET>
688 <RED>*** Commands ***<RESET>
689 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
690 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
691 <YELLOW>What now<RESET>> Bye.
693 test_cmp expect actual &&
695 : exercise recolor_hunk by editing and then look at the hunk again &&
696 test_write_lines s e K q >input &&
698 -c color.interactive.prompt=yellow \
699 -c color.diff.meta=italic \
700 -c color.diff.frag=magenta \
701 -c color.diff.context=cyan \
702 -c color.diff.old=bold \
703 -c color.diff.new=blue \
704 -c core.editor=touch \
705 add -p >actual.raw <input &&
706 test_decode_color <actual.raw >actual.decoded &&
707 sed "s/index [0-9a-f]*\\.\\.[0-9a-f]* 100644/<INDEX-LINE>/" <actual.decoded >actual &&
708 cat >expect <<-\EOF &&
709 <ITALIC>diff --git a/color-test b/color-test<RESET>
710 <ITALIC><INDEX-LINE><RESET>
711 <ITALIC>--- a/color-test<RESET>
712 <ITALIC>+++ b/color-test<RESET>
713 <MAGENTA>@@ -1,3 +1,4 @@<RESET>
714 <CYAN> context<RESET>
716 <BLUE>+<RESET><BLUE>new<RESET>
717 <CYAN> more-context<RESET>
718 <BLUE>+<RESET><BLUE>another-one<RESET>
719 <YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,p,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
720 <MAGENTA>@@ -1,3 +1,3 @@<RESET>
721 <CYAN> context<RESET>
723 <BLUE>+<RESET><BLUE>new<RESET>
724 <CYAN> more-context<RESET>
725 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
726 <CYAN> more-context<RESET>
727 <BLUE>+<RESET><BLUE>another-one<RESET>
728 <YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
729 <CYAN> context<RESET>
732 <CYAN> more-context<RESET>
733 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
735 test_cmp expect actual
738 test_expect_success
'brackets appear without color' '
740 test_when_finished "git rm -f bracket-test" &&
741 test_write_lines context old more-context >bracket-test &&
742 git add bracket-test &&
743 test_write_lines context new more-context another-one >bracket-test &&
745 test_write_lines quit >input &&
746 git add -i >actual <input &&
748 sed "s/^|//" >expect <<-\EOF &&
749 | staged unstaged path
750 | 1: +3/-0 +2/-1 bracket-test
753 | 1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
754 | 5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
758 test_cmp expect actual
761 test_expect_success
'colors can be skipped with color.ui=false' '
763 test_when_finished "git rm -f color-test" &&
764 test_write_lines context old more-context >color-test &&
765 git add color-test &&
766 test_write_lines context new more-context another-one >color-test &&
768 test_write_lines help quit >input &&
771 add -i >actual.raw <input &&
772 test_decode_color <actual.raw >actual &&
773 test_cmp actual.raw actual
776 test_expect_success
'colorized diffs respect diff.wsErrorHighlight' '
784 force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
785 test_decode_color <output.raw >output &&
789 test_expect_success
'diffFilter filters diff' '
792 echo content >test &&
793 test_config interactive.diffFilter "sed s/^/foo:/" &&
795 force_color git add -p >output 2>&1 <y &&
797 # avoid depending on the exact coloring or content of the prompts,
798 # and just make sure we saw our diff prefixed
799 grep foo:.*content output
802 test_expect_success
'detect bogus diffFilter output' '
805 echo content >test &&
806 test_config interactive.diffFilter "sed 6d" &&
808 force_color test_must_fail git add -p <y >output 2>&1 &&
809 grep "mismatched output" output
812 test_expect_success
'handle iffy colored hunk headers' '
815 echo content >test &&
817 force_color git -c interactive.diffFilter="sed s/.*@@.*/XX/" \
818 add -p >output 2>&1 <n &&
822 test_expect_success
'handle very large filtered diff' '
824 # The specific number here is not important, but it must
825 # be large enough that the output of "git diff --color"
826 # fills up the pipe buffer. 10,000 results in ~200k of
828 test_seq 10000 >test &&
829 test_config interactive.diffFilter cat &&
831 force_color git add -p >output 2>&1 <y &&
832 git diff-files --exit-code -- test
835 test_expect_success
'diff.algorithm is passed to `git diff-files`' '
840 echo changed >file &&
841 test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
842 test_grep "error: option diff-algorithm accepts " err
845 test_expect_success
'patch-mode via -i prompts for files' '
850 git add -i <<-\EOF &&
859 git diff --cached --name-only >actual &&
860 diff_cmp expect actual
863 test_expect_success
'add -p handles globs' '
868 echo base >subdir/two.c &&
870 git commit -m base &&
872 echo change >one.c &&
873 echo change >subdir/two.c &&
874 git add -p "*.c" <<-\EOF &&
879 cat >expect <<-\EOF &&
883 git diff --cached --name-only >actual &&
884 test_cmp expect actual
887 test_expect_success
'add -p handles relative paths' '
890 echo base >relpath.c &&
892 git commit -m relpath &&
894 echo change >relpath.c &&
896 git -C subdir add -p .. 2>error <<-\EOF &&
900 test_must_be_empty error &&
902 cat >expect <<-\EOF &&
905 git diff --cached --name-only >actual &&
906 test_cmp expect actual
909 test_expect_success
'add -p does not expand argument lists' '
912 echo content >not-changed &&
913 git add not-changed &&
914 git commit -m "add not-changed file" &&
917 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
921 # we know that "file" must be mentioned since we actually
922 # update it, but we want to be sure that our "." pathspec
923 # was not expanded into the argument list of any command.
924 # So look only for "not-changed".
925 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
928 test_expect_success
'hunk-editing handles custom comment char' '
930 echo change >>file &&
931 test_config core.commentChar "\$" &&
932 echo e | GIT_EDITOR=true git add -p &&
936 test_expect_success
'add -p works even with color.ui=always' '
938 echo change >>file &&
939 test_config color.ui always &&
940 echo y | git add -p &&
942 git diff --cached --name-only >actual &&
943 test_cmp expect actual
946 test_expect_success
'setup different kinds of dirty submodules' '
947 test_create_repo for-submodules &&
950 test_commit initial &&
951 test_create_repo dirty-head &&
956 cp -R dirty-head dirty-otherwise &&
957 cp -R dirty-head dirty-both-ways &&
958 git add dirty-head &&
959 git add dirty-otherwise dirty-both-ways &&
960 git commit -m initial &&
963 test_commit updated &&
964 cd ../dirty-both-ways &&
965 test_commit updated &&
966 echo dirty >>initial &&
968 cd ../dirty-otherwise &&
969 echo dirty >>initial &&
972 git -C for-submodules diff-files --name-only >actual &&
973 cat >expected <<-\EOF &&
977 test_cmp expected actual &&
978 git -C for-submodules diff-files --name-only --ignore-submodules=none >actual &&
979 cat >expected <<-\EOF &&
984 test_cmp expected actual &&
985 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
986 cat >expected <<-\EOF &&
990 test_cmp expected actual
993 test_expect_success
'status ignores dirty submodules (except HEAD)' '
994 git -C for-submodules add -i </dev/null >output &&
995 grep dirty-head output &&
996 grep dirty-both-ways output &&
997 ! grep dirty-otherwise output
1000 test_expect_success
'handle submodules' '
1001 echo 123 >>for-submodules/dirty-otherwise/initial.t &&
1003 force_color git -C for-submodules add -p dirty-otherwise >output 2>&1 &&
1004 grep "No changes" output &&
1006 force_color git -C for-submodules add -p dirty-head >output 2>&1 <y &&
1007 git -C for-submodules ls-files --stage dirty-head >actual &&
1008 rev="$(git -C for-submodules/dirty-head rev-parse HEAD)" &&
1012 test_expect_success
'set up pathological context' '
1014 test_write_lines a a a a a a a a a a a >a &&
1017 test_write_lines c b a a a a a a a b a a a a >a &&
1018 test_write_lines a a a a a a a b a a a a >expected-1 &&
1019 test_write_lines b a a a a a a a b a a a a >expected-2 &&
1020 # check editing can cope with missing header and deleted context lines
1021 # as well as changes to other lines
1022 test_write_lines +b " a" >patch
1025 test_expect_success
'add -p works with pathological context lines' '
1029 git cat-file blob :a >actual &&
1030 test_cmp expected-1 actual
1033 test_expect_success
'add -p patch editing works with pathological context lines' '
1035 # n q q below is in case edit fails
1036 printf "%s\n" e y n q q |
1038 git cat-file blob :a >actual &&
1039 test_cmp expected-2 actual
1042 test_expect_success
'checkout -p works with pathological context lines' '
1043 test_write_lines a a a a a a >a &&
1045 test_write_lines a b a b a b a b a b a >a &&
1046 test_write_lines s n n y q | git checkout -p &&
1047 test_write_lines a b a b a a b a b a >expect &&
1051 # This should be called from a subshell as it sets a temporary editor
1053 write_script new-file-editor.sh
<<-\EOF &&
1054 sed /^#/d "$1" >patch &&
1055 sed /^+c/d patch >"$1"
1057 test_set_editor "$(pwd)/new-file-editor.sh" &&
1058 test_write_lines a b c d e f >new-file &&
1059 test_write_lines a b d e f >new-file-expect &&
1060 test_write_lines "@@ -0,0 +1,6 @@" +a +b +c +d +e +f >patch-expect
1063 test_expect_success 'add -N followed by add -p patch editing' '
1067 git add -N new-file &&
1068 test_write_lines e n q | git add -p &&
1069 git cat-file blob :new-file >actual &&
1070 test_cmp new-file-expect actual &&
1071 test_cmp patch-expect patch
1075 test_expect_success 'checkout -p patch editing of added file' '
1080 git commit -m "add new file" &&
1082 git commit -m "remove new file" &&
1083 test_write_lines e n q | git checkout -p HEAD^ &&
1084 test_cmp new-file-expect new-file &&
1085 test_cmp patch-expect patch
1089 test_expect_success 'show help from add--helper' '
1091 cat >expect <<-EOF &&
1093 <BOLD>*** Commands ***<RESET>
1094 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
1095 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1096 <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes
<RESET
>
1097 <BOLD
;RED
>update
- add working tree state to the staged
set of changes
<RESET
>
1098 <BOLD
;RED
>revert
- revert staged
set of changes back to the HEAD version
<RESET
>
1099 <BOLD
;RED
>patch - pick hunks and update selectively
<RESET
>
1100 <BOLD
;RED
>diff - view
diff between HEAD and index
<RESET
>
1101 <BOLD
;RED
>add untracked
- add contents of untracked files to the staged
set of changes
<RESET
>
1102 <BOLD
>*** Commands
***<RESET
>
1103 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
1104 5: <BOLD
;BLUE
>p
<RESET
>atch
6: <BOLD
;BLUE
>d
<RESET
>iff
7: <BOLD
;BLUE
>q
<RESET
>uit
8: <BOLD
;BLUE
>h
<RESET
>elp
1105 <BOLD
;BLUE
>What now
<RESET
>>$SP
1108 test_write_lines h | force_color git add
-i >actual.colored
&&
1109 test_decode_color
<actual.colored
>actual
&&
1110 test_cmp expect actual
1113 test_expect_success 'reset -p with unmerged files
' '
1114 test_when_finished
"git checkout --force main" &&
1115 test_commit one conflict
&&
1116 git checkout
-B side HEAD^
&&
1117 test_commit two conflict
&&
1118 test_must_fail git merge one
&&
1120 # this is a noop with only an unmerged entry
1123 # add files that sort before and after unmerged entry
1128 # confirm that we can reset those files
1129 printf "%s\n" y y | git
reset -p &&
1130 git diff-index
--cached --diff-filter=u HEAD
>staged
&&
1131 test_must_be_empty staged