The eighth batch
[alt-git.git] / t / t3701-add-interactive.sh
blob718438ffc7d2a8d3482544378749b0d4dfe6313b
1 #!/bin/sh
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
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-terminal.sh
11 SP=" "
13 diff_cmp () {
14 for x
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 /' \
21 "$x" >"$x.filtered"
22 done
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.
33 force_color () {
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 &&
40 TERM=vt100 &&
41 export GIT_PAGER_IN_USE TERM &&
42 "$@"
46 test_expect_success 'unknown command' '
47 test_when_finished "git reset --hard; rm -f command" &&
48 echo W >command &&
49 git add -N 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
54 EOF
55 git add -p -- command <command >actual 2>&1 &&
56 test_cmp expect actual
59 test_expect_success 'setup (initial)' '
60 echo content >file &&
61 git add file &&
62 echo more >>file &&
63 echo lines >>file
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' '
71 cat >expected <<-\EOF
72 new file mode 100644
73 index 0000000..d95f3ad
74 --- /dev/null
75 +++ b/file
76 @@ -0,0 +1 @@
77 +content
78 EOF
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)' '
87 git add file &&
88 test_write_lines r 1 | git add -i &&
89 git ls-files >output &&
90 ! grep . 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 &&
104 git add file &&
105 git commit -m commit &&
106 echo content >>file &&
107 git add file &&
108 echo more >>file &&
109 echo lines >>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' '
117 >to-delete &&
118 git add to-delete &&
119 rm to-delete &&
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
128 --- a/file
129 +++ b/file
130 @@ -1 +1,2 @@
131 baseline
132 +content
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)' '
142 git add file &&
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' '
162 test_set_editor : &&
163 test_write_lines e a | git add -p &&
164 git diff > diff &&
165 diff_cmp expected diff
168 test_expect_success 'setup patch' '
169 cat >patch <<-\EOF
170 @@ -1,1 +1,4 @@
171 this
172 +patch
173 -does not
174 apply
178 test_expect_success 'setup fake editor' '
179 write_script "fake_editor.sh" <<-\EOF &&
180 mv -f "$1" oldpatch &&
181 mv -f patch "$1"
183 test_set_editor "$(pwd)/fake_editor.sh"
186 test_expect_success 'bad edit rejected' '
187 git reset &&
188 test_write_lines e n d | git add -p >output &&
189 grep "hunk does not apply" output
192 test_expect_success 'setup patch' '
193 cat >patch <<-\EOF
194 this patch
195 is garbage
199 test_expect_success 'garbage edit rejected' '
200 git reset &&
201 test_write_lines e n d | git add -p >output &&
202 grep "hunk does not apply" output
205 test_expect_success 'setup patch' '
206 cat >patch <<-\EOF
207 @@ -1,0 +1,0 @@
208 baseline
209 +content
210 +newcontent
211 +lines
215 test_expect_success 'setup expected' '
216 cat >expected <<-\EOF
217 diff --git a/file b/file
218 index b5dd6c9..f910ae9 100644
219 --- a/file
220 +++ b/file
221 @@ -1,4 +1,4 @@
222 baseline
223 content
224 -newcontent
225 +more
226 lines
230 test_expect_success 'real edit works' '
231 test_write_lines e n d | git add -p &&
232 git diff >output &&
233 diff_cmp expected output
236 test_expect_success 'setup file' '
237 test_write_lines a "" b "" c >file &&
238 git add file &&
239 test_write_lines a "" d "" c >file
242 test_expect_success 'setup patch' '
243 NULL="" &&
244 cat >patch <<-EOF
245 @@ -1,4 +1,4 @@
247 $NULL
255 test_expect_success 'setup expected' '
256 cat >expected <<-EOF
257 diff --git a/file b/file
258 index b5dd6c9..f910ae9 100644
259 --- a/file
260 +++ b/file
261 @@ -1,5 +1,5 @@
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 &&
274 git diff >output &&
275 diff_cmp expected output
278 test_expect_success 'skip files similarly as commit -a' '
279 git reset &&
280 echo file >.gitignore &&
281 echo changed >file &&
282 echo y | git add -p file &&
283 git diff >output &&
284 git reset &&
285 git commit -am commit &&
286 git diff >expected &&
287 diff_cmp expected output &&
288 git reset --hard HEAD^
290 rm -f .gitignore
292 test_expect_success FILEMODE 'patch does not affect mode' '
293 git reset --hard &&
294 echo content >>file &&
295 chmod +x 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' '
302 git reset --hard &&
303 echo content >>file &&
304 chmod +x 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' '
312 git reset --hard &&
313 echo content >>file &&
314 chmod +x 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' '
326 git reset --hard &&
327 >file &&
328 >deleted &&
329 git add --chmod=+x file deleted &&
330 echo changed >file &&
331 rm deleted &&
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' '
344 git reset --hard &&
345 git add -p >out &&
346 test_grep "No changes" out &&
347 printf "\\0123" >binary &&
348 git add binary &&
349 printf "\\0abc" >binary &&
350 git add -p >out &&
351 test_grep "Only binary files changed" out
354 test_expect_success 'setup again' '
355 git reset --hard &&
356 test_chmod +x file &&
357 echo content >>file &&
358 test_write_lines A B C D>file2 &&
359 git add file2
362 # Write the patch file with a new line at the top and bottom
363 test_expect_success 'setup patch' '
364 cat >patch <<-\EOF
365 index 180b47c..b6f2c08 100644
366 --- a/file
367 +++ b/file
368 @@ -1,2 +1,4 @@
369 +firstline
370 baseline
371 content
372 +lastline
373 \ No newline at end of file
374 diff --git a/file2 b/file2
375 index 8422d40..35b930a 100644
376 --- a/file2
377 +++ b/file2
378 @@ -1,4 +1,5 @@
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/" \
393 -e /1,5/s//1,4/ \
394 -e /Y/d patch >>expected &&
395 cat >expected-output <<-\EOF
396 --- a/file
397 +++ b/file
398 @@ -1,2 +1,4 @@
399 +firstline
400 baseline
401 content
402 +lastline
403 \ No newline at end of file
404 @@ -1,2 +1,3 @@
405 +firstline
406 baseline
407 content
408 @@ -1,2 +2,3 @@
409 baseline
410 content
411 +lastline
412 \ No newline at end of file
413 --- a/file2
414 +++ b/file2
415 @@ -1,4 +1,5 @@
423 @@ -1,2 +1,2 @@
427 @@ -2,2 +2,3 @@
431 @@ -3,2 +4,2 @@
438 # Test splitting the first patch, then adding both
439 test_expect_success 'add first line works' '
440 git commit -am "clear local changes" &&
441 git apply patch &&
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
456 --- a/non-empty
457 +++ /dev/null
458 @@ -1 +0,0 @@
459 -content
463 test_expect_success 'deleting a non-empty file' '
464 git reset --hard &&
465 echo content >non-empty &&
466 git add non-empty &&
467 git commit -m non-empty &&
468 rm 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' '
483 git reset --hard &&
484 > empty &&
485 git add empty &&
486 git commit -m empty &&
487 rm 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' '
494 git init added &&
496 cd added &&
497 test_commit initial &&
498 >empty &&
499 git add empty &&
500 test_tick &&
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' '
513 git reset --hard &&
514 test_write_lines 10 20 30 40 50 60 >test &&
515 git add test &&
516 test_tick &&
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
526 _ 2: -2,4 +3,8 +21
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>
598 20<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>
600 PAGER 10<RESET>
601 PAGER <GREEN>+<RESET><GREEN>15<RESET>
602 PAGER 20<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\ /" &&
608 export GIT_PAGER &&
609 test_terminal git add -p >actual
610 ) &&
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" &&
621 export GIT_PAGER &&
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
632 # times to get out.
634 # 2. Correct version applies the (not)edited version, and asks
635 # about the next hunk, against which we say q and program
636 # exits.
637 printf "%s\n" s e q n q q |
638 EDITOR=: git add -p &&
639 git diff >actual &&
640 ! grep "^+15" actual
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 &&
645 git reset &&
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 &&
651 git diff >actual &&
652 ! grep "^+31" actual
655 test_expect_success 'split hunk with incomplete line at end' '
656 git reset --hard &&
657 printf "missing LF" >>test &&
658 git add 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 &&
663 git grep before &&
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 &&
669 git reset &&
670 tr _ " " >patch <<-EOF &&
671 @@ -1,5 +1,6 @@
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 &&
686 grep "^+22" actual
689 test_expect_success 'patch mode ignores unmerged entries' '
690 git reset --hard &&
691 test_commit conflict &&
692 test_commit non-conflict &&
693 git checkout -b side &&
694 test_commit side conflict.t &&
695 git checkout main &&
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
705 --- a/non-conflict.t
706 +++ b/non-conflict.t
707 @@ -1 +1 @@
708 -non-conflict
709 +changed
711 git diff --cached >diff &&
712 diff_cmp expected diff
715 test_expect_success 'index is refreshed after applying patch' '
716 git reset --hard &&
717 echo content >test &&
718 printf y | git add -p &&
719 git diff-files --exit-code
722 test_expect_success 'diffs can be colorized' '
723 git reset --hard &&
725 echo content >test &&
726 printf y >y &&
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' '
736 git reset --hard &&
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 &&
743 force_color git \
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 &&
750 force_color git \
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 &&
778 force_color git \
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>
796 <BOLD>-old<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>
803 <BOLD>-old<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>
811 <BOLD>-old<RESET>
812 <BLUE>+new<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' '
820 git reset --hard &&
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
833 |*** Commands ***
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
836 |What now> Bye.
839 test_cmp expect actual
842 test_expect_success 'colors can be skipped with color.ui=false' '
843 git reset --hard &&
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 &&
850 force_color git \
851 -c color.ui=false \
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' '
858 git reset --hard &&
860 echo "old " >test &&
861 git add test &&
862 echo "new " >test &&
864 printf y >y &&
865 force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
866 test_decode_color <output.raw >output &&
867 grep "old<" output
870 test_expect_success 'diffFilter filters diff' '
871 git reset --hard &&
873 echo content >test &&
874 test_config interactive.diffFilter "sed s/^/foo:/" &&
875 printf y >y &&
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' '
884 git reset --hard &&
886 echo content >test &&
887 test_config interactive.diffFilter "sed 6d" &&
888 printf y >y &&
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' '
894 git reset --hard &&
896 echo content >test &&
897 printf n >n &&
898 force_color git -c interactive.diffFilter="sed s/.*@@.*/XX/" \
899 add -p >output 2>&1 <n &&
900 grep "^XX$" output
903 test_expect_success 'handle very large filtered diff' '
904 git reset --hard &&
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
908 # colored output.
909 test_seq 10000 >test &&
910 test_config interactive.diffFilter cat &&
911 printf y >y &&
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`' '
917 git reset --hard &&
919 >file &&
920 git add file &&
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' '
927 git reset --hard &&
929 echo one >file &&
930 echo two >test &&
931 git add -i <<-\EOF &&
932 patch
933 test
936 quit
939 echo test >expect &&
940 git diff --cached --name-only >actual &&
941 diff_cmp expect actual
944 test_expect_success 'add -p handles globs' '
945 git reset --hard &&
947 mkdir -p subdir &&
948 echo base >one.c &&
949 echo base >subdir/two.c &&
950 git add "*.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 &&
961 one.c
962 subdir/two.c
964 git diff --cached --name-only >actual &&
965 test_cmp expect actual
968 test_expect_success 'add -p handles relative paths' '
969 git reset --hard &&
971 echo base >relpath.c &&
972 git add "*.c" &&
973 git commit -m relpath &&
975 echo change >relpath.c &&
976 mkdir -p subdir &&
977 git -C subdir add -p .. 2>error <<-\EOF &&
981 test_must_be_empty error &&
983 cat >expect <<-\EOF &&
984 relpath.c
986 git diff --cached --name-only >actual &&
987 test_cmp expect actual
990 test_expect_success 'add -p does not expand argument lists' '
991 git reset --hard &&
993 echo content >not-changed &&
994 git add not-changed &&
995 git commit -m "add not-changed file" &&
997 echo change >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' '
1010 git reset --hard &&
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' '
1018 git reset --hard &&
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 &&
1034 cd dirty-head &&
1035 test_commit initial
1036 ) &&
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 &&
1043 cd dirty-head &&
1044 test_commit updated &&
1045 cd ../dirty-both-ways &&
1046 test_commit updated &&
1047 echo dirty >>initial &&
1048 : >untracked &&
1049 cd ../dirty-otherwise &&
1050 echo dirty >>initial &&
1051 : >untracked
1052 ) &&
1053 git -C for-submodules diff-files --name-only >actual &&
1054 cat >expected <<-\EOF &&
1055 dirty-both-ways
1056 dirty-head
1058 test_cmp expected actual &&
1059 git -C for-submodules diff-files --name-only --ignore-submodules=none >actual &&
1060 cat >expected <<-\EOF &&
1061 dirty-both-ways
1062 dirty-head
1063 dirty-otherwise
1065 test_cmp expected actual &&
1066 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
1067 cat >expected <<-\EOF &&
1068 dirty-both-ways
1069 dirty-head
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)" &&
1090 grep "$rev" actual
1093 test_expect_success 'set up pathological context' '
1094 git reset --hard &&
1095 test_write_lines a a a a a a a a a a a >a &&
1096 git add a &&
1097 git commit -m 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' '
1107 git reset &&
1108 printf "%s\n" n y |
1109 git add -p &&
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' '
1115 git reset &&
1116 # n q q below is in case edit fails
1117 printf "%s\n" e y n q q |
1118 git add -p &&
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 &&
1125 git add 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 &&
1129 test_cmp expect a
1132 # This should be called from a subshell as it sets a temporary editor
1133 setup_new_file() {
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' '
1145 git reset --hard &&
1147 setup_new_file &&
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' '
1157 git reset --hard &&
1159 setup_new_file &&
1160 git add new-file &&
1161 git commit -m "add new file" &&
1162 git rm 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' '
1171 git reset --hard &&
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
1187 Bye.
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
1202 git reset -p &&
1204 # add files that sort before and after unmerged entry
1205 echo a >a &&
1206 echo z >z &&
1207 git add a z &&
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" &&
1219 mv "$1.tmp" "$1"
1222 test_write_lines a b "" c d "" e f "" >file &&
1223 git add 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
1228 ) &&
1229 git cat-file blob :file >actual &&
1230 test_write_lines a b "" c D "" e G "" >expect &&
1231 test_cmp expect actual
1234 test_done