]> git.ipfire.org Git - thirdparty/git.git/blob - t/t3701-add-interactive.sh
clone: allow "--bare" with "-o"
[thirdparty/git.git] / t / t3701-add-interactive.sh
1 #!/bin/sh
2
3 test_description='add -i basic tests'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 . ./test-lib.sh
8 . "$TEST_DIRECTORY"/lib-terminal.sh
9
10 if ! test_have_prereq PERL
11 then
12 skip_all='skipping add -i tests, perl not available'
13 test_done
14 fi
15
16 diff_cmp () {
17 for x
18 do
19 sed -e '/^index/s/[0-9a-f]*[1-9a-f][0-9a-f]*\.\./1234567../' \
20 -e '/^index/s/\.\.[0-9a-f]*[1-9a-f][0-9a-f]*/..9abcdef/' \
21 -e '/^index/s/ 00*\.\./ 0000000../' \
22 -e '/^index/s/\.\.00*$/..0000000/' \
23 -e '/^index/s/\.\.00* /..0000000 /' \
24 "$x" >"$x.filtered"
25 done
26 test_cmp "$1.filtered" "$2.filtered"
27 }
28
29 # This function uses a trick to manipulate the interactive add to use color:
30 # the `want_color()` function special-cases the situation where a pager was
31 # spawned and Git now wants to output colored text: to detect that situation,
32 # the environment variable `GIT_PAGER_IN_USE` is set. However, color is
33 # suppressed despite that environment variable if the `TERM` variable
34 # indicates a dumb terminal, so we set that variable, too.
35
36 force_color () {
37 # The first element of $@ may be a shell function, as a result POSIX
38 # does not guarantee that "one-shot assignment" will not persist after
39 # the function call. Thus, we prevent these variables from escaping
40 # this function's context with this subshell.
41 (
42 GIT_PAGER_IN_USE=true &&
43 TERM=vt100 &&
44 export GIT_PAGER_IN_USE TERM &&
45 "$@"
46 )
47 }
48
49 test_expect_success 'setup (initial)' '
50 echo content >file &&
51 git add file &&
52 echo more >>file &&
53 echo lines >>file
54 '
55 test_expect_success 'status works (initial)' '
56 git add -i </dev/null >output &&
57 grep "+1/-0 *+2/-0 file" output
58 '
59
60 test_expect_success 'setup expected' '
61 cat >expected <<-\EOF
62 new file mode 100644
63 index 0000000..d95f3ad
64 --- /dev/null
65 +++ b/file
66 @@ -0,0 +1 @@
67 +content
68 EOF
69 '
70
71 test_expect_success 'diff works (initial)' '
72 test_write_lines d 1 | git add -i >output &&
73 sed -ne "/new file/,/content/p" <output >diff &&
74 diff_cmp expected diff
75 '
76 test_expect_success 'revert works (initial)' '
77 git add file &&
78 test_write_lines r 1 | git add -i &&
79 git ls-files >output &&
80 ! grep . output
81 '
82
83 test_expect_success 'add untracked (multiple)' '
84 test_when_finished "git reset && rm [1-9]" &&
85 touch $(test_seq 9) &&
86 test_write_lines a "2-5 8-" | git add -i -- [1-9] &&
87 test_write_lines 2 3 4 5 8 9 >expected &&
88 git ls-files [1-9] >output &&
89 test_cmp expected output
90 '
91
92 test_expect_success 'setup (commit)' '
93 echo baseline >file &&
94 git add file &&
95 git commit -m commit &&
96 echo content >>file &&
97 git add file &&
98 echo more >>file &&
99 echo lines >>file
100 '
101 test_expect_success 'status works (commit)' '
102 git add -i </dev/null >output &&
103 grep "+1/-0 *+2/-0 file" output
104 '
105
106 test_expect_success 'update can stage deletions' '
107 >to-delete &&
108 git add to-delete &&
109 rm to-delete &&
110 test_write_lines u t "" | git add -i &&
111 git ls-files to-delete >output &&
112 test_must_be_empty output
113 '
114
115 test_expect_success 'setup expected' '
116 cat >expected <<-\EOF
117 index 180b47c..b6f2c08 100644
118 --- a/file
119 +++ b/file
120 @@ -1 +1,2 @@
121 baseline
122 +content
123 EOF
124 '
125
126 test_expect_success 'diff works (commit)' '
127 test_write_lines d 1 | git add -i >output &&
128 sed -ne "/^index/,/content/p" <output >diff &&
129 diff_cmp expected diff
130 '
131 test_expect_success 'revert works (commit)' '
132 git add file &&
133 test_write_lines r 1 | git add -i &&
134 git add -i </dev/null >output &&
135 grep "unchanged *+3/-0 file" output
136 '
137
138 test_expect_success 'setup expected' '
139 cat >expected <<-\EOF
140 EOF
141 '
142
143 test_expect_success 'dummy edit works' '
144 test_set_editor : &&
145 test_write_lines e a | git add -p &&
146 git diff > diff &&
147 diff_cmp expected diff
148 '
149
150 test_expect_success 'setup patch' '
151 cat >patch <<-\EOF
152 @@ -1,1 +1,4 @@
153 this
154 +patch
155 -does not
156 apply
157 EOF
158 '
159
160 test_expect_success 'setup fake editor' '
161 write_script "fake_editor.sh" <<-\EOF &&
162 mv -f "$1" oldpatch &&
163 mv -f patch "$1"
164 EOF
165 test_set_editor "$(pwd)/fake_editor.sh"
166 '
167
168 test_expect_success 'bad edit rejected' '
169 git reset &&
170 test_write_lines e n d | git add -p >output &&
171 grep "hunk does not apply" output
172 '
173
174 test_expect_success 'setup patch' '
175 cat >patch <<-\EOF
176 this patch
177 is garbage
178 EOF
179 '
180
181 test_expect_success 'garbage edit rejected' '
182 git reset &&
183 test_write_lines e n d | git add -p >output &&
184 grep "hunk does not apply" output
185 '
186
187 test_expect_success 'setup patch' '
188 cat >patch <<-\EOF
189 @@ -1,0 +1,0 @@
190 baseline
191 +content
192 +newcontent
193 +lines
194 EOF
195 '
196
197 test_expect_success 'setup expected' '
198 cat >expected <<-\EOF
199 diff --git a/file b/file
200 index b5dd6c9..f910ae9 100644
201 --- a/file
202 +++ b/file
203 @@ -1,4 +1,4 @@
204 baseline
205 content
206 -newcontent
207 +more
208 lines
209 EOF
210 '
211
212 test_expect_success 'real edit works' '
213 test_write_lines e n d | git add -p &&
214 git diff >output &&
215 diff_cmp expected output
216 '
217
218 test_expect_success 'setup file' '
219 test_write_lines a "" b "" c >file &&
220 git add file &&
221 test_write_lines a "" d "" c >file
222 '
223
224 test_expect_success 'setup patch' '
225 SP=" " &&
226 NULL="" &&
227 cat >patch <<-EOF
228 @@ -1,4 +1,4 @@
229 a
230 $NULL
231 -b
232 +f
233 $SP
234 c
235 EOF
236 '
237
238 test_expect_success 'setup expected' '
239 cat >expected <<-EOF
240 diff --git a/file b/file
241 index b5dd6c9..f910ae9 100644
242 --- a/file
243 +++ b/file
244 @@ -1,5 +1,5 @@
245 a
246 $SP
247 -f
248 +d
249 $SP
250 c
251 EOF
252 '
253
254 test_expect_success 'edit can strip spaces from empty context lines' '
255 test_write_lines e n q | git add -p 2>error &&
256 test_must_be_empty error &&
257 git diff >output &&
258 diff_cmp expected output
259 '
260
261 test_expect_success 'skip files similarly as commit -a' '
262 git reset &&
263 echo file >.gitignore &&
264 echo changed >file &&
265 echo y | git add -p file &&
266 git diff >output &&
267 git reset &&
268 git commit -am commit &&
269 git diff >expected &&
270 diff_cmp expected output &&
271 git reset --hard HEAD^
272 '
273 rm -f .gitignore
274
275 test_expect_success FILEMODE 'patch does not affect mode' '
276 git reset --hard &&
277 echo content >>file &&
278 chmod +x file &&
279 printf "n\\ny\\n" | git add -p &&
280 git show :file | grep content &&
281 git diff file | grep "new mode"
282 '
283
284 test_expect_success FILEMODE 'stage mode but not hunk' '
285 git reset --hard &&
286 echo content >>file &&
287 chmod +x file &&
288 printf "y\\nn\\n" | git add -p &&
289 git diff --cached file | grep "new mode" &&
290 git diff file | grep "+content"
291 '
292
293
294 test_expect_success FILEMODE 'stage mode and hunk' '
295 git reset --hard &&
296 echo content >>file &&
297 chmod +x file &&
298 printf "y\\ny\\n" | git add -p &&
299 git diff --cached file | grep "new mode" &&
300 git diff --cached file | grep "+content" &&
301 test -z "$(git diff file)"
302 '
303
304 # end of tests disabled when filemode is not usable
305
306 test_expect_success 'different prompts for mode change/deleted' '
307 git reset --hard &&
308 >file &&
309 >deleted &&
310 git add --chmod=+x file deleted &&
311 echo changed >file &&
312 rm deleted &&
313 test_write_lines n n n |
314 git -c core.filemode=true add -p >actual &&
315 sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
316 cat >expect <<-\EOF &&
317 (1/1) Stage deletion [y,n,q,a,d,?]?
318 (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,?]?
319 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
320 EOF
321 test_cmp expect actual.filtered
322 '
323
324 test_expect_success 'correct message when there is nothing to do' '
325 git reset --hard &&
326 git add -p 2>err &&
327 test_i18ngrep "No changes" err &&
328 printf "\\0123" >binary &&
329 git add binary &&
330 printf "\\0abc" >binary &&
331 git add -p 2>err &&
332 test_i18ngrep "Only binary files changed" err
333 '
334
335 test_expect_success 'setup again' '
336 git reset --hard &&
337 test_chmod +x file &&
338 echo content >>file &&
339 test_write_lines A B C D>file2 &&
340 git add file2
341 '
342
343 # Write the patch file with a new line at the top and bottom
344 test_expect_success 'setup patch' '
345 cat >patch <<-\EOF
346 index 180b47c..b6f2c08 100644
347 --- a/file
348 +++ b/file
349 @@ -1,2 +1,4 @@
350 +firstline
351 baseline
352 content
353 +lastline
354 \ No newline at end of file
355 diff --git a/file2 b/file2
356 index 8422d40..35b930a 100644
357 --- a/file2
358 +++ b/file2
359 @@ -1,4 +1,5 @@
360 -A
361 +Z
362 B
363 +Y
364 C
365 -D
366 +X
367 EOF
368 '
369
370 # Expected output, diff is similar to the patch but w/ diff at the top
371 test_expect_success 'setup expected' '
372 echo diff --git a/file b/file >expected &&
373 sed -e "/^index 180b47c/s/ 100644/ 100755/" \
374 -e /1,5/s//1,4/ \
375 -e /Y/d patch >>expected &&
376 cat >expected-output <<-\EOF
377 --- a/file
378 +++ b/file
379 @@ -1,2 +1,4 @@
380 +firstline
381 baseline
382 content
383 +lastline
384 \ No newline at end of file
385 @@ -1,2 +1,3 @@
386 +firstline
387 baseline
388 content
389 @@ -1,2 +2,3 @@
390 baseline
391 content
392 +lastline
393 \ No newline at end of file
394 --- a/file2
395 +++ b/file2
396 @@ -1,4 +1,5 @@
397 -A
398 +Z
399 B
400 +Y
401 C
402 -D
403 +X
404 @@ -1,2 +1,2 @@
405 -A
406 +Z
407 B
408 @@ -2,2 +2,3 @@
409 B
410 +Y
411 C
412 @@ -3,2 +4,2 @@
413 C
414 -D
415 +X
416 EOF
417 '
418
419 # Test splitting the first patch, then adding both
420 test_expect_success 'add first line works' '
421 git commit -am "clear local changes" &&
422 git apply patch &&
423 test_write_lines s y y s y n y | git add -p 2>error >raw-output &&
424 sed -n -e "s/^([1-9]\/[1-9]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
425 -e "/^[-+@ \\\\]"/p raw-output >output &&
426 test_must_be_empty error &&
427 git diff --cached >diff &&
428 diff_cmp expected diff &&
429 test_cmp expected-output output
430 '
431
432 test_expect_success 'setup expected' '
433 cat >expected <<-\EOF
434 diff --git a/non-empty b/non-empty
435 deleted file mode 100644
436 index d95f3ad..0000000
437 --- a/non-empty
438 +++ /dev/null
439 @@ -1 +0,0 @@
440 -content
441 EOF
442 '
443
444 test_expect_success 'deleting a non-empty file' '
445 git reset --hard &&
446 echo content >non-empty &&
447 git add non-empty &&
448 git commit -m non-empty &&
449 rm non-empty &&
450 echo y | git add -p non-empty &&
451 git diff --cached >diff &&
452 diff_cmp expected diff
453 '
454
455 test_expect_success 'setup expected' '
456 cat >expected <<-\EOF
457 diff --git a/empty b/empty
458 deleted file mode 100644
459 index e69de29..0000000
460 EOF
461 '
462
463 test_expect_success 'deleting an empty file' '
464 git reset --hard &&
465 > empty &&
466 git add empty &&
467 git commit -m empty &&
468 rm empty &&
469 echo y | git add -p empty &&
470 git diff --cached >diff &&
471 diff_cmp expected diff
472 '
473
474 test_expect_success 'adding an empty file' '
475 git init added &&
476 (
477 cd added &&
478 test_commit initial &&
479 >empty &&
480 git add empty &&
481 test_tick &&
482 git commit -m empty &&
483 git tag added-file &&
484 git reset --hard HEAD^ &&
485 test_path_is_missing empty &&
486
487 echo y | git checkout -p added-file -- >actual &&
488 test_path_is_file empty &&
489 test_i18ngrep "Apply addition to index and worktree" actual
490 )
491 '
492
493 test_expect_success 'split hunk setup' '
494 git reset --hard &&
495 test_write_lines 10 20 30 40 50 60 >test &&
496 git add test &&
497 test_tick &&
498 git commit -m test &&
499
500 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
501 '
502
503 test_expect_success 'goto hunk' '
504 test_when_finished "git reset" &&
505 tr _ " " >expect <<-EOF &&
506 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? + 1: -1,2 +1,3 +15
507 _ 2: -2,4 +3,8 +21
508 go to which hunk? @@ -1,2 +1,3 @@
509 _10
510 +15
511 _20
512 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
513 EOF
514 test_write_lines s y g 1 | git add -p >actual &&
515 tail -n 7 <actual >actual.trimmed &&
516 test_cmp expect actual.trimmed
517 '
518
519 test_expect_success 'navigate to hunk via regex' '
520 test_when_finished "git reset" &&
521 tr _ " " >expect <<-EOF &&
522 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? @@ -1,2 +1,3 @@
523 _10
524 +15
525 _20
526 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
527 EOF
528 test_write_lines s y /1,2 | git add -p >actual &&
529 tail -n 5 <actual >actual.trimmed &&
530 test_cmp expect actual.trimmed
531 '
532
533 test_expect_success 'split hunk "add -p (edit)"' '
534 # Split, say Edit and do nothing. Then:
535 #
536 # 1. Broken version results in a patch that does not apply and
537 # only takes [y/n] (edit again) so the first q is discarded
538 # and then n attempts to discard the edit. Repeat q enough
539 # times to get out.
540 #
541 # 2. Correct version applies the (not)edited version, and asks
542 # about the next hunk, against which we say q and program
543 # exits.
544 printf "%s\n" s e q n q q |
545 EDITOR=: git add -p &&
546 git diff >actual &&
547 ! grep "^+15" actual
548 '
549
550 test_expect_success 'setup ADD_I_USE_BUILTIN check' '
551 result=success &&
552 if ! test_have_prereq ADD_I_USE_BUILTIN
553 then
554 result=failure
555 fi
556 '
557
558 test_expect_$result 'split hunk "add -p (no, yes, edit)"' '
559 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
560 git reset &&
561 # test sequence is s(plit), n(o), y(es), e(dit)
562 # q n q q is there to make sure we exit at the end.
563 printf "%s\n" s n y e q n q q |
564 EDITOR=: git add -p 2>error &&
565 test_must_be_empty error &&
566 git diff >actual &&
567 ! grep "^+31" actual
568 '
569
570 test_expect_success 'split hunk with incomplete line at end' '
571 git reset --hard &&
572 printf "missing LF" >>test &&
573 git add test &&
574 test_write_lines before 10 20 30 40 50 60 70 >test &&
575 git grep --cached missing &&
576 test_write_lines s n y q | git add -p &&
577 test_must_fail git grep --cached missing &&
578 git grep before &&
579 test_must_fail git grep --cached before
580 '
581
582 test_expect_$result 'edit, adding lines to the first hunk' '
583 test_write_lines 10 11 20 30 40 50 51 60 >test &&
584 git reset &&
585 tr _ " " >patch <<-EOF &&
586 @@ -1,5 +1,6 @@
587 _10
588 +11
589 +12
590 _20
591 +21
592 +22
593 _30
594 EOF
595 # test sequence is s(plit), e(dit), n(o)
596 # q n q q is there to make sure we exit at the end.
597 printf "%s\n" s e n q n q q |
598 EDITOR=./fake_editor.sh git add -p 2>error &&
599 test_must_be_empty error &&
600 git diff --cached >actual &&
601 grep "^+22" actual
602 '
603
604 test_expect_success 'patch mode ignores unmerged entries' '
605 git reset --hard &&
606 test_commit conflict &&
607 test_commit non-conflict &&
608 git checkout -b side &&
609 test_commit side conflict.t &&
610 git checkout main &&
611 test_commit main conflict.t &&
612 test_must_fail git merge side &&
613 echo changed >non-conflict.t &&
614 echo y | git add -p >output &&
615 ! grep a/conflict.t output &&
616 cat >expected <<-\EOF &&
617 * Unmerged path conflict.t
618 diff --git a/non-conflict.t b/non-conflict.t
619 index f766221..5ea2ed4 100644
620 --- a/non-conflict.t
621 +++ b/non-conflict.t
622 @@ -1 +1 @@
623 -non-conflict
624 +changed
625 EOF
626 git diff --cached >diff &&
627 diff_cmp expected diff
628 '
629
630 test_expect_success 'index is refreshed after applying patch' '
631 git reset --hard &&
632 echo content >test &&
633 printf y | git add -p &&
634 git diff-files --exit-code
635 '
636
637 test_expect_success 'diffs can be colorized' '
638 git reset --hard &&
639
640 echo content >test &&
641 printf y >y &&
642 force_color git add -p >output 2>&1 <y &&
643 git diff-files --exit-code &&
644
645 # We do not want to depend on the exact coloring scheme
646 # git uses for diffs, so just check that we saw some kind of color.
647 grep "$(printf "\\033")" output
648 '
649
650 test_expect_success 'colors can be overridden' '
651 git reset --hard &&
652 test_when_finished "git rm -f color-test" &&
653 test_write_lines context old more-context >color-test &&
654 git add color-test &&
655 test_write_lines context new more-context another-one >color-test &&
656
657 echo trigger an error message >input &&
658 force_color git \
659 -c color.interactive.error=blue \
660 add -i 2>err.raw <input &&
661 test_decode_color <err.raw >err &&
662 grep "<BLUE>Huh (trigger)?<RESET>" err &&
663
664 test_write_lines help quit >input &&
665 force_color git \
666 -c color.interactive.header=red \
667 -c color.interactive.help=green \
668 -c color.interactive.prompt=yellow \
669 add -i >actual.raw <input &&
670 test_decode_color <actual.raw >actual &&
671 cat >expect <<-\EOF &&
672 <RED> staged unstaged path<RESET>
673 1: +3/-0 +2/-1 color-test
674
675 <RED>*** Commands ***<RESET>
676 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
677 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
678 <YELLOW>What now<RESET>> <GREEN>status - show paths with changes<RESET>
679 <GREEN>update - add working tree state to the staged set of changes<RESET>
680 <GREEN>revert - revert staged set of changes back to the HEAD version<RESET>
681 <GREEN>patch - pick hunks and update selectively<RESET>
682 <GREEN>diff - view diff between HEAD and index<RESET>
683 <GREEN>add untracked - add contents of untracked files to the staged set of changes<RESET>
684 <RED>*** Commands ***<RESET>
685 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
686 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
687 <YELLOW>What now<RESET>> Bye.
688 EOF
689 test_cmp expect actual &&
690
691 : exercise recolor_hunk by editing and then look at the hunk again &&
692 test_write_lines s e K q >input &&
693 force_color git \
694 -c color.interactive.prompt=yellow \
695 -c color.diff.meta=italic \
696 -c color.diff.frag=magenta \
697 -c color.diff.context=cyan \
698 -c color.diff.old=bold \
699 -c color.diff.new=blue \
700 -c core.editor=touch \
701 add -p >actual.raw <input &&
702 test_decode_color <actual.raw >actual.decoded &&
703 sed "s/index [0-9a-f]*\\.\\.[0-9a-f]* 100644/<INDEX-LINE>/" <actual.decoded >actual &&
704 cat >expect <<-\EOF &&
705 <ITALIC>diff --git a/color-test b/color-test<RESET>
706 <ITALIC><INDEX-LINE><RESET>
707 <ITALIC>--- a/color-test<RESET>
708 <ITALIC>+++ b/color-test<RESET>
709 <MAGENTA>@@ -1,3 +1,4 @@<RESET>
710 <CYAN> context<RESET>
711 <BOLD>-old<RESET>
712 <BLUE>+<RESET><BLUE>new<RESET>
713 <CYAN> more-context<RESET>
714 <BLUE>+<RESET><BLUE>another-one<RESET>
715 <YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
716 <MAGENTA>@@ -1,3 +1,3 @@<RESET>
717 <CYAN> context<RESET>
718 <BOLD>-old<RESET>
719 <BLUE>+<RESET><BLUE>new<RESET>
720 <CYAN> more-context<RESET>
721 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
722 <CYAN> more-context<RESET>
723 <BLUE>+<RESET><BLUE>another-one<RESET>
724 <YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
725 <CYAN> context<RESET>
726 <BOLD>-old<RESET>
727 <BLUE>+new<RESET>
728 <CYAN> more-context<RESET>
729 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? <RESET>
730 EOF
731 test_cmp expect actual
732 '
733
734 test_expect_success 'colorized diffs respect diff.wsErrorHighlight' '
735 git reset --hard &&
736
737 echo "old " >test &&
738 git add test &&
739 echo "new " >test &&
740
741 printf y >y &&
742 force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
743 test_decode_color <output.raw >output &&
744 grep "old<" output
745 '
746
747 test_expect_success 'diffFilter filters diff' '
748 git reset --hard &&
749
750 echo content >test &&
751 test_config interactive.diffFilter "sed s/^/foo:/" &&
752 printf y >y &&
753 force_color git add -p >output 2>&1 <y &&
754
755 # avoid depending on the exact coloring or content of the prompts,
756 # and just make sure we saw our diff prefixed
757 grep foo:.*content output
758 '
759
760 test_expect_success 'detect bogus diffFilter output' '
761 git reset --hard &&
762
763 echo content >test &&
764 test_config interactive.diffFilter "sed 1d" &&
765 printf y >y &&
766 force_color test_must_fail git add -p <y
767 '
768
769 test_expect_success 'handle very large filtered diff' '
770 git reset --hard &&
771 # The specific number here is not important, but it must
772 # be large enough that the output of "git diff --color"
773 # fills up the pipe buffer. 10,000 results in ~200k of
774 # colored output.
775 test_seq 10000 >test &&
776 test_config interactive.diffFilter cat &&
777 printf y >y &&
778 force_color git add -p >output 2>&1 <y &&
779 git diff-files --exit-code -- test
780 '
781
782 test_expect_success 'diff.algorithm is passed to `git diff-files`' '
783 git reset --hard &&
784
785 >file &&
786 git add file &&
787 echo changed >file &&
788 test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
789 test_i18ngrep "error: option diff-algorithm accepts " err
790 '
791
792 test_expect_success 'patch-mode via -i prompts for files' '
793 git reset --hard &&
794
795 echo one >file &&
796 echo two >test &&
797 git add -i <<-\EOF &&
798 patch
799 test
800
801 y
802 quit
803 EOF
804
805 echo test >expect &&
806 git diff --cached --name-only >actual &&
807 diff_cmp expect actual
808 '
809
810 test_expect_success 'add -p handles globs' '
811 git reset --hard &&
812
813 mkdir -p subdir &&
814 echo base >one.c &&
815 echo base >subdir/two.c &&
816 git add "*.c" &&
817 git commit -m base &&
818
819 echo change >one.c &&
820 echo change >subdir/two.c &&
821 git add -p "*.c" <<-\EOF &&
822 y
823 y
824 EOF
825
826 cat >expect <<-\EOF &&
827 one.c
828 subdir/two.c
829 EOF
830 git diff --cached --name-only >actual &&
831 test_cmp expect actual
832 '
833
834 test_expect_success 'add -p handles relative paths' '
835 git reset --hard &&
836
837 echo base >relpath.c &&
838 git add "*.c" &&
839 git commit -m relpath &&
840
841 echo change >relpath.c &&
842 mkdir -p subdir &&
843 git -C subdir add -p .. 2>error <<-\EOF &&
844 y
845 EOF
846
847 test_must_be_empty error &&
848
849 cat >expect <<-\EOF &&
850 relpath.c
851 EOF
852 git diff --cached --name-only >actual &&
853 test_cmp expect actual
854 '
855
856 test_expect_success 'add -p does not expand argument lists' '
857 git reset --hard &&
858
859 echo content >not-changed &&
860 git add not-changed &&
861 git commit -m "add not-changed file" &&
862
863 echo change >file &&
864 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
865 y
866 EOF
867
868 # we know that "file" must be mentioned since we actually
869 # update it, but we want to be sure that our "." pathspec
870 # was not expanded into the argument list of any command.
871 # So look only for "not-changed".
872 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
873 '
874
875 test_expect_success 'hunk-editing handles custom comment char' '
876 git reset --hard &&
877 echo change >>file &&
878 test_config core.commentChar "\$" &&
879 echo e | GIT_EDITOR=true git add -p &&
880 git diff --exit-code
881 '
882
883 test_expect_success 'add -p works even with color.ui=always' '
884 git reset --hard &&
885 echo change >>file &&
886 test_config color.ui always &&
887 echo y | git add -p &&
888 echo file >expect &&
889 git diff --cached --name-only >actual &&
890 test_cmp expect actual
891 '
892
893 test_expect_success 'setup different kinds of dirty submodules' '
894 test_create_repo for-submodules &&
895 (
896 cd for-submodules &&
897 test_commit initial &&
898 test_create_repo dirty-head &&
899 (
900 cd dirty-head &&
901 test_commit initial
902 ) &&
903 cp -R dirty-head dirty-otherwise &&
904 cp -R dirty-head dirty-both-ways &&
905 git add dirty-head &&
906 git add dirty-otherwise dirty-both-ways &&
907 git commit -m initial &&
908
909 cd dirty-head &&
910 test_commit updated &&
911 cd ../dirty-both-ways &&
912 test_commit updated &&
913 echo dirty >>initial &&
914 : >untracked &&
915 cd ../dirty-otherwise &&
916 echo dirty >>initial &&
917 : >untracked
918 ) &&
919 git -C for-submodules diff-files --name-only >actual &&
920 cat >expected <<-\EOF &&
921 dirty-both-ways
922 dirty-head
923 EOF
924 test_cmp expected actual &&
925 git -C for-submodules diff-files --name-only --ignore-submodules=none >actual &&
926 cat >expected <<-\EOF &&
927 dirty-both-ways
928 dirty-head
929 dirty-otherwise
930 EOF
931 test_cmp expected actual &&
932 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
933 cat >expected <<-\EOF &&
934 dirty-both-ways
935 dirty-head
936 EOF
937 test_cmp expected actual
938 '
939
940 test_expect_success 'status ignores dirty submodules (except HEAD)' '
941 git -C for-submodules add -i </dev/null >output &&
942 grep dirty-head output &&
943 grep dirty-both-ways output &&
944 ! grep dirty-otherwise output
945 '
946
947 test_expect_success 'set up pathological context' '
948 git reset --hard &&
949 test_write_lines a a a a a a a a a a a >a &&
950 git add a &&
951 git commit -m a &&
952 test_write_lines c b a a a a a a a b a a a a >a &&
953 test_write_lines a a a a a a a b a a a a >expected-1 &&
954 test_write_lines b a a a a a a a b a a a a >expected-2 &&
955 # check editing can cope with missing header and deleted context lines
956 # as well as changes to other lines
957 test_write_lines +b " a" >patch
958 '
959
960 test_expect_success 'add -p works with pathological context lines' '
961 git reset &&
962 printf "%s\n" n y |
963 git add -p &&
964 git cat-file blob :a >actual &&
965 test_cmp expected-1 actual
966 '
967
968 test_expect_success 'add -p patch editing works with pathological context lines' '
969 git reset &&
970 # n q q below is in case edit fails
971 printf "%s\n" e y n q q |
972 git add -p &&
973 git cat-file blob :a >actual &&
974 test_cmp expected-2 actual
975 '
976
977 test_expect_success 'checkout -p works with pathological context lines' '
978 test_write_lines a a a a a a >a &&
979 git add a &&
980 test_write_lines a b a b a b a b a b a >a &&
981 test_write_lines s n n y q | git checkout -p &&
982 test_write_lines a b a b a a b a b a >expect &&
983 test_cmp expect a
984 '
985
986 # This should be called from a subshell as it sets a temporary editor
987 setup_new_file() {
988 write_script new-file-editor.sh <<-\EOF &&
989 sed /^#/d "$1" >patch &&
990 sed /^+c/d patch >"$1"
991 EOF
992 test_set_editor "$(pwd)/new-file-editor.sh" &&
993 test_write_lines a b c d e f >new-file &&
994 test_write_lines a b d e f >new-file-expect &&
995 test_write_lines "@@ -0,0 +1,6 @@" +a +b +c +d +e +f >patch-expect
996 }
997
998 test_expect_success 'add -N followed by add -p patch editing' '
999 git reset --hard &&
1000 (
1001 setup_new_file &&
1002 git add -N new-file &&
1003 test_write_lines e n q | git add -p &&
1004 git cat-file blob :new-file >actual &&
1005 test_cmp new-file-expect actual &&
1006 test_cmp patch-expect patch
1007 )
1008 '
1009
1010 test_expect_success 'checkout -p patch editing of added file' '
1011 git reset --hard &&
1012 (
1013 setup_new_file &&
1014 git add new-file &&
1015 git commit -m "add new file" &&
1016 git rm new-file &&
1017 git commit -m "remove new file" &&
1018 test_write_lines e n q | git checkout -p HEAD^ &&
1019 test_cmp new-file-expect new-file &&
1020 test_cmp patch-expect patch
1021 )
1022 '
1023
1024 test_expect_success 'show help from add--helper' '
1025 git reset --hard &&
1026 cat >expect <<-EOF &&
1027
1028 <BOLD>*** Commands ***<RESET>
1029 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
1030 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1031 <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes<RESET>
1032 <BOLD;RED>update - add working tree state to the staged set of changes<RESET>
1033 <BOLD;RED>revert - revert staged set of changes back to the HEAD version<RESET>
1034 <BOLD;RED>patch - pick hunks and update selectively<RESET>
1035 <BOLD;RED>diff - view diff between HEAD and index<RESET>
1036 <BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
1037 <BOLD>*** Commands ***<RESET>
1038 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
1039 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1040 <BOLD;BLUE>What now<RESET>>$SP
1041 Bye.
1042 EOF
1043 test_write_lines h | force_color git add -i >actual.colored &&
1044 test_decode_color <actual.colored >actual &&
1045 test_cmp expect actual
1046 '
1047
1048 test_done