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'
13 test_expect_success
'setup (initial)' '
19 test_expect_success
'status works (initial)' '
20 git add -i </dev/null >output &&
21 grep "+1/-0 *+2/-0 file" output
24 test_expect_success
'setup expected' '
27 index 0000000..d95f3ad
35 test_expect_success
'diff works (initial)' '
36 (echo d; echo 1) | git add -i >output &&
37 sed -ne "/new file/,/content/p" <output >diff &&
38 test_cmp expected diff
40 test_expect_success
'revert works (initial)' '
42 (echo r; echo 1) | git add -i &&
43 git ls-files >output &&
47 test_expect_success
'setup (commit)' '
48 echo baseline >file &&
50 git commit -m commit &&
51 echo content >>file &&
56 test_expect_success
'status works (commit)' '
57 git add -i </dev/null >output &&
58 grep "+1/-0 *+2/-0 file" output
61 test_expect_success
'setup expected' '
63 index 180b47c..b6f2c08 100644
72 test_expect_success
'diff works (commit)' '
73 (echo d; echo 1) | git add -i >output &&
74 sed -ne "/^index/,/content/p" <output >diff &&
75 test_cmp expected diff
77 test_expect_success
'revert works (commit)' '
79 (echo r; echo 1) | git add -i &&
80 git add -i </dev/null >output &&
81 grep "unchanged *+3/-0 file" output
85 test_expect_success
'setup expected' '
90 test_expect_success
'setup fake editor' '
92 chmod a+x fake_editor.sh &&
93 test_set_editor "$(pwd)/fake_editor.sh"
96 test_expect_success
'dummy edit works' '
97 (echo e; echo a) | git add -p &&
99 test_cmp expected diff
102 test_expect_success
'setup patch' '
112 test_expect_success
'setup fake editor' '
113 echo "#!$SHELL_PATH" >fake_editor.sh &&
114 cat >>fake_editor.sh <<\EOF &&
115 mv -f "$1" oldpatch &&
118 chmod a+x fake_editor.sh &&
119 test_set_editor "$(pwd)/fake_editor.sh"
122 test_expect_success
'bad edit rejected' '
124 (echo e; echo n; echo d) | git add -p >output &&
125 grep "hunk does not apply" output
128 test_expect_success
'setup patch' '
135 test_expect_success
'garbage edit rejected' '
137 (echo e; echo n; echo d) | git add -p >output &&
138 grep "hunk does not apply" output
141 test_expect_success
'setup patch' '
151 test_expect_success
'setup expected' '
153 diff --git a/file b/file
154 index b5dd6c9..f910ae9 100644
166 test_expect_success
'real edit works' '
167 (echo e; echo n; echo d) | git add -p &&
169 test_cmp expected output
172 test_expect_success
'skip files similarly as commit -a' '
174 echo file >.gitignore &&
175 echo changed >file &&
176 echo y | git add -p file &&
179 git commit -am commit &&
180 git diff >expected &&
181 test_cmp expected output &&
182 git reset --hard HEAD^
186 test_expect_success FILEMODE
'patch does not affect mode' '
188 echo content >>file &&
190 printf "n\\ny\\n" | git add -p &&
191 git show :file | grep content &&
192 git diff file | grep "new mode"
195 test_expect_success FILEMODE
'stage mode but not hunk' '
197 echo content >>file &&
199 printf "y\\nn\\n" | git add -p &&
200 git diff --cached file | grep "new mode" &&
201 git diff file | grep "+content"
205 test_expect_success FILEMODE
'stage mode and hunk' '
207 echo content >>file &&
209 printf "y\\ny\\n" | git add -p &&
210 git diff --cached file | grep "new mode" &&
211 git diff --cached file | grep "+content" &&
212 test -z "$(git diff file)"
215 # end of tests disabled when filemode is not usable
217 test_expect_success
'setup again' '
219 test_chmod +x file &&
223 # Write the patch file with a new line at the top and bottom
224 test_expect_success
'setup patch' '
226 index 180b47c..b6f2c08 100644
237 # Expected output, similar to the patch but w/ diff at the top
238 test_expect_success
'setup expected' '
240 diff --git a/file b/file
241 index b6f2c08..61b9053 100755
252 # Test splitting the first patch, then adding both
253 test_expect_success
'add first line works' '
254 git commit -am "clear local changes" &&
256 (echo s; echo y; echo y) | git add -p file &&
257 git diff --cached > diff &&
258 test_cmp expected diff
261 test_expect_success
'setup expected' '
263 diff --git a/non-empty b/non-empty
264 deleted file mode 100644
265 index d95f3ad..0000000
273 test_expect_success
'deleting a non-empty file' '
275 echo content >non-empty &&
277 git commit -m non-empty &&
279 echo y | git add -p non-empty &&
280 git diff --cached >diff &&
281 test_cmp expected diff
284 test_expect_success
'setup expected' '
286 diff --git a/empty b/empty
287 deleted file mode 100644
288 index e69de29..0000000
292 test_expect_success
'deleting an empty file' '
296 git commit -m empty &&
298 echo y | git add -p empty &&
299 git diff --cached >diff &&
300 test_cmp expected diff
303 test_expect_success
'split hunk setup' '
305 for i in 10 20 30 40 50 60
311 git commit -m test &&
313 for i in 10 15 20 21 22 23 24 30 40 50 60
319 test_expect_success
'split hunk "add -p (edit)"' '
320 # Split, say Edit and do nothing. Then:
322 # 1. Broken version results in a patch that does not apply and
323 # only takes [y/n] (edit again) so the first q is discarded
324 # and then n attempts to discard the edit. Repeat q enough
327 # 2. Correct version applies the (not)edited version, and asks
328 # about the next hunk, against which we say q and program
330 printf "%s\n" s e q n q q |
331 EDITOR=: git add -p &&
336 test_expect_failure
'split hunk "add -p (no, yes, edit)"' '
349 # test sequence is s(plit), n(o), y(es), e(dit)
350 # q n q q is there to make sure we exit at the end.
351 printf "%s\n" s n y e q n q q |
352 EDITOR=: git add -p 2>error &&
353 test_must_be_empty error &&
358 test_expect_success
'patch mode ignores unmerged entries' '
360 test_commit conflict &&
361 test_commit non-conflict &&
362 git checkout -b side &&
363 test_commit side conflict.t &&
364 git checkout master &&
365 test_commit master conflict.t &&
366 test_must_fail git merge side &&
367 echo changed >non-conflict.t &&
368 echo y | git add -p >output &&
369 ! grep a/conflict.t output &&
370 cat >expected <<-\EOF &&
371 * Unmerged path conflict.t
372 diff --git a/non-conflict.t b/non-conflict.t
373 index f766221..5ea2ed4 100644
380 git diff --cached >diff &&
381 test_cmp expected diff
384 test_expect_success TTY
'diffs can be colorized' '
387 echo content >test &&
388 printf y | test_terminal git add -p >output 2>&1 &&
390 # We do not want to depend on the exact coloring scheme
391 # git uses for diffs, so just check that we saw some kind of color.
392 grep "$(printf "\\033")" output
395 test_expect_success
'patch-mode via -i prompts for files' '
400 git add -i <<-\EOF &&
409 git diff --cached --name-only >actual &&
410 test_cmp expect actual
413 test_expect_success
'add -p handles globs' '
418 echo base >subdir/two.c &&
420 git commit -m base &&
422 echo change >one.c &&
423 echo change >subdir/two.c &&
424 git add -p "*.c" <<-\EOF &&
429 cat >expect <<-\EOF &&
433 git diff --cached --name-only >actual &&
434 test_cmp expect actual
437 test_expect_success
'add -p handles relative paths' '
440 echo base >relpath.c &&
442 git commit -m relpath &&
444 echo change >relpath.c &&
446 git -C subdir add -p .. 2>error <<-\EOF &&
450 test_must_be_empty error &&
452 cat >expect <<-\EOF &&
455 git diff --cached --name-only >actual &&
456 test_cmp expect actual
459 test_expect_success
'add -p does not expand argument lists' '
462 echo content >not-changed &&
463 git add not-changed &&
464 git commit -m "add not-changed file" &&
467 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
471 # we know that "file" must be mentioned since we actually
472 # update it, but we want to be sure that our "." pathspec
473 # was not expanded into the argument list of any command.
474 # So look only for "not-changed".
475 ! grep not-changed trace.out
478 test_expect_success
'hunk-editing handles custom comment char' '
480 echo change >>file &&
481 test_config core.commentChar "\$" &&
482 echo e | GIT_EDITOR=true git add -p &&
486 test_expect_success
'add -p works even with color.ui=always' '
488 echo change >>file &&
489 test_config color.ui always &&
490 echo y | git add -p &&
492 git diff --cached --name-only >actual &&
493 test_cmp expect actual