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
109 test_expect_success
'setup expected' '
110 cat >expected <<-\EOF
114 test_expect_success
'dummy edit works' '
116 test_write_lines e a | git add -p &&
118 diff_cmp expected diff
121 test_expect_success
'setup patch' '
131 test_expect_success
'setup fake editor' '
132 write_script "fake_editor.sh" <<-\EOF &&
133 mv -f "$1" oldpatch &&
136 test_set_editor "$(pwd)/fake_editor.sh"
139 test_expect_success
'bad edit rejected' '
141 test_write_lines e n d | git add -p >output &&
142 grep "hunk does not apply" output
145 test_expect_success
'setup patch' '
152 test_expect_success
'garbage edit rejected' '
154 test_write_lines e n d | git add -p >output &&
155 grep "hunk does not apply" output
158 test_expect_success
'setup patch' '
168 test_expect_success
'setup expected' '
169 cat >expected <<-\EOF
170 diff --git a/file b/file
171 index b5dd6c9..f910ae9 100644
183 test_expect_success
'real edit works' '
184 test_write_lines e n d | git add -p &&
186 diff_cmp expected output
189 test_expect_success
'setup file' '
190 test_write_lines a "" b "" c >file &&
192 test_write_lines a "" d "" c >file
195 test_expect_success
'setup patch' '
209 test_expect_success
'setup expected' '
211 diff --git a/file b/file
212 index b5dd6c9..f910ae9 100644
225 test_expect_success
'edit can strip spaces from empty context lines' '
226 test_write_lines e n q | git add -p 2>error &&
227 test_must_be_empty error &&
229 diff_cmp expected output
232 test_expect_success
'skip files similarly as commit -a' '
234 echo file >.gitignore &&
235 echo changed >file &&
236 echo y | git add -p file &&
239 git commit -am commit &&
240 git diff >expected &&
241 diff_cmp expected output &&
242 git reset --hard HEAD^
246 test_expect_success FILEMODE
'patch does not affect mode' '
248 echo content >>file &&
250 printf "n\\ny\\n" | git add -p &&
251 git show :file | grep content &&
252 git diff file | grep "new mode"
255 test_expect_success FILEMODE
'stage mode but not hunk' '
257 echo content >>file &&
259 printf "y\\nn\\n" | git add -p &&
260 git diff --cached file | grep "new mode" &&
261 git diff file | grep "+content"
265 test_expect_success FILEMODE
'stage mode and hunk' '
267 echo content >>file &&
269 printf "y\\ny\\n" | git add -p &&
270 git diff --cached file | grep "new mode" &&
271 git diff --cached file | grep "+content" &&
272 test -z "$(git diff file)"
275 # end of tests disabled when filemode is not usable
277 test_expect_success
'setup again' '
279 test_chmod +x file &&
283 # Write the patch file with a new line at the top and bottom
284 test_expect_success
'setup patch' '
286 index 180b47c..b6f2c08 100644
294 \ No newline at end of file
298 # Expected output, diff is similar to the patch but w/ diff at the top
299 test_expect_success
'setup expected' '
300 echo diff --git a/file b/file >expected &&
301 cat patch |sed "/^index/s/ 100644/ 100755/" >>expected &&
302 cat >expected-output <<-\EOF
310 \ No newline at end of file
319 \ No newline at end of file
323 # Test splitting the first patch, then adding both
324 test_expect_success C_LOCALE_OUTPUT
'add first line works' '
325 git commit -am "clear local changes" &&
327 printf "%s\n" s y y | git add -p file 2>error |
328 sed -n -e "s/^([1-2]\/[1-2]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
329 -e "/^[-+@ \\\\]"/p >output &&
330 test_must_be_empty error &&
331 git diff --cached >diff &&
332 diff_cmp expected diff &&
333 test_cmp expected-output output
336 test_expect_success
'setup expected' '
337 cat >expected <<-\EOF
338 diff --git a/non-empty b/non-empty
339 deleted file mode 100644
340 index d95f3ad..0000000
348 test_expect_success
'deleting a non-empty file' '
350 echo content >non-empty &&
352 git commit -m non-empty &&
354 echo y | git add -p non-empty &&
355 git diff --cached >diff &&
356 diff_cmp expected diff
359 test_expect_success
'setup expected' '
360 cat >expected <<-\EOF
361 diff --git a/empty b/empty
362 deleted file mode 100644
363 index e69de29..0000000
367 test_expect_success
'deleting an empty file' '
371 git commit -m empty &&
373 echo y | git add -p empty &&
374 git diff --cached >diff &&
375 diff_cmp expected diff
378 test_expect_success
'split hunk setup' '
380 test_write_lines 10 20 30 40 50 60 >test &&
383 git commit -m test &&
385 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
388 test_expect_success
'split hunk "add -p (edit)"' '
389 # Split, say Edit and do nothing. Then:
391 # 1. Broken version results in a patch that does not apply and
392 # only takes [y/n] (edit again) so the first q is discarded
393 # and then n attempts to discard the edit. Repeat q enough
396 # 2. Correct version applies the (not)edited version, and asks
397 # about the next hunk, against which we say q and program
399 printf "%s\n" s e q n q q |
400 EDITOR=: git add -p &&
405 test_expect_failure
'split hunk "add -p (no, yes, edit)"' '
406 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
408 # test sequence is s(plit), n(o), y(es), e(dit)
409 # q n q q is there to make sure we exit at the end.
410 printf "%s\n" s n y e q n q q |
411 EDITOR=: git add -p 2>error &&
412 test_must_be_empty error &&
417 test_expect_failure
'edit, adding lines to the first hunk' '
418 test_write_lines 10 11 20 30 40 50 51 60 >test &&
420 tr _ " " >patch <<-EOF &&
430 # test sequence is s(plit), e(dit), n(o)
431 # q n q q is there to make sure we exit at the end.
432 printf "%s\n" s e n q n q q |
433 EDITOR=./fake_editor.sh git add -p 2>error &&
434 test_must_be_empty error &&
435 git diff --cached >actual &&
439 test_expect_success
'patch mode ignores unmerged entries' '
441 test_commit conflict &&
442 test_commit non-conflict &&
443 git checkout -b side &&
444 test_commit side conflict.t &&
445 git checkout master &&
446 test_commit master conflict.t &&
447 test_must_fail git merge side &&
448 echo changed >non-conflict.t &&
449 echo y | git add -p >output &&
450 ! grep a/conflict.t output &&
451 cat >expected <<-\EOF &&
452 * Unmerged path conflict.t
453 diff --git a/non-conflict.t b/non-conflict.t
454 index f766221..5ea2ed4 100644
461 git diff --cached >diff &&
462 diff_cmp expected diff
465 test_expect_success
'diffs can be colorized' '
468 echo content >test &&
470 force_color git add -p >output 2>&1 <y &&
472 # We do not want to depend on the exact coloring scheme
473 # git uses for diffs, so just check that we saw some kind of color.
474 grep "$(printf "\\033")" output
477 test_expect_success
'diffFilter filters diff' '
480 echo content >test &&
481 test_config interactive.diffFilter "sed s/^/foo:/" &&
483 force_color git add -p >output 2>&1 <y &&
485 # avoid depending on the exact coloring or content of the prompts,
486 # and just make sure we saw our diff prefixed
487 grep foo:.*content output
490 test_expect_success
'detect bogus diffFilter output' '
493 echo content >test &&
494 test_config interactive.diffFilter "echo too-short" &&
496 test_must_fail force_color git add -p <y
499 test_expect_success
'patch-mode via -i prompts for files' '
504 git add -i <<-\EOF &&
513 git diff --cached --name-only >actual &&
514 diff_cmp expect actual
517 test_expect_success
'add -p handles globs' '
522 echo base >subdir/two.c &&
524 git commit -m base &&
526 echo change >one.c &&
527 echo change >subdir/two.c &&
528 git add -p "*.c" <<-\EOF &&
533 cat >expect <<-\EOF &&
537 git diff --cached --name-only >actual &&
538 test_cmp expect actual
541 test_expect_success
'add -p handles relative paths' '
544 echo base >relpath.c &&
546 git commit -m relpath &&
548 echo change >relpath.c &&
550 git -C subdir add -p .. 2>error <<-\EOF &&
554 test_must_be_empty error &&
556 cat >expect <<-\EOF &&
559 git diff --cached --name-only >actual &&
560 test_cmp expect actual
563 test_expect_success
'add -p does not expand argument lists' '
566 echo content >not-changed &&
567 git add not-changed &&
568 git commit -m "add not-changed file" &&
571 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
575 # we know that "file" must be mentioned since we actually
576 # update it, but we want to be sure that our "." pathspec
577 # was not expanded into the argument list of any command.
578 # So look only for "not-changed".
579 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
582 test_expect_success
'hunk-editing handles custom comment char' '
584 echo change >>file &&
585 test_config core.commentChar "\$" &&
586 echo e | GIT_EDITOR=true git add -p &&
590 test_expect_success
'add -p works even with color.ui=always' '
592 echo change >>file &&
593 test_config color.ui always &&
594 echo y | git add -p &&
596 git diff --cached --name-only >actual &&
597 test_cmp expect actual
600 test_expect_success
'setup different kinds of dirty submodules' '
601 test_create_repo for-submodules &&
604 test_commit initial &&
605 test_create_repo dirty-head &&
610 cp -R dirty-head dirty-otherwise &&
611 cp -R dirty-head dirty-both-ways &&
612 git add dirty-head &&
613 git add dirty-otherwise dirty-both-ways &&
614 git commit -m initial &&
617 test_commit updated &&
618 cd ../dirty-both-ways &&
619 test_commit updated &&
620 echo dirty >>initial &&
622 cd ../dirty-otherwise &&
623 echo dirty >>initial &&
626 git -C for-submodules diff-files --name-only >actual &&
627 cat >expected <<-\EOF &&
632 test_cmp expected actual &&
633 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
634 cat >expected <<-\EOF &&
638 test_cmp expected actual
641 test_expect_success
'status ignores dirty submodules (except HEAD)' '
642 git -C for-submodules add -i </dev/null >output &&
643 grep dirty-head output &&
644 grep dirty-both-ways output &&
645 ! grep dirty-otherwise output
648 test_expect_success
'set up pathological context' '
650 test_write_lines a a a a a a a a a a a >a &&
653 test_write_lines c b a a a a a a a b a a a a >a &&
654 test_write_lines a a a a a a a b a a a a >expected-1 &&
655 test_write_lines b a a a a a a a b a a a a >expected-2 &&
656 # check editing can cope with missing header and deleted context lines
657 # as well as changes to other lines
658 test_write_lines +b " a" >patch
661 test_expect_success
'add -p works with pathological context lines' '
665 git cat-file blob :a >actual &&
666 test_cmp expected-1 actual
669 test_expect_success
'add -p patch editing works with pathological context lines' '
671 # n q q below is in case edit fails
672 printf "%s\n" e y n q q |
674 git cat-file blob :a >actual &&
675 test_cmp expected-2 actual
678 test_expect_success
'checkout -p works with pathological context lines' '
679 test_write_lines a a a a a a >a &&
681 test_write_lines a b a b a b a b a b a > a&&
682 test_write_lines s n n y q | git checkout -p &&
683 test_write_lines a b a b a a b a b a >expect &&
687 test_expect_success
'show help from add--helper' '
689 cat >expect <<-EOF &&
691 <BOLD>*** Commands ***<RESET>
692 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
693 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
694 <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes<RESET>
695 <BOLD;RED>update - add working tree state to the staged set of changes<RESET>
696 <BOLD;RED>revert - revert staged set of changes back to the HEAD version<RESET>
697 <BOLD;RED>patch - pick hunks and update selectively<RESET>
698 <BOLD;RED>diff - view diff between HEAD and index<RESET>
699 <BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
700 <BOLD>*** Commands ***<RESET>
701 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
702 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
703 <BOLD;BLUE>What now<RESET>>$SP
706 test_write_lines h | force_color git add -i >actual.colored &&
707 test_decode_color <actual.colored >actual &&
708 test_i18ncmp expect actual