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 env GIT_PAGER_IN_USE
=true TERM
=vt100
"$@"
37 test_expect_success
'setup (initial)' '
43 test_expect_success
'status works (initial)' '
44 git add -i </dev/null >output &&
45 grep "+1/-0 *+2/-0 file" output
48 test_expect_success
'setup expected' '
51 index 0000000..d95f3ad
59 test_expect_success
'diff works (initial)' '
60 test_write_lines d 1 | git add -i >output &&
61 sed -ne "/new file/,/content/p" <output >diff &&
62 diff_cmp expected diff
64 test_expect_success
'revert works (initial)' '
66 test_write_lines r 1 | git add -i &&
67 git ls-files >output &&
71 test_expect_success
'add untracked (multiple)' '
72 test_when_finished "git reset && rm [1-9]" &&
73 touch $(test_seq 9) &&
74 test_write_lines a "2-5 8-" | git add -i -- [1-9] &&
75 test_write_lines 2 3 4 5 8 9 >expected &&
76 git ls-files [1-9] >output &&
77 test_cmp expected output
80 test_expect_success
'setup (commit)' '
81 echo baseline >file &&
83 git commit -m commit &&
84 echo content >>file &&
89 test_expect_success
'status works (commit)' '
90 git add -i </dev/null >output &&
91 grep "+1/-0 *+2/-0 file" output
94 test_expect_success
'setup expected' '
96 index 180b47c..b6f2c08 100644
105 test_expect_success
'diff works (commit)' '
106 test_write_lines d 1 | git add -i >output &&
107 sed -ne "/^index/,/content/p" <output >diff &&
108 diff_cmp expected diff
110 test_expect_success
'revert works (commit)' '
112 test_write_lines r 1 | git add -i &&
113 git add -i </dev/null >output &&
114 grep "unchanged *+3/-0 file" output
117 test_expect_success
'setup expected' '
118 cat >expected <<-\EOF
122 test_expect_success
'dummy edit works' '
124 test_write_lines e a | git add -p &&
126 diff_cmp expected diff
129 test_expect_success
'setup patch' '
139 test_expect_success
'setup fake editor' '
140 write_script "fake_editor.sh" <<-\EOF &&
141 mv -f "$1" oldpatch &&
144 test_set_editor "$(pwd)/fake_editor.sh"
147 test_expect_success
'bad edit rejected' '
149 test_write_lines e n d | git add -p >output &&
150 grep "hunk does not apply" output
153 test_expect_success
'setup patch' '
160 test_expect_success
'garbage edit rejected' '
162 test_write_lines e n d | git add -p >output &&
163 grep "hunk does not apply" output
166 test_expect_success
'setup patch' '
176 test_expect_success
'setup expected' '
177 cat >expected <<-\EOF
178 diff --git a/file b/file
179 index b5dd6c9..f910ae9 100644
191 test_expect_success
'real edit works' '
192 test_write_lines e n d | git add -p &&
194 diff_cmp expected output
197 test_expect_success
'setup file' '
198 test_write_lines a "" b "" c >file &&
200 test_write_lines a "" d "" c >file
203 test_expect_success
'setup patch' '
217 test_expect_success
'setup expected' '
219 diff --git a/file b/file
220 index b5dd6c9..f910ae9 100644
233 test_expect_success
'edit can strip spaces from empty context lines' '
234 test_write_lines e n q | git add -p 2>error &&
235 test_must_be_empty error &&
237 diff_cmp expected output
240 test_expect_success
'skip files similarly as commit -a' '
242 echo file >.gitignore &&
243 echo changed >file &&
244 echo y | git add -p file &&
247 git commit -am commit &&
248 git diff >expected &&
249 diff_cmp expected output &&
250 git reset --hard HEAD^
254 test_expect_success FILEMODE
'patch does not affect mode' '
256 echo content >>file &&
258 printf "n\\ny\\n" | git add -p &&
259 git show :file | grep content &&
260 git diff file | grep "new mode"
263 test_expect_success FILEMODE
'stage mode but not hunk' '
265 echo content >>file &&
267 printf "y\\nn\\n" | git add -p &&
268 git diff --cached file | grep "new mode" &&
269 git diff file | grep "+content"
273 test_expect_success FILEMODE
'stage mode and hunk' '
275 echo content >>file &&
277 printf "y\\ny\\n" | git add -p &&
278 git diff --cached file | grep "new mode" &&
279 git diff --cached file | grep "+content" &&
280 test -z "$(git diff file)"
283 # end of tests disabled when filemode is not usable
285 test_expect_success
'different prompts for mode change/deleted' '
289 git add --chmod=+x file deleted &&
290 echo changed >file &&
292 test_write_lines n n n |
293 git -c core.filemode=true add -p >actual &&
294 sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
295 cat >expect <<-\EOF &&
296 (1/1) Stage deletion [y,n,q,a,d,?]?
297 (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,?]?
298 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
300 test_cmp expect actual.filtered
303 test_expect_success
'correct message when there is nothing to do' '
306 test_i18ngrep "No changes" err &&
307 printf "\\0123" >binary &&
309 printf "\\0abc" >binary &&
311 test_i18ngrep "Only binary files changed" err
314 test_expect_success
'setup again' '
316 test_chmod +x file &&
320 # Write the patch file with a new line at the top and bottom
321 test_expect_success
'setup patch' '
323 index 180b47c..b6f2c08 100644
331 \ No newline at end of file
335 # Expected output, diff is similar to the patch but w/ diff at the top
336 test_expect_success
'setup expected' '
337 echo diff --git a/file b/file >expected &&
338 cat patch |sed "/^index/s/ 100644/ 100755/" >>expected &&
339 cat >expected-output <<-\EOF
347 \ No newline at end of file
356 \ No newline at end of file
360 # Test splitting the first patch, then adding both
361 test_expect_success C_LOCALE_OUTPUT
'add first line works' '
362 git commit -am "clear local changes" &&
364 printf "%s\n" s y y | git add -p file 2>error |
365 sed -n -e "s/^([1-2]\/[1-2]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
366 -e "/^[-+@ \\\\]"/p >output &&
367 test_must_be_empty error &&
368 git diff --cached >diff &&
369 diff_cmp expected diff &&
370 test_cmp expected-output output
373 test_expect_success
'setup expected' '
374 cat >expected <<-\EOF
375 diff --git a/non-empty b/non-empty
376 deleted file mode 100644
377 index d95f3ad..0000000
385 test_expect_success
'deleting a non-empty file' '
387 echo content >non-empty &&
389 git commit -m non-empty &&
391 echo y | git add -p non-empty &&
392 git diff --cached >diff &&
393 diff_cmp expected diff
396 test_expect_success
'setup expected' '
397 cat >expected <<-\EOF
398 diff --git a/empty b/empty
399 deleted file mode 100644
400 index e69de29..0000000
404 test_expect_success
'deleting an empty file' '
408 git commit -m empty &&
410 echo y | git add -p empty &&
411 git diff --cached >diff &&
412 diff_cmp expected diff
415 test_expect_success
'adding an empty file' '
419 test_commit initial &&
423 git commit -m empty &&
424 git tag added-file &&
425 git reset --hard HEAD^ &&
426 test_path_is_missing empty &&
428 echo y | git checkout -p added-file -- >actual &&
429 test_path_is_file empty &&
430 test_i18ngrep "Apply addition to index and worktree" actual
434 test_expect_success
'split hunk setup' '
436 test_write_lines 10 20 30 40 50 60 >test &&
439 git commit -m test &&
441 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
444 test_expect_success
'goto hunk' '
445 test_when_finished "git reset" &&
446 tr _ " " >expect <<-EOF &&
447 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? + 1: -1,2 +1,3 +15
449 go to which hunk? @@ -1,2 +1,3 @@
453 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
455 test_write_lines s y g 1 | git add -p >actual &&
456 tail -n 7 <actual >actual.trimmed &&
457 test_cmp expect actual.trimmed
460 test_expect_success
'navigate to hunk via regex' '
461 test_when_finished "git reset" &&
462 tr _ " " >expect <<-EOF &&
463 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? @@ -1,2 +1,3 @@
467 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
469 test_write_lines s y /1,2 | git add -p >actual &&
470 tail -n 5 <actual >actual.trimmed &&
471 test_cmp expect actual.trimmed
474 test_expect_success
'split hunk "add -p (edit)"' '
475 # Split, say Edit and do nothing. Then:
477 # 1. Broken version results in a patch that does not apply and
478 # only takes [y/n] (edit again) so the first q is discarded
479 # and then n attempts to discard the edit. Repeat q enough
482 # 2. Correct version applies the (not)edited version, and asks
483 # about the next hunk, against which we say q and program
485 printf "%s\n" s e q n q q |
486 EDITOR=: git add -p &&
491 test_expect_failure
'split hunk "add -p (no, yes, edit)"' '
492 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
494 # test sequence is s(plit), n(o), y(es), e(dit)
495 # q n q q is there to make sure we exit at the end.
496 printf "%s\n" s n y e q n q q |
497 EDITOR=: git add -p 2>error &&
498 test_must_be_empty error &&
503 test_expect_success
'split hunk with incomplete line at end' '
505 printf "missing LF" >>test &&
507 test_write_lines before 10 20 30 40 50 60 70 >test &&
508 git grep --cached missing &&
509 test_write_lines s n y q | git add -p &&
510 test_must_fail git grep --cached missing &&
512 test_must_fail git grep --cached before
515 test_expect_failure
'edit, adding lines to the first hunk' '
516 test_write_lines 10 11 20 30 40 50 51 60 >test &&
518 tr _ " " >patch <<-EOF &&
528 # test sequence is s(plit), e(dit), n(o)
529 # q n q q is there to make sure we exit at the end.
530 printf "%s\n" s e n q n q q |
531 EDITOR=./fake_editor.sh git add -p 2>error &&
532 test_must_be_empty error &&
533 git diff --cached >actual &&
537 test_expect_success
'patch mode ignores unmerged entries' '
539 test_commit conflict &&
540 test_commit non-conflict &&
541 git checkout -b side &&
542 test_commit side conflict.t &&
543 git checkout master &&
544 test_commit master conflict.t &&
545 test_must_fail git merge side &&
546 echo changed >non-conflict.t &&
547 echo y | git add -p >output &&
548 ! grep a/conflict.t output &&
549 cat >expected <<-\EOF &&
550 * Unmerged path conflict.t
551 diff --git a/non-conflict.t b/non-conflict.t
552 index f766221..5ea2ed4 100644
559 git diff --cached >diff &&
560 diff_cmp expected diff
563 test_expect_success
'diffs can be colorized' '
566 echo content >test &&
568 force_color git add -p >output 2>&1 <y &&
570 # We do not want to depend on the exact coloring scheme
571 # git uses for diffs, so just check that we saw some kind of color.
572 grep "$(printf "\\033")" output
575 test_expect_success
'colorized diffs respect diff.wsErrorHighlight' '
583 force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
584 test_decode_color <output.raw >output &&
588 test_expect_success
'diffFilter filters diff' '
591 echo content >test &&
592 test_config interactive.diffFilter "sed s/^/foo:/" &&
594 force_color git add -p >output 2>&1 <y &&
596 # avoid depending on the exact coloring or content of the prompts,
597 # and just make sure we saw our diff prefixed
598 grep foo:.*content output
601 test_expect_success
'detect bogus diffFilter output' '
604 echo content >test &&
605 test_config interactive.diffFilter "sed 1d" &&
607 test_must_fail force_color git add -p <y
610 test_expect_success
'diff.algorithm is passed to `git diff-files`' '
615 echo changed >file &&
616 test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
617 test_i18ngrep "error: option diff-algorithm accepts " err
620 test_expect_success
'patch-mode via -i prompts for files' '
625 git add -i <<-\EOF &&
634 git diff --cached --name-only >actual &&
635 diff_cmp expect actual
638 test_expect_success
'add -p handles globs' '
643 echo base >subdir/two.c &&
645 git commit -m base &&
647 echo change >one.c &&
648 echo change >subdir/two.c &&
649 git add -p "*.c" <<-\EOF &&
654 cat >expect <<-\EOF &&
658 git diff --cached --name-only >actual &&
659 test_cmp expect actual
662 test_expect_success
'add -p handles relative paths' '
665 echo base >relpath.c &&
667 git commit -m relpath &&
669 echo change >relpath.c &&
671 git -C subdir add -p .. 2>error <<-\EOF &&
675 test_must_be_empty error &&
677 cat >expect <<-\EOF &&
680 git diff --cached --name-only >actual &&
681 test_cmp expect actual
684 test_expect_success
'add -p does not expand argument lists' '
687 echo content >not-changed &&
688 git add not-changed &&
689 git commit -m "add not-changed file" &&
692 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
696 # we know that "file" must be mentioned since we actually
697 # update it, but we want to be sure that our "." pathspec
698 # was not expanded into the argument list of any command.
699 # So look only for "not-changed".
700 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
703 test_expect_success
'hunk-editing handles custom comment char' '
705 echo change >>file &&
706 test_config core.commentChar "\$" &&
707 echo e | GIT_EDITOR=true git add -p &&
711 test_expect_success
'add -p works even with color.ui=always' '
713 echo change >>file &&
714 test_config color.ui always &&
715 echo y | git add -p &&
717 git diff --cached --name-only >actual &&
718 test_cmp expect actual
721 test_expect_success
'setup different kinds of dirty submodules' '
722 test_create_repo for-submodules &&
725 test_commit initial &&
726 test_create_repo dirty-head &&
731 cp -R dirty-head dirty-otherwise &&
732 cp -R dirty-head dirty-both-ways &&
733 git add dirty-head &&
734 git add dirty-otherwise dirty-both-ways &&
735 git commit -m initial &&
738 test_commit updated &&
739 cd ../dirty-both-ways &&
740 test_commit updated &&
741 echo dirty >>initial &&
743 cd ../dirty-otherwise &&
744 echo dirty >>initial &&
747 git -C for-submodules diff-files --name-only >actual &&
748 cat >expected <<-\EOF &&
753 test_cmp expected actual &&
754 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
755 cat >expected <<-\EOF &&
759 test_cmp expected actual
762 test_expect_success
'status ignores dirty submodules (except HEAD)' '
763 git -C for-submodules add -i </dev/null >output &&
764 grep dirty-head output &&
765 grep dirty-both-ways output &&
766 ! grep dirty-otherwise output
769 test_expect_success
'set up pathological context' '
771 test_write_lines a a a a a a a a a a a >a &&
774 test_write_lines c b a a a a a a a b a a a a >a &&
775 test_write_lines a a a a a a a b a a a a >expected-1 &&
776 test_write_lines b a a a a a a a b a a a a >expected-2 &&
777 # check editing can cope with missing header and deleted context lines
778 # as well as changes to other lines
779 test_write_lines +b " a" >patch
782 test_expect_success
'add -p works with pathological context lines' '
786 git cat-file blob :a >actual &&
787 test_cmp expected-1 actual
790 test_expect_success
'add -p patch editing works with pathological context lines' '
792 # n q q below is in case edit fails
793 printf "%s\n" e y n q q |
795 git cat-file blob :a >actual &&
796 test_cmp expected-2 actual
799 test_expect_success
'checkout -p works with pathological context lines' '
800 test_write_lines a a a a a a >a &&
802 test_write_lines a b a b a b a b a b a >a &&
803 test_write_lines s n n y q | git checkout -p &&
804 test_write_lines a b a b a a b a b a >expect &&
808 test_expect_success
'show help from add--helper' '
810 cat >expect <<-EOF &&
812 <BOLD>*** Commands ***<RESET>
813 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
814 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
815 <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes<RESET>
816 <BOLD;RED>update - add working tree state to the staged set of changes<RESET>
817 <BOLD;RED>revert - revert staged set of changes back to the HEAD version<RESET>
818 <BOLD;RED>patch - pick hunks and update selectively<RESET>
819 <BOLD;RED>diff - view diff between HEAD and index<RESET>
820 <BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
821 <BOLD>*** Commands ***<RESET>
822 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
823 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
824 <BOLD;BLUE>What now<RESET>>$SP
827 test_write_lines h | force_color git add -i >actual.colored &&
828 test_decode_color <actual.colored >actual &&
829 test_i18ncmp expect actual