Start the 2.46 cycle
[git/gitster.git] / t / t3701-add-interactive.sh
blob04d833337342cff2fd9842a0b555727b49644059
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 diff_cmp () {
12 for x
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 /' \
19 "$x" >"$x.filtered"
20 done
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.
31 force_color () {
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 &&
38 TERM=vt100 &&
39 export GIT_PAGER_IN_USE TERM &&
40 "$@"
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.
48 No changes.
49 EOF
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
56 done
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 'setup expected' '
149 cat >expected <<-\EOF
153 test_expect_success 'dummy edit works' '
154 test_set_editor : &&
155 test_write_lines e a | git add -p &&
156 git diff > diff &&
157 diff_cmp expected diff
160 test_expect_success 'setup patch' '
161 cat >patch <<-\EOF
162 @@ -1,1 +1,4 @@
163 this
164 +patch
165 -does not
166 apply
170 test_expect_success 'setup fake editor' '
171 write_script "fake_editor.sh" <<-\EOF &&
172 mv -f "$1" oldpatch &&
173 mv -f patch "$1"
175 test_set_editor "$(pwd)/fake_editor.sh"
178 test_expect_success 'bad edit rejected' '
179 git reset &&
180 test_write_lines e n d | git add -p >output &&
181 grep "hunk does not apply" output
184 test_expect_success 'setup patch' '
185 cat >patch <<-\EOF
186 this patch
187 is garbage
191 test_expect_success 'garbage edit rejected' '
192 git reset &&
193 test_write_lines e n d | git add -p >output &&
194 grep "hunk does not apply" output
197 test_expect_success 'setup patch' '
198 cat >patch <<-\EOF
199 @@ -1,0 +1,0 @@
200 baseline
201 +content
202 +newcontent
203 +lines
207 test_expect_success 'setup expected' '
208 cat >expected <<-\EOF
209 diff --git a/file b/file
210 index b5dd6c9..f910ae9 100644
211 --- a/file
212 +++ b/file
213 @@ -1,4 +1,4 @@
214 baseline
215 content
216 -newcontent
217 +more
218 lines
222 test_expect_success 'real edit works' '
223 test_write_lines e n d | git add -p &&
224 git diff >output &&
225 diff_cmp expected output
228 test_expect_success 'setup file' '
229 test_write_lines a "" b "" c >file &&
230 git add file &&
231 test_write_lines a "" d "" c >file
234 test_expect_success 'setup patch' '
235 SP=" " &&
236 NULL="" &&
237 cat >patch <<-EOF
238 @@ -1,4 +1,4 @@
240 $NULL
248 test_expect_success 'setup expected' '
249 cat >expected <<-EOF
250 diff --git a/file b/file
251 index b5dd6c9..f910ae9 100644
252 --- a/file
253 +++ b/file
254 @@ -1,5 +1,5 @@
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 &&
267 git diff >output &&
268 diff_cmp expected output
271 test_expect_success 'skip files similarly as commit -a' '
272 git reset &&
273 echo file >.gitignore &&
274 echo changed >file &&
275 echo y | git add -p file &&
276 git diff >output &&
277 git reset &&
278 git commit -am commit &&
279 git diff >expected &&
280 diff_cmp expected output &&
281 git reset --hard HEAD^
283 rm -f .gitignore
285 test_expect_success FILEMODE 'patch does not affect mode' '
286 git reset --hard &&
287 echo content >>file &&
288 chmod +x 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' '
295 git reset --hard &&
296 echo content >>file &&
297 chmod +x 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' '
305 git reset --hard &&
306 echo content >>file &&
307 chmod +x 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' '
319 git reset --hard &&
320 >file &&
321 >deleted &&
322 git add --chmod=+x file deleted &&
323 echo changed >file &&
324 rm deleted &&
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' '
337 git reset --hard &&
338 git add -p 2>err &&
339 test_grep "No changes" err &&
340 printf "\\0123" >binary &&
341 git add binary &&
342 printf "\\0abc" >binary &&
343 git add -p 2>err &&
344 test_grep "Only binary files changed" err
347 test_expect_success 'setup again' '
348 git reset --hard &&
349 test_chmod +x file &&
350 echo content >>file &&
351 test_write_lines A B C D>file2 &&
352 git add file2
355 # Write the patch file with a new line at the top and bottom
356 test_expect_success 'setup patch' '
357 cat >patch <<-\EOF
358 index 180b47c..b6f2c08 100644
359 --- a/file
360 +++ b/file
361 @@ -1,2 +1,4 @@
362 +firstline
363 baseline
364 content
365 +lastline
366 \ No newline at end of file
367 diff --git a/file2 b/file2
368 index 8422d40..35b930a 100644
369 --- a/file2
370 +++ b/file2
371 @@ -1,4 +1,5 @@
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/" \
386 -e /1,5/s//1,4/ \
387 -e /Y/d patch >>expected &&
388 cat >expected-output <<-\EOF
389 --- a/file
390 +++ b/file
391 @@ -1,2 +1,4 @@
392 +firstline
393 baseline
394 content
395 +lastline
396 \ No newline at end of file
397 @@ -1,2 +1,3 @@
398 +firstline
399 baseline
400 content
401 @@ -1,2 +2,3 @@
402 baseline
403 content
404 +lastline
405 \ No newline at end of file
406 --- a/file2
407 +++ b/file2
408 @@ -1,4 +1,5 @@
416 @@ -1,2 +1,2 @@
420 @@ -2,2 +2,3 @@
424 @@ -3,2 +4,2 @@
431 # Test splitting the first patch, then adding both
432 test_expect_success 'add first line works' '
433 git commit -am "clear local changes" &&
434 git apply patch &&
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
449 --- a/non-empty
450 +++ /dev/null
451 @@ -1 +0,0 @@
452 -content
456 test_expect_success 'deleting a non-empty file' '
457 git reset --hard &&
458 echo content >non-empty &&
459 git add non-empty &&
460 git commit -m non-empty &&
461 rm 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' '
476 git reset --hard &&
477 > empty &&
478 git add empty &&
479 git commit -m empty &&
480 rm 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' '
487 git init added &&
489 cd added &&
490 test_commit initial &&
491 >empty &&
492 git add empty &&
493 test_tick &&
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' '
506 git reset --hard &&
507 test_write_lines 10 20 30 40 50 60 >test &&
508 git add test &&
509 test_tick &&
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
519 _ 2: -2,4 +3,8 +21
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
551 # times to get out.
553 # 2. Correct version applies the (not)edited version, and asks
554 # about the next hunk, against which we say q and program
555 # exits.
556 printf "%s\n" s e q n q q |
557 EDITOR=: git add -p &&
558 git diff >actual &&
559 ! grep "^+15" actual
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 &&
564 git reset &&
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 &&
570 git diff >actual &&
571 ! grep "^+31" actual
574 test_expect_success 'split hunk with incomplete line at end' '
575 git reset --hard &&
576 printf "missing LF" >>test &&
577 git add 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 &&
582 git grep before &&
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 &&
588 git reset &&
589 tr _ " " >patch <<-EOF &&
590 @@ -1,5 +1,6 @@
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 &&
605 grep "^+22" actual
608 test_expect_success 'patch mode ignores unmerged entries' '
609 git reset --hard &&
610 test_commit conflict &&
611 test_commit non-conflict &&
612 git checkout -b side &&
613 test_commit side conflict.t &&
614 git checkout main &&
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
624 --- a/non-conflict.t
625 +++ b/non-conflict.t
626 @@ -1 +1 @@
627 -non-conflict
628 +changed
630 git diff --cached >diff &&
631 diff_cmp expected diff
634 test_expect_success 'index is refreshed after applying patch' '
635 git reset --hard &&
636 echo content >test &&
637 printf y | git add -p &&
638 git diff-files --exit-code
641 test_expect_success 'diffs can be colorized' '
642 git reset --hard &&
644 echo content >test &&
645 printf y >y &&
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' '
655 git reset --hard &&
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 &&
662 force_color git \
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 &&
669 force_color git \
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 &&
697 force_color git \
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>
715 <BOLD>-old<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>
722 <BOLD>-old<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>
730 <BOLD>-old<RESET>
731 <BLUE>+new<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' '
739 git reset --hard &&
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
752 |*** Commands ***
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
755 |What now> Bye.
758 test_cmp expect actual
761 test_expect_success 'colors can be skipped with color.ui=false' '
762 git reset --hard &&
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 &&
769 force_color git \
770 -c color.ui=false \
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' '
777 git reset --hard &&
779 echo "old " >test &&
780 git add test &&
781 echo "new " >test &&
783 printf y >y &&
784 force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
785 test_decode_color <output.raw >output &&
786 grep "old<" output
789 test_expect_success 'diffFilter filters diff' '
790 git reset --hard &&
792 echo content >test &&
793 test_config interactive.diffFilter "sed s/^/foo:/" &&
794 printf y >y &&
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' '
803 git reset --hard &&
805 echo content >test &&
806 test_config interactive.diffFilter "sed 6d" &&
807 printf y >y &&
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' '
813 git reset --hard &&
815 echo content >test &&
816 printf n >n &&
817 force_color git -c interactive.diffFilter="sed s/.*@@.*/XX/" \
818 add -p >output 2>&1 <n &&
819 grep "^XX$" output
822 test_expect_success 'handle very large filtered diff' '
823 git reset --hard &&
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
827 # colored output.
828 test_seq 10000 >test &&
829 test_config interactive.diffFilter cat &&
830 printf y >y &&
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`' '
836 git reset --hard &&
838 >file &&
839 git add file &&
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' '
846 git reset --hard &&
848 echo one >file &&
849 echo two >test &&
850 git add -i <<-\EOF &&
851 patch
852 test
855 quit
858 echo test >expect &&
859 git diff --cached --name-only >actual &&
860 diff_cmp expect actual
863 test_expect_success 'add -p handles globs' '
864 git reset --hard &&
866 mkdir -p subdir &&
867 echo base >one.c &&
868 echo base >subdir/two.c &&
869 git add "*.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 &&
880 one.c
881 subdir/two.c
883 git diff --cached --name-only >actual &&
884 test_cmp expect actual
887 test_expect_success 'add -p handles relative paths' '
888 git reset --hard &&
890 echo base >relpath.c &&
891 git add "*.c" &&
892 git commit -m relpath &&
894 echo change >relpath.c &&
895 mkdir -p subdir &&
896 git -C subdir add -p .. 2>error <<-\EOF &&
900 test_must_be_empty error &&
902 cat >expect <<-\EOF &&
903 relpath.c
905 git diff --cached --name-only >actual &&
906 test_cmp expect actual
909 test_expect_success 'add -p does not expand argument lists' '
910 git reset --hard &&
912 echo content >not-changed &&
913 git add not-changed &&
914 git commit -m "add not-changed file" &&
916 echo change >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' '
929 git reset --hard &&
930 echo change >>file &&
931 test_config core.commentChar "\$" &&
932 echo e | GIT_EDITOR=true git add -p &&
933 git diff --exit-code
936 test_expect_success 'add -p works even with color.ui=always' '
937 git reset --hard &&
938 echo change >>file &&
939 test_config color.ui always &&
940 echo y | git add -p &&
941 echo file >expect &&
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 &&
949 cd for-submodules &&
950 test_commit initial &&
951 test_create_repo dirty-head &&
953 cd dirty-head &&
954 test_commit initial
955 ) &&
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 &&
962 cd dirty-head &&
963 test_commit updated &&
964 cd ../dirty-both-ways &&
965 test_commit updated &&
966 echo dirty >>initial &&
967 : >untracked &&
968 cd ../dirty-otherwise &&
969 echo dirty >>initial &&
970 : >untracked
971 ) &&
972 git -C for-submodules diff-files --name-only >actual &&
973 cat >expected <<-\EOF &&
974 dirty-both-ways
975 dirty-head
977 test_cmp expected actual &&
978 git -C for-submodules diff-files --name-only --ignore-submodules=none >actual &&
979 cat >expected <<-\EOF &&
980 dirty-both-ways
981 dirty-head
982 dirty-otherwise
984 test_cmp expected actual &&
985 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
986 cat >expected <<-\EOF &&
987 dirty-both-ways
988 dirty-head
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)" &&
1009 grep "$rev" actual
1012 test_expect_success 'set up pathological context' '
1013 git reset --hard &&
1014 test_write_lines a a a a a a a a a a a >a &&
1015 git add a &&
1016 git commit -m 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' '
1026 git reset &&
1027 printf "%s\n" n y |
1028 git add -p &&
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' '
1034 git reset &&
1035 # n q q below is in case edit fails
1036 printf "%s\n" e y n q q |
1037 git add -p &&
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 &&
1044 git add 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 &&
1048 test_cmp expect a
1051 # This should be called from a subshell as it sets a temporary editor
1052 setup_new_file() {
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' '
1064 git reset --hard &&
1066 setup_new_file &&
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' '
1076 git reset --hard &&
1078 setup_new_file &&
1079 git add new-file &&
1080 git commit -m "add new file" &&
1081 git rm 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' '
1090 git reset --hard &&
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
1106 Bye.
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
1121 git reset -p &&
1123 # add files that sort before and after unmerged entry
1124 echo a >a &&
1125 echo z >z &&
1126 git add a z &&
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
1134 test_done