]> git.ipfire.org Git - thirdparty/git.git/blob - t/t3701-add-interactive.sh
Merge branch 'gt/add-u-commit-i-pathspec-check'
[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,p,?]?
329 (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,p,?]?
330 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]?
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_grep "No changes" err &&
339 printf "\\0123" >binary &&
340 git add binary &&
341 printf "\\0abc" >binary &&
342 git add -p 2>err &&
343 test_grep "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_grep "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,p,?]? + 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,p,?]?_
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,p,?]? @@ -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,p,?]?_
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,p,?]? <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,p,?]? <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,p,?]? <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,p,?]? <RESET>
733 EOF
734 test_cmp expect actual
735 '
736
737 test_expect_success 'brackets appear without color' '
738 git reset --hard &&
739 test_when_finished "git rm -f bracket-test" &&
740 test_write_lines context old more-context >bracket-test &&
741 git add bracket-test &&
742 test_write_lines context new more-context another-one >bracket-test &&
743
744 test_write_lines quit >input &&
745 git add -i >actual <input &&
746
747 sed "s/^|//" >expect <<-\EOF &&
748 | staged unstaged path
749 | 1: +3/-0 +2/-1 bracket-test
750 |
751 |*** Commands ***
752 | 1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
753 | 5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
754 |What now> Bye.
755 EOF
756
757 test_cmp expect actual
758 '
759
760 test_expect_success 'colors can be skipped with color.ui=false' '
761 git reset --hard &&
762 test_when_finished "git rm -f color-test" &&
763 test_write_lines context old more-context >color-test &&
764 git add color-test &&
765 test_write_lines context new more-context another-one >color-test &&
766
767 test_write_lines help quit >input &&
768 force_color git \
769 -c color.ui=false \
770 add -i >actual.raw <input &&
771 test_decode_color <actual.raw >actual &&
772 test_cmp actual.raw actual
773 '
774
775 test_expect_success 'colorized diffs respect diff.wsErrorHighlight' '
776 git reset --hard &&
777
778 echo "old " >test &&
779 git add test &&
780 echo "new " >test &&
781
782 printf y >y &&
783 force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
784 test_decode_color <output.raw >output &&
785 grep "old<" output
786 '
787
788 test_expect_success 'diffFilter filters diff' '
789 git reset --hard &&
790
791 echo content >test &&
792 test_config interactive.diffFilter "sed s/^/foo:/" &&
793 printf y >y &&
794 force_color git add -p >output 2>&1 <y &&
795
796 # avoid depending on the exact coloring or content of the prompts,
797 # and just make sure we saw our diff prefixed
798 grep foo:.*content output
799 '
800
801 test_expect_success 'detect bogus diffFilter output' '
802 git reset --hard &&
803
804 echo content >test &&
805 test_config interactive.diffFilter "sed 6d" &&
806 printf y >y &&
807 force_color test_must_fail git add -p <y >output 2>&1 &&
808 grep "mismatched output" output
809 '
810
811 test_expect_success 'handle iffy colored hunk headers' '
812 git reset --hard &&
813
814 echo content >test &&
815 printf n >n &&
816 force_color git -c interactive.diffFilter="sed s/.*@@.*/XX/" \
817 add -p >output 2>&1 <n &&
818 grep "^XX$" output
819 '
820
821 test_expect_success 'handle very large filtered diff' '
822 git reset --hard &&
823 # The specific number here is not important, but it must
824 # be large enough that the output of "git diff --color"
825 # fills up the pipe buffer. 10,000 results in ~200k of
826 # colored output.
827 test_seq 10000 >test &&
828 test_config interactive.diffFilter cat &&
829 printf y >y &&
830 force_color git add -p >output 2>&1 <y &&
831 git diff-files --exit-code -- test
832 '
833
834 test_expect_success 'diff.algorithm is passed to `git diff-files`' '
835 git reset --hard &&
836
837 >file &&
838 git add file &&
839 echo changed >file &&
840 test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
841 test_grep "error: option diff-algorithm accepts " err
842 '
843
844 test_expect_success 'patch-mode via -i prompts for files' '
845 git reset --hard &&
846
847 echo one >file &&
848 echo two >test &&
849 git add -i <<-\EOF &&
850 patch
851 test
852
853 y
854 quit
855 EOF
856
857 echo test >expect &&
858 git diff --cached --name-only >actual &&
859 diff_cmp expect actual
860 '
861
862 test_expect_success 'add -p handles globs' '
863 git reset --hard &&
864
865 mkdir -p subdir &&
866 echo base >one.c &&
867 echo base >subdir/two.c &&
868 git add "*.c" &&
869 git commit -m base &&
870
871 echo change >one.c &&
872 echo change >subdir/two.c &&
873 git add -p "*.c" <<-\EOF &&
874 y
875 y
876 EOF
877
878 cat >expect <<-\EOF &&
879 one.c
880 subdir/two.c
881 EOF
882 git diff --cached --name-only >actual &&
883 test_cmp expect actual
884 '
885
886 test_expect_success 'add -p handles relative paths' '
887 git reset --hard &&
888
889 echo base >relpath.c &&
890 git add "*.c" &&
891 git commit -m relpath &&
892
893 echo change >relpath.c &&
894 mkdir -p subdir &&
895 git -C subdir add -p .. 2>error <<-\EOF &&
896 y
897 EOF
898
899 test_must_be_empty error &&
900
901 cat >expect <<-\EOF &&
902 relpath.c
903 EOF
904 git diff --cached --name-only >actual &&
905 test_cmp expect actual
906 '
907
908 test_expect_success 'add -p does not expand argument lists' '
909 git reset --hard &&
910
911 echo content >not-changed &&
912 git add not-changed &&
913 git commit -m "add not-changed file" &&
914
915 echo change >file &&
916 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
917 y
918 EOF
919
920 # we know that "file" must be mentioned since we actually
921 # update it, but we want to be sure that our "." pathspec
922 # was not expanded into the argument list of any command.
923 # So look only for "not-changed".
924 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
925 '
926
927 test_expect_success 'hunk-editing handles custom comment char' '
928 git reset --hard &&
929 echo change >>file &&
930 test_config core.commentChar "\$" &&
931 echo e | GIT_EDITOR=true git add -p &&
932 git diff --exit-code
933 '
934
935 test_expect_success 'add -p works even with color.ui=always' '
936 git reset --hard &&
937 echo change >>file &&
938 test_config color.ui always &&
939 echo y | git add -p &&
940 echo file >expect &&
941 git diff --cached --name-only >actual &&
942 test_cmp expect actual
943 '
944
945 test_expect_success 'setup different kinds of dirty submodules' '
946 test_create_repo for-submodules &&
947 (
948 cd for-submodules &&
949 test_commit initial &&
950 test_create_repo dirty-head &&
951 (
952 cd dirty-head &&
953 test_commit initial
954 ) &&
955 cp -R dirty-head dirty-otherwise &&
956 cp -R dirty-head dirty-both-ways &&
957 git add dirty-head &&
958 git add dirty-otherwise dirty-both-ways &&
959 git commit -m initial &&
960
961 cd dirty-head &&
962 test_commit updated &&
963 cd ../dirty-both-ways &&
964 test_commit updated &&
965 echo dirty >>initial &&
966 : >untracked &&
967 cd ../dirty-otherwise &&
968 echo dirty >>initial &&
969 : >untracked
970 ) &&
971 git -C for-submodules diff-files --name-only >actual &&
972 cat >expected <<-\EOF &&
973 dirty-both-ways
974 dirty-head
975 EOF
976 test_cmp expected actual &&
977 git -C for-submodules diff-files --name-only --ignore-submodules=none >actual &&
978 cat >expected <<-\EOF &&
979 dirty-both-ways
980 dirty-head
981 dirty-otherwise
982 EOF
983 test_cmp expected actual &&
984 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
985 cat >expected <<-\EOF &&
986 dirty-both-ways
987 dirty-head
988 EOF
989 test_cmp expected actual
990 '
991
992 test_expect_success 'status ignores dirty submodules (except HEAD)' '
993 git -C for-submodules add -i </dev/null >output &&
994 grep dirty-head output &&
995 grep dirty-both-ways output &&
996 ! grep dirty-otherwise output
997 '
998
999 test_expect_success 'handle submodules' '
1000 echo 123 >>for-submodules/dirty-otherwise/initial.t &&
1001
1002 force_color git -C for-submodules add -p dirty-otherwise >output 2>&1 &&
1003 grep "No changes" output &&
1004
1005 force_color git -C for-submodules add -p dirty-head >output 2>&1 <y &&
1006 git -C for-submodules ls-files --stage dirty-head >actual &&
1007 rev="$(git -C for-submodules/dirty-head rev-parse HEAD)" &&
1008 grep "$rev" actual
1009 '
1010
1011 test_expect_success 'set up pathological context' '
1012 git reset --hard &&
1013 test_write_lines a a a a a a a a a a a >a &&
1014 git add a &&
1015 git commit -m a &&
1016 test_write_lines c b a a a a a a a b a a a a >a &&
1017 test_write_lines a a a a a a a b a a a a >expected-1 &&
1018 test_write_lines b a a a a a a a b a a a a >expected-2 &&
1019 # check editing can cope with missing header and deleted context lines
1020 # as well as changes to other lines
1021 test_write_lines +b " a" >patch
1022 '
1023
1024 test_expect_success 'add -p works with pathological context lines' '
1025 git reset &&
1026 printf "%s\n" n y |
1027 git add -p &&
1028 git cat-file blob :a >actual &&
1029 test_cmp expected-1 actual
1030 '
1031
1032 test_expect_success 'add -p patch editing works with pathological context lines' '
1033 git reset &&
1034 # n q q below is in case edit fails
1035 printf "%s\n" e y n q q |
1036 git add -p &&
1037 git cat-file blob :a >actual &&
1038 test_cmp expected-2 actual
1039 '
1040
1041 test_expect_success 'checkout -p works with pathological context lines' '
1042 test_write_lines a a a a a a >a &&
1043 git add a &&
1044 test_write_lines a b a b a b a b a b a >a &&
1045 test_write_lines s n n y q | git checkout -p &&
1046 test_write_lines a b a b a a b a b a >expect &&
1047 test_cmp expect a
1048 '
1049
1050 # This should be called from a subshell as it sets a temporary editor
1051 setup_new_file() {
1052 write_script new-file-editor.sh <<-\EOF &&
1053 sed /^#/d "$1" >patch &&
1054 sed /^+c/d patch >"$1"
1055 EOF
1056 test_set_editor "$(pwd)/new-file-editor.sh" &&
1057 test_write_lines a b c d e f >new-file &&
1058 test_write_lines a b d e f >new-file-expect &&
1059 test_write_lines "@@ -0,0 +1,6 @@" +a +b +c +d +e +f >patch-expect
1060 }
1061
1062 test_expect_success 'add -N followed by add -p patch editing' '
1063 git reset --hard &&
1064 (
1065 setup_new_file &&
1066 git add -N new-file &&
1067 test_write_lines e n q | git add -p &&
1068 git cat-file blob :new-file >actual &&
1069 test_cmp new-file-expect actual &&
1070 test_cmp patch-expect patch
1071 )
1072 '
1073
1074 test_expect_success 'checkout -p patch editing of added file' '
1075 git reset --hard &&
1076 (
1077 setup_new_file &&
1078 git add new-file &&
1079 git commit -m "add new file" &&
1080 git rm new-file &&
1081 git commit -m "remove new file" &&
1082 test_write_lines e n q | git checkout -p HEAD^ &&
1083 test_cmp new-file-expect new-file &&
1084 test_cmp patch-expect patch
1085 )
1086 '
1087
1088 test_expect_success 'show help from add--helper' '
1089 git reset --hard &&
1090 cat >expect <<-EOF &&
1091
1092 <BOLD>*** Commands ***<RESET>
1093 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
1094 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1095 <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes<RESET>
1096 <BOLD;RED>update - add working tree state to the staged set of changes<RESET>
1097 <BOLD;RED>revert - revert staged set of changes back to the HEAD version<RESET>
1098 <BOLD;RED>patch - pick hunks and update selectively<RESET>
1099 <BOLD;RED>diff - view diff between HEAD and index<RESET>
1100 <BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
1101 <BOLD>*** Commands ***<RESET>
1102 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
1103 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1104 <BOLD;BLUE>What now<RESET>>$SP
1105 Bye.
1106 EOF
1107 test_write_lines h | force_color git add -i >actual.colored &&
1108 test_decode_color <actual.colored >actual &&
1109 test_cmp expect actual
1110 '
1111
1112 test_expect_success 'reset -p with unmerged files' '
1113 test_when_finished "git checkout --force main" &&
1114 test_commit one conflict &&
1115 git checkout -B side HEAD^ &&
1116 test_commit two conflict &&
1117 test_must_fail git merge one &&
1118
1119 # this is a noop with only an unmerged entry
1120 git reset -p &&
1121
1122 # add files that sort before and after unmerged entry
1123 echo a >a &&
1124 echo z >z &&
1125 git add a z &&
1126
1127 # confirm that we can reset those files
1128 printf "%s\n" y y | git reset -p &&
1129 git diff-index --cached --diff-filter=u HEAD >staged &&
1130 test_must_be_empty staged
1131 '
1132
1133 test_done