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