1#!/bin/sh
2
3test_description='add -i basic tests'
4. ./test-lib.sh
5. "$TEST_DIRECTORY"/lib-terminal.sh
6
7if ! test_have_prereq PERL
8then
9 skip_all='skipping add -i tests, perl not available'
10 test_done
11fi
12
13diff_cmp () {
14 for x
15 do
16 sed -e '/^index/s/[0-9a-f]*[1-9a-f][0-9a-f]*\.\./1234567../' \
17 -e '/^index/s/\.\.[0-9a-f]*[1-9a-f][0-9a-f]*/..9abcdef/' \
18 -e '/^index/s/ 00*\.\./ 0000000../' \
19 -e '/^index/s/\.\.00*$/..0000000/' \
20 -e '/^index/s/\.\.00* /..0000000 /' \
21 "$x" >"$x.filtered"
22 done
23 test_cmp "$1.filtered" "$2.filtered"
24}
25
26test_expect_success 'setup (initial)' '
27 echo content >file &&
28 git add file &&
29 echo more >>file &&
30 echo lines >>file
31'
32test_expect_success 'status works (initial)' '
33 git add -i </dev/null >output &&
34 grep "+1/-0 *+2/-0 file" output
35'
36
37test_expect_success 'setup expected' '
38 cat >expected <<-\EOF
39 new file mode 100644
40 index 0000000..d95f3ad
41 --- /dev/null
42 +++ b/file
43 @@ -0,0 +1 @@
44 +content
45 EOF
46'
47
48test_expect_success 'diff works (initial)' '
49 (echo d; echo 1) | git add -i >output &&
50 sed -ne "/new file/,/content/p" <output >diff &&
51 diff_cmp expected diff
52'
53test_expect_success 'revert works (initial)' '
54 git add file &&
55 (echo r; echo 1) | git add -i &&
56 git ls-files >output &&
57 ! grep . output
58'
59
60test_expect_success 'setup (commit)' '
61 echo baseline >file &&
62 git add file &&
63 git commit -m commit &&
64 echo content >>file &&
65 git add file &&
66 echo more >>file &&
67 echo lines >>file
68'
69test_expect_success 'status works (commit)' '
70 git add -i </dev/null >output &&
71 grep "+1/-0 *+2/-0 file" output
72'
73
74test_expect_success 'setup expected' '
75 cat >expected <<-\EOF
76 index 180b47c..b6f2c08 100644
77 --- a/file
78 +++ b/file
79 @@ -1 +1,2 @@
80 baseline
81 +content
82 EOF
83'
84
85test_expect_success 'diff works (commit)' '
86 (echo d; echo 1) | git add -i >output &&
87 sed -ne "/^index/,/content/p" <output >diff &&
88 diff_cmp expected diff
89'
90test_expect_success 'revert works (commit)' '
91 git add file &&
92 (echo r; echo 1) | git add -i &&
93 git add -i </dev/null >output &&
94 grep "unchanged *+3/-0 file" output
95'
96
97
98test_expect_success 'setup expected' '
99 cat >expected <<-\EOF
100 EOF
101'
102
103test_expect_success 'dummy edit works' '
104 test_set_editor : &&
105 (echo e; echo a) | git add -p &&
106 git diff > diff &&
107 diff_cmp expected diff
108'
109
110test_expect_success 'setup patch' '
111 cat >patch <<-\EOF
112 @@ -1,1 +1,4 @@
113 this
114 +patch
115 -does not
116 apply
117 EOF
118'
119
120test_expect_success 'setup fake editor' '
121 write_script "fake_editor.sh" <<-\EOF &&
122 mv -f "$1" oldpatch &&
123 mv -f patch "$1"
124 EOF
125 test_set_editor "$(pwd)/fake_editor.sh"
126'
127
128test_expect_success 'bad edit rejected' '
129 git reset &&
130 (echo e; echo n; echo d) | git add -p >output &&
131 grep "hunk does not apply" output
132'
133
134test_expect_success 'setup patch' '
135 cat >patch <<-\EOF
136 this patch
137 is garbage
138 EOF
139'
140
141test_expect_success 'garbage edit rejected' '
142 git reset &&
143 (echo e; echo n; echo d) | git add -p >output &&
144 grep "hunk does not apply" output
145'
146
147test_expect_success 'setup patch' '
148 cat >patch <<-\EOF
149 @@ -1,0 +1,0 @@
150 baseline
151 +content
152 +newcontent
153 +lines
154 EOF
155'
156
157test_expect_success 'setup expected' '
158 cat >expected <<-\EOF
159 diff --git a/file b/file
160 index b5dd6c9..f910ae9 100644
161 --- a/file
162 +++ b/file
163 @@ -1,4 +1,4 @@
164 baseline
165 content
166 -newcontent
167 +more
168 lines
169 EOF
170'
171
172test_expect_success 'real edit works' '
173 (echo e; echo n; echo d) | git add -p &&
174 git diff >output &&
175 diff_cmp expected output
176'
177
178test_expect_success 'setup file' '
179 test_write_lines a "" b "" c >file &&
180 git add file &&
181 test_write_lines a "" d "" c >file
182'
183
184test_expect_success 'setup patch' '
185 SP=" " &&
186 NULL="" &&
187 cat >patch <<-EOF
188 @@ -1,4 +1,4 @@
189 a
190 $NULL
191 -b
192 +f
193 $SP
194 c
195 EOF
196'
197
198test_expect_success 'setup expected' '
199 cat >expected <<-EOF
200 diff --git a/file b/file
201 index b5dd6c9..f910ae9 100644
202 --- a/file
203 +++ b/file
204 @@ -1,5 +1,5 @@
205 a
206 $SP
207 -f
208 +d
209 $SP
210 c
211 EOF
212'
213
214test_expect_success 'edit can strip spaces from empty context lines' '
215 test_write_lines e n q | git add -p 2>error &&
216 test_must_be_empty error &&
217 git diff >output &&
218 diff_cmp expected output
219'
220
221test_expect_success 'skip files similarly as commit -a' '
222 git reset &&
223 echo file >.gitignore &&
224 echo changed >file &&
225 echo y | git add -p file &&
226 git diff >output &&
227 git reset &&
228 git commit -am commit &&
229 git diff >expected &&
230 diff_cmp expected output &&
231 git reset --hard HEAD^
232'
233rm -f .gitignore
234
235test_expect_success FILEMODE 'patch does not affect mode' '
236 git reset --hard &&
237 echo content >>file &&
238 chmod +x file &&
239 printf "n\\ny\\n" | git add -p &&
240 git show :file | grep content &&
241 git diff file | grep "new mode"
242'
243
244test_expect_success FILEMODE 'stage mode but not hunk' '
245 git reset --hard &&
246 echo content >>file &&
247 chmod +x file &&
248 printf "y\\nn\\n" | git add -p &&
249 git diff --cached file | grep "new mode" &&
250 git diff file | grep "+content"
251'
252
253
254test_expect_success FILEMODE 'stage mode and hunk' '
255 git reset --hard &&
256 echo content >>file &&
257 chmod +x file &&
258 printf "y\\ny\\n" | git add -p &&
259 git diff --cached file | grep "new mode" &&
260 git diff --cached file | grep "+content" &&
261 test -z "$(git diff file)"
262'
263
264# end of tests disabled when filemode is not usable
265
266test_expect_success 'setup again' '
267 git reset --hard &&
268 test_chmod +x file &&
269 echo content >>file
270'
271
272# Write the patch file with a new line at the top and bottom
273test_expect_success 'setup patch' '
274 cat >patch <<-\EOF
275 index 180b47c..b6f2c08 100644
276 --- a/file
277 +++ b/file
278 @@ -1,2 +1,4 @@
279 +firstline
280 baseline
281 content
282 +lastline
283 \ No newline at end of file
284 EOF
285'
286
287# Expected output, diff is similar to the patch but w/ diff at the top
288test_expect_success 'setup expected' '
289 echo diff --git a/file b/file >expected &&
290 cat patch |sed "/^index/s/ 100644/ 100755/" >>expected &&
291 cat >expected-output <<-\EOF
292 --- a/file
293 +++ b/file
294 @@ -1,2 +1,4 @@
295 +firstline
296 baseline
297 content
298 +lastline
299 \ No newline at end of file
300 @@ -1,2 +1,3 @@
301 +firstline
302 baseline
303 content
304 @@ -1,2 +2,3 @@
305 baseline
306 content
307 +lastline
308 \ No newline at end of file
309 EOF
310'
311
312# Test splitting the first patch, then adding both
313test_expect_success C_LOCALE_OUTPUT 'add first line works' '
314 git commit -am "clear local changes" &&
315 git apply patch &&
316 printf "%s\n" s y y | git add -p file 2>error |
317 sed -n -e "s/^Stage this hunk[^@]*\(@@ .*\)/\1/" \
318 -e "/^[-+@ \\\\]"/p >output &&
319 test_must_be_empty error &&
320 git diff --cached >diff &&
321 diff_cmp expected diff &&
322 test_cmp expected-output output
323'
324
325test_expect_success 'setup expected' '
326 cat >expected <<-\EOF
327 diff --git a/non-empty b/non-empty
328 deleted file mode 100644
329 index d95f3ad..0000000
330 --- a/non-empty
331 +++ /dev/null
332 @@ -1 +0,0 @@
333 -content
334 EOF
335'
336
337test_expect_success 'deleting a non-empty file' '
338 git reset --hard &&
339 echo content >non-empty &&
340 git add non-empty &&
341 git commit -m non-empty &&
342 rm non-empty &&
343 echo y | git add -p non-empty &&
344 git diff --cached >diff &&
345 diff_cmp expected diff
346'
347
348test_expect_success 'setup expected' '
349 cat >expected <<-\EOF
350 diff --git a/empty b/empty
351 deleted file mode 100644
352 index e69de29..0000000
353 EOF
354'
355
356test_expect_success 'deleting an empty file' '
357 git reset --hard &&
358 > empty &&
359 git add empty &&
360 git commit -m empty &&
361 rm empty &&
362 echo y | git add -p empty &&
363 git diff --cached >diff &&
364 diff_cmp expected diff
365'
366
367test_expect_success 'split hunk setup' '
368 git reset --hard &&
369 test_write_lines 10 20 30 40 50 60 >test &&
370 git add test &&
371 test_tick &&
372 git commit -m test &&
373
374 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
375'
376
377test_expect_success 'split hunk "add -p (edit)"' '
378 # Split, say Edit and do nothing. Then:
379 #
380 # 1. Broken version results in a patch that does not apply and
381 # only takes [y/n] (edit again) so the first q is discarded
382 # and then n attempts to discard the edit. Repeat q enough
383 # times to get out.
384 #
385 # 2. Correct version applies the (not)edited version, and asks
386 # about the next hunk, against which we say q and program
387 # exits.
388 printf "%s\n" s e q n q q |
389 EDITOR=: git add -p &&
390 git diff >actual &&
391 ! grep "^+15" actual
392'
393
394test_expect_failure 'split hunk "add -p (no, yes, edit)"' '
395 test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
396 git reset &&
397 # test sequence is s(plit), n(o), y(es), e(dit)
398 # q n q q is there to make sure we exit at the end.
399 printf "%s\n" s n y e q n q q |
400 EDITOR=: git add -p 2>error &&
401 test_must_be_empty error &&
402 git diff >actual &&
403 ! grep "^+31" actual
404'
405
406test_expect_success 'patch mode ignores unmerged entries' '
407 git reset --hard &&
408 test_commit conflict &&
409 test_commit non-conflict &&
410 git checkout -b side &&
411 test_commit side conflict.t &&
412 git checkout master &&
413 test_commit master conflict.t &&
414 test_must_fail git merge side &&
415 echo changed >non-conflict.t &&
416 echo y | git add -p >output &&
417 ! grep a/conflict.t output &&
418 cat >expected <<-\EOF &&
419 * Unmerged path conflict.t
420 diff --git a/non-conflict.t b/non-conflict.t
421 index f766221..5ea2ed4 100644
422 --- a/non-conflict.t
423 +++ b/non-conflict.t
424 @@ -1 +1 @@
425 -non-conflict
426 +changed
427 EOF
428 git diff --cached >diff &&
429 diff_cmp expected diff
430'
431
432test_expect_success TTY 'diffs can be colorized' '
433 git reset --hard &&
434
435 echo content >test &&
436 printf y | test_terminal git add -p >output 2>&1 &&
437
438 # We do not want to depend on the exact coloring scheme
439 # git uses for diffs, so just check that we saw some kind of color.
440 grep "$(printf "\\033")" output
441'
442
443test_expect_success 'patch-mode via -i prompts for files' '
444 git reset --hard &&
445
446 echo one >file &&
447 echo two >test &&
448 git add -i <<-\EOF &&
449 patch
450 test
451
452 y
453 quit
454 EOF
455
456 echo test >expect &&
457 git diff --cached --name-only >actual &&
458 diff_cmp expect actual
459'
460
461test_expect_success 'add -p handles globs' '
462 git reset --hard &&
463
464 mkdir -p subdir &&
465 echo base >one.c &&
466 echo base >subdir/two.c &&
467 git add "*.c" &&
468 git commit -m base &&
469
470 echo change >one.c &&
471 echo change >subdir/two.c &&
472 git add -p "*.c" <<-\EOF &&
473 y
474 y
475 EOF
476
477 cat >expect <<-\EOF &&
478 one.c
479 subdir/two.c
480 EOF
481 git diff --cached --name-only >actual &&
482 test_cmp expect actual
483'
484
485test_expect_success 'add -p handles relative paths' '
486 git reset --hard &&
487
488 echo base >relpath.c &&
489 git add "*.c" &&
490 git commit -m relpath &&
491
492 echo change >relpath.c &&
493 mkdir -p subdir &&
494 git -C subdir add -p .. 2>error <<-\EOF &&
495 y
496 EOF
497
498 test_must_be_empty error &&
499
500 cat >expect <<-\EOF &&
501 relpath.c
502 EOF
503 git diff --cached --name-only >actual &&
504 test_cmp expect actual
505'
506
507test_expect_success 'add -p does not expand argument lists' '
508 git reset --hard &&
509
510 echo content >not-changed &&
511 git add not-changed &&
512 git commit -m "add not-changed file" &&
513
514 echo change >file &&
515 GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
516 y
517 EOF
518
519 # we know that "file" must be mentioned since we actually
520 # update it, but we want to be sure that our "." pathspec
521 # was not expanded into the argument list of any command.
522 # So look only for "not-changed".
523 ! grep not-changed trace.out
524'
525
526test_expect_success 'hunk-editing handles custom comment char' '
527 git reset --hard &&
528 echo change >>file &&
529 test_config core.commentChar "\$" &&
530 echo e | GIT_EDITOR=true git add -p &&
531 git diff --exit-code
532'
533
534test_expect_success 'add -p works even with color.ui=always' '
535 git reset --hard &&
536 echo change >>file &&
537 test_config color.ui always &&
538 echo y | git add -p &&
539 echo file >expect &&
540 git diff --cached --name-only >actual &&
541 test_cmp expect actual
542'
543
544test_expect_success 'set up pathological context' '
545 git reset --hard &&
546 test_write_lines a a a a a a a a a a a >a &&
547 git add a &&
548 git commit -m a &&
549 test_write_lines c b a a a a a a a b a a a a >a &&
550 test_write_lines a a a a a a a b a a a a >expected-1 &&
551 test_write_lines b a a a a a a a b a a a a >expected-2 &&
552 # check editing can cope with missing header and deleted context lines
553 # as well as changes to other lines
554 test_write_lines +b " a" >patch
555'
556
557test_expect_success 'add -p works with pathological context lines' '
558 git reset &&
559 printf "%s\n" n y |
560 git add -p &&
561 git cat-file blob :a >actual &&
562 test_cmp expected-1 actual
563'
564
565test_expect_success 'add -p patch editing works with pathological context lines' '
566 git reset &&
567 # n q q below is in case edit fails
568 printf "%s\n" e y n q q |
569 git add -p &&
570 git cat-file blob :a >actual &&
571 test_cmp expected-2 actual
572'
573
574test_done