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