]>
Commit | Line | Data |
---|---|---|
18bc7616 JK |
1 | #!/bin/sh |
2 | ||
3 | test_description='add -i basic tests' | |
4 | . ./test-lib.sh | |
535d9742 | 5 | . "$TEST_DIRECTORY"/lib-prereq-FILEMODE.sh |
18bc7616 | 6 | |
f0459319 | 7 | test_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 | 13 | test_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 | |
18 | test_expect_success PERL 'setup expected' ' | |
18bc7616 JK |
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 | |
f0459319 ÆAB |
27 | ' |
28 | ||
29 | test_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 | 34 | test_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 | 41 | test_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 | 50 | test_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 | |
55 | test_expect_success PERL 'setup expected' ' | |
18bc7616 JK |
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 | |
f0459319 ÆAB |
64 | ' |
65 | ||
66 | test_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 | 71 | test_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 | |
79 | test_expect_success PERL 'setup expected' ' | |
ac083c47 TR |
80 | cat >expected <<EOF |
81 | EOF | |
f0459319 ÆAB |
82 | ' |
83 | ||
84 | test_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 | ||
90 | test_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 | 96 | test_expect_success PERL 'setup patch' ' |
ac083c47 TR |
97 | cat >patch <<EOF |
98 | @@ -1,1 +1,4 @@ | |
99 | this | |
100 | +patch | |
f0459319 | 101 | -does not |
ac083c47 TR |
102 | apply |
103 | EOF | |
f0459319 ÆAB |
104 | ' |
105 | ||
106 | test_expect_success PERL 'setup fake editor' ' | |
107 | echo "#!$SHELL_PATH" >fake_editor.sh && | |
108 | cat >>fake_editor.sh <<\EOF && | |
ac083c47 TR |
109 | mv -f "$1" oldpatch && |
110 | mv -f patch "$1" | |
111 | EOF | |
f0459319 ÆAB |
112 | chmod a+x fake_editor.sh && |
113 | test_set_editor "$(pwd)/fake_editor.sh" | |
114 | ' | |
115 | ||
116 | test_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 | 122 | test_expect_success PERL 'setup patch' ' |
ac083c47 TR |
123 | cat >patch <<EOF |
124 | this patch | |
125 | is garbage | |
126 | EOF | |
f0459319 ÆAB |
127 | ' |
128 | ||
129 | test_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 | 135 | test_expect_success PERL 'setup patch' ' |
ac083c47 TR |
136 | cat >patch <<EOF |
137 | @@ -1,0 +1,0 @@ | |
138 | baseline | |
139 | +content | |
140 | +newcontent | |
141 | +lines | |
142 | EOF | |
f0459319 ÆAB |
143 | ' |
144 | ||
145 | test_expect_success PERL 'setup expected' ' | |
ac083c47 TR |
146 | cat >expected <<EOF |
147 | diff --git a/file b/file | |
148 | index b5dd6c9..f910ae9 100644 | |
149 | --- a/file | |
150 | +++ b/file | |
151 | @@ -1,4 +1,4 @@ | |
152 | baseline | |
153 | content | |
154 | -newcontent | |
155 | +more | |
156 | lines | |
157 | EOF | |
f0459319 ÆAB |
158 | ' |
159 | ||
160 | test_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 | 166 | test_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 | ' | |
178 | rm -f .gitignore | |
179 | ||
f0459319 | 180 | test_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 | 189 | test_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 | 199 | test_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 | 211 | test_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 | 218 | test_expect_success PERL 'setup patch' ' |
f67182bf MG |
219 | cat >patch <<EOF |
220 | index 180b47c..b6f2c08 100644 | |
221 | --- a/file | |
222 | +++ b/file | |
223 | @@ -1,2 +1,4 @@ | |
224 | +firstline | |
225 | baseline | |
226 | content | |
227 | +lastline | |
228 | EOF | |
f0459319 ÆAB |
229 | ' |
230 | ||
f67182bf | 231 | # Expected output, similar to the patch but w/ diff at the top |
f0459319 | 232 | test_expect_success PERL 'setup expected' ' |
f67182bf MG |
233 | cat >expected <<EOF |
234 | diff --git a/file b/file | |
235 | index b6f2c08..61b9053 100755 | |
236 | --- a/file | |
237 | +++ b/file | |
238 | @@ -1,2 +1,4 @@ | |
239 | +firstline | |
240 | baseline | |
241 | content | |
242 | +lastline | |
243 | EOF | |
f0459319 ÆAB |
244 | ' |
245 | ||
f67182bf | 246 | # Test splitting the first patch, then adding both |
f0459319 | 247 | test_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 | 255 | test_expect_success PERL 'setup expected' ' |
8947fdd5 JK |
256 | cat >expected <<EOF |
257 | diff --git a/non-empty b/non-empty | |
258 | deleted file mode 100644 | |
259 | index d95f3ad..0000000 | |
260 | --- a/non-empty | |
261 | +++ /dev/null | |
262 | @@ -1 +0,0 @@ | |
263 | -content | |
264 | EOF | |
f0459319 ÆAB |
265 | ' |
266 | ||
267 | test_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 | 278 | test_expect_success PERL 'setup expected' ' |
24ab81ae JK |
279 | cat >expected <<EOF |
280 | diff --git a/empty b/empty | |
281 | deleted file mode 100644 | |
282 | index e69de29..0000000 | |
283 | EOF | |
f0459319 | 284 | ' |
24ab81ae | 285 | |
f0459319 | 286 | test_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 |
297 | test_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 | 313 | test_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 | ||
18bc7616 | 333 | test_done |