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