1#!/bin/sh
2
3test_description='add -i basic tests'
4. ./test-lib.sh
5. "$TEST_DIRECTORY"/lib-prereq-FILEMODE.sh
6
7test_expect_success PERL 'setup (initial)' '
8 echo content >file &&
9 git add file &&
10 echo more >>file &&
11 echo lines >>file
12'
13test_expect_success PERL 'status works (initial)' '
14 git add -i </dev/null >output &&
15 grep "+1/-0 *+2/-0 file" output
16'
17
18test_expect_success PERL 'setup expected' '
19cat >expected <<EOF
20new file mode 100644
21index 0000000..d95f3ad
22--- /dev/null
23+++ b/file
24@@ -0,0 +1 @@
25+content
26EOF
27'
28
29test_expect_success PERL 'diff works (initial)' '
30 (echo d; echo 1) | git add -i >output &&
31 sed -ne "/new file/,/content/p" <output >diff &&
32 test_cmp expected diff
33'
34test_expect_success PERL 'revert works (initial)' '
35 git add file &&
36 (echo r; echo 1) | git add -i &&
37 git ls-files >output &&
38 ! grep . output
39'
40
41test_expect_success PERL 'setup (commit)' '
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'
50test_expect_success PERL 'status works (commit)' '
51 git add -i </dev/null >output &&
52 grep "+1/-0 *+2/-0 file" output
53'
54
55test_expect_success PERL 'setup expected' '
56cat >expected <<EOF
57index 180b47c..b6f2c08 100644
58--- a/file
59+++ b/file
60@@ -1 +1,2 @@
61 baseline
62+content
63EOF
64'
65
66test_expect_success PERL 'diff works (commit)' '
67 (echo d; echo 1) | git add -i >output &&
68 sed -ne "/^index/,/content/p" <output >diff &&
69 test_cmp expected diff
70'
71test_expect_success PERL 'revert works (commit)' '
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
78
79test_expect_success PERL 'setup expected' '
80cat >expected <<EOF
81EOF
82'
83
84test_expect_success PERL 'setup fake editor' '
85 >fake_editor.sh &&
86 chmod a+x fake_editor.sh &&
87 test_set_editor "$(pwd)/fake_editor.sh"
88'
89
90test_expect_success PERL 'dummy edit works' '
91 (echo e; echo a) | git add -p &&
92 git diff > diff &&
93 test_cmp expected diff
94'
95
96test_expect_success PERL 'setup patch' '
97cat >patch <<EOF
98@@ -1,1 +1,4 @@
99 this
100+patch
101-does not
102 apply
103EOF
104'
105
106test_expect_success PERL 'setup fake editor' '
107 echo "#!$SHELL_PATH" >fake_editor.sh &&
108 cat >>fake_editor.sh <<\EOF &&
109mv -f "$1" oldpatch &&
110mv -f patch "$1"
111EOF
112 chmod a+x fake_editor.sh &&
113 test_set_editor "$(pwd)/fake_editor.sh"
114'
115
116test_expect_success PERL 'bad edit rejected' '
117 git reset &&
118 (echo e; echo n; echo d) | git add -p >output &&
119 grep "hunk does not apply" output
120'
121
122test_expect_success PERL 'setup patch' '
123cat >patch <<EOF
124this patch
125is garbage
126EOF
127'
128
129test_expect_success PERL 'garbage edit rejected' '
130 git reset &&
131 (echo e; echo n; echo d) | git add -p >output &&
132 grep "hunk does not apply" output
133'
134
135test_expect_success PERL 'setup patch' '
136cat >patch <<EOF
137@@ -1,0 +1,0 @@
138 baseline
139+content
140+newcontent
141+lines
142EOF
143'
144
145test_expect_success PERL 'setup expected' '
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
158'
159
160test_expect_success PERL 'real edit works' '
161 (echo e; echo n; echo d) | git add -p &&
162 git diff >output &&
163 test_cmp expected output
164'
165
166test_expect_success PERL 'skip files similarly as commit -a' '
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
180test_expect_success PERL,FILEMODE 'patch does not affect mode' '
181 git reset --hard &&
182 echo content >>file &&
183 chmod +x file &&
184 printf "n\\ny\\n" | git add -p &&
185 git show :file | grep content &&
186 git diff file | grep "new mode"
187'
188
189test_expect_success PERL,FILEMODE 'stage mode but not hunk' '
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
198
199test_expect_success PERL,FILEMODE 'stage mode and hunk' '
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
209# end of tests disabled when filemode is not usable
210
211test_expect_success PERL 'setup again' '
212 git reset --hard &&
213 test_chmod +x file &&
214 echo content >>file
215'
216
217# Write the patch file with a new line at the top and bottom
218test_expect_success PERL 'setup patch' '
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
229'
230
231# Expected output, similar to the patch but w/ diff at the top
232test_expect_success PERL 'setup expected' '
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
244'
245
246# Test splitting the first patch, then adding both
247test_expect_success PERL 'add first line works' '
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
255test_expect_success PERL 'setup expected' '
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
265'
266
267test_expect_success PERL 'deleting a non-empty file' '
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
278test_expect_success PERL 'setup expected' '
279cat >expected <<EOF
280diff --git a/empty b/empty
281deleted file mode 100644
282index e69de29..0000000
283EOF
284'
285
286test_expect_success PERL 'deleting an empty file' '
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
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
313test_expect_success PERL 'split hunk "add -p (edit)"' '
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
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
359test_done