]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3701-add-interactive.sh
add-patch: introduce 'p' in interactive-patch
[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
902f414a
PW
10diff_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
8539b465
JS
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
30force_color () {
4d9e7c15
DL
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 )
8539b465
JS
41}
42
20b813d7
ÆAB
43test_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
a070221e 58test_expect_success 'setup (initial)' '
18bc7616
JK
59 echo content >file &&
60 git add file &&
61 echo more >>file &&
62 echo lines >>file
63'
a070221e 64test_expect_success 'status works (initial)' '
18bc7616
JK
65 git add -i </dev/null >output &&
66 grep "+1/-0 *+2/-0 file" output
67'
f0459319 68
a070221e 69test_expect_success 'setup expected' '
e4d671c6
PW
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
f0459319
ÆAB
78'
79
a070221e 80test_expect_success 'diff works (initial)' '
0590ff26 81 test_write_lines d 1 | git add -i >output &&
18bc7616 82 sed -ne "/new file/,/content/p" <output >diff &&
902f414a 83 diff_cmp expected diff
18bc7616 84'
a070221e 85test_expect_success 'revert works (initial)' '
18bc7616 86 git add file &&
0590ff26 87 test_write_lines r 1 | git add -i &&
18bc7616
JK
88 git ls-files >output &&
89 ! grep . output
90'
91
849e43cc
JS
92test_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
a070221e 101test_expect_success 'setup (commit)' '
18bc7616
JK
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'
a070221e 110test_expect_success 'status works (commit)' '
18bc7616
JK
111 git add -i </dev/null >output &&
112 grep "+1/-0 *+2/-0 file" output
113'
f0459319 114
4788e8b2
JS
115test_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
a070221e 124test_expect_success 'setup expected' '
e4d671c6
PW
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
f0459319
ÆAB
133'
134
a070221e 135test_expect_success 'diff works (commit)' '
0590ff26 136 test_write_lines d 1 | git add -i >output &&
18bc7616 137 sed -ne "/^index/,/content/p" <output >diff &&
902f414a 138 diff_cmp expected diff
18bc7616 139'
a070221e 140test_expect_success 'revert works (commit)' '
18bc7616 141 git add file &&
0590ff26 142 test_write_lines r 1 | git add -i &&
18bc7616
JK
143 git add -i </dev/null >output &&
144 grep "unchanged *+3/-0 file" output
145'
146
a070221e 147test_expect_success 'setup expected' '
e4d671c6
PW
148 cat >expected <<-\EOF
149 EOF
f0459319
ÆAB
150'
151
a070221e 152test_expect_success 'dummy edit works' '
11489a65 153 test_set_editor : &&
0590ff26 154 test_write_lines e a | git add -p &&
ac083c47 155 git diff > diff &&
902f414a 156 diff_cmp expected diff
ac083c47
TR
157'
158
a070221e 159test_expect_success 'setup patch' '
e4d671c6
PW
160 cat >patch <<-\EOF
161 @@ -1,1 +1,4 @@
162 this
163 +patch
164 -does not
165 apply
166 EOF
f0459319
ÆAB
167'
168
a070221e 169test_expect_success 'setup fake editor' '
11489a65 170 write_script "fake_editor.sh" <<-\EOF &&
e4d671c6
PW
171 mv -f "$1" oldpatch &&
172 mv -f patch "$1"
173 EOF
f0459319
ÆAB
174 test_set_editor "$(pwd)/fake_editor.sh"
175'
176
a070221e 177test_expect_success 'bad edit rejected' '
ac083c47 178 git reset &&
0590ff26 179 test_write_lines e n d | git add -p >output &&
ac083c47
TR
180 grep "hunk does not apply" output
181'
182
a070221e 183test_expect_success 'setup patch' '
e4d671c6
PW
184 cat >patch <<-\EOF
185 this patch
186 is garbage
187 EOF
f0459319
ÆAB
188'
189
a070221e 190test_expect_success 'garbage edit rejected' '
ac083c47 191 git reset &&
0590ff26 192 test_write_lines e n d | git add -p >output &&
ac083c47
TR
193 grep "hunk does not apply" output
194'
195
a070221e 196test_expect_success 'setup patch' '
e4d671c6
PW
197 cat >patch <<-\EOF
198 @@ -1,0 +1,0 @@
199 baseline
200 +content
201 +newcontent
202 +lines
203 EOF
f0459319
ÆAB
204'
205
a070221e 206test_expect_success 'setup expected' '
e4d671c6
PW
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
f0459319
ÆAB
219'
220
a070221e 221test_expect_success 'real edit works' '
0590ff26 222 test_write_lines e n d | git add -p &&
ac083c47 223 git diff >output &&
902f414a 224 diff_cmp expected output
ac083c47
TR
225'
226
f4d35a6b
PW
227test_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
233test_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
247test_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
263test_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
a070221e 270test_expect_success 'skip files similarly as commit -a' '
b145b211
PV
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 &&
902f414a 279 diff_cmp expected output &&
b145b211
PV
280 git reset --hard HEAD^
281'
282rm -f .gitignore
283
a070221e 284test_expect_success FILEMODE 'patch does not affect mode' '
b717a627
JK
285 git reset --hard &&
286 echo content >>file &&
287 chmod +x file &&
ca724686 288 printf "n\\ny\\n" | git add -p &&
b717a627
JK
289 git show :file | grep content &&
290 git diff file | grep "new mode"
291'
292
a070221e 293test_expect_success FILEMODE 'stage mode but not hunk' '
ca724686
JK
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
87ca2eaa 302
a070221e 303test_expect_success FILEMODE 'stage mode and hunk' '
87ca2eaa
KS
304 git reset --hard &&
305 echo content >>file &&
306 chmod +x file &&
307 printf "y\\ny\\n" | git add -p &&
9fdc79ec
ÆAB
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
87ca2eaa
KS
313'
314
26ec126a 315# end of tests disabled when filemode is not usable
ca724686 316
24be352d
JS
317test_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 &&
66c14ab5
RJ
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,?]?
24be352d
JS
331 EOF
332 test_cmp expect actual.filtered
333'
334
0c3222c4
JS
335test_expect_success 'correct message when there is nothing to do' '
336 git reset --hard &&
337 git add -p 2>err &&
6789275d 338 test_grep "No changes" err &&
0c3222c4
JS
339 printf "\\0123" >binary &&
340 git add binary &&
341 printf "\\0abc" >binary &&
342 git add -p 2>err &&
6789275d 343 test_grep "Only binary files changed" err
0c3222c4
JS
344'
345
a070221e 346test_expect_success 'setup again' '
32a90233
JS
347 git reset --hard &&
348 test_chmod +x file &&
7008ddc6
PW
349 echo content >>file &&
350 test_write_lines A B C D>file2 &&
351 git add file2
32a90233
JS
352'
353
f67182bf 354# Write the patch file with a new line at the top and bottom
a070221e 355test_expect_success 'setup patch' '
e4d671c6
PW
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
b3e0fcfe 365 \ No newline at end of file
7008ddc6
PW
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
e4d671c6 378 EOF
f0459319
ÆAB
379'
380
b3e0fcfe 381# Expected output, diff is similar to the patch but w/ diff at the top
a070221e 382test_expect_success 'setup expected' '
b3e0fcfe 383 echo diff --git a/file b/file >expected &&
7008ddc6
PW
384 sed -e "/^index 180b47c/s/ 100644/ 100755/" \
385 -e /1,5/s//1,4/ \
386 -e /Y/d patch >>expected &&
b3e0fcfe 387 cat >expected-output <<-\EOF
e4d671c6
PW
388 --- a/file
389 +++ b/file
390 @@ -1,2 +1,4 @@
391 +firstline
392 baseline
393 content
394 +lastline
b3e0fcfe
PW
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
7008ddc6
PW
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
e4d671c6 427 EOF
f0459319
ÆAB
428'
429
f67182bf 430# Test splitting the first patch, then adding both
a926c4b9 431test_expect_success 'add first line works' '
f67182bf
MG
432 git commit -am "clear local changes" &&
433 git apply patch &&
7008ddc6
PW
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/" \
d16632f6 436 -e "/^[-+@ \\\\]"/p raw-output >output &&
b3e0fcfe
PW
437 test_must_be_empty error &&
438 git diff --cached >diff &&
439 diff_cmp expected diff &&
440 test_cmp expected-output output
f67182bf
MG
441'
442
a070221e 443test_expect_success 'setup expected' '
e4d671c6
PW
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
f0459319
ÆAB
453'
454
a070221e 455test_expect_success 'deleting a non-empty file' '
8947fdd5
JK
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 &&
902f414a 463 diff_cmp expected diff
8947fdd5
JK
464'
465
a070221e 466test_expect_success 'setup expected' '
e4d671c6
PW
467 cat >expected <<-\EOF
468 diff --git a/empty b/empty
469 deleted file mode 100644
470 index e69de29..0000000
471 EOF
f0459319 472'
24ab81ae 473
a070221e 474test_expect_success 'deleting an empty file' '
24ab81ae
JK
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 &&
902f414a 482 diff_cmp expected diff
24ab81ae
JK
483'
484
2c8bd847
JS
485test_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 &&
6789275d 500 test_grep "Apply addition to index and worktree" actual
2c8bd847
JS
501 )
502'
503
a070221e 504test_expect_success 'split hunk setup' '
f3217e2b 505 git reset --hard &&
11489a65 506 test_write_lines 10 20 30 40 50 60 >test &&
f3217e2b
JH
507 git add test &&
508 test_tick &&
509 git commit -m test &&
510
11489a65 511 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
f3217e2b
JH
512'
513
9254bdfb
JS
514test_expect_success 'goto hunk' '
515 test_when_finished "git reset" &&
516 tr _ " " >expect <<-EOF &&
66c14ab5 517 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + 1: -1,2 +1,3 +15
9254bdfb
JS
518 _ 2: -2,4 +3,8 +21
519 go to which hunk? @@ -1,2 +1,3 @@
520 _10
521 +15
522 _20
66c14ab5 523 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
9254bdfb
JS
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
d6cf8733
JS
530test_expect_success 'navigate to hunk via regex' '
531 test_when_finished "git reset" &&
532 tr _ " " >expect <<-EOF &&
66c14ab5 533 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -1,2 +1,3 @@
d6cf8733
JS
534 _10
535 +15
536 _20
66c14ab5 537 (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
d6cf8733
JS
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
a070221e 544test_expect_success 'split hunk "add -p (edit)"' '
f3217e2b
JH
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
41ccfdd9 553 # about the next hunk, against which we say q and program
f3217e2b 554 # exits.
416145f0 555 printf "%s\n" s e q n q q |
f3217e2b
JH
556 EDITOR=: git add -p &&
557 git diff >actual &&
558 ! grep "^+15" actual
559'
560
20b813d7 561test_expect_success 'split hunk "add -p (no, yes, edit)"' '
11489a65 562 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
1bf01040
MM
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
510aeca1
JS
573test_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
20b813d7 585test_expect_success 'edit, adding lines to the first hunk' '
0f0fba2c
JS
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
4066bd67
JK
607test_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 &&
cbc75a12
JS
613 git checkout main &&
614 test_commit main conflict.t &&
4066bd67
JK
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 &&
902f414a 630 diff_cmp expected diff
4066bd67
JK
631'
632
dc626415
JK
633test_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
8539b465 640test_expect_success 'diffs can be colorized' '
55cccf4b
JK
641 git reset --hard &&
642
55cccf4b 643 echo content >test &&
8539b465
JS
644 printf y >y &&
645 force_color git add -p >output 2>&1 <y &&
1c6ffb54 646 git diff-files --exit-code &&
55cccf4b
JK
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
96386faa
JS
653test_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>
66c14ab5 718 <YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,p,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
96386faa
JS
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>
66c14ab5 724 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
96386faa
JS
725 <CYAN> more-context<RESET>
726 <BLUE>+<RESET><BLUE>another-one<RESET>
66c14ab5 727 <YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
96386faa
JS
728 <CYAN> context<RESET>
729 <BOLD>-old<RESET>
730 <BLUE>+new<RESET>
731 <CYAN> more-context<RESET>
66c14ab5 732 <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
96386faa
JS
733 EOF
734 test_cmp expect actual
735'
736
6f74648c
DS
737test_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
7cf3b49f
DS
760test_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
da806352
JK
775test_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
8539b465 788test_expect_success 'diffFilter filters diff' '
af3570ed
JK
789 git reset --hard &&
790
791 echo content >test &&
792 test_config interactive.diffFilter "sed s/^/foo:/" &&
8539b465
JS
793 printf y >y &&
794 force_color git add -p >output 2>&1 <y &&
af3570ed
JK
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
8539b465 801test_expect_success 'detect bogus diffFilter output' '
42f7d454
JK
802 git reset --hard &&
803
804 echo content >test &&
b6633a00 805 test_config interactive.diffFilter "sed 6d" &&
8539b465 806 printf y >y &&
b6633a00
JS
807 force_color test_must_fail git add -p <y >output 2>&1 &&
808 grep "mismatched output" output
42f7d454
JK
809'
810
fd3f7f61
JS
811test_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
42f7d454
JK
819'
820
716c1f64
JK
821test_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
e91162be
JS
834test_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 &&
89c85593 840 test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
6789275d 841 test_grep "error: option diff-algorithm accepts " err
e91162be
JS
842'
843
c852bd54
JK
844test_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 &&
902f414a 859 diff_cmp expect actual
c852bd54
JK
860'
861
7288e12c
JK
862test_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
be4dbbbe
PS
886test_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
7288e12c
JK
908test_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".
79336116 924 ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
7288e12c
JK
925'
926
d85d7ecb
JK
927test_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
6be4595e
JK
935test_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
12434efc
NTND
945test_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
8ef93124
SJ
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
12434efc
NTND
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
992test_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
0a101676
JS
999test_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
23fea4c2
PW
1011test_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
fecc6f3a 1024test_expect_success 'add -p works with pathological context lines' '
23fea4c2
PW
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
2b8ea7f3 1032test_expect_success 'add -p patch editing works with pathological context lines' '
23fea4c2
PW
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
2bd69b90
PW
1041test_expect_success 'checkout -p works with pathological context lines' '
1042 test_write_lines a a a a a a >a &&
1043 git add a &&
64d1022e 1044 test_write_lines a b a b a b a b a b a >a &&
2bd69b90
PW
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'
8c159044 1049
75a009dc
PW
1050# This should be called from a subshell as it sets a temporary editor
1051setup_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
1062test_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
1074test_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
8c159044
1088test_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
8539b465 1107 test_write_lines h | force_color git add -i >actual.colored &&
8c159044 1108 test_decode_color <actual.colored >actual &&
1108cea7 1109 test_cmp expect actual
8c159044
1110'
1111
28d1122f
JK
1112test_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
18bc7616 1133test_done