Merge branch 'mr/bisect-use-after-free'
[git] / t / t3701-add-interactive.sh
1 #!/bin/sh
2
3 test_description='add -i basic tests'
4 . ./test-lib.sh
5 . "$TEST_DIRECTORY"/lib-terminal.sh
6
7 if ! test_have_prereq PERL
8 then
9         skip_all='skipping add -i tests, perl not available'
10         test_done
11 fi
12
13 diff_cmp () {
14         for x
15         do
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"
24 }
25
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.
32
33 force_color () {
34         env GIT_PAGER_IN_USE=true TERM=vt100 "$@"
35 }
36
37 test_expect_success 'setup (initial)' '
38         echo content >file &&
39         git add file &&
40         echo more >>file &&
41         echo lines >>file
42 '
43 test_expect_success 'status works (initial)' '
44         git add -i </dev/null >output &&
45         grep "+1/-0 *+2/-0 file" output
46 '
47
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
57 '
58
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
63 '
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
69 '
70
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
79 '
80 test_expect_success 'status works (commit)' '
81         git add -i </dev/null >output &&
82         grep "+1/-0 *+2/-0 file" output
83 '
84
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
94 '
95
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
100 '
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
106 '
107
108 test_expect_success 'setup expected' '
109         cat >expected <<-\EOF
110         EOF
111 '
112
113 test_expect_success 'dummy edit works' '
114         test_set_editor : &&
115         test_write_lines e a | git add -p &&
116         git diff > diff &&
117         diff_cmp expected diff
118 '
119
120 test_expect_success 'setup patch' '
121         cat >patch <<-\EOF
122         @@ -1,1 +1,4 @@
123          this
124         +patch
125         -does not
126          apply
127         EOF
128 '
129
130 test_expect_success 'setup fake editor' '
131         write_script "fake_editor.sh" <<-\EOF &&
132         mv -f "$1" oldpatch &&
133         mv -f patch "$1"
134         EOF
135         test_set_editor "$(pwd)/fake_editor.sh"
136 '
137
138 test_expect_success 'bad edit rejected' '
139         git reset &&
140         test_write_lines e n d | git add -p >output &&
141         grep "hunk does not apply" output
142 '
143
144 test_expect_success 'setup patch' '
145         cat >patch <<-\EOF
146         this patch
147         is garbage
148         EOF
149 '
150
151 test_expect_success 'garbage edit rejected' '
152         git reset &&
153         test_write_lines e n d | git add -p >output &&
154         grep "hunk does not apply" output
155 '
156
157 test_expect_success 'setup patch' '
158         cat >patch <<-\EOF
159         @@ -1,0 +1,0 @@
160          baseline
161         +content
162         +newcontent
163         +lines
164         EOF
165 '
166
167 test_expect_success 'setup expected' '
168         cat >expected <<-\EOF
169         diff --git a/file b/file
170         index b5dd6c9..f910ae9 100644
171         --- a/file
172         +++ b/file
173         @@ -1,4 +1,4 @@
174          baseline
175          content
176         -newcontent
177         +more
178          lines
179         EOF
180 '
181
182 test_expect_success 'real edit works' '
183         test_write_lines e n d | git add -p &&
184         git diff >output &&
185         diff_cmp expected output
186 '
187
188 test_expect_success 'setup file' '
189         test_write_lines a "" b "" c >file &&
190         git add file &&
191         test_write_lines a "" d "" c >file
192 '
193
194 test_expect_success 'setup patch' '
195         SP=" " &&
196         NULL="" &&
197         cat >patch <<-EOF
198         @@ -1,4 +1,4 @@
199          a
200         $NULL
201         -b
202         +f
203         $SP
204         c
205         EOF
206 '
207
208 test_expect_success 'setup expected' '
209         cat >expected <<-EOF
210         diff --git a/file b/file
211         index b5dd6c9..f910ae9 100644
212         --- a/file
213         +++ b/file
214         @@ -1,5 +1,5 @@
215          a
216         $SP
217         -f
218         +d
219         $SP
220          c
221         EOF
222 '
223
224 test_expect_success 'edit can strip spaces from empty context lines' '
225         test_write_lines e n q | git add -p 2>error &&
226         test_must_be_empty error &&
227         git diff >output &&
228         diff_cmp expected output
229 '
230
231 test_expect_success 'skip files similarly as commit -a' '
232         git reset &&
233         echo file >.gitignore &&
234         echo changed >file &&
235         echo y | git add -p file &&
236         git diff >output &&
237         git reset &&
238         git commit -am commit &&
239         git diff >expected &&
240         diff_cmp expected output &&
241         git reset --hard HEAD^
242 '
243 rm -f .gitignore
244
245 test_expect_success FILEMODE 'patch does not affect mode' '
246         git reset --hard &&
247         echo content >>file &&
248         chmod +x file &&
249         printf "n\\ny\\n" | git add -p &&
250         git show :file | grep content &&
251         git diff file | grep "new mode"
252 '
253
254 test_expect_success FILEMODE 'stage mode but not hunk' '
255         git reset --hard &&
256         echo content >>file &&
257         chmod +x file &&
258         printf "y\\nn\\n" | git add -p &&
259         git diff --cached file | grep "new mode" &&
260         git diff          file | grep "+content"
261 '
262
263
264 test_expect_success FILEMODE 'stage mode and hunk' '
265         git reset --hard &&
266         echo content >>file &&
267         chmod +x file &&
268         printf "y\\ny\\n" | git add -p &&
269         git diff --cached file | grep "new mode" &&
270         git diff --cached file | grep "+content" &&
271         test -z "$(git diff file)"
272 '
273
274 # end of tests disabled when filemode is not usable
275
276 test_expect_success 'different prompts for mode change/deleted' '
277         git reset --hard &&
278         >file &&
279         >deleted &&
280         git add --chmod=+x file deleted &&
281         echo changed >file &&
282         rm deleted &&
283         test_write_lines n n n |
284         git -c core.filemode=true add -p >actual &&
285         sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
286         cat >expect <<-\EOF &&
287         (1/1) Stage deletion [y,n,q,a,d,?]?
288         (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,?]?
289         (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
290         EOF
291         test_cmp expect actual.filtered
292 '
293
294 test_expect_success 'correct message when there is nothing to do' '
295         git reset --hard &&
296         git add -p 2>err &&
297         test_i18ngrep "No changes" err &&
298         printf "\\0123" >binary &&
299         git add binary &&
300         printf "\\0abc" >binary &&
301         git add -p 2>err &&
302         test_i18ngrep "Only binary files changed" err
303 '
304
305 test_expect_success 'setup again' '
306         git reset --hard &&
307         test_chmod +x file &&
308         echo content >>file
309 '
310
311 # Write the patch file with a new line at the top and bottom
312 test_expect_success 'setup patch' '
313         cat >patch <<-\EOF
314         index 180b47c..b6f2c08 100644
315         --- a/file
316         +++ b/file
317         @@ -1,2 +1,4 @@
318         +firstline
319          baseline
320          content
321         +lastline
322         \ No newline at end of file
323         EOF
324 '
325
326 # Expected output, diff is similar to the patch but w/ diff at the top
327 test_expect_success 'setup expected' '
328         echo diff --git a/file b/file >expected &&
329         cat patch |sed "/^index/s/ 100644/ 100755/" >>expected &&
330         cat >expected-output <<-\EOF
331         --- a/file
332         +++ b/file
333         @@ -1,2 +1,4 @@
334         +firstline
335          baseline
336          content
337         +lastline
338         \ No newline at end of file
339         @@ -1,2 +1,3 @@
340         +firstline
341          baseline
342          content
343         @@ -1,2 +2,3 @@
344          baseline
345          content
346         +lastline
347         \ No newline at end of file
348         EOF
349 '
350
351 # Test splitting the first patch, then adding both
352 test_expect_success C_LOCALE_OUTPUT 'add first line works' '
353         git commit -am "clear local changes" &&
354         git apply patch &&
355         printf "%s\n" s y y | git add -p file 2>error |
356                 sed -n -e "s/^([1-2]\/[1-2]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
357                        -e "/^[-+@ \\\\]"/p  >output &&
358         test_must_be_empty error &&
359         git diff --cached >diff &&
360         diff_cmp expected diff &&
361         test_cmp expected-output output
362 '
363
364 test_expect_success 'setup expected' '
365         cat >expected <<-\EOF
366         diff --git a/non-empty b/non-empty
367         deleted file mode 100644
368         index d95f3ad..0000000
369         --- a/non-empty
370         +++ /dev/null
371         @@ -1 +0,0 @@
372         -content
373         EOF
374 '
375
376 test_expect_success 'deleting a non-empty file' '
377         git reset --hard &&
378         echo content >non-empty &&
379         git add non-empty &&
380         git commit -m non-empty &&
381         rm non-empty &&
382         echo y | git add -p non-empty &&
383         git diff --cached >diff &&
384         diff_cmp expected diff
385 '
386
387 test_expect_success 'setup expected' '
388         cat >expected <<-\EOF
389         diff --git a/empty b/empty
390         deleted file mode 100644
391         index e69de29..0000000
392         EOF
393 '
394
395 test_expect_success 'deleting an empty file' '
396         git reset --hard &&
397         > empty &&
398         git add empty &&
399         git commit -m empty &&
400         rm empty &&
401         echo y | git add -p empty &&
402         git diff --cached >diff &&
403         diff_cmp expected diff
404 '
405
406 test_expect_success 'split hunk setup' '
407         git reset --hard &&
408         test_write_lines 10 20 30 40 50 60 >test &&
409         git add test &&
410         test_tick &&
411         git commit -m test &&
412
413         test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
414 '
415
416 test_expect_success 'split hunk "add -p (edit)"' '
417         # Split, say Edit and do nothing.  Then:
418         #
419         # 1. Broken version results in a patch that does not apply and
420         # only takes [y/n] (edit again) so the first q is discarded
421         # and then n attempts to discard the edit. Repeat q enough
422         # times to get out.
423         #
424         # 2. Correct version applies the (not)edited version, and asks
425         #    about the next hunk, against which we say q and program
426         #    exits.
427         printf "%s\n" s e     q n q q |
428         EDITOR=: git add -p &&
429         git diff >actual &&
430         ! grep "^+15" actual
431 '
432
433 test_expect_failure 'split hunk "add -p (no, yes, edit)"' '
434         test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
435         git reset &&
436         # test sequence is s(plit), n(o), y(es), e(dit)
437         # q n q q is there to make sure we exit at the end.
438         printf "%s\n" s n y e   q n q q |
439         EDITOR=: git add -p 2>error &&
440         test_must_be_empty error &&
441         git diff >actual &&
442         ! grep "^+31" actual
443 '
444
445 test_expect_failure 'edit, adding lines to the first hunk' '
446         test_write_lines 10 11 20 30 40 50 51 60 >test &&
447         git reset &&
448         tr _ " " >patch <<-EOF &&
449         @@ -1,5 +1,6 @@
450         _10
451         +11
452         +12
453         _20
454         +21
455         +22
456         _30
457         EOF
458         # test sequence is s(plit), e(dit), n(o)
459         # q n q q is there to make sure we exit at the end.
460         printf "%s\n" s e n   q n q q |
461         EDITOR=./fake_editor.sh git add -p 2>error &&
462         test_must_be_empty error &&
463         git diff --cached >actual &&
464         grep "^+22" actual
465 '
466
467 test_expect_success 'patch mode ignores unmerged entries' '
468         git reset --hard &&
469         test_commit conflict &&
470         test_commit non-conflict &&
471         git checkout -b side &&
472         test_commit side conflict.t &&
473         git checkout master &&
474         test_commit master conflict.t &&
475         test_must_fail git merge side &&
476         echo changed >non-conflict.t &&
477         echo y | git add -p >output &&
478         ! grep a/conflict.t output &&
479         cat >expected <<-\EOF &&
480         * Unmerged path conflict.t
481         diff --git a/non-conflict.t b/non-conflict.t
482         index f766221..5ea2ed4 100644
483         --- a/non-conflict.t
484         +++ b/non-conflict.t
485         @@ -1 +1 @@
486         -non-conflict
487         +changed
488         EOF
489         git diff --cached >diff &&
490         diff_cmp expected diff
491 '
492
493 test_expect_success 'diffs can be colorized' '
494         git reset --hard &&
495
496         echo content >test &&
497         printf y >y &&
498         force_color git add -p >output 2>&1 <y &&
499
500         # We do not want to depend on the exact coloring scheme
501         # git uses for diffs, so just check that we saw some kind of color.
502         grep "$(printf "\\033")" output
503 '
504
505 test_expect_success 'diffFilter filters diff' '
506         git reset --hard &&
507
508         echo content >test &&
509         test_config interactive.diffFilter "sed s/^/foo:/" &&
510         printf y >y &&
511         force_color git add -p >output 2>&1 <y &&
512
513         # avoid depending on the exact coloring or content of the prompts,
514         # and just make sure we saw our diff prefixed
515         grep foo:.*content output
516 '
517
518 test_expect_success 'detect bogus diffFilter output' '
519         git reset --hard &&
520
521         echo content >test &&
522         test_config interactive.diffFilter "echo too-short" &&
523         printf y >y &&
524         test_must_fail force_color git add -p <y
525 '
526
527 test_expect_success 'diff.algorithm is passed to `git diff-files`' '
528         git reset --hard &&
529
530         >file &&
531         git add file &&
532         echo changed >file &&
533         test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
534         test_i18ngrep "error: option diff-algorithm accepts " err
535 '
536
537 test_expect_success 'patch-mode via -i prompts for files' '
538         git reset --hard &&
539
540         echo one >file &&
541         echo two >test &&
542         git add -i <<-\EOF &&
543         patch
544         test
545
546         y
547         quit
548         EOF
549
550         echo test >expect &&
551         git diff --cached --name-only >actual &&
552         diff_cmp expect actual
553 '
554
555 test_expect_success 'add -p handles globs' '
556         git reset --hard &&
557
558         mkdir -p subdir &&
559         echo base >one.c &&
560         echo base >subdir/two.c &&
561         git add "*.c" &&
562         git commit -m base &&
563
564         echo change >one.c &&
565         echo change >subdir/two.c &&
566         git add -p "*.c" <<-\EOF &&
567         y
568         y
569         EOF
570
571         cat >expect <<-\EOF &&
572         one.c
573         subdir/two.c
574         EOF
575         git diff --cached --name-only >actual &&
576         test_cmp expect actual
577 '
578
579 test_expect_success 'add -p handles relative paths' '
580         git reset --hard &&
581
582         echo base >relpath.c &&
583         git add "*.c" &&
584         git commit -m relpath &&
585
586         echo change >relpath.c &&
587         mkdir -p subdir &&
588         git -C subdir add -p .. 2>error <<-\EOF &&
589         y
590         EOF
591
592         test_must_be_empty error &&
593
594         cat >expect <<-\EOF &&
595         relpath.c
596         EOF
597         git diff --cached --name-only >actual &&
598         test_cmp expect actual
599 '
600
601 test_expect_success 'add -p does not expand argument lists' '
602         git reset --hard &&
603
604         echo content >not-changed &&
605         git add not-changed &&
606         git commit -m "add not-changed file" &&
607
608         echo change >file &&
609         GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
610         y
611         EOF
612
613         # we know that "file" must be mentioned since we actually
614         # update it, but we want to be sure that our "." pathspec
615         # was not expanded into the argument list of any command.
616         # So look only for "not-changed".
617         ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
618 '
619
620 test_expect_success 'hunk-editing handles custom comment char' '
621         git reset --hard &&
622         echo change >>file &&
623         test_config core.commentChar "\$" &&
624         echo e | GIT_EDITOR=true git add -p &&
625         git diff --exit-code
626 '
627
628 test_expect_success 'add -p works even with color.ui=always' '
629         git reset --hard &&
630         echo change >>file &&
631         test_config color.ui always &&
632         echo y | git add -p &&
633         echo file >expect &&
634         git diff --cached --name-only >actual &&
635         test_cmp expect actual
636 '
637
638 test_expect_success 'setup different kinds of dirty submodules' '
639         test_create_repo for-submodules &&
640         (
641                 cd for-submodules &&
642                 test_commit initial &&
643                 test_create_repo dirty-head &&
644                 (
645                         cd dirty-head &&
646                         test_commit initial
647                 ) &&
648                 cp -R dirty-head dirty-otherwise &&
649                 cp -R dirty-head dirty-both-ways &&
650                 git add dirty-head &&
651                 git add dirty-otherwise dirty-both-ways &&
652                 git commit -m initial &&
653
654                 cd dirty-head &&
655                 test_commit updated &&
656                 cd ../dirty-both-ways &&
657                 test_commit updated &&
658                 echo dirty >>initial &&
659                 : >untracked &&
660                 cd ../dirty-otherwise &&
661                 echo dirty >>initial &&
662                 : >untracked
663         ) &&
664         git -C for-submodules diff-files --name-only >actual &&
665         cat >expected <<-\EOF &&
666         dirty-both-ways
667         dirty-head
668         dirty-otherwise
669         EOF
670         test_cmp expected actual &&
671         git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
672         cat >expected <<-\EOF &&
673         dirty-both-ways
674         dirty-head
675         EOF
676         test_cmp expected actual
677 '
678
679 test_expect_success 'status ignores dirty submodules (except HEAD)' '
680         git -C for-submodules add -i </dev/null >output &&
681         grep dirty-head output &&
682         grep dirty-both-ways output &&
683         ! grep dirty-otherwise output
684 '
685
686 test_expect_success 'set up pathological context' '
687         git reset --hard &&
688         test_write_lines a a a a a a a a a a a >a &&
689         git add a &&
690         git commit -m a &&
691         test_write_lines c b a a a a a a a b a a a a >a &&
692         test_write_lines     a a a a a a a b a a a a >expected-1 &&
693         test_write_lines   b a a a a a a a b a a a a >expected-2 &&
694         # check editing can cope with missing header and deleted context lines
695         # as well as changes to other lines
696         test_write_lines +b " a" >patch
697 '
698
699 test_expect_success 'add -p works with pathological context lines' '
700         git reset &&
701         printf "%s\n" n y |
702         git add -p &&
703         git cat-file blob :a >actual &&
704         test_cmp expected-1 actual
705 '
706
707 test_expect_success 'add -p patch editing works with pathological context lines' '
708         git reset &&
709         # n q q below is in case edit fails
710         printf "%s\n" e y    n q q |
711         git add -p &&
712         git cat-file blob :a >actual &&
713         test_cmp expected-2 actual
714 '
715
716 test_expect_success 'checkout -p works with pathological context lines' '
717         test_write_lines a a a a a a >a &&
718         git add a &&
719         test_write_lines a b a b a b a b a b a > a&&
720         test_write_lines s n n y q | git checkout -p &&
721         test_write_lines a b a b a a b a b a >expect &&
722         test_cmp expect a
723 '
724
725 test_expect_success 'show help from add--helper' '
726         git reset --hard &&
727         cat >expect <<-EOF &&
728
729         <BOLD>*** Commands ***<RESET>
730           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
731           5: <BOLD;BLUE>p<RESET>atch      6: <BOLD;BLUE>d<RESET>iff       7: <BOLD;BLUE>q<RESET>uit       8: <BOLD;BLUE>h<RESET>elp
732         <BOLD;BLUE>What now<RESET>> <BOLD;RED>status        - show paths with changes<RESET>
733         <BOLD;RED>update        - add working tree state to the staged set of changes<RESET>
734         <BOLD;RED>revert        - revert staged set of changes back to the HEAD version<RESET>
735         <BOLD;RED>patch         - pick hunks and update selectively<RESET>
736         <BOLD;RED>diff          - view diff between HEAD and index<RESET>
737         <BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
738         <BOLD>*** Commands ***<RESET>
739           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
740           5: <BOLD;BLUE>p<RESET>atch      6: <BOLD;BLUE>d<RESET>iff       7: <BOLD;BLUE>q<RESET>uit       8: <BOLD;BLUE>h<RESET>elp
741         <BOLD;BLUE>What now<RESET>>$SP
742         Bye.
743         EOF
744         test_write_lines h | force_color git add -i >actual.colored &&
745         test_decode_color <actual.colored >actual &&
746         test_i18ncmp expect actual
747 '
748
749 test_done