3 test_description
='add -i basic tests'
5 .
"$TEST_DIRECTORY"/lib-terminal.sh
7 if ! test_have_prereq PERL
9 skip_all
='skipping add -i tests, perl not available'
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 /' \
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.
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
&&
41 export GIT_PAGER_IN_USE TERM
&&
46 test_expect_success
'setup (initial)' '
52 test_expect_success
'status works (initial)' '
53 git add -i </dev/null >output &&
54 grep "+1/-0 *+2/-0 file" output
57 test_expect_success
'setup expected' '
60 index 0000000..d95f3ad
68 test_expect_success
'diff works (initial)' '
69 test_write_lines d 1 | git add -i >output &&
70 sed -ne "/new file/,/content/p" <output >diff &&
71 diff_cmp expected diff
73 test_expect_success
'revert works (initial)' '
75 test_write_lines r 1 | git add -i &&
76 git ls-files >output &&
80 test_expect_success
'add untracked (multiple)' '
81 test_when_finished "git reset && rm [1-9]" &&
82 touch $(test_seq 9) &&
83 test_write_lines a "2-5 8-" | git add -i -- [1-9] &&
84 test_write_lines 2 3 4 5 8 9 >expected &&
85 git ls-files [1-9] >output &&
86 test_cmp expected output
89 test_expect_success
'setup (commit)' '
90 echo baseline >file &&
92 git commit -m commit &&
93 echo content >>file &&
98 test_expect_success
'status works (commit)' '
99 git add -i </dev/null >output &&
100 grep "+1/-0 *+2/-0 file" output
103 test_expect_success
'setup expected' '
104 cat >expected <<-\EOF
105 index 180b47c..b6f2c08 100644
114 test_expect_success
'diff works (commit)' '
115 test_write_lines d 1 | git add -i >output &&
116 sed -ne "/^index/,/content/p" <output >diff &&
117 diff_cmp expected diff
119 test_expect_success
'revert works (commit)' '
121 test_write_lines r 1 | git add -i &&
122 git add -i </dev/null >output &&
123 grep "unchanged *+3/-0 file" output
126 test_expect_success
'setup expected' '
127 cat >expected <<-\EOF
131 test_expect_success
'dummy edit works' '
133 test_write_lines e a | git add -p &&
135 diff_cmp expected diff
138 test_expect_success
'setup patch' '
148 test_expect_success
'setup fake editor' '
149 write_script "fake_editor.sh" <<-\EOF &&
150 mv -f "$1" oldpatch &&
153 test_set_editor "$(pwd)/fake_editor.sh"
156 test_expect_success
'bad edit rejected' '
158 test_write_lines e n d | git add -p >output &&
159 grep "hunk does not apply" output
162 test_expect_success
'setup patch' '
169 test_expect_success
'garbage edit rejected' '
171 test_write_lines e n d | git add -p >output &&
172 grep "hunk does not apply" output
175 test_expect_success
'setup patch' '
185 test_expect_success
'setup expected' '
186 cat >expected <<-\EOF
187 diff --git a/file b/file
188 index b5dd6c9..f910ae9 100644
200 test_expect_success
'real edit works' '
201 test_write_lines e n d | git add -p &&
203 diff_cmp expected output
206 test_expect_success
'setup file' '
207 test_write_lines a "" b "" c >file &&
209 test_write_lines a "" d "" c >file
212 test_expect_success
'setup patch' '
226 test_expect_success
'setup expected' '
228 diff --git a/file b/file
229 index b5dd6c9..f910ae9 100644
242 test_expect_success
'edit can strip spaces from empty context lines' '
243 test_write_lines e n q | git add -p 2>error &&
244 test_must_be_empty error &&
246 diff_cmp expected output
249 test_expect_success
'skip files similarly as commit -a' '
251 echo file >.gitignore &&
252 echo changed >file &&
253 echo y | git add -p file &&
256 git commit -am commit &&
257 git diff >expected &&
258 diff_cmp expected output &&
259 git reset --hard HEAD^
263 test_expect_success FILEMODE
'patch does not affect mode' '
265 echo content >>file &&
267 printf "n\\ny\\n" | git add -p &&
268 git show :file | grep content &&
269 git diff file | grep "new mode"
272 test_expect_success FILEMODE
'stage mode but not hunk' '
274 echo content >>file &&
276 printf "y\\nn\\n" | git add -p &&
277 git diff --cached file | grep "new mode" &&
278 git diff file | grep "+content"
282 test_expect_success FILEMODE
'stage mode and hunk' '
284 echo content >>file &&
286 printf "y\\ny\\n" | git add -p &&
287 git diff --cached file | grep "new mode" &&
288 git diff --cached file | grep "+content" &&
289 test -z "$(git diff file)"
292 # end of tests disabled when filemode is not usable
294 test_expect_success
'different prompts for mode change/deleted' '
298 git add --chmod=+x file deleted &&
299 echo changed >file &&
301 test_write_lines n n n |
302 git -c core.filemode=true add -p >actual &&
303 sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
304 cat >expect <<-\EOF &&
305 (1/1) Stage deletion [y,n,q,a,d,?]?
306 (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,?]?
307 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
309 test_cmp expect actual.filtered
312 test_expect_success
'correct message when there is nothing to do' '
315 test_i18ngrep "No changes" err &&
316 printf "\\0123" >binary &&
318 printf "\\0abc" >binary &&
320 test_i18ngrep "Only binary files changed" err
323 test_expect_success
'setup again' '
325 test_chmod +x file &&
329 # Write the patch file with a new line at the top and bottom
330 test_expect_success
'setup patch' '
332 index 180b47c..b6f2c08 100644
340 \ No newline at end of file
344 # Expected output, diff is similar to the patch but w/ diff at the top
345 test_expect_success
'setup expected' '
346 echo diff --git a/file b/file >expected &&
347 cat patch |sed "/^index/s/ 100644/ 100755/" >>expected &&
348 cat >expected-output <<-\EOF
356 \ No newline at end of file
365 \ No newline at end of file
369 # Test splitting the first patch, then adding both
370 test_expect_success C_LOCALE_OUTPUT
'add first line works' '
371 git commit -am "clear local changes" &&
373 printf "%s\n" s y y | git add -p file 2>error |
374 sed -n -e "s/^([1-2]\/[1-2]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
375 -e "/^[-+@ \\\\]"/p >output &&
376 test_must_be_empty error &&
377 git diff --cached >diff &&
378 diff_cmp expected diff &&
379 test_cmp expected-output output
382 test_expect_success
'setup expected' '
383 cat >expected <<-\EOF
384 diff --git a/non-empty b/non-empty
385 deleted file mode 100644
386 index d95f3ad..0000000
394 test_expect_success
'deleting a non-empty file' '
396 echo content >non-empty &&
398 git commit -m non-empty &&
400 echo y | git add -p non-empty &&
401 git diff --cached >diff &&
402 diff_cmp expected diff
405 test_expect_success
'setup expected' '
406 cat >expected <<-\EOF
407 diff --git a/empty b/empty
408 deleted file mode 100644
409 index e69de29..0000000
413 test_expect_success
'deleting an empty file' '
417 git commit -m empty &&
419 echo y | git add -p empty &&
420 git diff --cached >diff &&
421 diff_cmp expected diff
424 test_expect_success
'adding an empty file' '
428 test_commit initial &&
432 git commit -m empty &&
433 git tag added-file &&
434 git reset --hard HEAD^ &&
435 test_path_is_missing empty &&
437 echo y | git checkout -p added-file -- >actual &&
438 test_path_is_file empty &&
439 test_i18ngrep "Apply addition to index and worktree" actual
443 test_expect_success
'split hunk setup' '
445 test_write_lines 10 20 30 40 50 60 >test &&
448 git commit -m test &&
450 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
453 test_expect_success
'goto hunk' '
454 test_when_finished "git reset" &&
455 tr _ " " >expect <<-EOF &&
456 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? + 1: -1,2 +1,3 +15
458 go to which hunk? @@ -1,2 +1,3 @@
462 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
464 test_write_lines s y g 1 | git add -p >actual &&
465 tail -n 7 <actual >actual.trimmed &&
466 test_cmp expect actual.trimmed
469 test_expect_success
'navigate to hunk via regex' '
470 test_when_finished "git reset" &&
471 tr _ " " >expect <<-EOF &&
472 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? @@ -1,2 +1,3 @@
476 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
478 test_write_lines s y /1,2 | git add -p >actual &&
479 tail -n 5 <actual >actual.trimmed &&
480 test_cmp expect actual.trimmed
483 test_expect_success
'split hunk "add -p (edit)"' '
484 # Split, say Edit and do nothing. Then:
486 # 1. Broken version results in a patch that does not apply and
487 # only takes [y/n] (edit again) so the first q is discarded
488 # and then n attempts to discard the edit. Repeat q enough
491 # 2. Correct version applies the (not)edited version, and asks
492 # about the next hunk, against which we say q and program
494 printf "%s\n" s e q n q q |
495 EDITOR=: git add -p &&
500 test_expect_failure
'split hunk "add -p (no, yes, edit)"' '
501 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
503 # test sequence is s(plit), n(o), y(es), e(dit)
504 # q n q q is there to make sure we exit at the end.
505 printf "%s\n" s n y e q n q q |
506 EDITOR=: git add -p 2>error &&
507 test_must_be_empty error &&
512 test_expect_success
'split hunk with incomplete line at end' '
514 printf "missing LF" >>test &&
516 test_write_lines before 10 20 30 40 50 60 70 >test &&
517 git grep --cached missing &&
518 test_write_lines s n y q | git add -p &&
519 test_must_fail git grep --cached missing &&
521 test_must_fail git grep --cached before
524 test_expect_failure
'edit, adding lines to the first hunk' '
525 test_write_lines 10 11 20 30 40 50 51 60 >test &&
527 tr _ " " >patch <<-EOF &&
537 # test sequence is s(plit), e(dit), n(o)
538 # q n q q is there to make sure we exit at the end.
539 printf "%s\n" s e n q n q q |
540 EDITOR=./fake_editor.sh git add -p 2>error &&
541 test_must_be_empty error &&
542 git diff --cached >actual &&
546 test_expect_success
'patch mode ignores unmerged entries' '
548 test_commit conflict &&
549 test_commit non-conflict &&
550 git checkout -b side &&
551 test_commit side conflict.t &&
552 git checkout master &&
553 test_commit master conflict.t &&
554 test_must_fail git merge side &&
555 echo changed >non-conflict.t &&
556 echo y | git add -p >output &&
557 ! grep a/conflict.t output &&
558 cat >expected <<-\EOF &&
559 * Unmerged path conflict.t
560 diff --git a/non-conflict.t b/non-conflict.t
561 index f766221..5ea2ed4 100644
568 git diff --cached >diff &&
569 diff_cmp expected diff
572 test_expect_success
'index is refreshed after applying patch' '
574 echo content >test &&
575 printf y | git add -p &&
576 git diff-files --exit-code
579 test_expect_success
'diffs can be colorized' '
582 echo content >test &&
584 force_color git add -p >output 2>&1 <y &&
585 git diff-files --exit-code &&
587 # We do not want to depend on the exact coloring scheme
588 # git uses for diffs, so just check that we saw some kind of color.
589 grep "$(printf "\\033")" output
592 test_expect_success
'colorized diffs respect diff.wsErrorHighlight' '
600 force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
601 test_decode_color <output.raw >output &&
605 test_expect_success
'diffFilter filters diff' '
608 echo content >test &&
609 test_config interactive.diffFilter "sed s/^/foo:/" &&
611 force_color git add -p >output 2>&1 <y &&
613 # avoid depending on the exact coloring or content of the prompts,
614 # and just make sure we saw our diff prefixed
615 grep foo:.*content output
618 test_expect_success
'detect bogus diffFilter output' '
621 echo content >test &&
622 test_config interactive.diffFilter "sed 1d" &&
624 force_color test_must_fail git add -p <y
627 test_expect_success
'diff.algorithm is passed to `git diff-files`' '
632 echo changed >file &&
633 test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
634 test_i18ngrep "error: option diff-algorithm accepts " err
637 test_expect_success
'patch-mode via -i prompts for files' '
642 git add -i <<-\EOF &&
651 git diff --cached --name-only >actual &&
652 diff_cmp expect actual
655 test_expect_success
'add -p handles globs' '
660 echo base >subdir/two.c &&
662 git commit -m base &&
664 echo change >one.c &&
665 echo change >subdir/two.c &&
666 git add -p "*.c" <<-\EOF &&
671 cat >expect <<-\EOF &&
675 git diff --cached --name-only >actual &&
676 test_cmp expect actual
679 test_expect_success
'add -p handles relative paths' '
682 echo base >relpath.c &&
684 git commit -m relpath &&
686 echo change >relpath.c &&
688 git -C subdir add -p .. 2>error <<-\EOF &&
692 test_must_be_empty error &&
694 cat >expect <<-\EOF &&
697 git diff --cached --name-only >actual &&
698 test_cmp expect actual
701 test_expect_success
'add -p does not expand argument lists' '
704 echo content >not-changed &&
705 git add not-changed &&
706 git commit -m "add not-changed file" &&
709 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
713 # we know that "file" must be mentioned since we actually
714 # update it, but we want to be sure that our "." pathspec
715 # was not expanded into the argument list of any command.
716 # So look only for "not-changed".
717 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
720 test_expect_success
'hunk-editing handles custom comment char' '
722 echo change >>file &&
723 test_config core.commentChar "\$" &&
724 echo e | GIT_EDITOR=true git add -p &&
728 test_expect_success
'add -p works even with color.ui=always' '
730 echo change >>file &&
731 test_config color.ui always &&
732 echo y | git add -p &&
734 git diff --cached --name-only >actual &&
735 test_cmp expect actual
738 test_expect_success
'setup different kinds of dirty submodules' '
739 test_create_repo for-submodules &&
742 test_commit initial &&
743 test_create_repo dirty-head &&
748 cp -R dirty-head dirty-otherwise &&
749 cp -R dirty-head dirty-both-ways &&
750 git add dirty-head &&
751 git add dirty-otherwise dirty-both-ways &&
752 git commit -m initial &&
755 test_commit updated &&
756 cd ../dirty-both-ways &&
757 test_commit updated &&
758 echo dirty >>initial &&
760 cd ../dirty-otherwise &&
761 echo dirty >>initial &&
764 git -C for-submodules diff-files --name-only >actual &&
765 cat >expected <<-\EOF &&
770 test_cmp expected actual &&
771 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
772 cat >expected <<-\EOF &&
776 test_cmp expected actual
779 test_expect_success
'status ignores dirty submodules (except HEAD)' '
780 git -C for-submodules add -i </dev/null >output &&
781 grep dirty-head output &&
782 grep dirty-both-ways output &&
783 ! grep dirty-otherwise output
786 test_expect_success
'set up pathological context' '
788 test_write_lines a a a a a a a a a a a >a &&
791 test_write_lines c b a a a a a a a b a a a a >a &&
792 test_write_lines a a a a a a a b a a a a >expected-1 &&
793 test_write_lines b a a a a a a a b a a a a >expected-2 &&
794 # check editing can cope with missing header and deleted context lines
795 # as well as changes to other lines
796 test_write_lines +b " a" >patch
799 test_expect_success
'add -p works with pathological context lines' '
803 git cat-file blob :a >actual &&
804 test_cmp expected-1 actual
807 test_expect_success
'add -p patch editing works with pathological context lines' '
809 # n q q below is in case edit fails
810 printf "%s\n" e y n q q |
812 git cat-file blob :a >actual &&
813 test_cmp expected-2 actual
816 test_expect_success
'checkout -p works with pathological context lines' '
817 test_write_lines a a a a a a >a &&
819 test_write_lines a b a b a b a b a b a >a &&
820 test_write_lines s n n y q | git checkout -p &&
821 test_write_lines a b a b a a b a b a >expect &&
825 # This should be called from a subshell as it sets a temporary editor
827 write_script new-file-editor.sh
<<-\EOF &&
828 sed /^#/d "$1" >patch &&
829 sed /^+c/d patch >"$1"
831 test_set_editor "$(pwd)/new-file-editor.sh" &&
832 test_write_lines a b c d e f >new-file &&
833 test_write_lines a b d e f >new-file-expect &&
834 test_write_lines "@@ -0,0 +1,6 @@" +a +b +c +d +e +f >patch-expect
837 test_expect_success 'add -N followed by add -p patch editing' '
841 git add -N new-file &&
842 test_write_lines e n q | git add -p &&
843 git cat-file blob :new-file >actual &&
844 test_cmp new-file-expect actual &&
845 test_cmp patch-expect patch
849 test_expect_success 'checkout -p patch editing of added file' '
854 git commit -m "add new file" &&
856 git commit -m "remove new file" &&
857 test_write_lines e n q | git checkout -p HEAD^ &&
858 test_cmp new-file-expect new-file &&
859 test_cmp patch-expect patch
863 test_expect_success 'show help from add--helper' '
865 cat >expect <<-EOF &&
867 <BOLD>*** Commands ***<RESET>
868 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
869 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
870 <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes
<RESET
>
871 <BOLD
;RED
>update
- add working tree state to the staged
set of changes
<RESET
>
872 <BOLD
;RED
>revert
- revert staged
set of changes back to the HEAD version
<RESET
>
873 <BOLD
;RED
>patch - pick hunks and update selectively
<RESET
>
874 <BOLD
;RED
>diff - view
diff between HEAD and index
<RESET
>
875 <BOLD
;RED
>add untracked
- add contents of untracked files to the staged
set of changes
<RESET
>
876 <BOLD
>*** Commands
***<RESET
>
877 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
878 5: <BOLD
;BLUE
>p
<RESET
>atch
6: <BOLD
;BLUE
>d
<RESET
>iff
7: <BOLD
;BLUE
>q
<RESET
>uit
8: <BOLD
;BLUE
>h
<RESET
>elp
879 <BOLD
;BLUE
>What now
<RESET
>>$SP
882 test_write_lines h | force_color git add
-i >actual.colored
&&
883 test_decode_color
<actual.colored
>actual
&&
884 test_i18ncmp expect actual