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