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