]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3701-add-interactive.sh
color_parse_mem: allow empty color spec
[thirdparty/git.git] / t / t3701-add-interactive.sh
CommitLineData
18bc7616
JK
1#!/bin/sh
2
3test_description='add -i basic tests'
4. ./test-lib.sh
5
a070221e
JN
6if ! test_have_prereq PERL
7then
8 skip_all='skipping add -i tests, perl not available'
9 test_done
10fi
11
12test_expect_success 'setup (initial)' '
18bc7616
JK
13 echo content >file &&
14 git add file &&
15 echo more >>file &&
16 echo lines >>file
17'
a070221e 18test_expect_success 'status works (initial)' '
18bc7616
JK
19 git add -i </dev/null >output &&
20 grep "+1/-0 *+2/-0 file" output
21'
f0459319 22
a070221e 23test_expect_success 'setup expected' '
18bc7616
JK
24cat >expected <<EOF
25new file mode 100644
26index 0000000..d95f3ad
27--- /dev/null
28+++ b/file
29@@ -0,0 +1 @@
30+content
31EOF
f0459319
ÆAB
32'
33
a070221e 34test_expect_success 'diff works (initial)' '
18bc7616
JK
35 (echo d; echo 1) | git add -i >output &&
36 sed -ne "/new file/,/content/p" <output >diff &&
82ebb0b6 37 test_cmp expected diff
18bc7616 38'
a070221e 39test_expect_success 'revert works (initial)' '
18bc7616
JK
40 git add file &&
41 (echo r; echo 1) | git add -i &&
42 git ls-files >output &&
43 ! grep . output
44'
45
a070221e 46test_expect_success 'setup (commit)' '
18bc7616
JK
47 echo baseline >file &&
48 git add file &&
49 git commit -m commit &&
50 echo content >>file &&
51 git add file &&
52 echo more >>file &&
53 echo lines >>file
54'
a070221e 55test_expect_success 'status works (commit)' '
18bc7616
JK
56 git add -i </dev/null >output &&
57 grep "+1/-0 *+2/-0 file" output
58'
f0459319 59
a070221e 60test_expect_success 'setup expected' '
18bc7616
JK
61cat >expected <<EOF
62index 180b47c..b6f2c08 100644
63--- a/file
64+++ b/file
65@@ -1 +1,2 @@
66 baseline
67+content
68EOF
f0459319
ÆAB
69'
70
a070221e 71test_expect_success 'diff works (commit)' '
18bc7616
JK
72 (echo d; echo 1) | git add -i >output &&
73 sed -ne "/^index/,/content/p" <output >diff &&
82ebb0b6 74 test_cmp expected diff
18bc7616 75'
a070221e 76test_expect_success 'revert works (commit)' '
18bc7616
JK
77 git add file &&
78 (echo r; echo 1) | git add -i &&
79 git add -i </dev/null >output &&
80 grep "unchanged *+3/-0 file" output
81'
82
f0459319 83
a070221e 84test_expect_success 'setup expected' '
ac083c47
TR
85cat >expected <<EOF
86EOF
f0459319
ÆAB
87'
88
a070221e 89test_expect_success 'setup fake editor' '
8fe6177a 90 >fake_editor.sh &&
f0459319 91 chmod a+x fake_editor.sh &&
8fe6177a 92 test_set_editor "$(pwd)/fake_editor.sh"
f0459319
ÆAB
93'
94
a070221e 95test_expect_success 'dummy edit works' '
ac083c47
TR
96 (echo e; echo a) | git add -p &&
97 git diff > diff &&
98 test_cmp expected diff
99'
100
a070221e 101test_expect_success 'setup patch' '
ac083c47
TR
102cat >patch <<EOF
103@@ -1,1 +1,4 @@
104 this
105+patch
f0459319 106-does not
ac083c47
TR
107 apply
108EOF
f0459319
ÆAB
109'
110
a070221e 111test_expect_success 'setup fake editor' '
f0459319
ÆAB
112 echo "#!$SHELL_PATH" >fake_editor.sh &&
113 cat >>fake_editor.sh <<\EOF &&
ac083c47
TR
114mv -f "$1" oldpatch &&
115mv -f patch "$1"
116EOF
f0459319
ÆAB
117 chmod a+x fake_editor.sh &&
118 test_set_editor "$(pwd)/fake_editor.sh"
119'
120
a070221e 121test_expect_success 'bad edit rejected' '
ac083c47
TR
122 git reset &&
123 (echo e; echo n; echo d) | git add -p >output &&
124 grep "hunk does not apply" output
125'
126
a070221e 127test_expect_success 'setup patch' '
ac083c47
TR
128cat >patch <<EOF
129this patch
130is garbage
131EOF
f0459319
ÆAB
132'
133
a070221e 134test_expect_success 'garbage edit rejected' '
ac083c47
TR
135 git reset &&
136 (echo e; echo n; echo d) | git add -p >output &&
137 grep "hunk does not apply" output
138'
139
a070221e 140test_expect_success 'setup patch' '
ac083c47
TR
141cat >patch <<EOF
142@@ -1,0 +1,0 @@
143 baseline
144+content
145+newcontent
146+lines
147EOF
f0459319
ÆAB
148'
149
a070221e 150test_expect_success 'setup expected' '
ac083c47
TR
151cat >expected <<EOF
152diff --git a/file b/file
153index b5dd6c9..f910ae9 100644
154--- a/file
155+++ b/file
156@@ -1,4 +1,4 @@
157 baseline
158 content
159-newcontent
160+more
161 lines
162EOF
f0459319
ÆAB
163'
164
a070221e 165test_expect_success 'real edit works' '
ac083c47
TR
166 (echo e; echo n; echo d) | git add -p &&
167 git diff >output &&
168 test_cmp expected output
169'
170
a070221e 171test_expect_success 'skip files similarly as commit -a' '
b145b211
PV
172 git reset &&
173 echo file >.gitignore &&
174 echo changed >file &&
175 echo y | git add -p file &&
176 git diff >output &&
177 git reset &&
178 git commit -am commit &&
179 git diff >expected &&
180 test_cmp expected output &&
181 git reset --hard HEAD^
182'
183rm -f .gitignore
184
a070221e 185test_expect_success FILEMODE 'patch does not affect mode' '
b717a627
JK
186 git reset --hard &&
187 echo content >>file &&
188 chmod +x file &&
ca724686 189 printf "n\\ny\\n" | git add -p &&
b717a627
JK
190 git show :file | grep content &&
191 git diff file | grep "new mode"
192'
193
a070221e 194test_expect_success FILEMODE 'stage mode but not hunk' '
ca724686
JK
195 git reset --hard &&
196 echo content >>file &&
197 chmod +x file &&
198 printf "y\\nn\\n" | git add -p &&
199 git diff --cached file | grep "new mode" &&
200 git diff file | grep "+content"
201'
202
87ca2eaa 203
a070221e 204test_expect_success FILEMODE 'stage mode and hunk' '
87ca2eaa
KS
205 git reset --hard &&
206 echo content >>file &&
207 chmod +x file &&
208 printf "y\\ny\\n" | git add -p &&
209 git diff --cached file | grep "new mode" &&
210 git diff --cached file | grep "+content" &&
211 test -z "$(git diff file)"
212'
213
26ec126a 214# end of tests disabled when filemode is not usable
ca724686 215
a070221e 216test_expect_success 'setup again' '
32a90233
JS
217 git reset --hard &&
218 test_chmod +x file &&
219 echo content >>file
220'
221
f67182bf 222# Write the patch file with a new line at the top and bottom
a070221e 223test_expect_success 'setup patch' '
f67182bf
MG
224cat >patch <<EOF
225index 180b47c..b6f2c08 100644
226--- a/file
227+++ b/file
228@@ -1,2 +1,4 @@
229+firstline
230 baseline
231 content
232+lastline
233EOF
f0459319
ÆAB
234'
235
f67182bf 236# Expected output, similar to the patch but w/ diff at the top
a070221e 237test_expect_success 'setup expected' '
f67182bf
MG
238cat >expected <<EOF
239diff --git a/file b/file
240index b6f2c08..61b9053 100755
241--- a/file
242+++ b/file
243@@ -1,2 +1,4 @@
244+firstline
245 baseline
246 content
247+lastline
248EOF
f0459319
ÆAB
249'
250
f67182bf 251# Test splitting the first patch, then adding both
a070221e 252test_expect_success 'add first line works' '
f67182bf
MG
253 git commit -am "clear local changes" &&
254 git apply patch &&
255 (echo s; echo y; echo y) | git add -p file &&
256 git diff --cached > diff &&
257 test_cmp expected diff
258'
259
a070221e 260test_expect_success 'setup expected' '
8947fdd5
JK
261cat >expected <<EOF
262diff --git a/non-empty b/non-empty
263deleted file mode 100644
264index d95f3ad..0000000
265--- a/non-empty
266+++ /dev/null
267@@ -1 +0,0 @@
268-content
269EOF
f0459319
ÆAB
270'
271
a070221e 272test_expect_success 'deleting a non-empty file' '
8947fdd5
JK
273 git reset --hard &&
274 echo content >non-empty &&
275 git add non-empty &&
276 git commit -m non-empty &&
277 rm non-empty &&
278 echo y | git add -p non-empty &&
279 git diff --cached >diff &&
280 test_cmp expected diff
281'
282
a070221e 283test_expect_success 'setup expected' '
24ab81ae
JK
284cat >expected <<EOF
285diff --git a/empty b/empty
286deleted file mode 100644
287index e69de29..0000000
288EOF
f0459319 289'
24ab81ae 290
a070221e 291test_expect_success 'deleting an empty file' '
24ab81ae
JK
292 git reset --hard &&
293 > empty &&
294 git add empty &&
295 git commit -m empty &&
296 rm empty &&
297 echo y | git add -p empty &&
298 git diff --cached >diff &&
299 test_cmp expected diff
300'
301
a070221e 302test_expect_success 'split hunk setup' '
f3217e2b
JH
303 git reset --hard &&
304 for i in 10 20 30 40 50 60
305 do
306 echo $i
307 done >test &&
308 git add test &&
309 test_tick &&
310 git commit -m test &&
311
312 for i in 10 15 20 21 22 23 24 30 40 50 60
313 do
314 echo $i
315 done >test
316'
317
a070221e 318test_expect_success 'split hunk "add -p (edit)"' '
f3217e2b
JH
319 # Split, say Edit and do nothing. Then:
320 #
321 # 1. Broken version results in a patch that does not apply and
322 # only takes [y/n] (edit again) so the first q is discarded
323 # and then n attempts to discard the edit. Repeat q enough
324 # times to get out.
325 #
326 # 2. Correct version applies the (not)edited version, and asks
41ccfdd9 327 # about the next hunk, against which we say q and program
f3217e2b 328 # exits.
416145f0 329 printf "%s\n" s e q n q q |
f3217e2b
JH
330 EDITOR=: git add -p &&
331 git diff >actual &&
332 ! grep "^+15" actual
333'
334
1bf01040
MM
335test_expect_failure 'split hunk "add -p (no, yes, edit)"' '
336 cat >test <<-\EOF &&
337 5
338 10
339 20
340 21
341 30
342 31
343 40
344 50
345 60
346 EOF
347 git reset &&
348 # test sequence is s(plit), n(o), y(es), e(dit)
349 # q n q q is there to make sure we exit at the end.
350 printf "%s\n" s n y e q n q q |
351 EDITOR=: git add -p 2>error &&
352 test_must_be_empty error &&
353 git diff >actual &&
354 ! grep "^+31" actual
355'
356
4066bd67
JK
357test_expect_success 'patch mode ignores unmerged entries' '
358 git reset --hard &&
359 test_commit conflict &&
360 test_commit non-conflict &&
361 git checkout -b side &&
362 test_commit side conflict.t &&
363 git checkout master &&
364 test_commit master conflict.t &&
365 test_must_fail git merge side &&
366 echo changed >non-conflict.t &&
367 echo y | git add -p >output &&
368 ! grep a/conflict.t output &&
369 cat >expected <<-\EOF &&
370 * Unmerged path conflict.t
371 diff --git a/non-conflict.t b/non-conflict.t
372 index f766221..5ea2ed4 100644
373 --- a/non-conflict.t
374 +++ b/non-conflict.t
375 @@ -1 +1 @@
376 -non-conflict
377 +changed
378 EOF
379 git diff --cached >diff &&
380 test_cmp expected diff
381'
382
55cccf4b
JK
383test_expect_success 'diffs can be colorized' '
384 git reset --hard &&
385
386 # force color even though the test script has no terminal
387 test_config color.ui always &&
388
389 echo content >test &&
390 printf y | git add -p >output 2>&1 &&
391
392 # We do not want to depend on the exact coloring scheme
393 # git uses for diffs, so just check that we saw some kind of color.
394 grep "$(printf "\\033")" output
395'
396
18bc7616 397test_done