compat/win32: fix const-correctness with string constants
[git.git] / t / t3701-add-interactive.sh
blob28a95a775d3d9a5c2274cc666c5b4c5f04c26893
1 #!/bin/sh
3 test_description='add -i basic tests'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-terminal.sh
11 SP=" "
13 diff_cmp () {
14 for x
16 sed -e '/^index/s/[0-9a-f]*[1-9a-f][0-9a-f]*\.\./1234567../' \
17 -e '/^index/s/\.\.[0-9a-f]*[1-9a-f][0-9a-f]*/..9abcdef/' \
18 -e '/^index/s/ 00*\.\./ 0000000../' \
19 -e '/^index/s/\.\.00*$/..0000000/' \
20 -e '/^index/s/\.\.00* /..0000000 /' \
21 "$x" >"$x.filtered"
22 done
23 test_cmp "$1.filtered" "$2.filtered"
26 # This function uses a trick to manipulate the interactive add to use color:
27 # the `want_color()` function special-cases the situation where a pager was
28 # spawned and Git now wants to output colored text: to detect that situation,
29 # the environment variable `GIT_PAGER_IN_USE` is set. However, color is
30 # suppressed despite that environment variable if the `TERM` variable
31 # indicates a dumb terminal, so we set that variable, too.
33 force_color () {
34 # The first element of $@ may be a shell function, as a result POSIX
35 # does not guarantee that "one-shot assignment" will not persist after
36 # the function call. Thus, we prevent these variables from escaping
37 # this function's context with this subshell.
39 GIT_PAGER_IN_USE=true &&
40 TERM=vt100 &&
41 export GIT_PAGER_IN_USE TERM &&
42 "$@"
46 test_expect_success 'warn about add.interactive.useBuiltin' '
47 cat >expect <<-\EOF &&
48 warning: the add.interactive.useBuiltin setting has been removed!
49 See its entry in '\''git help config'\'' for details.
50 EOF
51 echo "No changes." >expect.out &&
53 for v in = =true =false
55 git -c "add.interactive.useBuiltin$v" add -p >out 2>actual &&
56 test_cmp expect.out out &&
57 test_cmp expect actual || return 1
58 done
61 test_expect_success 'unknown command' '
62 test_when_finished "git reset --hard; rm -f command" &&
63 echo W >command &&
64 git add -N command &&
65 git diff command >expect &&
66 cat >>expect <<-EOF &&
67 (1/1) Stage addition [y,n,q,a,d,e,p,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help)
68 (1/1) Stage addition [y,n,q,a,d,e,p,?]?$SP
69 EOF
70 git add -p -- command <command >actual 2>&1 &&
71 test_cmp expect actual
74 test_expect_success 'setup (initial)' '
75 echo content >file &&
76 git add file &&
77 echo more >>file &&
78 echo lines >>file
80 test_expect_success 'status works (initial)' '
81 git add -i </dev/null >output &&
82 grep "+1/-0 *+2/-0 file" output
85 test_expect_success 'setup expected' '
86 cat >expected <<-\EOF
87 new file mode 100644
88 index 0000000..d95f3ad
89 --- /dev/null
90 +++ b/file
91 @@ -0,0 +1 @@
92 +content
93 EOF
96 test_expect_success 'diff works (initial)' '
97 test_write_lines d 1 | git add -i >output &&
98 sed -ne "/new file/,/content/p" <output >diff &&
99 diff_cmp expected diff
101 test_expect_success 'revert works (initial)' '
102 git add file &&
103 test_write_lines r 1 | git add -i &&
104 git ls-files >output &&
105 ! grep . output
108 test_expect_success 'add untracked (multiple)' '
109 test_when_finished "git reset && rm [1-9]" &&
110 touch $(test_seq 9) &&
111 test_write_lines a "2-5 8-" | git add -i -- [1-9] &&
112 test_write_lines 2 3 4 5 8 9 >expected &&
113 git ls-files [1-9] >output &&
114 test_cmp expected output
117 test_expect_success 'setup (commit)' '
118 echo baseline >file &&
119 git add file &&
120 git commit -m commit &&
121 echo content >>file &&
122 git add file &&
123 echo more >>file &&
124 echo lines >>file
126 test_expect_success 'status works (commit)' '
127 git add -i </dev/null >output &&
128 grep "+1/-0 *+2/-0 file" output
131 test_expect_success 'update can stage deletions' '
132 >to-delete &&
133 git add to-delete &&
134 rm to-delete &&
135 test_write_lines u t "" | git add -i &&
136 git ls-files to-delete >output &&
137 test_must_be_empty output
140 test_expect_success 'setup expected' '
141 cat >expected <<-\EOF
142 index 180b47c..b6f2c08 100644
143 --- a/file
144 +++ b/file
145 @@ -1 +1,2 @@
146 baseline
147 +content
151 test_expect_success 'diff works (commit)' '
152 test_write_lines d 1 | git add -i >output &&
153 sed -ne "/^index/,/content/p" <output >diff &&
154 diff_cmp expected diff
156 test_expect_success 'revert works (commit)' '
157 git add file &&
158 test_write_lines r 1 | git add -i &&
159 git add -i </dev/null >output &&
160 grep "unchanged *+3/-0 file" output
163 test_expect_success 'setup expected' '
164 cat >expected <<-\EOF
168 test_expect_success 'dummy edit works' '
169 test_set_editor : &&
170 test_write_lines e a | git add -p &&
171 git diff > diff &&
172 diff_cmp expected diff
175 test_expect_success 'setup patch' '
176 cat >patch <<-\EOF
177 @@ -1,1 +1,4 @@
178 this
179 +patch
180 -does not
181 apply
185 test_expect_success 'setup fake editor' '
186 write_script "fake_editor.sh" <<-\EOF &&
187 mv -f "$1" oldpatch &&
188 mv -f patch "$1"
190 test_set_editor "$(pwd)/fake_editor.sh"
193 test_expect_success 'bad edit rejected' '
194 git reset &&
195 test_write_lines e n d | git add -p >output &&
196 grep "hunk does not apply" output
199 test_expect_success 'setup patch' '
200 cat >patch <<-\EOF
201 this patch
202 is garbage
206 test_expect_success 'garbage edit rejected' '
207 git reset &&
208 test_write_lines e n d | git add -p >output &&
209 grep "hunk does not apply" output
212 test_expect_success 'setup patch' '
213 cat >patch <<-\EOF
214 @@ -1,0 +1,0 @@
215 baseline
216 +content
217 +newcontent
218 +lines
222 test_expect_success 'setup expected' '
223 cat >expected <<-\EOF
224 diff --git a/file b/file
225 index b5dd6c9..f910ae9 100644
226 --- a/file
227 +++ b/file
228 @@ -1,4 +1,4 @@
229 baseline
230 content
231 -newcontent
232 +more
233 lines
237 test_expect_success 'real edit works' '
238 test_write_lines e n d | git add -p &&
239 git diff >output &&
240 diff_cmp expected output
243 test_expect_success 'setup file' '
244 test_write_lines a "" b "" c >file &&
245 git add file &&
246 test_write_lines a "" d "" c >file
249 test_expect_success 'setup patch' '
250 NULL="" &&
251 cat >patch <<-EOF
252 @@ -1,4 +1,4 @@
254 $NULL
262 test_expect_success 'setup expected' '
263 cat >expected <<-EOF
264 diff --git a/file b/file
265 index b5dd6c9..f910ae9 100644
266 --- a/file
267 +++ b/file
268 @@ -1,5 +1,5 @@
278 test_expect_success 'edit can strip spaces from empty context lines' '
279 test_write_lines e n q | git add -p 2>error &&
280 test_must_be_empty error &&
281 git diff >output &&
282 diff_cmp expected output
285 test_expect_success 'skip files similarly as commit -a' '
286 git reset &&
287 echo file >.gitignore &&
288 echo changed >file &&
289 echo y | git add -p file &&
290 git diff >output &&
291 git reset &&
292 git commit -am commit &&
293 git diff >expected &&
294 diff_cmp expected output &&
295 git reset --hard HEAD^
297 rm -f .gitignore
299 test_expect_success FILEMODE 'patch does not affect mode' '
300 git reset --hard &&
301 echo content >>file &&
302 chmod +x file &&
303 printf "n\\ny\\n" | git add -p &&
304 git show :file | grep content &&
305 git diff file | grep "new mode"
308 test_expect_success FILEMODE 'stage mode but not hunk' '
309 git reset --hard &&
310 echo content >>file &&
311 chmod +x file &&
312 printf "y\\nn\\n" | git add -p &&
313 git diff --cached file | grep "new mode" &&
314 git diff file | grep "+content"
318 test_expect_success FILEMODE 'stage mode and hunk' '
319 git reset --hard &&
320 echo content >>file &&
321 chmod +x file &&
322 printf "y\\ny\\n" | git add -p &&
323 git diff --cached file >out &&
324 grep "new mode" out &&
325 grep "+content" out &&
326 git diff file >out &&
327 test_must_be_empty out
330 # end of tests disabled when filemode is not usable
332 test_expect_success 'different prompts for mode change/deleted' '
333 git reset --hard &&
334 >file &&
335 >deleted &&
336 git add --chmod=+x file deleted &&
337 echo changed >file &&
338 rm deleted &&
339 test_write_lines n n n |
340 git -c core.filemode=true add -p >actual &&
341 sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
342 cat >expect <<-\EOF &&
343 (1/1) Stage deletion [y,n,q,a,d,p,?]?
344 (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,p,?]?
345 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]?
347 test_cmp expect actual.filtered
350 test_expect_success 'correct message when there is nothing to do' '
351 git reset --hard &&
352 git add -p >out &&
353 test_grep "No changes" out &&
354 printf "\\0123" >binary &&
355 git add binary &&
356 printf "\\0abc" >binary &&
357 git add -p >out &&
358 test_grep "Only binary files changed" out
361 test_expect_success 'setup again' '
362 git reset --hard &&
363 test_chmod +x file &&
364 echo content >>file &&
365 test_write_lines A B C D>file2 &&
366 git add file2
369 # Write the patch file with a new line at the top and bottom
370 test_expect_success 'setup patch' '
371 cat >patch <<-\EOF
372 index 180b47c..b6f2c08 100644
373 --- a/file
374 +++ b/file
375 @@ -1,2 +1,4 @@
376 +firstline
377 baseline
378 content
379 +lastline
380 \ No newline at end of file
381 diff --git a/file2 b/file2
382 index 8422d40..35b930a 100644
383 --- a/file2
384 +++ b/file2
385 @@ -1,4 +1,5 @@
396 # Expected output, diff is similar to the patch but w/ diff at the top
397 test_expect_success 'setup expected' '
398 echo diff --git a/file b/file >expected &&
399 sed -e "/^index 180b47c/s/ 100644/ 100755/" \
400 -e /1,5/s//1,4/ \
401 -e /Y/d patch >>expected &&
402 cat >expected-output <<-\EOF
403 --- a/file
404 +++ b/file
405 @@ -1,2 +1,4 @@
406 +firstline
407 baseline
408 content
409 +lastline
410 \ No newline at end of file
411 @@ -1,2 +1,3 @@
412 +firstline
413 baseline
414 content
415 @@ -1,2 +2,3 @@
416 baseline
417 content
418 +lastline
419 \ No newline at end of file
420 --- a/file2
421 +++ b/file2
422 @@ -1,4 +1,5 @@
430 @@ -1,2 +1,2 @@
434 @@ -2,2 +2,3 @@
438 @@ -3,2 +4,2 @@
445 # Test splitting the first patch, then adding both
446 test_expect_success 'add first line works' '
447 git commit -am "clear local changes" &&
448 git apply patch &&
449 test_write_lines s y y s y n y | git add -p 2>error >raw-output &&
450 sed -n -e "s/^([1-9]\/[1-9]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
451 -e "/^[-+@ \\\\]"/p raw-output >output &&
452 test_must_be_empty error &&
453 git diff --cached >diff &&
454 diff_cmp expected diff &&
455 test_cmp expected-output output
458 test_expect_success 'setup expected' '
459 cat >expected <<-\EOF
460 diff --git a/non-empty b/non-empty
461 deleted file mode 100644
462 index d95f3ad..0000000
463 --- a/non-empty
464 +++ /dev/null
465 @@ -1 +0,0 @@
466 -content
470 test_expect_success 'deleting a non-empty file' '
471 git reset --hard &&
472 echo content >non-empty &&
473 git add non-empty &&
474 git commit -m non-empty &&
475 rm non-empty &&
476 echo y | git add -p non-empty &&
477 git diff --cached >diff &&
478 diff_cmp expected diff
481 test_expect_success 'setup expected' '
482 cat >expected <<-\EOF
483 diff --git a/empty b/empty
484 deleted file mode 100644
485 index e69de29..0000000
489 test_expect_success 'deleting an empty file' '
490 git reset --hard &&
491 > empty &&
492 git add empty &&
493 git commit -m empty &&
494 rm empty &&
495 echo y | git add -p empty &&
496 git diff --cached >diff &&
497 diff_cmp expected diff
500 test_expect_success 'adding an empty file' '
501 git init added &&
503 cd added &&
504 test_commit initial &&
505 >empty &&
506 git add empty &&
507 test_tick &&
508 git commit -m empty &&
509 git tag added-file &&
510 git reset --hard HEAD^ &&
511 test_path_is_missing empty &&
513 echo y | git checkout -p added-file -- >actual &&
514 test_path_is_file empty &&
515 test_grep "Apply addition to index and worktree" actual
519 test_expect_success 'split hunk setup' '
520 git reset --hard &&
521 test_write_lines 10 20 30 40 50 60 >test &&
522 git add test &&
523 test_tick &&
524 git commit -m test &&
526 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
529 test_expect_success 'goto hunk' '
530 test_when_finished "git reset" &&
531 tr _ " " >expect <<-EOF &&
532 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + 1: -1,2 +1,3 +15
533 _ 2: -2,4 +3,8 +21
534 go to which hunk? @@ -1,2 +1,3 @@
538 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
540 test_write_lines s y g 1 | git add -p >actual &&
541 tail -n 7 <actual >actual.trimmed &&
542 test_cmp expect actual.trimmed
545 test_expect_success 'navigate to hunk via regex' '
546 test_when_finished "git reset" &&
547 tr _ " " >expect <<-EOF &&
548 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -1,2 +1,3 @@
552 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
554 test_write_lines s y /1,2 | git add -p >actual &&
555 tail -n 5 <actual >actual.trimmed &&
556 test_cmp expect actual.trimmed
559 test_expect_success 'split hunk "add -p (edit)"' '
560 # Split, say Edit and do nothing. Then:
562 # 1. Broken version results in a patch that does not apply and
563 # only takes [y/n] (edit again) so the first q is discarded
564 # and then n attempts to discard the edit. Repeat q enough
565 # times to get out.
567 # 2. Correct version applies the (not)edited version, and asks
568 # about the next hunk, against which we say q and program
569 # exits.
570 printf "%s\n" s e q n q q |
571 EDITOR=: git add -p &&
572 git diff >actual &&
573 ! grep "^+15" actual
576 test_expect_success 'split hunk "add -p (no, yes, edit)"' '
577 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
578 git reset &&
579 # test sequence is s(plit), n(o), y(es), e(dit)
580 # q n q q is there to make sure we exit at the end.
581 printf "%s\n" s n y e q n q q |
582 EDITOR=: git add -p 2>error &&
583 test_must_be_empty error &&
584 git diff >actual &&
585 ! grep "^+31" actual
588 test_expect_success 'split hunk with incomplete line at end' '
589 git reset --hard &&
590 printf "missing LF" >>test &&
591 git add test &&
592 test_write_lines before 10 20 30 40 50 60 70 >test &&
593 git grep --cached missing &&
594 test_write_lines s n y q | git add -p &&
595 test_must_fail git grep --cached missing &&
596 git grep before &&
597 test_must_fail git grep --cached before
600 test_expect_success 'edit, adding lines to the first hunk' '
601 test_write_lines 10 11 20 30 40 50 51 60 >test &&
602 git reset &&
603 tr _ " " >patch <<-EOF &&
604 @@ -1,5 +1,6 @@
613 # test sequence is s(plit), e(dit), n(o)
614 # q n q q is there to make sure we exit at the end.
615 printf "%s\n" s e n q n q q |
616 EDITOR=./fake_editor.sh git add -p 2>error &&
617 test_must_be_empty error &&
618 git diff --cached >actual &&
619 grep "^+22" actual
622 test_expect_success 'patch mode ignores unmerged entries' '
623 git reset --hard &&
624 test_commit conflict &&
625 test_commit non-conflict &&
626 git checkout -b side &&
627 test_commit side conflict.t &&
628 git checkout main &&
629 test_commit main conflict.t &&
630 test_must_fail git merge side &&
631 echo changed >non-conflict.t &&
632 echo y | git add -p >output &&
633 ! grep a/conflict.t output &&
634 cat >expected <<-\EOF &&
635 * Unmerged path conflict.t
636 diff --git a/non-conflict.t b/non-conflict.t
637 index f766221..5ea2ed4 100644
638 --- a/non-conflict.t
639 +++ b/non-conflict.t
640 @@ -1 +1 @@
641 -non-conflict
642 +changed
644 git diff --cached >diff &&
645 diff_cmp expected diff
648 test_expect_success 'index is refreshed after applying patch' '
649 git reset --hard &&
650 echo content >test &&
651 printf y | git add -p &&
652 git diff-files --exit-code
655 test_expect_success 'diffs can be colorized' '
656 git reset --hard &&
658 echo content >test &&
659 printf y >y &&
660 force_color git add -p >output 2>&1 <y &&
661 git diff-files --exit-code &&
663 # We do not want to depend on the exact coloring scheme
664 # git uses for diffs, so just check that we saw some kind of color.
665 grep "$(printf "\\033")" output
668 test_expect_success 'colors can be overridden' '
669 git reset --hard &&
670 test_when_finished "git rm -f color-test" &&
671 test_write_lines context old more-context >color-test &&
672 git add color-test &&
673 test_write_lines context new more-context another-one >color-test &&
675 echo trigger an error message >input &&
676 force_color git \
677 -c color.interactive.error=blue \
678 add -i 2>err.raw <input &&
679 test_decode_color <err.raw >err &&
680 grep "<BLUE>Huh (trigger)?<RESET>" err &&
682 test_write_lines help quit >input &&
683 force_color git \
684 -c color.interactive.header=red \
685 -c color.interactive.help=green \
686 -c color.interactive.prompt=yellow \
687 add -i >actual.raw <input &&
688 test_decode_color <actual.raw >actual &&
689 cat >expect <<-\EOF &&
690 <RED> staged unstaged path<RESET>
691 1: +3/-0 +2/-1 color-test
693 <RED>*** Commands ***<RESET>
694 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
695 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
696 <YELLOW>What now<RESET>> <GREEN>status - show paths with changes<RESET>
697 <GREEN>update - add working tree state to the staged set of changes<RESET>
698 <GREEN>revert - revert staged set of changes back to the HEAD version<RESET>
699 <GREEN>patch - pick hunks and update selectively<RESET>
700 <GREEN>diff - view diff between HEAD and index<RESET>
701 <GREEN>add untracked - add contents of untracked files to the staged set of changes<RESET>
702 <RED>*** Commands ***<RESET>
703 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
704 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
705 <YELLOW>What now<RESET>> Bye.
707 test_cmp expect actual &&
709 : exercise recolor_hunk by editing and then look at the hunk again &&
710 test_write_lines s e K q >input &&
711 force_color git \
712 -c color.interactive.prompt=yellow \
713 -c color.diff.meta=italic \
714 -c color.diff.frag=magenta \
715 -c color.diff.context=cyan \
716 -c color.diff.old=bold \
717 -c color.diff.new=blue \
718 -c core.editor=touch \
719 add -p >actual.raw <input &&
720 test_decode_color <actual.raw >actual.decoded &&
721 sed "s/index [0-9a-f]*\\.\\.[0-9a-f]* 100644/<INDEX-LINE>/" <actual.decoded >actual &&
722 cat >expect <<-\EOF &&
723 <ITALIC>diff --git a/color-test b/color-test<RESET>
724 <ITALIC><INDEX-LINE><RESET>
725 <ITALIC>--- a/color-test<RESET>
726 <ITALIC>+++ b/color-test<RESET>
727 <MAGENTA>@@ -1,3 +1,4 @@<RESET>
728 <CYAN> context<RESET>
729 <BOLD>-old<RESET>
730 <BLUE>+<RESET><BLUE>new<RESET>
731 <CYAN> more-context<RESET>
732 <BLUE>+<RESET><BLUE>another-one<RESET>
733 <YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,p,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
734 <MAGENTA>@@ -1,3 +1,3 @@<RESET>
735 <CYAN> context<RESET>
736 <BOLD>-old<RESET>
737 <BLUE>+<RESET><BLUE>new<RESET>
738 <CYAN> more-context<RESET>
739 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
740 <CYAN> more-context<RESET>
741 <BLUE>+<RESET><BLUE>another-one<RESET>
742 <YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
743 <CYAN> context<RESET>
744 <BOLD>-old<RESET>
745 <BLUE>+new<RESET>
746 <CYAN> more-context<RESET>
747 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
749 test_cmp expect actual
752 test_expect_success 'brackets appear without color' '
753 git reset --hard &&
754 test_when_finished "git rm -f bracket-test" &&
755 test_write_lines context old more-context >bracket-test &&
756 git add bracket-test &&
757 test_write_lines context new more-context another-one >bracket-test &&
759 test_write_lines quit >input &&
760 git add -i >actual <input &&
762 sed "s/^|//" >expect <<-\EOF &&
763 | staged unstaged path
764 | 1: +3/-0 +2/-1 bracket-test
766 |*** Commands ***
767 | 1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
768 | 5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
769 |What now> Bye.
772 test_cmp expect actual
775 test_expect_success 'colors can be skipped with color.ui=false' '
776 git reset --hard &&
777 test_when_finished "git rm -f color-test" &&
778 test_write_lines context old more-context >color-test &&
779 git add color-test &&
780 test_write_lines context new more-context another-one >color-test &&
782 test_write_lines help quit >input &&
783 force_color git \
784 -c color.ui=false \
785 add -i >actual.raw <input &&
786 test_decode_color <actual.raw >actual &&
787 test_cmp actual.raw actual
790 test_expect_success 'colorized diffs respect diff.wsErrorHighlight' '
791 git reset --hard &&
793 echo "old " >test &&
794 git add test &&
795 echo "new " >test &&
797 printf y >y &&
798 force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
799 test_decode_color <output.raw >output &&
800 grep "old<" output
803 test_expect_success 'diffFilter filters diff' '
804 git reset --hard &&
806 echo content >test &&
807 test_config interactive.diffFilter "sed s/^/foo:/" &&
808 printf y >y &&
809 force_color git add -p >output 2>&1 <y &&
811 # avoid depending on the exact coloring or content of the prompts,
812 # and just make sure we saw our diff prefixed
813 grep foo:.*content output
816 test_expect_success 'detect bogus diffFilter output' '
817 git reset --hard &&
819 echo content >test &&
820 test_config interactive.diffFilter "sed 6d" &&
821 printf y >y &&
822 force_color test_must_fail git add -p <y >output 2>&1 &&
823 grep "mismatched output" output
826 test_expect_success 'handle iffy colored hunk headers' '
827 git reset --hard &&
829 echo content >test &&
830 printf n >n &&
831 force_color git -c interactive.diffFilter="sed s/.*@@.*/XX/" \
832 add -p >output 2>&1 <n &&
833 grep "^XX$" output
836 test_expect_success 'handle very large filtered diff' '
837 git reset --hard &&
838 # The specific number here is not important, but it must
839 # be large enough that the output of "git diff --color"
840 # fills up the pipe buffer. 10,000 results in ~200k of
841 # colored output.
842 test_seq 10000 >test &&
843 test_config interactive.diffFilter cat &&
844 printf y >y &&
845 force_color git add -p >output 2>&1 <y &&
846 git diff-files --exit-code -- test
849 test_expect_success 'diff.algorithm is passed to `git diff-files`' '
850 git reset --hard &&
852 >file &&
853 git add file &&
854 echo changed >file &&
855 test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
856 test_grep "error: option diff-algorithm accepts " err
859 test_expect_success 'patch-mode via -i prompts for files' '
860 git reset --hard &&
862 echo one >file &&
863 echo two >test &&
864 git add -i <<-\EOF &&
865 patch
866 test
869 quit
872 echo test >expect &&
873 git diff --cached --name-only >actual &&
874 diff_cmp expect actual
877 test_expect_success 'add -p handles globs' '
878 git reset --hard &&
880 mkdir -p subdir &&
881 echo base >one.c &&
882 echo base >subdir/two.c &&
883 git add "*.c" &&
884 git commit -m base &&
886 echo change >one.c &&
887 echo change >subdir/two.c &&
888 git add -p "*.c" <<-\EOF &&
893 cat >expect <<-\EOF &&
894 one.c
895 subdir/two.c
897 git diff --cached --name-only >actual &&
898 test_cmp expect actual
901 test_expect_success 'add -p handles relative paths' '
902 git reset --hard &&
904 echo base >relpath.c &&
905 git add "*.c" &&
906 git commit -m relpath &&
908 echo change >relpath.c &&
909 mkdir -p subdir &&
910 git -C subdir add -p .. 2>error <<-\EOF &&
914 test_must_be_empty error &&
916 cat >expect <<-\EOF &&
917 relpath.c
919 git diff --cached --name-only >actual &&
920 test_cmp expect actual
923 test_expect_success 'add -p does not expand argument lists' '
924 git reset --hard &&
926 echo content >not-changed &&
927 git add not-changed &&
928 git commit -m "add not-changed file" &&
930 echo change >file &&
931 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
935 # we know that "file" must be mentioned since we actually
936 # update it, but we want to be sure that our "." pathspec
937 # was not expanded into the argument list of any command.
938 # So look only for "not-changed".
939 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
942 test_expect_success 'hunk-editing handles custom comment char' '
943 git reset --hard &&
944 echo change >>file &&
945 test_config core.commentChar "\$" &&
946 echo e | GIT_EDITOR=true git add -p &&
947 git diff --exit-code
950 test_expect_success 'add -p works even with color.ui=always' '
951 git reset --hard &&
952 echo change >>file &&
953 test_config color.ui always &&
954 echo y | git add -p &&
955 echo file >expect &&
956 git diff --cached --name-only >actual &&
957 test_cmp expect actual
960 test_expect_success 'setup different kinds of dirty submodules' '
961 test_create_repo for-submodules &&
963 cd for-submodules &&
964 test_commit initial &&
965 test_create_repo dirty-head &&
967 cd dirty-head &&
968 test_commit initial
969 ) &&
970 cp -R dirty-head dirty-otherwise &&
971 cp -R dirty-head dirty-both-ways &&
972 git add dirty-head &&
973 git add dirty-otherwise dirty-both-ways &&
974 git commit -m initial &&
976 cd dirty-head &&
977 test_commit updated &&
978 cd ../dirty-both-ways &&
979 test_commit updated &&
980 echo dirty >>initial &&
981 : >untracked &&
982 cd ../dirty-otherwise &&
983 echo dirty >>initial &&
984 : >untracked
985 ) &&
986 git -C for-submodules diff-files --name-only >actual &&
987 cat >expected <<-\EOF &&
988 dirty-both-ways
989 dirty-head
991 test_cmp expected actual &&
992 git -C for-submodules diff-files --name-only --ignore-submodules=none >actual &&
993 cat >expected <<-\EOF &&
994 dirty-both-ways
995 dirty-head
996 dirty-otherwise
998 test_cmp expected actual &&
999 git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
1000 cat >expected <<-\EOF &&
1001 dirty-both-ways
1002 dirty-head
1004 test_cmp expected actual
1007 test_expect_success 'status ignores dirty submodules (except HEAD)' '
1008 git -C for-submodules add -i </dev/null >output &&
1009 grep dirty-head output &&
1010 grep dirty-both-ways output &&
1011 ! grep dirty-otherwise output
1014 test_expect_success 'handle submodules' '
1015 echo 123 >>for-submodules/dirty-otherwise/initial.t &&
1017 force_color git -C for-submodules add -p dirty-otherwise >output 2>&1 &&
1018 grep "No changes" output &&
1020 force_color git -C for-submodules add -p dirty-head >output 2>&1 <y &&
1021 git -C for-submodules ls-files --stage dirty-head >actual &&
1022 rev="$(git -C for-submodules/dirty-head rev-parse HEAD)" &&
1023 grep "$rev" actual
1026 test_expect_success 'set up pathological context' '
1027 git reset --hard &&
1028 test_write_lines a a a a a a a a a a a >a &&
1029 git add a &&
1030 git commit -m a &&
1031 test_write_lines c b a a a a a a a b a a a a >a &&
1032 test_write_lines a a a a a a a b a a a a >expected-1 &&
1033 test_write_lines b a a a a a a a b a a a a >expected-2 &&
1034 # check editing can cope with missing header and deleted context lines
1035 # as well as changes to other lines
1036 test_write_lines +b " a" >patch
1039 test_expect_success 'add -p works with pathological context lines' '
1040 git reset &&
1041 printf "%s\n" n y |
1042 git add -p &&
1043 git cat-file blob :a >actual &&
1044 test_cmp expected-1 actual
1047 test_expect_success 'add -p patch editing works with pathological context lines' '
1048 git reset &&
1049 # n q q below is in case edit fails
1050 printf "%s\n" e y n q q |
1051 git add -p &&
1052 git cat-file blob :a >actual &&
1053 test_cmp expected-2 actual
1056 test_expect_success 'checkout -p works with pathological context lines' '
1057 test_write_lines a a a a a a >a &&
1058 git add a &&
1059 test_write_lines a b a b a b a b a b a >a &&
1060 test_write_lines s n n y q | git checkout -p &&
1061 test_write_lines a b a b a a b a b a >expect &&
1062 test_cmp expect a
1065 # This should be called from a subshell as it sets a temporary editor
1066 setup_new_file() {
1067 write_script new-file-editor.sh <<-\EOF &&
1068 sed /^#/d "$1" >patch &&
1069 sed /^+c/d patch >"$1"
1071 test_set_editor "$(pwd)/new-file-editor.sh" &&
1072 test_write_lines a b c d e f >new-file &&
1073 test_write_lines a b d e f >new-file-expect &&
1074 test_write_lines "@@ -0,0 +1,6 @@" +a +b +c +d +e +f >patch-expect
1077 test_expect_success 'add -N followed by add -p patch editing' '
1078 git reset --hard &&
1080 setup_new_file &&
1081 git add -N new-file &&
1082 test_write_lines e n q | git add -p &&
1083 git cat-file blob :new-file >actual &&
1084 test_cmp new-file-expect actual &&
1085 test_cmp patch-expect patch
1089 test_expect_success 'checkout -p patch editing of added file' '
1090 git reset --hard &&
1092 setup_new_file &&
1093 git add new-file &&
1094 git commit -m "add new file" &&
1095 git rm new-file &&
1096 git commit -m "remove new file" &&
1097 test_write_lines e n q | git checkout -p HEAD^ &&
1098 test_cmp new-file-expect new-file &&
1099 test_cmp patch-expect patch
1103 test_expect_success 'show help from add--helper' '
1104 git reset --hard &&
1105 cat >expect <<-EOF &&
1107 <BOLD>*** Commands ***<RESET>
1108 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
1109 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1110 <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes<RESET>
1111 <BOLD;RED>update - add working tree state to the staged set of changes<RESET>
1112 <BOLD;RED>revert - revert staged set of changes back to the HEAD version<RESET>
1113 <BOLD;RED>patch - pick hunks and update selectively<RESET>
1114 <BOLD;RED>diff - view diff between HEAD and index<RESET>
1115 <BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
1116 <BOLD>*** Commands ***<RESET>
1117 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
1118 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
1119 <BOLD;BLUE>What now<RESET>>$SP
1120 Bye.
1122 test_write_lines h | force_color git add -i >actual.colored &&
1123 test_decode_color <actual.colored >actual &&
1124 test_cmp expect actual
1127 test_expect_success 'reset -p with unmerged files' '
1128 test_when_finished "git checkout --force main" &&
1129 test_commit one conflict &&
1130 git checkout -B side HEAD^ &&
1131 test_commit two conflict &&
1132 test_must_fail git merge one &&
1134 # this is a noop with only an unmerged entry
1135 git reset -p &&
1137 # add files that sort before and after unmerged entry
1138 echo a >a &&
1139 echo z >z &&
1140 git add a z &&
1142 # confirm that we can reset those files
1143 printf "%s\n" y y | git reset -p &&
1144 git diff-index --cached --diff-filter=u HEAD >staged &&
1145 test_must_be_empty staged
1148 test_done