Code

tests: Move FILEMODE prerequisite to lib-prereq-FILEMODE.sh
[git.git] / t / t3701-add-interactive.sh
1 #!/bin/sh
3 test_description='add -i basic tests'
4 . ./test-lib.sh
5 . "$TEST_DIRECTORY"/lib-prereq-FILEMODE.sh
7 if ! test_have_prereq PERL; then
8         skip_all='skipping git add -i tests, perl not available'
9         test_done
10 fi
12 test_expect_success 'setup (initial)' '
13         echo content >file &&
14         git add file &&
15         echo more >>file &&
16         echo lines >>file
17 '
18 test_expect_success 'status works (initial)' '
19         git add -i </dev/null >output &&
20         grep "+1/-0 *+2/-0 file" output
21 '
22 cat >expected <<EOF
23 new file mode 100644
24 index 0000000..d95f3ad
25 --- /dev/null
26 +++ b/file
27 @@ -0,0 +1 @@
28 +content
29 EOF
30 test_expect_success 'diff works (initial)' '
31         (echo d; echo 1) | git add -i >output &&
32         sed -ne "/new file/,/content/p" <output >diff &&
33         test_cmp expected diff
34 '
35 test_expect_success 'revert works (initial)' '
36         git add file &&
37         (echo r; echo 1) | git add -i &&
38         git ls-files >output &&
39         ! grep . output
40 '
42 test_expect_success 'setup (commit)' '
43         echo baseline >file &&
44         git add file &&
45         git commit -m commit &&
46         echo content >>file &&
47         git add file &&
48         echo more >>file &&
49         echo lines >>file
50 '
51 test_expect_success 'status works (commit)' '
52         git add -i </dev/null >output &&
53         grep "+1/-0 *+2/-0 file" output
54 '
55 cat >expected <<EOF
56 index 180b47c..b6f2c08 100644
57 --- a/file
58 +++ b/file
59 @@ -1 +1,2 @@
60  baseline
61 +content
62 EOF
63 test_expect_success 'diff works (commit)' '
64         (echo d; echo 1) | git add -i >output &&
65         sed -ne "/^index/,/content/p" <output >diff &&
66         test_cmp expected diff
67 '
68 test_expect_success 'revert works (commit)' '
69         git add file &&
70         (echo r; echo 1) | git add -i &&
71         git add -i </dev/null >output &&
72         grep "unchanged *+3/-0 file" output
73 '
75 cat >expected <<EOF
76 EOF
77 cat >fake_editor.sh <<EOF
78 EOF
79 chmod a+x fake_editor.sh
80 test_set_editor "$(pwd)/fake_editor.sh"
81 test_expect_success 'dummy edit works' '
82         (echo e; echo a) | git add -p &&
83         git diff > diff &&
84         test_cmp expected diff
85 '
87 cat >patch <<EOF
88 @@ -1,1 +1,4 @@
89  this
90 +patch
91 -doesn't
92  apply
93 EOF
94 echo "#!$SHELL_PATH" >fake_editor.sh
95 cat >>fake_editor.sh <<\EOF
96 mv -f "$1" oldpatch &&
97 mv -f patch "$1"
98 EOF
99 chmod a+x fake_editor.sh
100 test_set_editor "$(pwd)/fake_editor.sh"
101 test_expect_success 'bad edit rejected' '
102         git reset &&
103         (echo e; echo n; echo d) | git add -p >output &&
104         grep "hunk does not apply" output
107 cat >patch <<EOF
108 this patch
109 is garbage
110 EOF
111 test_expect_success 'garbage edit rejected' '
112         git reset &&
113         (echo e; echo n; echo d) | git add -p >output &&
114         grep "hunk does not apply" output
117 cat >patch <<EOF
118 @@ -1,0 +1,0 @@
119  baseline
120 +content
121 +newcontent
122 +lines
123 EOF
124 cat >expected <<EOF
125 diff --git a/file b/file
126 index b5dd6c9..f910ae9 100644
127 --- a/file
128 +++ b/file
129 @@ -1,4 +1,4 @@
130  baseline
131  content
132 -newcontent
133 +more
134  lines
135 EOF
136 test_expect_success 'real edit works' '
137         (echo e; echo n; echo d) | git add -p &&
138         git diff >output &&
139         test_cmp expected output
142 test_expect_success 'skip files similarly as commit -a' '
143         git reset &&
144         echo file >.gitignore &&
145         echo changed >file &&
146         echo y | git add -p file &&
147         git diff >output &&
148         git reset &&
149         git commit -am commit &&
150         git diff >expected &&
151         test_cmp expected output &&
152         git reset --hard HEAD^
154 rm -f .gitignore
156 test_expect_success FILEMODE 'patch does not affect mode' '
157         git reset --hard &&
158         echo content >>file &&
159         chmod +x file &&
160         printf "n\\ny\\n" | git add -p &&
161         git show :file | grep content &&
162         git diff file | grep "new mode"
165 test_expect_success FILEMODE 'stage mode but not hunk' '
166         git reset --hard &&
167         echo content >>file &&
168         chmod +x file &&
169         printf "y\\nn\\n" | git add -p &&
170         git diff --cached file | grep "new mode" &&
171         git diff          file | grep "+content"
175 test_expect_success FILEMODE 'stage mode and hunk' '
176         git reset --hard &&
177         echo content >>file &&
178         chmod +x file &&
179         printf "y\\ny\\n" | git add -p &&
180         git diff --cached file | grep "new mode" &&
181         git diff --cached file | grep "+content" &&
182         test -z "$(git diff file)"
185 # end of tests disabled when filemode is not usable
187 test_expect_success 'setup again' '
188         git reset --hard &&
189         test_chmod +x file &&
190         echo content >>file
193 # Write the patch file with a new line at the top and bottom
194 cat >patch <<EOF
195 index 180b47c..b6f2c08 100644
196 --- a/file
197 +++ b/file
198 @@ -1,2 +1,4 @@
199 +firstline
200  baseline
201  content
202 +lastline
203 EOF
204 # Expected output, similar to the patch but w/ diff at the top
205 cat >expected <<EOF
206 diff --git a/file b/file
207 index b6f2c08..61b9053 100755
208 --- a/file
209 +++ b/file
210 @@ -1,2 +1,4 @@
211 +firstline
212  baseline
213  content
214 +lastline
215 EOF
216 # Test splitting the first patch, then adding both
217 test_expect_success 'add first line works' '
218         git commit -am "clear local changes" &&
219         git apply patch &&
220         (echo s; echo y; echo y) | git add -p file &&
221         git diff --cached > diff &&
222         test_cmp expected diff
225 cat >expected <<EOF
226 diff --git a/non-empty b/non-empty
227 deleted file mode 100644
228 index d95f3ad..0000000
229 --- a/non-empty
230 +++ /dev/null
231 @@ -1 +0,0 @@
232 -content
233 EOF
234 test_expect_success 'deleting a non-empty file' '
235         git reset --hard &&
236         echo content >non-empty &&
237         git add non-empty &&
238         git commit -m non-empty &&
239         rm non-empty &&
240         echo y | git add -p non-empty &&
241         git diff --cached >diff &&
242         test_cmp expected diff
245 cat >expected <<EOF
246 diff --git a/empty b/empty
247 deleted file mode 100644
248 index e69de29..0000000
249 EOF
251 test_expect_success 'deleting an empty file' '
252         git reset --hard &&
253         > empty &&
254         git add empty &&
255         git commit -m empty &&
256         rm empty &&
257         echo y | git add -p empty &&
258         git diff --cached >diff &&
259         test_cmp expected diff
262 test_done