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 cat >fake_editor.sh <<EOF
86 EOF
87 chmod a+x fake_editor.sh &&
88 test_set_editor "$(pwd)/fake_editor.sh" &&
89'
90
91test_expect_success PERL 'dummy edit works' '
92 (echo e; echo a) | git add -p &&
93 git diff > diff &&
94 test_cmp expected diff
95'
96
97test_expect_success PERL 'setup patch' '
98cat >patch <<EOF
99@@ -1,1 +1,4 @@
100 this
101+patch
102-does not
103 apply
104EOF
105'
106
107test_expect_success PERL 'setup fake editor' '
108 echo "#!$SHELL_PATH" >fake_editor.sh &&
109 cat >>fake_editor.sh <<\EOF &&
110mv -f "$1" oldpatch &&
111mv -f patch "$1"
112EOF
113 chmod a+x fake_editor.sh &&
114 test_set_editor "$(pwd)/fake_editor.sh"
115'
116
117test_expect_success PERL 'bad edit rejected' '
118 git reset &&
119 (echo e; echo n; echo d) | git add -p >output &&
120 grep "hunk does not apply" output
121'
122
123test_expect_success PERL 'setup patch' '
124cat >patch <<EOF
125this patch
126is garbage
127EOF
128'
129
130test_expect_success PERL 'garbage edit rejected' '
131 git reset &&
132 (echo e; echo n; echo d) | git add -p >output &&
133 grep "hunk does not apply" output
134'
135
136test_expect_success PERL 'setup patch' '
137cat >patch <<EOF
138@@ -1,0 +1,0 @@
139 baseline
140+content
141+newcontent
142+lines
143EOF
144'
145
146test_expect_success PERL 'setup expected' '
147cat >expected <<EOF
148diff --git a/file b/file
149index b5dd6c9..f910ae9 100644
150--- a/file
151+++ b/file
152@@ -1,4 +1,4 @@
153 baseline
154 content
155-newcontent
156+more
157 lines
158EOF
159'
160
161test_expect_success PERL 'real edit works' '
162 (echo e; echo n; echo d) | git add -p &&
163 git diff >output &&
164 test_cmp expected output
165'
166
167test_expect_success PERL 'skip files similarly as commit -a' '
168 git reset &&
169 echo file >.gitignore &&
170 echo changed >file &&
171 echo y | git add -p file &&
172 git diff >output &&
173 git reset &&
174 git commit -am commit &&
175 git diff >expected &&
176 test_cmp expected output &&
177 git reset --hard HEAD^
178'
179rm -f .gitignore
180
181test_expect_success PERL,FILEMODE 'patch does not affect mode' '
182 git reset --hard &&
183 echo content >>file &&
184 chmod +x file &&
185 printf "n\\ny\\n" | git add -p &&
186 git show :file | grep content &&
187 git diff file | grep "new mode"
188'
189
190test_expect_success PERL,FILEMODE 'stage mode but not hunk' '
191 git reset --hard &&
192 echo content >>file &&
193 chmod +x file &&
194 printf "y\\nn\\n" | git add -p &&
195 git diff --cached file | grep "new mode" &&
196 git diff file | grep "+content"
197'
198
199
200test_expect_success PERL,FILEMODE 'stage mode and hunk' '
201 git reset --hard &&
202 echo content >>file &&
203 chmod +x file &&
204 printf "y\\ny\\n" | git add -p &&
205 git diff --cached file | grep "new mode" &&
206 git diff --cached file | grep "+content" &&
207 test -z "$(git diff file)"
208'
209
210# end of tests disabled when filemode is not usable
211
212test_expect_success PERL 'setup again' '
213 git reset --hard &&
214 test_chmod +x file &&
215 echo content >>file
216'
217
218# Write the patch file with a new line at the top and bottom
219test_expect_success PERL 'setup patch' '
220cat >patch <<EOF
221index 180b47c..b6f2c08 100644
222--- a/file
223+++ b/file
224@@ -1,2 +1,4 @@
225+firstline
226 baseline
227 content
228+lastline
229EOF
230'
231
232# Expected output, similar to the patch but w/ diff at the top
233test_expect_success PERL 'setup expected' '
234cat >expected <<EOF
235diff --git a/file b/file
236index b6f2c08..61b9053 100755
237--- a/file
238+++ b/file
239@@ -1,2 +1,4 @@
240+firstline
241 baseline
242 content
243+lastline
244EOF
245'
246
247# Test splitting the first patch, then adding both
248test_expect_success PERL 'add first line works' '
249 git commit -am "clear local changes" &&
250 git apply patch &&
251 (echo s; echo y; echo y) | git add -p file &&
252 git diff --cached > diff &&
253 test_cmp expected diff
254'
255
256test_expect_success PERL 'setup expected' '
257cat >expected <<EOF
258diff --git a/non-empty b/non-empty
259deleted file mode 100644
260index d95f3ad..0000000
261--- a/non-empty
262+++ /dev/null
263@@ -1 +0,0 @@
264-content
265EOF
266'
267
268test_expect_success PERL 'deleting a non-empty file' '
269 git reset --hard &&
270 echo content >non-empty &&
271 git add non-empty &&
272 git commit -m non-empty &&
273 rm non-empty &&
274 echo y | git add -p non-empty &&
275 git diff --cached >diff &&
276 test_cmp expected diff
277'
278
279test_expect_success PERL 'setup expected' '
280cat >expected <<EOF
281diff --git a/empty b/empty
282deleted file mode 100644
283index e69de29..0000000
284EOF
285'
286
287test_expect_success PERL 'deleting an empty file' '
288 git reset --hard &&
289 > empty &&
290 git add empty &&
291 git commit -m empty &&
292 rm empty &&
293 echo y | git add -p empty &&
294 git diff --cached >diff &&
295 test_cmp expected diff
296'
297
298test_done