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