1 #!/bin/sh
3 test_description='add -i basic tests'
4 . ./test-lib.sh
5 . "$TEST_DIRECTORY"/lib-prereq-FILEMODE.sh
7 test_expect_success PERL 'setup (initial)' '
8 echo content >file &&
9 git add file &&
10 echo more >>file &&
11 echo lines >>file
12 '
13 test_expect_success PERL 'status works (initial)' '
14 git add -i </dev/null >output &&
15 grep "+1/-0 *+2/-0 file" output
16 '
18 test_expect_success PERL 'setup expected' '
19 cat >expected <<EOF
20 new file mode 100644
21 index 0000000..d95f3ad
22 --- /dev/null
23 +++ b/file
24 @@ -0,0 +1 @@
25 +content
26 EOF
27 '
29 test_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 '
34 test_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 '
41 test_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 '
50 test_expect_success PERL 'status works (commit)' '
51 git add -i </dev/null >output &&
52 grep "+1/-0 *+2/-0 file" output
53 '
55 test_expect_success PERL 'setup expected' '
56 cat >expected <<EOF
57 index 180b47c..b6f2c08 100644
58 --- a/file
59 +++ b/file
60 @@ -1 +1,2 @@
61 baseline
62 +content
63 EOF
64 '
66 test_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 '
71 test_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 '
79 test_expect_success PERL 'setup expected' '
80 cat >expected <<EOF
81 EOF
82 '
84 test_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 '
91 test_expect_success PERL 'dummy edit works' '
92 (echo e; echo a) | git add -p &&
93 git diff > diff &&
94 test_cmp expected diff
95 '
97 test_expect_success PERL 'setup patch' '
98 cat >patch <<EOF
99 @@ -1,1 +1,4 @@
100 this
101 +patch
102 -does not
103 apply
104 EOF
105 '
107 test_expect_success PERL 'setup fake editor' '
108 echo "#!$SHELL_PATH" >fake_editor.sh &&
109 cat >>fake_editor.sh <<\EOF &&
110 mv -f "$1" oldpatch &&
111 mv -f patch "$1"
112 EOF
113 chmod a+x fake_editor.sh &&
114 test_set_editor "$(pwd)/fake_editor.sh"
115 '
117 test_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 '
123 test_expect_success PERL 'setup patch' '
124 cat >patch <<EOF
125 this patch
126 is garbage
127 EOF
128 '
130 test_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 '
136 test_expect_success PERL 'setup patch' '
137 cat >patch <<EOF
138 @@ -1,0 +1,0 @@
139 baseline
140 +content
141 +newcontent
142 +lines
143 EOF
144 '
146 test_expect_success PERL 'setup expected' '
147 cat >expected <<EOF
148 diff --git a/file b/file
149 index b5dd6c9..f910ae9 100644
150 --- a/file
151 +++ b/file
152 @@ -1,4 +1,4 @@
153 baseline
154 content
155 -newcontent
156 +more
157 lines
158 EOF
159 '
161 test_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 '
167 test_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 '
179 rm -f .gitignore
181 test_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 '
190 test_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 '
200 test_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 '
210 # end of tests disabled when filemode is not usable
212 test_expect_success PERL 'setup again' '
213 git reset --hard &&
214 test_chmod +x file &&
215 echo content >>file
216 '
218 # Write the patch file with a new line at the top and bottom
219 test_expect_success PERL 'setup patch' '
220 cat >patch <<EOF
221 index 180b47c..b6f2c08 100644
222 --- a/file
223 +++ b/file
224 @@ -1,2 +1,4 @@
225 +firstline
226 baseline
227 content
228 +lastline
229 EOF
230 '
232 # Expected output, similar to the patch but w/ diff at the top
233 test_expect_success PERL 'setup expected' '
234 cat >expected <<EOF
235 diff --git a/file b/file
236 index b6f2c08..61b9053 100755
237 --- a/file
238 +++ b/file
239 @@ -1,2 +1,4 @@
240 +firstline
241 baseline
242 content
243 +lastline
244 EOF
245 '
247 # Test splitting the first patch, then adding both
248 test_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 '
256 test_expect_success PERL 'setup expected' '
257 cat >expected <<EOF
258 diff --git a/non-empty b/non-empty
259 deleted file mode 100644
260 index d95f3ad..0000000
261 --- a/non-empty
262 +++ /dev/null
263 @@ -1 +0,0 @@
264 -content
265 EOF
266 '
268 test_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 '
279 test_expect_success PERL 'setup expected' '
280 cat >expected <<EOF
281 diff --git a/empty b/empty
282 deleted file mode 100644
283 index e69de29..0000000
284 EOF
285 '
287 test_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 '
298 test_done