add-patch: handle "* Unmerged path" lines
[git.git] / t / t3701-add-interactive.sh
blobd0a2ba739e1c16099f523775c42ac133890da54e
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-lib.sh
8 . "$TEST_DIRECTORY"/lib-terminal.sh
10 if test_have_prereq !ADD_I_USE_BUILTIN,!PERL
11 then
12 skip_all='skipping add -i (scripted) tests, perl not available'
13 test_done
16 diff_cmp () {
17 for x
19 sed -e '/^index/s/[0-9a-f]*[1-9a-f][0-9a-f]*\.\./1234567../' \
20 -e '/^index/s/\.\.[0-9a-f]*[1-9a-f][0-9a-f]*/..9abcdef/' \
21 -e '/^index/s/ 00*\.\./ 0000000../' \
22 -e '/^index/s/\.\.00*$/..0000000/' \
23 -e '/^index/s/\.\.00* /..0000000 /' \
24 "$x" >"$x.filtered"
25 done
26 test_cmp "$1.filtered" "$2.filtered"
29 # This function uses a trick to manipulate the interactive add to use color:
30 # the `want_color()` function special-cases the situation where a pager was
31 # spawned and Git now wants to output colored text: to detect that situation,
32 # the environment variable `GIT_PAGER_IN_USE` is set. However, color is
33 # suppressed despite that environment variable if the `TERM` variable
34 # indicates a dumb terminal, so we set that variable, too.
36 force_color () {
37 # The first element of $@ may be a shell function, as a result POSIX
38 # does not guarantee that "one-shot assignment" will not persist after
39 # the function call. Thus, we prevent these variables from escaping
40 # this function's context with this subshell.
42 GIT_PAGER_IN_USE=true &&
43 TERM=vt100 &&
44 export GIT_PAGER_IN_USE TERM &&
45 "$@"
49 test_expect_success 'setup (initial)' '
50 echo content >file &&
51 git add file &&
52 echo more >>file &&
53 echo lines >>file
55 test_expect_success 'status works (initial)' '
56 git add -i </dev/null >output &&
57 grep "+1/-0 *+2/-0 file" output
60 test_expect_success 'setup expected' '
61 cat >expected <<-\EOF
62 new file mode 100644
63 index 0000000..d95f3ad
64 --- /dev/null
65 +++ b/file
66 @@ -0,0 +1 @@
67 +content
68 EOF
71 test_expect_success 'diff works (initial)' '
72 test_write_lines d 1 | git add -i >output &&
73 sed -ne "/new file/,/content/p" <output >diff &&
74 diff_cmp expected diff
76 test_expect_success 'revert works (initial)' '
77 git add file &&
78 test_write_lines r 1 | git add -i &&
79 git ls-files >output &&
80 ! grep . output
83 test_expect_success 'add untracked (multiple)' '
84 test_when_finished "git reset && rm [1-9]" &&
85 touch $(test_seq 9) &&
86 test_write_lines a "2-5 8-" | git add -i -- [1-9] &&
87 test_write_lines 2 3 4 5 8 9 >expected &&
88 git ls-files [1-9] >output &&
89 test_cmp expected output
92 test_expect_success 'setup (commit)' '
93 echo baseline >file &&
94 git add file &&
95 git commit -m commit &&
96 echo content >>file &&
97 git add file &&
98 echo more >>file &&
99 echo lines >>file
101 test_expect_success 'status works (commit)' '
102 git add -i </dev/null >output &&
103 grep "+1/-0 *+2/-0 file" output
106 test_expect_success 'update can stage deletions' '
107 >to-delete &&
108 git add to-delete &&
109 rm to-delete &&
110 test_write_lines u t "" | git add -i &&
111 git ls-files to-delete >output &&
112 test_must_be_empty output
115 test_expect_success 'setup expected' '
116 cat >expected <<-\EOF
117 index 180b47c..b6f2c08 100644
118 --- a/file
119 +++ b/file
120 @@ -1 +1,2 @@
121 baseline
122 +content
126 test_expect_success 'diff works (commit)' '
127 test_write_lines d 1 | git add -i >output &&
128 sed -ne "/^index/,/content/p" <output >diff &&
129 diff_cmp expected diff
131 test_expect_success 'revert works (commit)' '
132 git add file &&
133 test_write_lines r 1 | git add -i &&
134 git add -i </dev/null >output &&
135 grep "unchanged *+3/-0 file" output
138 test_expect_success 'setup expected' '
139 cat >expected <<-\EOF
143 test_expect_success 'dummy edit works' '
144 test_set_editor : &&
145 test_write_lines e a | git add -p &&
146 git diff > diff &&
147 diff_cmp expected diff
150 test_expect_success 'setup patch' '
151 cat >patch <<-\EOF
152 @@ -1,1 +1,4 @@
153 this
154 +patch
155 -does not
156 apply
160 test_expect_success 'setup fake editor' '
161 write_script "fake_editor.sh" <<-\EOF &&
162 mv -f "$1" oldpatch &&
163 mv -f patch "$1"
165 test_set_editor "$(pwd)/fake_editor.sh"
168 test_expect_success 'bad edit rejected' '
169 git reset &&
170 test_write_lines e n d | git add -p >output &&
171 grep "hunk does not apply" output
174 test_expect_success 'setup patch' '
175 cat >patch <<-\EOF
176 this patch
177 is garbage
181 test_expect_success 'garbage edit rejected' '
182 git reset &&
183 test_write_lines e n d | git add -p >output &&
184 grep "hunk does not apply" output
187 test_expect_success 'setup patch' '
188 cat >patch <<-\EOF
189 @@ -1,0 +1,0 @@
190 baseline
191 +content
192 +newcontent
193 +lines
197 test_expect_success 'setup expected' '
198 cat >expected <<-\EOF
199 diff --git a/file b/file
200 index b5dd6c9..f910ae9 100644
201 --- a/file
202 +++ b/file
203 @@ -1,4 +1,4 @@
204 baseline
205 content
206 -newcontent
207 +more
208 lines
212 test_expect_success 'real edit works' '
213 test_write_lines e n d | git add -p &&
214 git diff >output &&
215 diff_cmp expected output
218 test_expect_success 'setup file' '
219 test_write_lines a "" b "" c >file &&
220 git add file &&
221 test_write_lines a "" d "" c >file
224 test_expect_success 'setup patch' '
225 SP=" " &&
226 NULL="" &&
227 cat >patch <<-EOF
228 @@ -1,4 +1,4 @@
230 $NULL
238 test_expect_success 'setup expected' '
239 cat >expected <<-EOF
240 diff --git a/file b/file
241 index b5dd6c9..f910ae9 100644
242 --- a/file
243 +++ b/file
244 @@ -1,5 +1,5 @@
254 test_expect_success 'edit can strip spaces from empty context lines' '
255 test_write_lines e n q | git add -p 2>error &&
256 test_must_be_empty error &&
257 git diff >output &&
258 diff_cmp expected output
261 test_expect_success 'skip files similarly as commit -a' '
262 git reset &&
263 echo file >.gitignore &&
264 echo changed >file &&
265 echo y | git add -p file &&
266 git diff >output &&
267 git reset &&
268 git commit -am commit &&
269 git diff >expected &&
270 diff_cmp expected output &&
271 git reset --hard HEAD^
273 rm -f .gitignore
275 test_expect_success FILEMODE 'patch does not affect mode' '
276 git reset --hard &&
277 echo content >>file &&
278 chmod +x file &&
279 printf "n\\ny\\n" | git add -p &&
280 git show :file | grep content &&
281 git diff file | grep "new mode"
284 test_expect_success FILEMODE 'stage mode but not hunk' '
285 git reset --hard &&
286 echo content >>file &&
287 chmod +x file &&
288 printf "y\\nn\\n" | git add -p &&
289 git diff --cached file | grep "new mode" &&
290 git diff file | grep "+content"
294 test_expect_success FILEMODE 'stage mode and hunk' '
295 git reset --hard &&
296 echo content >>file &&
297 chmod +x file &&
298 printf "y\\ny\\n" | git add -p &&
299 git diff --cached file | grep "new mode" &&
300 git diff --cached file | grep "+content" &&
301 test -z "$(git diff file)"
304 # end of tests disabled when filemode is not usable
306 test_expect_success 'different prompts for mode change/deleted' '
307 git reset --hard &&
308 >file &&
309 >deleted &&
310 git add --chmod=+x file deleted &&
311 echo changed >file &&
312 rm deleted &&
313 test_write_lines n n n |
314 git -c core.filemode=true add -p >actual &&
315 sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
316 cat >expect <<-\EOF &&
317 (1/1) Stage deletion [y,n,q,a,d,?]?
318 (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,?]?
319 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
321 test_cmp expect actual.filtered
324 test_expect_success 'correct message when there is nothing to do' '
325 git reset --hard &&
326 git add -p 2>err &&
327 test_i18ngrep "No changes" err &&
328 printf "\\0123" >binary &&
329 git add binary &&
330 printf "\\0abc" >binary &&
331 git add -p 2>err &&
332 test_i18ngrep "Only binary files changed" err
335 test_expect_success 'setup again' '
336 git reset --hard &&
337 test_chmod +x file &&
338 echo content >>file &&
339 test_write_lines A B C D>file2 &&
340 git add file2
343 # Write the patch file with a new line at the top and bottom
344 test_expect_success 'setup patch' '
345 cat >patch <<-\EOF
346 index 180b47c..b6f2c08 100644
347 --- a/file
348 +++ b/file
349 @@ -1,2 +1,4 @@
350 +firstline
351 baseline
352 content
353 +lastline
354 \ No newline at end of file
355 diff --git a/file2 b/file2
356 index 8422d40..35b930a 100644
357 --- a/file2
358 +++ b/file2
359 @@ -1,4 +1,5 @@
370 # Expected output, diff is similar to the patch but w/ diff at the top
371 test_expect_success 'setup expected' '
372 echo diff --git a/file b/file >expected &&
373 sed -e "/^index 180b47c/s/ 100644/ 100755/" \
374 -e /1,5/s//1,4/ \
375 -e /Y/d patch >>expected &&
376 cat >expected-output <<-\EOF
377 --- a/file
378 +++ b/file
379 @@ -1,2 +1,4 @@
380 +firstline
381 baseline
382 content
383 +lastline
384 \ No newline at end of file
385 @@ -1,2 +1,3 @@
386 +firstline
387 baseline
388 content
389 @@ -1,2 +2,3 @@
390 baseline
391 content
392 +lastline
393 \ No newline at end of file
394 --- a/file2
395 +++ b/file2
396 @@ -1,4 +1,5 @@
404 @@ -1,2 +1,2 @@
408 @@ -2,2 +2,3 @@
412 @@ -3,2 +4,2 @@
419 # Test splitting the first patch, then adding both
420 test_expect_success 'add first line works' '
421 git commit -am "clear local changes" &&
422 git apply patch &&
423 test_write_lines s y y s y n y | git add -p 2>error >raw-output &&
424 sed -n -e "s/^([1-9]\/[1-9]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
425 -e "/^[-+@ \\\\]"/p raw-output >output &&
426 test_must_be_empty error &&
427 git diff --cached >diff &&
428 diff_cmp expected diff &&
429 test_cmp expected-output output
432 test_expect_success 'setup expected' '
433 cat >expected <<-\EOF
434 diff --git a/non-empty b/non-empty
435 deleted file mode 100644
436 index d95f3ad..0000000
437 --- a/non-empty
438 +++ /dev/null
439 @@ -1 +0,0 @@
440 -content
444 test_expect_success 'deleting a non-empty file' '
445 git reset --hard &&
446 echo content >non-empty &&
447 git add non-empty &&
448 git commit -m non-empty &&
449 rm non-empty &&
450 echo y | git add -p non-empty &&
451 git diff --cached >diff &&
452 diff_cmp expected diff
455 test_expect_success 'setup expected' '
456 cat >expected <<-\EOF
457 diff --git a/empty b/empty
458 deleted file mode 100644
459 index e69de29..0000000
463 test_expect_success 'deleting an empty file' '
464 git reset --hard &&
465 > empty &&
466 git add empty &&
467 git commit -m empty &&
468 rm empty &&
469 echo y | git add -p empty &&
470 git diff --cached >diff &&
471 diff_cmp expected diff
474 test_expect_success 'adding an empty file' '
475 git init added &&
477 cd added &&
478 test_commit initial &&
479 >empty &&
480 git add empty &&
481 test_tick &&
482 git commit -m empty &&
483 git tag added-file &&
484 git reset --hard HEAD^ &&
485 test_path_is_missing empty &&
487 echo y | git checkout -p added-file -- >actual &&
488 test_path_is_file empty &&
489 test_i18ngrep "Apply addition to index and worktree" actual
493 test_expect_success 'split hunk setup' '
494 git reset --hard &&
495 test_write_lines 10 20 30 40 50 60 >test &&
496 git add test &&
497 test_tick &&
498 git commit -m test &&
500 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
503 test_expect_success 'goto hunk' '
504 test_when_finished "git reset" &&
505 tr _ " " >expect <<-EOF &&
506 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? + 1: -1,2 +1,3 +15
507 _ 2: -2,4 +3,8 +21
508 go to which hunk? @@ -1,2 +1,3 @@
512 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
514 test_write_lines s y g 1 | git add -p >actual &&
515 tail -n 7 <actual >actual.trimmed &&
516 test_cmp expect actual.trimmed
519 test_expect_success 'navigate to hunk via regex' '
520 test_when_finished "git reset" &&
521 tr _ " " >expect <<-EOF &&
522 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? @@ -1,2 +1,3 @@
526 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
528 test_write_lines s y /1,2 | git add -p >actual &&
529 tail -n 5 <actual >actual.trimmed &&
530 test_cmp expect actual.trimmed
533 test_expect_success 'split hunk "add -p (edit)"' '
534 # Split, say Edit and do nothing. Then:
536 # 1. Broken version results in a patch that does not apply and
537 # only takes [y/n] (edit again) so the first q is discarded
538 # and then n attempts to discard the edit. Repeat q enough
539 # times to get out.
541 # 2. Correct version applies the (not)edited version, and asks
542 # about the next hunk, against which we say q and program
543 # exits.
544 printf "%s\n" s e q n q q |
545 EDITOR=: git add -p &&
546 git diff >actual &&
547 ! grep "^+15" actual
550 test_expect_success 'setup ADD_I_USE_BUILTIN check' '
551 result=success &&
552 if ! test_have_prereq ADD_I_USE_BUILTIN
553 then
554 result=failure
558 test_expect_$result 'split hunk "add -p (no, yes, edit)"' '
559 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
560 git reset &&
561 # test sequence is s(plit), n(o), y(es), e(dit)
562 # q n q q is there to make sure we exit at the end.
563 printf "%s\n" s n y e q n q q |
564 EDITOR=: git add -p 2>error &&
565 test_must_be_empty error &&
566 git diff >actual &&
567 ! grep "^+31" actual
570 test_expect_success 'split hunk with incomplete line at end' '
571 git reset --hard &&
572 printf "missing LF" >>test &&
573 git add test &&
574 test_write_lines before 10 20 30 40 50 60 70 >test &&
575 git grep --cached missing &&
576 test_write_lines s n y q | git add -p &&
577 test_must_fail git grep --cached missing &&
578 git grep before &&
579 test_must_fail git grep --cached before
582 test_expect_$result 'edit, adding lines to the first hunk' '
583 test_write_lines 10 11 20 30 40 50 51 60 >test &&
584 git reset &&
585 tr _ " " >patch <<-EOF &&
586 @@ -1,5 +1,6 @@
595 # test sequence is s(plit), e(dit), n(o)
596 # q n q q is there to make sure we exit at the end.
597 printf "%s\n" s e n q n q q |
598 EDITOR=./fake_editor.sh git add -p 2>error &&
599 test_must_be_empty error &&
600 git diff --cached >actual &&
601 grep "^+22" actual
604 test_expect_success 'patch mode ignores unmerged entries' '
605 git reset --hard &&
606 test_commit conflict &&
607 test_commit non-conflict &&
608 git checkout -b side &&
609 test_commit side conflict.t &&
610 git checkout main &&
611 test_commit main conflict.t &&
612 test_must_fail git merge side &&
613 echo changed >non-conflict.t &&
614 echo y | git add -p >output &&
615 ! grep a/conflict.t output &&
616 cat >expected <<-\EOF &&
617 * Unmerged path conflict.t
618 diff --git a/non-conflict.t b/non-conflict.t
619 index f766221..5ea2ed4 100644
620 --- a/non-conflict.t
621 +++ b/non-conflict.t
622 @@ -1 +1 @@
623 -non-conflict
624 +changed
626 git diff --cached >diff &&
627 diff_cmp expected diff
630 test_expect_success 'index is refreshed after applying patch' '
631 git reset --hard &&
632 echo content >test &&
633 printf y | git add -p &&
634 git diff-files --exit-code
637 test_expect_success 'diffs can be colorized' '
638 git reset --hard &&
640 echo content >test &&
641 printf y >y &&
642 force_color git add -p >output 2>&1 <y &&
643 git diff-files --exit-code &&
645 # We do not want to depend on the exact coloring scheme
646 # git uses for diffs, so just check that we saw some kind of color.
647 grep "$(printf "\\033")" output
650 test_expect_success 'colors can be overridden' '
651 git reset --hard &&
652 test_when_finished "git rm -f color-test" &&
653 test_write_lines context old more-context >color-test &&
654 git add color-test &&
655 test_write_lines context new more-context another-one >color-test &&
657 echo trigger an error message >input &&
658 force_color git \
659 -c color.interactive.error=blue \
660 add -i 2>err.raw <input &&
661 test_decode_color <err.raw >err &&
662 grep "<BLUE>Huh (trigger)?<RESET>" err &&
664 test_write_lines help quit >input &&
665 force_color git \
666 -c color.interactive.header=red \
667 -c color.interactive.help=green \
668 -c color.interactive.prompt=yellow \
669 add -i >actual.raw <input &&
670 test_decode_color <actual.raw >actual &&
671 cat >expect <<-\EOF &&
672 <RED> staged unstaged path<RESET>
673 1: +3/-0 +2/-1 color-test
675 <RED>*** Commands ***<RESET>
676 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
677 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
678 <YELLOW>What now<RESET>> <GREEN>status - show paths with changes<RESET>
679 <GREEN>update - add working tree state to the staged set of changes<RESET>
680 <GREEN>revert - revert staged set of changes back to the HEAD version<RESET>
681 <GREEN>patch - pick hunks and update selectively<RESET>
682 <GREEN>diff - view diff between HEAD and index<RESET>
683 <GREEN>add untracked - add contents of untracked files to the staged set of changes<RESET>
684 <RED>*** Commands ***<RESET>
685 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
686 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
687 <YELLOW>What now<RESET>> Bye.
689 test_cmp expect actual &&
691 : exercise recolor_hunk by editing and then look at the hunk again &&
692 test_write_lines s e K q >input &&
693 force_color git \
694 -c color.interactive.prompt=yellow \
695 -c color.diff.meta=italic \
696 -c color.diff.frag=magenta \
697 -c color.diff.context=cyan \
698 -c color.diff.old=bold \
699 -c color.diff.new=blue \
700 -c core.editor=touch \
701 add -p >actual.raw <input &&
702 test_decode_color <actual.raw >actual.decoded &&
703 sed "s/index [0-9a-f]*\\.\\.[0-9a-f]* 100644/<INDEX-LINE>/" <actual.decoded >actual &&
704 cat >expect <<-\EOF &&
705 <ITALIC>diff --git a/color-test b/color-test<RESET>
706 <ITALIC><INDEX-LINE><RESET>
707 <ITALIC>--- a/color-test<RESET>
708 <ITALIC>+++ b/color-test<RESET>
709 <MAGENTA>@@ -1,3 +1,4 @@<RESET>
710 <CYAN> context<RESET>
711 <BOLD>-old<RESET>
712 <BLUE>+<RESET><BLUE>new<RESET>
713 <CYAN> more-context<RESET>
714 <BLUE>+<RESET><BLUE>another-one<RESET>
715 <YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
716 <MAGENTA>@@ -1,3 +1,3 @@<RESET>
717 <CYAN> context<RESET>
718 <BOLD>-old<RESET>
719 <BLUE>+<RESET><BLUE>new<RESET>
720 <CYAN> more-context<RESET>
721 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
722 <CYAN> more-context<RESET>
723 <BLUE>+<RESET><BLUE>another-one<RESET>
724 <YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
725 <CYAN> context<RESET>
726 <BOLD>-old<RESET>
727 <BLUE>+new<RESET>
728 <CYAN> more-context<RESET>
729 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? <RESET>
731 test_cmp expect actual
734 test_expect_success 'colorized diffs respect diff.wsErrorHighlight' '
735 git reset --hard &&
737 echo "old " >test &&
738 git add test &&
739 echo "new " >test &&
741 printf y >y &&
742 force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
743 test_decode_color <output.raw >output &&
744 grep "old<" output
747 test_expect_success 'diffFilter filters diff' '
748 git reset --hard &&
750 echo content >test &&
751 test_config interactive.diffFilter "sed s/^/foo:/" &&
752 printf y >y &&
753 force_color git add -p >output 2>&1 <y &&
755 # avoid depending on the exact coloring or content of the prompts,
756 # and just make sure we saw our diff prefixed
757 grep foo:.*content output
760 test_expect_success 'detect bogus diffFilter output' '
761 git reset --hard &&
763 echo content >test &&
764 test_config interactive.diffFilter "sed 6d" &&
765 printf y >y &&
766 force_color test_must_fail git add -p <y >output 2>&1 &&
767 grep "mismatched output" output
770 test_expect_success 'handle iffy colored hunk headers' '
771 git reset --hard &&
773 echo content >test &&
774 printf n >n &&
775 force_color git -c interactive.diffFilter="sed s/.*@@.*/XX/" \
776 add -p >output 2>&1 <n &&
777 grep "^XX$" output
780 test_expect_success 'handle very large filtered diff' '
781 git reset --hard &&
782 # The specific number here is not important, but it must
783 # be large enough that the output of "git diff --color"
784 # fills up the pipe buffer. 10,000 results in ~200k of
785 # colored output.
786 test_seq 10000 >test &&
787 test_config interactive.diffFilter cat &&
788 printf y >y &&
789 force_color git add -p >output 2>&1 <y &&
790 git diff-files --exit-code -- test
793 test_expect_success 'diff.algorithm is passed to `git diff-files`' '
794 git reset --hard &&
796 >file &&
797 git add file &&
798 echo changed >file &&
799 test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
800 test_i18ngrep "error: option diff-algorithm accepts " err
803 test_expect_success 'patch-mode via -i prompts for files' '
804 git reset --hard &&
806 echo one >file &&
807 echo two >test &&
808 git add -i <<-\EOF &&
809 patch
810 test
813 quit
816 echo test >expect &&
817 git diff --cached --name-only >actual &&
818 diff_cmp expect actual
821 test_expect_success 'add -p handles globs' '
822 git reset --hard &&
824 mkdir -p subdir &&
825 echo base >one.c &&
826 echo base >subdir/two.c &&
827 git add "*.c" &&
828 git commit -m base &&
830 echo change >one.c &&
831 echo change >subdir/two.c &&
832 git add -p "*.c" <<-\EOF &&
837 cat >expect <<-\EOF &&
838 one.c
839 subdir/two.c
841 git diff --cached --name-only >actual &&
842 test_cmp expect actual
845 test_expect_success 'add -p handles relative paths' '
846 git reset --hard &&
848 echo base >relpath.c &&
849 git add "*.c" &&
850 git commit -m relpath &&
852 echo change >relpath.c &&
853 mkdir -p subdir &&
854 git -C subdir add -p .. 2>error <<-\EOF &&
858 test_must_be_empty error &&
860 cat >expect <<-\EOF &&
861 relpath.c
863 git diff --cached --name-only >actual &&
864 test_cmp expect actual
867 test_expect_success 'add -p does not expand argument lists' '
868 git reset --hard &&
870 echo content >not-changed &&
871 git add not-changed &&
872 git commit -m "add not-changed file" &&
874 echo change >file &&
875 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
879 # we know that "file" must be mentioned since we actually
880 # update it, but we want to be sure that our "." pathspec
881 # was not expanded into the argument list of any command.
882 # So look only for "not-changed".
883 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
886 test_expect_success 'hunk-editing handles custom comment char' '
887 git reset --hard &&
888 echo change >>file &&
889 test_config core.commentChar "\$" &&
890 echo e | GIT_EDITOR=true git add -p &&
891 git diff --exit-code
894 test_expect_success 'add -p works even with color.ui=always' '
895 git reset --hard &&
896 echo change >>file &&
897 test_config color.ui always &&
898 echo y | git add -p &&
899 echo file >expect &&
900 git diff --cached --name-only >actual &&
901 test_cmp expect actual
904 test_expect_success 'setup different kinds of dirty submodules' '
905 test_create_repo for-submodules &&
907 cd for-submodules &&
908 test_commit initial &&
909 test_create_repo dirty-head &&
911 cd dirty-head &&
912 test_commit initial
913 ) &&
914 cp -R dirty-head dirty-otherwise &&
915 cp -R dirty-head dirty-both-ways &&
916 git add dirty-head &&
917 git add dirty-otherwise dirty-both-ways &&
918 git commit -m initial &&
920 cd dirty-head &&
921 test_commit updated &&
922 cd ../dirty-both-ways &&
923 test_commit updated &&
924 echo dirty >>initial &&
925 : >untracked &&
926 cd ../dirty-otherwise &&
927 echo dirty >>initial &&
928 : >untracked
929 ) &&
930 git -C for-submodules diff-files --name-only >actual &&
931 cat >expected <<-\EOF &&
932 dirty-both-ways
933 dirty-head
935 test_cmp expected actual &&
936 git -C for-submodules diff-files --name-only --ignore-submodules=none >actual &&
937 cat >expected <<-\EOF &&
938 dirty-both-ways
939 dirty-head
940 dirty-otherwise
942 test_cmp expected actual &&
943 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
944 cat >expected <<-\EOF &&
945 dirty-both-ways
946 dirty-head
948 test_cmp expected actual
951 test_expect_success 'status ignores dirty submodules (except HEAD)' '
952 git -C for-submodules add -i </dev/null >output &&
953 grep dirty-head output &&
954 grep dirty-both-ways output &&
955 ! grep dirty-otherwise output
958 test_expect_success 'handle submodules' '
959 echo 123 >>for-submodules/dirty-otherwise/initial.t &&
961 force_color git -C for-submodules add -p dirty-otherwise >output 2>&1 &&
962 grep "No changes" output &&
964 force_color git -C for-submodules add -p dirty-head >output 2>&1 <y &&
965 git -C for-submodules ls-files --stage dirty-head >actual &&
966 rev="$(git -C for-submodules/dirty-head rev-parse HEAD)" &&
967 grep "$rev" actual
970 test_expect_success 'set up pathological context' '
971 git reset --hard &&
972 test_write_lines a a a a a a a a a a a >a &&
973 git add a &&
974 git commit -m a &&
975 test_write_lines c b a a a a a a a b a a a a >a &&
976 test_write_lines a a a a a a a b a a a a >expected-1 &&
977 test_write_lines b a a a a a a a b a a a a >expected-2 &&
978 # check editing can cope with missing header and deleted context lines
979 # as well as changes to other lines
980 test_write_lines +b " a" >patch
983 test_expect_success 'add -p works with pathological context lines' '
984 git reset &&
985 printf "%s\n" n y |
986 git add -p &&
987 git cat-file blob :a >actual &&
988 test_cmp expected-1 actual
991 test_expect_success 'add -p patch editing works with pathological context lines' '
992 git reset &&
993 # n q q below is in case edit fails
994 printf "%s\n" e y n q q |
995 git add -p &&
996 git cat-file blob :a >actual &&
997 test_cmp expected-2 actual
1000 test_expect_success 'checkout -p works with pathological context lines' '
1001 test_write_lines a a a a a a >a &&
1002 git add a &&
1003 test_write_lines a b a b a b a b a b a >a &&
1004 test_write_lines s n n y q | git checkout -p &&
1005 test_write_lines a b a b a a b a b a >expect &&
1006 test_cmp expect a
1009 # This should be called from a subshell as it sets a temporary editor
1010 setup_new_file() {
1011 write_script new-file-editor.sh <<-\EOF &&
1012 sed /^#/d "$1" >patch &&
1013 sed /^+c/d patch >"$1"
1015 test_set_editor "$(pwd)/new-file-editor.sh" &&
1016 test_write_lines a b c d e f >new-file &&
1017 test_write_lines a b d e f >new-file-expect &&
1018 test_write_lines "@@ -0,0 +1,6 @@" +a +b +c +d +e +f >patch-expect
1021 test_expect_success 'add -N followed by add -p patch editing' '
1022 git reset --hard &&
1024 setup_new_file &&
1025 git add -N new-file &&
1026 test_write_lines e n q | git add -p &&
1027 git cat-file blob :new-file >actual &&
1028 test_cmp new-file-expect actual &&
1029 test_cmp patch-expect patch
1033 test_expect_success 'checkout -p patch editing of added file' '
1034 git reset --hard &&
1036 setup_new_file &&
1037 git add new-file &&
1038 git commit -m "add new file" &&
1039 git rm new-file &&
1040 git commit -m "remove new file" &&
1041 test_write_lines e n q | git checkout -p HEAD^ &&
1042 test_cmp new-file-expect new-file &&
1043 test_cmp patch-expect patch
1047 test_expect_success 'show help from add--helper' '
1048 git reset --hard &&
1049 cat >expect <<-EOF &&
1051 <BOLD>*** Commands ***<RESET>
1052 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
1053 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1054 <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes<RESET>
1055 <BOLD;RED>update - add working tree state to the staged set of changes<RESET>
1056 <BOLD;RED>revert - revert staged set of changes back to the HEAD version<RESET>
1057 <BOLD;RED>patch - pick hunks and update selectively<RESET>
1058 <BOLD;RED>diff - view diff between HEAD and index<RESET>
1059 <BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
1060 <BOLD>*** Commands ***<RESET>
1061 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
1062 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1063 <BOLD;BLUE>What now<RESET>>$SP
1064 Bye.
1066 test_write_lines h | force_color git add -i >actual.colored &&
1067 test_decode_color <actual.colored >actual &&
1068 test_cmp expect actual
1071 test_expect_success 'reset -p with unmerged files' '
1072 test_when_finished "git checkout --force main" &&
1073 test_commit one conflict &&
1074 git checkout -B side HEAD^ &&
1075 test_commit two conflict &&
1076 test_must_fail git merge one &&
1078 # this is a noop with only an unmerged entry
1079 git reset -p &&
1081 # add files that sort before and after unmerged entry
1082 echo a >a &&
1083 echo z >z &&
1084 git add a z &&
1086 # confirm that we can reset those files
1087 printf "%s\n" y y | git reset -p &&
1088 git diff-index --cached --diff-filter=u HEAD >staged &&
1089 test_must_be_empty staged
1092 test_done