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 (echo d; echo 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 (echo r; echo 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 'setup expected' '
76 index 180b47c..b6f2c08 100644
85 test_expect_success 'diff works (commit)' '
86 (echo d; echo 1) | git add -i >output &&
87 sed -ne "/^index/,/content/p" <output >diff &&
88 diff_cmp expected diff
90 test_expect_success 'revert works (commit)' '
92 (echo r; echo 1) | git add -i &&
93 git add -i </dev/null >output &&
94 grep "unchanged *+3/-0 file" output
98 test_expect_success 'setup expected' '
103 test_expect_success 'dummy edit works' '
105 (echo e; echo a) | git add -p &&
107 diff_cmp expected diff
110 test_expect_success 'setup patch' '
120 test_expect_success 'setup fake editor' '
121 write_script "fake_editor.sh" <<-\EOF &&
122 mv -f "$1" oldpatch &&
125 test_set_editor "$(pwd)/fake_editor.sh"
128 test_expect_success 'bad edit rejected' '
130 (echo e; echo n; echo d) | git add -p >output &&
131 grep "hunk does not apply" output
134 test_expect_success 'setup patch' '
141 test_expect_success 'garbage edit rejected' '
143 (echo e; echo n; echo d) | git add -p >output &&
144 grep "hunk does not apply" output
147 test_expect_success 'setup patch' '
157 test_expect_success 'setup expected' '
158 cat >expected <<-\EOF
159 diff --git a/file b/file
160 index b5dd6c9..f910ae9 100644
172 test_expect_success 'real edit works' '
173 (echo e; echo n; echo d) | git add -p &&
175 diff_cmp expected output
178 test_expect_success 'skip files similarly as commit -a' '
180 echo file >.gitignore &&
181 echo changed >file &&
182 echo y | git add -p file &&
185 git commit -am commit &&
186 git diff >expected &&
187 diff_cmp expected output &&
188 git reset --hard HEAD^
192 test_expect_success FILEMODE 'patch does not affect mode' '
194 echo content >>file &&
196 printf "n\\ny\\n" | git add -p &&
197 git show :file | grep content &&
198 git diff file | grep "new mode"
201 test_expect_success FILEMODE 'stage mode but not hunk' '
203 echo content >>file &&
205 printf "y\\nn\\n" | git add -p &&
206 git diff --cached file | grep "new mode" &&
207 git diff file | grep "+content"
211 test_expect_success FILEMODE 'stage mode and hunk' '
213 echo content >>file &&
215 printf "y\\ny\\n" | git add -p &&
216 git diff --cached file | grep "new mode" &&
217 git diff --cached file | grep "+content" &&
218 test -z "$(git diff file)"
221 # end of tests disabled when filemode is not usable
223 test_expect_success 'setup again' '
225 test_chmod +x file &&
229 # Write the patch file with a new line at the top and bottom
230 test_expect_success 'setup patch' '
232 index 180b47c..b6f2c08 100644
240 \ No newline at end of file
244 # Expected output, diff is similar to the patch but w/ diff at the top
245 test_expect_success 'setup expected' '
246 echo diff --git a/file b/file >expected &&
247 cat patch |sed "/^index/s/ 100644/ 100755/" >>expected &&
248 cat >expected-output <<-\EOF
256 \ No newline at end of file
265 \ No newline at end of file
269 # Test splitting the first patch, then adding both
270 test_expect_success C_LOCALE_OUTPUT 'add first line works' '
271 git commit -am "clear local changes" &&
273 printf "%s\n" s y y | git add -p file 2>error |
274 sed -n -e "s/^Stage this hunk[^@]*\(@@ .*\)/\1/" \
275 -e "/^[-+@ \\\\]"/p >output &&
276 test_must_be_empty error &&
277 git diff --cached >diff &&
278 diff_cmp expected diff &&
279 test_cmp expected-output output
282 test_expect_success 'setup expected' '
283 cat >expected <<-\EOF
284 diff --git a/non-empty b/non-empty
285 deleted file mode 100644
286 index d95f3ad..0000000
294 test_expect_success 'deleting a non-empty file' '
296 echo content >non-empty &&
298 git commit -m non-empty &&
300 echo y | git add -p non-empty &&
301 git diff --cached >diff &&
302 diff_cmp expected diff
305 test_expect_success 'setup expected' '
306 cat >expected <<-\EOF
307 diff --git a/empty b/empty
308 deleted file mode 100644
309 index e69de29..0000000
313 test_expect_success 'deleting an empty file' '
317 git commit -m empty &&
319 echo y | git add -p empty &&
320 git diff --cached >diff &&
321 diff_cmp expected diff
324 test_expect_success 'split hunk setup' '
326 test_write_lines 10 20 30 40 50 60 >test &&
329 git commit -m test &&
331 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
334 test_expect_success 'split hunk "add -p (edit)"' '
335 # Split, say Edit and do nothing. Then:
337 # 1. Broken version results in a patch that does not apply and
338 # only takes [y/n] (edit again) so the first q is discarded
339 # and then n attempts to discard the edit. Repeat q enough
342 # 2. Correct version applies the (not)edited version, and asks
343 # about the next hunk, against which we say q and program
345 printf "%s\n" s e q n q q |
346 EDITOR=: git add -p &&
351 test_expect_failure 'split hunk "add -p (no, yes, edit)"' '
352 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
354 # test sequence is s(plit), n(o), y(es), e(dit)
355 # q n q q is there to make sure we exit at the end.
356 printf "%s\n" s n y e q n q q |
357 EDITOR=: git add -p 2>error &&
358 test_must_be_empty error &&
363 test_expect_success 'patch mode ignores unmerged entries' '
365 test_commit conflict &&
366 test_commit non-conflict &&
367 git checkout -b side &&
368 test_commit side conflict.t &&
369 git checkout master &&
370 test_commit master conflict.t &&
371 test_must_fail git merge side &&
372 echo changed >non-conflict.t &&
373 echo y | git add -p >output &&
374 ! grep a/conflict.t output &&
375 cat >expected <<-\EOF &&
376 * Unmerged path conflict.t
377 diff --git a/non-conflict.t b/non-conflict.t
378 index f766221..5ea2ed4 100644
385 git diff --cached >diff &&
386 diff_cmp expected diff
389 test_expect_success TTY 'diffs can be colorized' '
392 echo content >test &&
393 printf y | test_terminal git add -p >output 2>&1 &&
395 # We do not want to depend on the exact coloring scheme
396 # git uses for diffs, so just check that we saw some kind of color.
397 grep "$(printf "\\033")" output
400 test_expect_success TTY 'diffFilter filters diff' '
403 echo content >test &&
404 test_config interactive.diffFilter "sed s/^/foo:/" &&
405 printf y | test_terminal git add -p >output 2>&1 &&
407 # avoid depending on the exact coloring or content of the prompts,
408 # and just make sure we saw our diff prefixed
409 grep foo:.*content output
412 test_expect_success TTY 'detect bogus diffFilter output' '
415 echo content >test &&
416 test_config interactive.diffFilter "echo too-short" &&
417 printf y | test_must_fail test_terminal git add -p
420 test_expect_success 'patch-mode via -i prompts for files' '
425 git add -i <<-\EOF &&
434 git diff --cached --name-only >actual &&
435 diff_cmp expect actual
438 test_expect_success 'add -p handles globs' '
443 echo base >subdir/two.c &&
445 git commit -m base &&
447 echo change >one.c &&
448 echo change >subdir/two.c &&
449 git add -p "*.c" <<-\EOF &&
454 cat >expect <<-\EOF &&
458 git diff --cached --name-only >actual &&
459 test_cmp expect actual
462 test_expect_success 'add -p handles relative paths' '
465 echo base >relpath.c &&
467 git commit -m relpath &&
469 echo change >relpath.c &&
471 git -C subdir add -p .. 2>error <<-\EOF &&
475 test_must_be_empty error &&
477 cat >expect <<-\EOF &&
480 git diff --cached --name-only >actual &&
481 test_cmp expect actual
484 test_expect_success 'add -p does not expand argument lists' '
487 echo content >not-changed &&
488 git add not-changed &&
489 git commit -m "add not-changed file" &&
492 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
496 # we know that "file" must be mentioned since we actually
497 # update it, but we want to be sure that our "." pathspec
498 # was not expanded into the argument list of any command.
499 # So look only for "not-changed".
500 ! grep not-changed trace.out
503 test_expect_success 'hunk-editing handles custom comment char' '
505 echo change >>file &&
506 test_config core.commentChar "\$" &&
507 echo e | GIT_EDITOR=true git add -p &&
511 test_expect_success 'add -p works even with color.ui=always' '
513 echo change >>file &&
514 test_config color.ui always &&
515 echo y | git add -p &&
517 git diff --cached --name-only >actual &&
518 test_cmp expect actual
521 test_expect_success 'setup different kinds of dirty submodules' '
522 test_create_repo for-submodules &&
525 test_commit initial &&
526 test_create_repo dirty-head &&
531 cp -R dirty-head dirty-otherwise &&
532 cp -R dirty-head dirty-both-ways &&
533 git add dirty-head &&
534 git add dirty-otherwise dirty-both-ways &&
535 git commit -m initial &&
538 test_commit updated &&
539 cd ../dirty-both-ways &&
540 test_commit updated &&
541 echo dirty >>initial &&
543 cd ../dirty-otherwise &&
544 echo dirty >>initial &&
547 git -C for-submodules diff-files --name-only >actual &&
548 cat >expected <<-\EOF &&
553 test_cmp expected actual &&
554 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
555 cat >expected <<-\EOF &&
559 test_cmp expected actual
562 test_expect_success 'status ignores dirty submodules (except HEAD)' '
563 git -C for-submodules add -i </dev/null >output &&
564 grep dirty-head output &&
565 grep dirty-both-ways output &&
566 ! grep dirty-otherwise output
569 test_expect_success 'set up pathological context' '
571 test_write_lines a a a a a a a a a a a >a &&
574 test_write_lines c b a a a a a a a b a a a a >a &&
575 test_write_lines a a a a a a a b a a a a >expected-1 &&
576 test_write_lines b a a a a a a a b a a a a >expected-2 &&
577 # check editing can cope with missing header and deleted context lines
578 # as well as changes to other lines
579 test_write_lines +b " a" >patch
582 test_expect_success 'add -p works with pathological context lines' '
586 git cat-file blob :a >actual &&
587 test_cmp expected-1 actual
590 test_expect_success 'add -p patch editing works with pathological context lines' '
592 # n q q below is in case edit fails
593 printf "%s\n" e y n q q |
595 git cat-file blob :a >actual &&
596 test_cmp expected-2 actual