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
'setup (commit)' '
72 echo baseline >file &&
74 git commit -m commit &&
75 echo content >>file &&
80 test_expect_success
'status works (commit)' '
81 git add -i </dev/null >output &&
82 grep "+1/-0 *+2/-0 file" output
85 test_expect_success
'setup expected' '
87 index 180b47c..b6f2c08 100644
96 test_expect_success
'diff works (commit)' '
97 test_write_lines d 1 | git add -i >output &&
98 sed -ne "/^index/,/content/p" <output >diff &&
99 diff_cmp expected diff
101 test_expect_success
'revert works (commit)' '
103 test_write_lines r 1 | git add -i &&
104 git add -i </dev/null >output &&
105 grep "unchanged *+3/-0 file" output
108 test_expect_success
'setup expected' '
109 cat >expected <<-\EOF
113 test_expect_success
'dummy edit works' '
115 test_write_lines e a | git add -p &&
117 diff_cmp expected diff
120 test_expect_success
'setup patch' '
130 test_expect_success
'setup fake editor' '
131 write_script "fake_editor.sh" <<-\EOF &&
132 mv -f "$1" oldpatch &&
135 test_set_editor "$(pwd)/fake_editor.sh"
138 test_expect_success
'bad edit rejected' '
140 test_write_lines e n d | git add -p >output &&
141 grep "hunk does not apply" output
144 test_expect_success
'setup patch' '
151 test_expect_success
'garbage edit rejected' '
153 test_write_lines e n d | git add -p >output &&
154 grep "hunk does not apply" output
157 test_expect_success
'setup patch' '
167 test_expect_success
'setup expected' '
168 cat >expected <<-\EOF
169 diff --git a/file b/file
170 index b5dd6c9..f910ae9 100644
182 test_expect_success
'real edit works' '
183 test_write_lines e n d | git add -p &&
185 diff_cmp expected output
188 test_expect_success
'setup file' '
189 test_write_lines a "" b "" c >file &&
191 test_write_lines a "" d "" c >file
194 test_expect_success
'setup patch' '
208 test_expect_success
'setup expected' '
210 diff --git a/file b/file
211 index b5dd6c9..f910ae9 100644
224 test_expect_success
'edit can strip spaces from empty context lines' '
225 test_write_lines e n q | git add -p 2>error &&
226 test_must_be_empty error &&
228 diff_cmp expected output
231 test_expect_success
'skip files similarly as commit -a' '
233 echo file >.gitignore &&
234 echo changed >file &&
235 echo y | git add -p file &&
238 git commit -am commit &&
239 git diff >expected &&
240 diff_cmp expected output &&
241 git reset --hard HEAD^
245 test_expect_success FILEMODE
'patch does not affect mode' '
247 echo content >>file &&
249 printf "n\\ny\\n" | git add -p &&
250 git show :file | grep content &&
251 git diff file | grep "new mode"
254 test_expect_success FILEMODE
'stage mode but not hunk' '
256 echo content >>file &&
258 printf "y\\nn\\n" | git add -p &&
259 git diff --cached file | grep "new mode" &&
260 git diff file | grep "+content"
264 test_expect_success FILEMODE
'stage mode and hunk' '
266 echo content >>file &&
268 printf "y\\ny\\n" | git add -p &&
269 git diff --cached file | grep "new mode" &&
270 git diff --cached file | grep "+content" &&
271 test -z "$(git diff file)"
274 # end of tests disabled when filemode is not usable
276 test_expect_success
'different prompts for mode change/deleted' '
280 git add --chmod=+x file deleted &&
281 echo changed >file &&
283 test_write_lines n n n |
284 git -c core.filemode=true add -p >actual &&
285 sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
286 cat >expect <<-\EOF &&
287 (1/1) Stage deletion [y,n,q,a,d,?]?
288 (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,?]?
289 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
291 test_cmp expect actual.filtered
294 test_expect_success
'correct message when there is nothing to do' '
297 test_i18ngrep "No changes" err &&
298 printf "\\0123" >binary &&
300 printf "\\0abc" >binary &&
302 test_i18ngrep "Only binary files changed" err
305 test_expect_success
'setup again' '
307 test_chmod +x file &&
311 # Write the patch file with a new line at the top and bottom
312 test_expect_success
'setup patch' '
314 index 180b47c..b6f2c08 100644
322 \ No newline at end of file
326 # Expected output, diff is similar to the patch but w/ diff at the top
327 test_expect_success
'setup expected' '
328 echo diff --git a/file b/file >expected &&
329 cat patch |sed "/^index/s/ 100644/ 100755/" >>expected &&
330 cat >expected-output <<-\EOF
338 \ No newline at end of file
347 \ No newline at end of file
351 # Test splitting the first patch, then adding both
352 test_expect_success C_LOCALE_OUTPUT
'add first line works' '
353 git commit -am "clear local changes" &&
355 printf "%s\n" s y y | git add -p file 2>error |
356 sed -n -e "s/^([1-2]\/[1-2]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
357 -e "/^[-+@ \\\\]"/p >output &&
358 test_must_be_empty error &&
359 git diff --cached >diff &&
360 diff_cmp expected diff &&
361 test_cmp expected-output output
364 test_expect_success
'setup expected' '
365 cat >expected <<-\EOF
366 diff --git a/non-empty b/non-empty
367 deleted file mode 100644
368 index d95f3ad..0000000
376 test_expect_success
'deleting a non-empty file' '
378 echo content >non-empty &&
380 git commit -m non-empty &&
382 echo y | git add -p non-empty &&
383 git diff --cached >diff &&
384 diff_cmp expected diff
387 test_expect_success
'setup expected' '
388 cat >expected <<-\EOF
389 diff --git a/empty b/empty
390 deleted file mode 100644
391 index e69de29..0000000
395 test_expect_success
'deleting an empty file' '
399 git commit -m empty &&
401 echo y | git add -p empty &&
402 git diff --cached >diff &&
403 diff_cmp expected diff
406 test_expect_success
'split hunk setup' '
408 test_write_lines 10 20 30 40 50 60 >test &&
411 git commit -m test &&
413 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
416 test_expect_success
'split hunk "add -p (edit)"' '
417 # Split, say Edit and do nothing. Then:
419 # 1. Broken version results in a patch that does not apply and
420 # only takes [y/n] (edit again) so the first q is discarded
421 # and then n attempts to discard the edit. Repeat q enough
424 # 2. Correct version applies the (not)edited version, and asks
425 # about the next hunk, against which we say q and program
427 printf "%s\n" s e q n q q |
428 EDITOR=: git add -p &&
433 test_expect_failure
'split hunk "add -p (no, yes, edit)"' '
434 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
436 # test sequence is s(plit), n(o), y(es), e(dit)
437 # q n q q is there to make sure we exit at the end.
438 printf "%s\n" s n y e q n q q |
439 EDITOR=: git add -p 2>error &&
440 test_must_be_empty error &&
445 test_expect_success
'split hunk with incomplete line at end' '
447 printf "missing LF" >>test &&
449 test_write_lines before 10 20 30 40 50 60 70 >test &&
450 git grep --cached missing &&
451 test_write_lines s n y q | git add -p &&
452 test_must_fail git grep --cached missing &&
454 test_must_fail git grep --cached before
457 test_expect_failure
'edit, adding lines to the first hunk' '
458 test_write_lines 10 11 20 30 40 50 51 60 >test &&
460 tr _ " " >patch <<-EOF &&
470 # test sequence is s(plit), e(dit), n(o)
471 # q n q q is there to make sure we exit at the end.
472 printf "%s\n" s e n q n q q |
473 EDITOR=./fake_editor.sh git add -p 2>error &&
474 test_must_be_empty error &&
475 git diff --cached >actual &&
479 test_expect_success
'patch mode ignores unmerged entries' '
481 test_commit conflict &&
482 test_commit non-conflict &&
483 git checkout -b side &&
484 test_commit side conflict.t &&
485 git checkout master &&
486 test_commit master conflict.t &&
487 test_must_fail git merge side &&
488 echo changed >non-conflict.t &&
489 echo y | git add -p >output &&
490 ! grep a/conflict.t output &&
491 cat >expected <<-\EOF &&
492 * Unmerged path conflict.t
493 diff --git a/non-conflict.t b/non-conflict.t
494 index f766221..5ea2ed4 100644
501 git diff --cached >diff &&
502 diff_cmp expected diff
505 test_expect_success
'diffs can be colorized' '
508 echo content >test &&
510 force_color git add -p >output 2>&1 <y &&
512 # We do not want to depend on the exact coloring scheme
513 # git uses for diffs, so just check that we saw some kind of color.
514 grep "$(printf "\\033")" output
517 test_expect_success
'diffFilter filters diff' '
520 echo content >test &&
521 test_config interactive.diffFilter "sed s/^/foo:/" &&
523 force_color git add -p >output 2>&1 <y &&
525 # avoid depending on the exact coloring or content of the prompts,
526 # and just make sure we saw our diff prefixed
527 grep foo:.*content output
530 test_expect_success
'detect bogus diffFilter output' '
533 echo content >test &&
534 test_config interactive.diffFilter "echo too-short" &&
536 test_must_fail force_color git add -p <y
539 test_expect_success
'diff.algorithm is passed to `git diff-files`' '
544 echo changed >file &&
545 test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
546 test_i18ngrep "error: option diff-algorithm accepts " err
549 test_expect_success
'patch-mode via -i prompts for files' '
554 git add -i <<-\EOF &&
563 git diff --cached --name-only >actual &&
564 diff_cmp expect actual
567 test_expect_success
'add -p handles globs' '
572 echo base >subdir/two.c &&
574 git commit -m base &&
576 echo change >one.c &&
577 echo change >subdir/two.c &&
578 git add -p "*.c" <<-\EOF &&
583 cat >expect <<-\EOF &&
587 git diff --cached --name-only >actual &&
588 test_cmp expect actual
591 test_expect_success
'add -p handles relative paths' '
594 echo base >relpath.c &&
596 git commit -m relpath &&
598 echo change >relpath.c &&
600 git -C subdir add -p .. 2>error <<-\EOF &&
604 test_must_be_empty error &&
606 cat >expect <<-\EOF &&
609 git diff --cached --name-only >actual &&
610 test_cmp expect actual
613 test_expect_success
'add -p does not expand argument lists' '
616 echo content >not-changed &&
617 git add not-changed &&
618 git commit -m "add not-changed file" &&
621 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
625 # we know that "file" must be mentioned since we actually
626 # update it, but we want to be sure that our "." pathspec
627 # was not expanded into the argument list of any command.
628 # So look only for "not-changed".
629 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
632 test_expect_success
'hunk-editing handles custom comment char' '
634 echo change >>file &&
635 test_config core.commentChar "\$" &&
636 echo e | GIT_EDITOR=true git add -p &&
640 test_expect_success
'add -p works even with color.ui=always' '
642 echo change >>file &&
643 test_config color.ui always &&
644 echo y | git add -p &&
646 git diff --cached --name-only >actual &&
647 test_cmp expect actual
650 test_expect_success
'setup different kinds of dirty submodules' '
651 test_create_repo for-submodules &&
654 test_commit initial &&
655 test_create_repo dirty-head &&
660 cp -R dirty-head dirty-otherwise &&
661 cp -R dirty-head dirty-both-ways &&
662 git add dirty-head &&
663 git add dirty-otherwise dirty-both-ways &&
664 git commit -m initial &&
667 test_commit updated &&
668 cd ../dirty-both-ways &&
669 test_commit updated &&
670 echo dirty >>initial &&
672 cd ../dirty-otherwise &&
673 echo dirty >>initial &&
676 git -C for-submodules diff-files --name-only >actual &&
677 cat >expected <<-\EOF &&
682 test_cmp expected actual &&
683 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
684 cat >expected <<-\EOF &&
688 test_cmp expected actual
691 test_expect_success
'status ignores dirty submodules (except HEAD)' '
692 git -C for-submodules add -i </dev/null >output &&
693 grep dirty-head output &&
694 grep dirty-both-ways output &&
695 ! grep dirty-otherwise output
698 test_expect_success
'set up pathological context' '
700 test_write_lines a a a a a a a a a a a >a &&
703 test_write_lines c b a a a a a a a b a a a a >a &&
704 test_write_lines a a a a a a a b a a a a >expected-1 &&
705 test_write_lines b a a a a a a a b a a a a >expected-2 &&
706 # check editing can cope with missing header and deleted context lines
707 # as well as changes to other lines
708 test_write_lines +b " a" >patch
711 test_expect_success
'add -p works with pathological context lines' '
715 git cat-file blob :a >actual &&
716 test_cmp expected-1 actual
719 test_expect_success
'add -p patch editing works with pathological context lines' '
721 # n q q below is in case edit fails
722 printf "%s\n" e y n q q |
724 git cat-file blob :a >actual &&
725 test_cmp expected-2 actual
728 test_expect_success
'checkout -p works with pathological context lines' '
729 test_write_lines a a a a a a >a &&
731 test_write_lines a b a b a b a b a b a > a&&
732 test_write_lines s n n y q | git checkout -p &&
733 test_write_lines a b a b a a b a b a >expect &&
737 test_expect_success
'show help from add--helper' '
739 cat >expect <<-EOF &&
741 <BOLD>*** Commands ***<RESET>
742 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
743 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
744 <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes<RESET>
745 <BOLD;RED>update - add working tree state to the staged set of changes<RESET>
746 <BOLD;RED>revert - revert staged set of changes back to the HEAD version<RESET>
747 <BOLD;RED>patch - pick hunks and update selectively<RESET>
748 <BOLD;RED>diff - view diff between HEAD and index<RESET>
749 <BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
750 <BOLD>*** Commands ***<RESET>
751 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
752 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
753 <BOLD;BLUE>What now<RESET>>$SP
756 test_write_lines h | force_color git add -i >actual.colored &&
757 test_decode_color <actual.colored >actual &&
758 test_i18ncmp expect actual