Merge branch 'sb/gpg-plug-fd-leak' into maint
[git.git] / t / t3701-add-interactive.sh
1 #!/bin/sh
2
3 test_description='add -i basic tests'
4 . ./test-lib.sh
5 . "$TEST_DIRECTORY"/lib-prereq-FILEMODE.sh
6
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 '
17
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 '
28
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 '
40
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 '
54
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 '
65
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 '
77
78
79 test_expect_success PERL 'setup expected' '
80 cat >expected <<EOF
81 EOF
82 '
83
84 test_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
90 test_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
96 test_expect_success PERL 'setup patch' '
97 cat >patch <<EOF
98 @@ -1,1 +1,4 @@
99  this
100 +patch
101 -does not
102  apply
103 EOF
104 '
105
106 test_expect_success PERL 'setup fake editor' '
107         echo "#!$SHELL_PATH" >fake_editor.sh &&
108         cat >>fake_editor.sh <<\EOF &&
109 mv -f "$1" oldpatch &&
110 mv -f patch "$1"
111 EOF
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' '
117         git reset &&
118         (echo e; echo n; echo d) | git add -p >output &&
119         grep "hunk does not apply" output
120 '
121
122 test_expect_success PERL 'setup patch' '
123 cat >patch <<EOF
124 this patch
125 is garbage
126 EOF
127 '
128
129 test_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
135 test_expect_success PERL 'setup patch' '
136 cat >patch <<EOF
137 @@ -1,0 +1,0 @@
138  baseline
139 +content
140 +newcontent
141 +lines
142 EOF
143 '
144
145 test_expect_success PERL 'setup expected' '
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
158 '
159
160 test_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
166 test_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 '
178 rm -f .gitignore
179
180 test_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
189 test_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
199 test_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
211 test_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
218 test_expect_success PERL 'setup patch' '
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
229 '
230
231 # Expected output, similar to the patch but w/ diff at the top
232 test_expect_success PERL 'setup expected' '
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
244 '
245
246 # Test splitting the first patch, then adding both
247 test_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
255 test_expect_success PERL 'setup expected' '
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
265 '
266
267 test_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
278 test_expect_success PERL 'setup expected' '
279 cat >expected <<EOF
280 diff --git a/empty b/empty
281 deleted file mode 100644
282 index e69de29..0000000
283 EOF
284 '
285
286 test_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
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
313 test_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
333 test_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
359 test_done