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