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 test_expect_success
'setup (initial)' '
32 test_expect_success
'status works (initial)' '
33 git add -i </dev/null >output &&
34 grep "+1/-0 *+2/-0 file" output
37 test_expect_success
'setup expected' '
40 index 0000000..d95f3ad
48 test_expect_success
'diff works (initial)' '
49 test_write_lines d 1 | git add -i >output &&
50 sed -ne "/new file/,/content/p" <output >diff &&
51 diff_cmp expected diff
53 test_expect_success
'revert works (initial)' '
55 test_write_lines r 1 | git add -i &&
56 git ls-files >output &&
60 test_expect_success
'setup (commit)' '
61 echo baseline >file &&
63 git commit -m commit &&
64 echo content >>file &&
69 test_expect_success
'status works (commit)' '
70 git add -i </dev/null >output &&
71 grep "+1/-0 *+2/-0 file" output
74 test_expect_success
'update can stage deletions' '
78 test_write_lines u t "" | git add -i &&
79 git ls-files to-delete >output &&
80 test_must_be_empty output
83 test_expect_success
'setup expected' '
85 index 180b47c..b6f2c08 100644
94 test_expect_success
'diff works (commit)' '
95 test_write_lines d 1 | git add -i >output &&
96 sed -ne "/^index/,/content/p" <output >diff &&
97 diff_cmp expected diff
99 test_expect_success
'revert works (commit)' '
101 test_write_lines r 1 | git add -i &&
102 git add -i </dev/null >output &&
103 grep "unchanged *+3/-0 file" output
107 test_expect_success
'setup expected' '
108 cat >expected <<-\EOF
112 test_expect_success
'dummy edit works' '
114 test_write_lines e a | git add -p &&
116 diff_cmp expected diff
119 test_expect_success
'setup patch' '
129 test_expect_success
'setup fake editor' '
130 write_script "fake_editor.sh" <<-\EOF &&
131 mv -f "$1" oldpatch &&
134 test_set_editor "$(pwd)/fake_editor.sh"
137 test_expect_success
'bad edit rejected' '
139 test_write_lines e n d | git add -p >output &&
140 grep "hunk does not apply" output
143 test_expect_success
'setup patch' '
150 test_expect_success
'garbage edit rejected' '
152 test_write_lines e n d | git add -p >output &&
153 grep "hunk does not apply" output
156 test_expect_success
'setup patch' '
166 test_expect_success
'setup expected' '
167 cat >expected <<-\EOF
168 diff --git a/file b/file
169 index b5dd6c9..f910ae9 100644
181 test_expect_success
'real edit works' '
182 test_write_lines e n d | git add -p &&
184 diff_cmp expected output
187 test_expect_success
'setup file' '
188 test_write_lines a "" b "" c >file &&
190 test_write_lines a "" d "" c >file
193 test_expect_success
'setup patch' '
207 test_expect_success
'setup expected' '
209 diff --git a/file b/file
210 index b5dd6c9..f910ae9 100644
223 test_expect_success
'edit can strip spaces from empty context lines' '
224 test_write_lines e n q | git add -p 2>error &&
225 test_must_be_empty error &&
227 diff_cmp expected output
230 test_expect_success
'skip files similarly as commit -a' '
232 echo file >.gitignore &&
233 echo changed >file &&
234 echo y | git add -p file &&
237 git commit -am commit &&
238 git diff >expected &&
239 diff_cmp expected output &&
240 git reset --hard HEAD^
244 test_expect_success FILEMODE
'patch does not affect mode' '
246 echo content >>file &&
248 printf "n\\ny\\n" | git add -p &&
249 git show :file | grep content &&
250 git diff file | grep "new mode"
253 test_expect_success FILEMODE
'stage mode but not hunk' '
255 echo content >>file &&
257 printf "y\\nn\\n" | git add -p &&
258 git diff --cached file | grep "new mode" &&
259 git diff file | grep "+content"
263 test_expect_success FILEMODE
'stage mode and hunk' '
265 echo content >>file &&
267 printf "y\\ny\\n" | git add -p &&
268 git diff --cached file | grep "new mode" &&
269 git diff --cached file | grep "+content" &&
270 test -z "$(git diff file)"
273 # end of tests disabled when filemode is not usable
275 test_expect_success
'setup again' '
277 test_chmod +x file &&
281 # Write the patch file with a new line at the top and bottom
282 test_expect_success
'setup patch' '
284 index 180b47c..b6f2c08 100644
292 \ No newline at end of file
296 # Expected output, diff is similar to the patch but w/ diff at the top
297 test_expect_success
'setup expected' '
298 echo diff --git a/file b/file >expected &&
299 cat patch |sed "/^index/s/ 100644/ 100755/" >>expected &&
300 cat >expected-output <<-\EOF
308 \ No newline at end of file
317 \ No newline at end of file
321 # Test splitting the first patch, then adding both
322 test_expect_success C_LOCALE_OUTPUT
'add first line works' '
323 git commit -am "clear local changes" &&
325 printf "%s\n" s y y | git add -p file 2>error |
326 sed -n -e "s/^([1-2]\/[1-2]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
327 -e "/^[-+@ \\\\]"/p >output &&
328 test_must_be_empty error &&
329 git diff --cached >diff &&
330 diff_cmp expected diff &&
331 test_cmp expected-output output
334 test_expect_success
'setup expected' '
335 cat >expected <<-\EOF
336 diff --git a/non-empty b/non-empty
337 deleted file mode 100644
338 index d95f3ad..0000000
346 test_expect_success
'deleting a non-empty file' '
348 echo content >non-empty &&
350 git commit -m non-empty &&
352 echo y | git add -p non-empty &&
353 git diff --cached >diff &&
354 diff_cmp expected diff
357 test_expect_success
'setup expected' '
358 cat >expected <<-\EOF
359 diff --git a/empty b/empty
360 deleted file mode 100644
361 index e69de29..0000000
365 test_expect_success
'deleting an empty file' '
369 git commit -m empty &&
371 echo y | git add -p empty &&
372 git diff --cached >diff &&
373 diff_cmp expected diff
376 test_expect_success
'split hunk setup' '
378 test_write_lines 10 20 30 40 50 60 >test &&
381 git commit -m test &&
383 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
386 test_expect_success
'split hunk "add -p (edit)"' '
387 # Split, say Edit and do nothing. Then:
389 # 1. Broken version results in a patch that does not apply and
390 # only takes [y/n] (edit again) so the first q is discarded
391 # and then n attempts to discard the edit. Repeat q enough
394 # 2. Correct version applies the (not)edited version, and asks
395 # about the next hunk, against which we say q and program
397 printf "%s\n" s e q n q q |
398 EDITOR=: git add -p &&
403 test_expect_failure
'split hunk "add -p (no, yes, edit)"' '
404 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
406 # test sequence is s(plit), n(o), y(es), e(dit)
407 # q n q q is there to make sure we exit at the end.
408 printf "%s\n" s n y e q n q q |
409 EDITOR=: git add -p 2>error &&
410 test_must_be_empty error &&
415 test_expect_success
'patch mode ignores unmerged entries' '
417 test_commit conflict &&
418 test_commit non-conflict &&
419 git checkout -b side &&
420 test_commit side conflict.t &&
421 git checkout master &&
422 test_commit master conflict.t &&
423 test_must_fail git merge side &&
424 echo changed >non-conflict.t &&
425 echo y | git add -p >output &&
426 ! grep a/conflict.t output &&
427 cat >expected <<-\EOF &&
428 * Unmerged path conflict.t
429 diff --git a/non-conflict.t b/non-conflict.t
430 index f766221..5ea2ed4 100644
437 git diff --cached >diff &&
438 diff_cmp expected diff
441 test_expect_success TTY
'diffs can be colorized' '
444 echo content >test &&
445 printf y | test_terminal git add -p >output 2>&1 &&
447 # We do not want to depend on the exact coloring scheme
448 # git uses for diffs, so just check that we saw some kind of color.
449 grep "$(printf "\\033")" output
452 test_expect_success TTY
'diffFilter filters diff' '
455 echo content >test &&
456 test_config interactive.diffFilter "sed s/^/foo:/" &&
457 printf y | test_terminal git add -p >output 2>&1 &&
459 # avoid depending on the exact coloring or content of the prompts,
460 # and just make sure we saw our diff prefixed
461 grep foo:.*content output
464 test_expect_success TTY
'detect bogus diffFilter output' '
467 echo content >test &&
468 test_config interactive.diffFilter "echo too-short" &&
469 printf y | test_must_fail test_terminal git add -p
472 test_expect_success
'patch-mode via -i prompts for files' '
477 git add -i <<-\EOF &&
486 git diff --cached --name-only >actual &&
487 diff_cmp expect actual
490 test_expect_success
'add -p handles globs' '
495 echo base >subdir/two.c &&
497 git commit -m base &&
499 echo change >one.c &&
500 echo change >subdir/two.c &&
501 git add -p "*.c" <<-\EOF &&
506 cat >expect <<-\EOF &&
510 git diff --cached --name-only >actual &&
511 test_cmp expect actual
514 test_expect_success
'add -p handles relative paths' '
517 echo base >relpath.c &&
519 git commit -m relpath &&
521 echo change >relpath.c &&
523 git -C subdir add -p .. 2>error <<-\EOF &&
527 test_must_be_empty error &&
529 cat >expect <<-\EOF &&
532 git diff --cached --name-only >actual &&
533 test_cmp expect actual
536 test_expect_success
'add -p does not expand argument lists' '
539 echo content >not-changed &&
540 git add not-changed &&
541 git commit -m "add not-changed file" &&
544 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
548 # we know that "file" must be mentioned since we actually
549 # update it, but we want to be sure that our "." pathspec
550 # was not expanded into the argument list of any command.
551 # So look only for "not-changed".
552 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
555 test_expect_success
'hunk-editing handles custom comment char' '
557 echo change >>file &&
558 test_config core.commentChar "\$" &&
559 echo e | GIT_EDITOR=true git add -p &&
563 test_expect_success
'add -p works even with color.ui=always' '
565 echo change >>file &&
566 test_config color.ui always &&
567 echo y | git add -p &&
569 git diff --cached --name-only >actual &&
570 test_cmp expect actual
573 test_expect_success
'setup different kinds of dirty submodules' '
574 test_create_repo for-submodules &&
577 test_commit initial &&
578 test_create_repo dirty-head &&
583 cp -R dirty-head dirty-otherwise &&
584 cp -R dirty-head dirty-both-ways &&
585 git add dirty-head &&
586 git add dirty-otherwise dirty-both-ways &&
587 git commit -m initial &&
590 test_commit updated &&
591 cd ../dirty-both-ways &&
592 test_commit updated &&
593 echo dirty >>initial &&
595 cd ../dirty-otherwise &&
596 echo dirty >>initial &&
599 git -C for-submodules diff-files --name-only >actual &&
600 cat >expected <<-\EOF &&
605 test_cmp expected actual &&
606 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
607 cat >expected <<-\EOF &&
611 test_cmp expected actual
614 test_expect_success
'status ignores dirty submodules (except HEAD)' '
615 git -C for-submodules add -i </dev/null >output &&
616 grep dirty-head output &&
617 grep dirty-both-ways output &&
618 ! grep dirty-otherwise output
621 test_expect_success
'set up pathological context' '
623 test_write_lines a a a a a a a a a a a >a &&
626 test_write_lines c b a a a a a a a b a a a a >a &&
627 test_write_lines a a a a a a a b a a a a >expected-1 &&
628 test_write_lines b a a a a a a a b a a a a >expected-2 &&
629 # check editing can cope with missing header and deleted context lines
630 # as well as changes to other lines
631 test_write_lines +b " a" >patch
634 test_expect_success
'add -p works with pathological context lines' '
638 git cat-file blob :a >actual &&
639 test_cmp expected-1 actual
642 test_expect_success
'add -p patch editing works with pathological context lines' '
644 # n q q below is in case edit fails
645 printf "%s\n" e y n q q |
647 git cat-file blob :a >actual &&
648 test_cmp expected-2 actual
651 test_expect_success
'checkout -p works with pathological context lines' '
652 test_write_lines a a a a a a >a &&
654 test_write_lines a b a b a b a b a b a > a&&
655 test_write_lines s n n y q | git checkout -p &&
656 test_write_lines a b a b a a b a b a >expect &&
660 test_expect_success
'show help from add--helper' '
662 cat >expect <<-EOF &&
664 <BOLD>*** Commands ***<RESET>
665 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
666 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
667 <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes<RESET>
668 <BOLD;RED>update - add working tree state to the staged set of changes<RESET>
669 <BOLD;RED>revert - revert staged set of changes back to the HEAD version<RESET>
670 <BOLD;RED>patch - pick hunks and update selectively<RESET>
671 <BOLD;RED>diff - view diff between HEAD and index<RESET>
672 <BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
673 <BOLD>*** Commands ***<RESET>
674 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
675 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
676 <BOLD;BLUE>What now<RESET>>$SP
679 test_write_lines h | GIT_PAGER_IN_USE=true TERM=vt100 git add -i >actual.colored &&
680 test_decode_color <actual.colored >actual &&
681 test_i18ncmp expect actual