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