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