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