t3701: avoid depending on the TTY prerequisite
[git/debian.git] / t / t3701-add-interactive.sh
blob793ce2829754803aa7a1299959531e9f01927684
1 #!/bin/sh
3 test_description='add -i basic tests'
4 . ./test-lib.sh
5 . "$TEST_DIRECTORY"/lib-terminal.sh
7 if ! test_have_prereq PERL
8 then
9 skip_all='skipping add -i tests, perl not available'
10 test_done
13 diff_cmp () {
14 for x
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 /' \
21 "$x" >"$x.filtered"
22 done
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.
33 force_color () {
34 env GIT_PAGER_IN_USE=true TERM=vt100 "$@"
37 test_expect_success 'setup (initial)' '
38 echo content >file &&
39 git add file &&
40 echo more >>file &&
41 echo lines >>file
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' '
49 cat >expected <<-\EOF
50 new file mode 100644
51 index 0000000..d95f3ad
52 --- /dev/null
53 +++ b/file
54 @@ -0,0 +1 @@
55 +content
56 EOF
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)' '
65 git add file &&
66 test_write_lines r 1 | git add -i &&
67 git ls-files >output &&
68 ! grep . output
71 test_expect_success 'setup (commit)' '
72 echo baseline >file &&
73 git add file &&
74 git commit -m commit &&
75 echo content >>file &&
76 git add file &&
77 echo more >>file &&
78 echo lines >>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' '
86 cat >expected <<-\EOF
87 index 180b47c..b6f2c08 100644
88 --- a/file
89 +++ b/file
90 @@ -1 +1,2 @@
91 baseline
92 +content
93 EOF
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)' '
102 git add file &&
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' '
115 test_set_editor : &&
116 test_write_lines e a | git add -p &&
117 git diff > diff &&
118 diff_cmp expected diff
121 test_expect_success 'setup patch' '
122 cat >patch <<-\EOF
123 @@ -1,1 +1,4 @@
124 this
125 +patch
126 -does not
127 apply
131 test_expect_success 'setup fake editor' '
132 write_script "fake_editor.sh" <<-\EOF &&
133 mv -f "$1" oldpatch &&
134 mv -f patch "$1"
136 test_set_editor "$(pwd)/fake_editor.sh"
139 test_expect_success 'bad edit rejected' '
140 git reset &&
141 test_write_lines e n d | git add -p >output &&
142 grep "hunk does not apply" output
145 test_expect_success 'setup patch' '
146 cat >patch <<-\EOF
147 this patch
148 is garbage
152 test_expect_success 'garbage edit rejected' '
153 git reset &&
154 test_write_lines e n d | git add -p >output &&
155 grep "hunk does not apply" output
158 test_expect_success 'setup patch' '
159 cat >patch <<-\EOF
160 @@ -1,0 +1,0 @@
161 baseline
162 +content
163 +newcontent
164 +lines
168 test_expect_success 'setup expected' '
169 cat >expected <<-\EOF
170 diff --git a/file b/file
171 index b5dd6c9..f910ae9 100644
172 --- a/file
173 +++ b/file
174 @@ -1,4 +1,4 @@
175 baseline
176 content
177 -newcontent
178 +more
179 lines
183 test_expect_success 'real edit works' '
184 test_write_lines e n d | git add -p &&
185 git diff >output &&
186 diff_cmp expected output
189 test_expect_success 'setup file' '
190 test_write_lines a "" b "" c >file &&
191 git add file &&
192 test_write_lines a "" d "" c >file
195 test_expect_success 'setup patch' '
196 SP=" " &&
197 NULL="" &&
198 cat >patch <<-EOF
199 @@ -1,4 +1,4 @@
201 $NULL
209 test_expect_success 'setup expected' '
210 cat >expected <<-EOF
211 diff --git a/file b/file
212 index b5dd6c9..f910ae9 100644
213 --- a/file
214 +++ b/file
215 @@ -1,5 +1,5 @@
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 &&
228 git diff >output &&
229 diff_cmp expected output
232 test_expect_success 'skip files similarly as commit -a' '
233 git reset &&
234 echo file >.gitignore &&
235 echo changed >file &&
236 echo y | git add -p file &&
237 git diff >output &&
238 git reset &&
239 git commit -am commit &&
240 git diff >expected &&
241 diff_cmp expected output &&
242 git reset --hard HEAD^
244 rm -f .gitignore
246 test_expect_success FILEMODE 'patch does not affect mode' '
247 git reset --hard &&
248 echo content >>file &&
249 chmod +x 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' '
256 git reset --hard &&
257 echo content >>file &&
258 chmod +x 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' '
266 git reset --hard &&
267 echo content >>file &&
268 chmod +x 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' '
278 git reset --hard &&
279 test_chmod +x file &&
280 echo content >>file
283 # Write the patch file with a new line at the top and bottom
284 test_expect_success 'setup patch' '
285 cat >patch <<-\EOF
286 index 180b47c..b6f2c08 100644
287 --- a/file
288 +++ b/file
289 @@ -1,2 +1,4 @@
290 +firstline
291 baseline
292 content
293 +lastline
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
303 --- a/file
304 +++ b/file
305 @@ -1,2 +1,4 @@
306 +firstline
307 baseline
308 content
309 +lastline
310 \ No newline at end of file
311 @@ -1,2 +1,3 @@
312 +firstline
313 baseline
314 content
315 @@ -1,2 +2,3 @@
316 baseline
317 content
318 +lastline
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" &&
326 git apply patch &&
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
341 --- a/non-empty
342 +++ /dev/null
343 @@ -1 +0,0 @@
344 -content
348 test_expect_success 'deleting a non-empty file' '
349 git reset --hard &&
350 echo content >non-empty &&
351 git add non-empty &&
352 git commit -m non-empty &&
353 rm 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' '
368 git reset --hard &&
369 > empty &&
370 git add empty &&
371 git commit -m empty &&
372 rm 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' '
379 git reset --hard &&
380 test_write_lines 10 20 30 40 50 60 >test &&
381 git add test &&
382 test_tick &&
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
394 # times to get out.
396 # 2. Correct version applies the (not)edited version, and asks
397 # about the next hunk, against which we say q and program
398 # exits.
399 printf "%s\n" s e q n q q |
400 EDITOR=: git add -p &&
401 git diff >actual &&
402 ! grep "^+15" actual
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 &&
407 git reset &&
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 &&
413 git diff >actual &&
414 ! grep "^+31" actual
417 test_expect_failure 'edit, adding lines to the first hunk' '
418 test_write_lines 10 11 20 30 40 50 51 60 >test &&
419 git reset &&
420 tr _ " " >patch <<-EOF &&
421 @@ -1,5 +1,6 @@
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 &&
436 grep "^+22" actual
439 test_expect_success 'patch mode ignores unmerged entries' '
440 git reset --hard &&
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
455 --- a/non-conflict.t
456 +++ b/non-conflict.t
457 @@ -1 +1 @@
458 -non-conflict
459 +changed
461 git diff --cached >diff &&
462 diff_cmp expected diff
465 test_expect_success 'diffs can be colorized' '
466 git reset --hard &&
468 echo content >test &&
469 printf y >y &&
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' '
478 git reset --hard &&
480 echo content >test &&
481 test_config interactive.diffFilter "sed s/^/foo:/" &&
482 printf y >y &&
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' '
491 git reset --hard &&
493 echo content >test &&
494 test_config interactive.diffFilter "echo too-short" &&
495 printf y >y &&
496 test_must_fail force_color git add -p <y
499 test_expect_success 'patch-mode via -i prompts for files' '
500 git reset --hard &&
502 echo one >file &&
503 echo two >test &&
504 git add -i <<-\EOF &&
505 patch
506 test
509 quit
512 echo test >expect &&
513 git diff --cached --name-only >actual &&
514 diff_cmp expect actual
517 test_expect_success 'add -p handles globs' '
518 git reset --hard &&
520 mkdir -p subdir &&
521 echo base >one.c &&
522 echo base >subdir/two.c &&
523 git add "*.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 &&
534 one.c
535 subdir/two.c
537 git diff --cached --name-only >actual &&
538 test_cmp expect actual
541 test_expect_success 'add -p handles relative paths' '
542 git reset --hard &&
544 echo base >relpath.c &&
545 git add "*.c" &&
546 git commit -m relpath &&
548 echo change >relpath.c &&
549 mkdir -p subdir &&
550 git -C subdir add -p .. 2>error <<-\EOF &&
554 test_must_be_empty error &&
556 cat >expect <<-\EOF &&
557 relpath.c
559 git diff --cached --name-only >actual &&
560 test_cmp expect actual
563 test_expect_success 'add -p does not expand argument lists' '
564 git reset --hard &&
566 echo content >not-changed &&
567 git add not-changed &&
568 git commit -m "add not-changed file" &&
570 echo change >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' '
583 git reset --hard &&
584 echo change >>file &&
585 test_config core.commentChar "\$" &&
586 echo e | GIT_EDITOR=true git add -p &&
587 git diff --exit-code
590 test_expect_success 'add -p works even with color.ui=always' '
591 git reset --hard &&
592 echo change >>file &&
593 test_config color.ui always &&
594 echo y | git add -p &&
595 echo file >expect &&
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 &&
603 cd for-submodules &&
604 test_commit initial &&
605 test_create_repo dirty-head &&
607 cd dirty-head &&
608 test_commit initial
609 ) &&
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 &&
616 cd dirty-head &&
617 test_commit updated &&
618 cd ../dirty-both-ways &&
619 test_commit updated &&
620 echo dirty >>initial &&
621 : >untracked &&
622 cd ../dirty-otherwise &&
623 echo dirty >>initial &&
624 : >untracked
625 ) &&
626 git -C for-submodules diff-files --name-only >actual &&
627 cat >expected <<-\EOF &&
628 dirty-both-ways
629 dirty-head
630 dirty-otherwise
632 test_cmp expected actual &&
633 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
634 cat >expected <<-\EOF &&
635 dirty-both-ways
636 dirty-head
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' '
649 git reset --hard &&
650 test_write_lines a a a a a a a a a a a >a &&
651 git add a &&
652 git commit -m 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' '
662 git reset &&
663 printf "%s\n" n y |
664 git add -p &&
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' '
670 git reset &&
671 # n q q below is in case edit fails
672 printf "%s\n" e y n q q |
673 git add -p &&
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 &&
680 git add 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 &&
684 test_cmp expect a
687 test_expect_success 'show help from add--helper' '
688 git reset --hard &&
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
704 Bye.
706 test_write_lines h | force_color git add -i >actual.colored &&
707 test_decode_color <actual.colored >actual &&
708 test_i18ncmp expect actual
711 test_done