chainlint.pl: force CRLF conversion when opening input files
[alt-git.git] / t / t3701-add-interactive.sh
blob5d78868ac168886c45dbb8d6359ca3b535a2990c
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 'split hunk "add -p (edit)"' '
579 # Split, say Edit and do nothing. Then:
581 # 1. Broken version results in a patch that does not apply and
582 # only takes [y/n] (edit again) so the first q is discarded
583 # and then n attempts to discard the edit. Repeat q enough
584 # times to get out.
586 # 2. Correct version applies the (not)edited version, and asks
587 # about the next hunk, against which we say q and program
588 # exits.
589 printf "%s\n" s e q n q q |
590 EDITOR=: git add -p &&
591 git diff >actual &&
592 ! grep "^+15" actual
595 test_expect_success 'split hunk "add -p (no, yes, edit)"' '
596 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
597 git reset &&
598 # test sequence is s(plit), n(o), y(es), e(dit)
599 # q n q q is there to make sure we exit at the end.
600 printf "%s\n" s n y e q n q q |
601 EDITOR=: git add -p 2>error &&
602 test_must_be_empty error &&
603 git diff >actual &&
604 ! grep "^+31" actual
607 test_expect_success 'split hunk with incomplete line at end' '
608 git reset --hard &&
609 printf "missing LF" >>test &&
610 git add test &&
611 test_write_lines before 10 20 30 40 50 60 70 >test &&
612 git grep --cached missing &&
613 test_write_lines s n y q | git add -p &&
614 test_must_fail git grep --cached missing &&
615 git grep before &&
616 test_must_fail git grep --cached before
619 test_expect_success 'edit, adding lines to the first hunk' '
620 test_write_lines 10 11 20 30 40 50 51 60 >test &&
621 git reset &&
622 tr _ " " >patch <<-EOF &&
623 @@ -1,5 +1,6 @@
632 # test sequence is s(plit), e(dit), n(o)
633 # q n q q is there to make sure we exit at the end.
634 printf "%s\n" s e n q n q q |
635 EDITOR=./fake_editor.sh git add -p 2>error &&
636 test_must_be_empty error &&
637 git diff --cached >actual &&
638 grep "^+22" actual
641 test_expect_success 'patch mode ignores unmerged entries' '
642 git reset --hard &&
643 test_commit conflict &&
644 test_commit non-conflict &&
645 git checkout -b side &&
646 test_commit side conflict.t &&
647 git checkout main &&
648 test_commit main conflict.t &&
649 test_must_fail git merge side &&
650 echo changed >non-conflict.t &&
651 echo y | git add -p >output &&
652 ! grep a/conflict.t output &&
653 cat >expected <<-\EOF &&
654 * Unmerged path conflict.t
655 diff --git a/non-conflict.t b/non-conflict.t
656 index f766221..5ea2ed4 100644
657 --- a/non-conflict.t
658 +++ b/non-conflict.t
659 @@ -1 +1 @@
660 -non-conflict
661 +changed
663 git diff --cached >diff &&
664 diff_cmp expected diff
667 test_expect_success 'index is refreshed after applying patch' '
668 git reset --hard &&
669 echo content >test &&
670 printf y | git add -p &&
671 git diff-files --exit-code
674 test_expect_success 'diffs can be colorized' '
675 git reset --hard &&
677 echo content >test &&
678 printf y >y &&
679 force_color git add -p >output 2>&1 <y &&
680 git diff-files --exit-code &&
682 # We do not want to depend on the exact coloring scheme
683 # git uses for diffs, so just check that we saw some kind of color.
684 grep "$(printf "\\033")" output
687 test_expect_success 'colors can be overridden' '
688 git reset --hard &&
689 test_when_finished "git rm -f color-test" &&
690 test_write_lines context old more-context >color-test &&
691 git add color-test &&
692 test_write_lines context new more-context another-one >color-test &&
694 echo trigger an error message >input &&
695 force_color git \
696 -c color.interactive.error=blue \
697 add -i 2>err.raw <input &&
698 test_decode_color <err.raw >err &&
699 grep "<BLUE>Huh (trigger)?<RESET>" err &&
701 test_write_lines help quit >input &&
702 force_color git \
703 -c color.interactive.header=red \
704 -c color.interactive.help=green \
705 -c color.interactive.prompt=yellow \
706 add -i >actual.raw <input &&
707 test_decode_color <actual.raw >actual &&
708 cat >expect <<-\EOF &&
709 <RED> staged unstaged path<RESET>
710 1: +3/-0 +2/-1 color-test
712 <RED>*** Commands ***<RESET>
713 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
714 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
715 <YELLOW>What now<RESET>> <GREEN>status - show paths with changes<RESET>
716 <GREEN>update - add working tree state to the staged set of changes<RESET>
717 <GREEN>revert - revert staged set of changes back to the HEAD version<RESET>
718 <GREEN>patch - pick hunks and update selectively<RESET>
719 <GREEN>diff - view diff between HEAD and index<RESET>
720 <GREEN>add untracked - add contents of untracked files to the staged set of changes<RESET>
721 <RED>*** Commands ***<RESET>
722 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
723 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
724 <YELLOW>What now<RESET>> Bye.
726 test_cmp expect actual &&
728 : exercise recolor_hunk by editing and then look at the hunk again &&
729 test_write_lines s e K q >input &&
730 force_color git \
731 -c color.interactive.prompt=yellow \
732 -c color.diff.meta=italic \
733 -c color.diff.frag=magenta \
734 -c color.diff.context=cyan \
735 -c color.diff.old=bold \
736 -c color.diff.new=blue \
737 -c core.editor=touch \
738 add -p >actual.raw <input &&
739 test_decode_color <actual.raw >actual.decoded &&
740 sed "s/index [0-9a-f]*\\.\\.[0-9a-f]* 100644/<INDEX-LINE>/" <actual.decoded >actual &&
741 cat >expect <<-\EOF &&
742 <ITALIC>diff --git a/color-test b/color-test<RESET>
743 <ITALIC><INDEX-LINE><RESET>
744 <ITALIC>--- a/color-test<RESET>
745 <ITALIC>+++ b/color-test<RESET>
746 <MAGENTA>@@ -1,3 +1,4 @@<RESET>
747 <CYAN> context<RESET>
748 <BOLD>-old<RESET>
749 <BLUE>+<RESET><BLUE>new<RESET>
750 <CYAN> more-context<RESET>
751 <BLUE>+<RESET><BLUE>another-one<RESET>
752 <YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,p,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
753 <MAGENTA>@@ -1,3 +1,3 @@<RESET>
754 <CYAN> context<RESET>
755 <BOLD>-old<RESET>
756 <BLUE>+<RESET><BLUE>new<RESET>
757 <CYAN> more-context<RESET>
758 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
759 <CYAN> more-context<RESET>
760 <BLUE>+<RESET><BLUE>another-one<RESET>
761 <YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
762 <CYAN> context<RESET>
763 <BOLD>-old<RESET>
764 <BLUE>+new<RESET>
765 <CYAN> more-context<RESET>
766 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
768 test_cmp expect actual
771 test_expect_success 'brackets appear without color' '
772 git reset --hard &&
773 test_when_finished "git rm -f bracket-test" &&
774 test_write_lines context old more-context >bracket-test &&
775 git add bracket-test &&
776 test_write_lines context new more-context another-one >bracket-test &&
778 test_write_lines quit >input &&
779 git add -i >actual <input &&
781 sed "s/^|//" >expect <<-\EOF &&
782 | staged unstaged path
783 | 1: +3/-0 +2/-1 bracket-test
785 |*** Commands ***
786 | 1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
787 | 5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
788 |What now> Bye.
791 test_cmp expect actual
794 test_expect_success 'colors can be skipped with color.ui=false' '
795 git reset --hard &&
796 test_when_finished "git rm -f color-test" &&
797 test_write_lines context old more-context >color-test &&
798 git add color-test &&
799 test_write_lines context new more-context another-one >color-test &&
801 test_write_lines help quit >input &&
802 force_color git \
803 -c color.ui=false \
804 add -i >actual.raw <input &&
805 test_decode_color <actual.raw >actual &&
806 test_cmp actual.raw actual
809 test_expect_success 'colorized diffs respect diff.wsErrorHighlight' '
810 git reset --hard &&
812 echo "old " >test &&
813 git add test &&
814 echo "new " >test &&
816 printf y >y &&
817 force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
818 test_decode_color <output.raw >output &&
819 grep "old<" output
822 test_expect_success 'diffFilter filters diff' '
823 git reset --hard &&
825 echo content >test &&
826 test_config interactive.diffFilter "sed s/^/foo:/" &&
827 printf y >y &&
828 force_color git add -p >output 2>&1 <y &&
830 # avoid depending on the exact coloring or content of the prompts,
831 # and just make sure we saw our diff prefixed
832 grep foo:.*content output
835 test_expect_success 'detect bogus diffFilter output' '
836 git reset --hard &&
838 echo content >test &&
839 test_config interactive.diffFilter "sed 6d" &&
840 printf y >y &&
841 force_color test_must_fail git add -p <y >output 2>&1 &&
842 grep "mismatched output" output
845 test_expect_success 'handle iffy colored hunk headers' '
846 git reset --hard &&
848 echo content >test &&
849 printf n >n &&
850 force_color git -c interactive.diffFilter="sed s/.*@@.*/XX/" \
851 add -p >output 2>&1 <n &&
852 grep "^XX$" output
855 test_expect_success 'handle very large filtered diff' '
856 git reset --hard &&
857 # The specific number here is not important, but it must
858 # be large enough that the output of "git diff --color"
859 # fills up the pipe buffer. 10,000 results in ~200k of
860 # colored output.
861 test_seq 10000 >test &&
862 test_config interactive.diffFilter cat &&
863 printf y >y &&
864 force_color git add -p >output 2>&1 <y &&
865 git diff-files --exit-code -- test
868 test_expect_success 'diff.algorithm is passed to `git diff-files`' '
869 git reset --hard &&
871 >file &&
872 git add file &&
873 echo changed >file &&
874 test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
875 test_grep "error: option diff-algorithm accepts " err
878 test_expect_success 'patch-mode via -i prompts for files' '
879 git reset --hard &&
881 echo one >file &&
882 echo two >test &&
883 git add -i <<-\EOF &&
884 patch
885 test
888 quit
891 echo test >expect &&
892 git diff --cached --name-only >actual &&
893 diff_cmp expect actual
896 test_expect_success 'add -p handles globs' '
897 git reset --hard &&
899 mkdir -p subdir &&
900 echo base >one.c &&
901 echo base >subdir/two.c &&
902 git add "*.c" &&
903 git commit -m base &&
905 echo change >one.c &&
906 echo change >subdir/two.c &&
907 git add -p "*.c" <<-\EOF &&
912 cat >expect <<-\EOF &&
913 one.c
914 subdir/two.c
916 git diff --cached --name-only >actual &&
917 test_cmp expect actual
920 test_expect_success 'add -p handles relative paths' '
921 git reset --hard &&
923 echo base >relpath.c &&
924 git add "*.c" &&
925 git commit -m relpath &&
927 echo change >relpath.c &&
928 mkdir -p subdir &&
929 git -C subdir add -p .. 2>error <<-\EOF &&
933 test_must_be_empty error &&
935 cat >expect <<-\EOF &&
936 relpath.c
938 git diff --cached --name-only >actual &&
939 test_cmp expect actual
942 test_expect_success 'add -p does not expand argument lists' '
943 git reset --hard &&
945 echo content >not-changed &&
946 git add not-changed &&
947 git commit -m "add not-changed file" &&
949 echo change >file &&
950 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
954 # we know that "file" must be mentioned since we actually
955 # update it, but we want to be sure that our "." pathspec
956 # was not expanded into the argument list of any command.
957 # So look only for "not-changed".
958 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
961 test_expect_success 'hunk-editing handles custom comment char' '
962 git reset --hard &&
963 echo change >>file &&
964 test_config core.commentChar "\$" &&
965 echo e | GIT_EDITOR=true git add -p &&
966 git diff --exit-code
969 test_expect_success 'add -p works even with color.ui=always' '
970 git reset --hard &&
971 echo change >>file &&
972 test_config color.ui always &&
973 echo y | git add -p &&
974 echo file >expect &&
975 git diff --cached --name-only >actual &&
976 test_cmp expect actual
979 test_expect_success 'setup different kinds of dirty submodules' '
980 test_create_repo for-submodules &&
982 cd for-submodules &&
983 test_commit initial &&
984 test_create_repo dirty-head &&
986 cd dirty-head &&
987 test_commit initial
988 ) &&
989 cp -R dirty-head dirty-otherwise &&
990 cp -R dirty-head dirty-both-ways &&
991 git add dirty-head &&
992 git add dirty-otherwise dirty-both-ways &&
993 git commit -m initial &&
995 cd dirty-head &&
996 test_commit updated &&
997 cd ../dirty-both-ways &&
998 test_commit updated &&
999 echo dirty >>initial &&
1000 : >untracked &&
1001 cd ../dirty-otherwise &&
1002 echo dirty >>initial &&
1003 : >untracked
1004 ) &&
1005 git -C for-submodules diff-files --name-only >actual &&
1006 cat >expected <<-\EOF &&
1007 dirty-both-ways
1008 dirty-head
1010 test_cmp expected actual &&
1011 git -C for-submodules diff-files --name-only --ignore-submodules=none >actual &&
1012 cat >expected <<-\EOF &&
1013 dirty-both-ways
1014 dirty-head
1015 dirty-otherwise
1017 test_cmp expected actual &&
1018 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
1019 cat >expected <<-\EOF &&
1020 dirty-both-ways
1021 dirty-head
1023 test_cmp expected actual
1026 test_expect_success 'status ignores dirty submodules (except HEAD)' '
1027 git -C for-submodules add -i </dev/null >output &&
1028 grep dirty-head output &&
1029 grep dirty-both-ways output &&
1030 ! grep dirty-otherwise output
1033 test_expect_success 'handle submodules' '
1034 echo 123 >>for-submodules/dirty-otherwise/initial.t &&
1036 force_color git -C for-submodules add -p dirty-otherwise >output 2>&1 &&
1037 grep "No changes" output &&
1039 force_color git -C for-submodules add -p dirty-head >output 2>&1 <y &&
1040 git -C for-submodules ls-files --stage dirty-head >actual &&
1041 rev="$(git -C for-submodules/dirty-head rev-parse HEAD)" &&
1042 grep "$rev" actual
1045 test_expect_success 'set up pathological context' '
1046 git reset --hard &&
1047 test_write_lines a a a a a a a a a a a >a &&
1048 git add a &&
1049 git commit -m a &&
1050 test_write_lines c b a a a a a a a b a a a a >a &&
1051 test_write_lines a a a a a a a b a a a a >expected-1 &&
1052 test_write_lines b a a a a a a a b a a a a >expected-2 &&
1053 # check editing can cope with missing header and deleted context lines
1054 # as well as changes to other lines
1055 test_write_lines +b " a" >patch
1058 test_expect_success 'add -p works with pathological context lines' '
1059 git reset &&
1060 printf "%s\n" n y |
1061 git add -p &&
1062 git cat-file blob :a >actual &&
1063 test_cmp expected-1 actual
1066 test_expect_success 'add -p patch editing works with pathological context lines' '
1067 git reset &&
1068 # n q q below is in case edit fails
1069 printf "%s\n" e y n q q |
1070 git add -p &&
1071 git cat-file blob :a >actual &&
1072 test_cmp expected-2 actual
1075 test_expect_success 'checkout -p works with pathological context lines' '
1076 test_write_lines a a a a a a >a &&
1077 git add a &&
1078 test_write_lines a b a b a b a b a b a >a &&
1079 test_write_lines s n n y q | git checkout -p &&
1080 test_write_lines a b a b a a b a b a >expect &&
1081 test_cmp expect a
1084 # This should be called from a subshell as it sets a temporary editor
1085 setup_new_file() {
1086 write_script new-file-editor.sh <<-\EOF &&
1087 sed /^#/d "$1" >patch &&
1088 sed /^+c/d patch >"$1"
1090 test_set_editor "$(pwd)/new-file-editor.sh" &&
1091 test_write_lines a b c d e f >new-file &&
1092 test_write_lines a b d e f >new-file-expect &&
1093 test_write_lines "@@ -0,0 +1,6 @@" +a +b +c +d +e +f >patch-expect
1096 test_expect_success 'add -N followed by add -p patch editing' '
1097 git reset --hard &&
1099 setup_new_file &&
1100 git add -N new-file &&
1101 test_write_lines e n q | git add -p &&
1102 git cat-file blob :new-file >actual &&
1103 test_cmp new-file-expect actual &&
1104 test_cmp patch-expect patch
1108 test_expect_success 'checkout -p patch editing of added file' '
1109 git reset --hard &&
1111 setup_new_file &&
1112 git add new-file &&
1113 git commit -m "add new file" &&
1114 git rm new-file &&
1115 git commit -m "remove new file" &&
1116 test_write_lines e n q | git checkout -p HEAD^ &&
1117 test_cmp new-file-expect new-file &&
1118 test_cmp patch-expect patch
1122 test_expect_success 'show help from add--helper' '
1123 git reset --hard &&
1124 cat >expect <<-EOF &&
1126 <BOLD>*** Commands ***<RESET>
1127 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
1128 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1129 <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes<RESET>
1130 <BOLD;RED>update - add working tree state to the staged set of changes<RESET>
1131 <BOLD;RED>revert - revert staged set of changes back to the HEAD version<RESET>
1132 <BOLD;RED>patch - pick hunks and update selectively<RESET>
1133 <BOLD;RED>diff - view diff between HEAD and index<RESET>
1134 <BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
1135 <BOLD>*** Commands ***<RESET>
1136 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
1137 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1138 <BOLD;BLUE>What now<RESET>>$SP
1139 Bye.
1141 test_write_lines h | force_color git add -i >actual.colored &&
1142 test_decode_color <actual.colored >actual &&
1143 test_cmp expect actual
1146 test_expect_success 'reset -p with unmerged files' '
1147 test_when_finished "git checkout --force main" &&
1148 test_commit one conflict &&
1149 git checkout -B side HEAD^ &&
1150 test_commit two conflict &&
1151 test_must_fail git merge one &&
1153 # this is a noop with only an unmerged entry
1154 git reset -p &&
1156 # add files that sort before and after unmerged entry
1157 echo a >a &&
1158 echo z >z &&
1159 git add a z &&
1161 # confirm that we can reset those files
1162 printf "%s\n" y y | git reset -p &&
1163 git diff-index --cached --diff-filter=u HEAD >staged &&
1164 test_must_be_empty staged
1167 test_done