3 test_description='add -i basic tests'
6 if ! test_have_prereq PERL; then
7 skip_all='skipping git add -i tests, perl not available'
11 test_expect_success 'setup (initial)' '
17 test_expect_success 'status works (initial)' '
18 git add -i </dev/null >output &&
19 grep "+1/-0 *+2/-0 file" output
23 index 0000000..d95f3ad
29 test_expect_success '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
34 test_expect_success 'revert works (initial)' '
36 (echo r; echo 1) | git add -i &&
37 git ls-files >output &&
41 test_expect_success 'setup (commit)' '
42 echo baseline >file &&
44 git commit -m commit &&
45 echo content >>file &&
50 test_expect_success 'status works (commit)' '
51 git add -i </dev/null >output &&
52 grep "+1/-0 *+2/-0 file" output
55 index 180b47c..b6f2c08 100644
62 test_expect_success 'diff works (commit)' '
63 (echo d; echo 1) | git add -i >output &&
64 sed -ne "/^index/,/content/p" <output >diff &&
65 test_cmp expected diff
67 test_expect_success 'revert works (commit)' '
69 (echo r; echo 1) | git add -i &&
70 git add -i </dev/null >output &&
71 grep "unchanged *+3/-0 file" output
76 cat >fake_editor.sh <<EOF
78 chmod a+x fake_editor.sh
79 test_set_editor "$(pwd)/fake_editor.sh"
80 test_expect_success 'dummy edit works' '
81 (echo e; echo a) | git add -p &&
83 test_cmp expected diff
93 echo "#!$SHELL_PATH" >fake_editor.sh
94 cat >>fake_editor.sh <<\EOF
95 mv -f "$1" oldpatch &&
98 chmod a+x fake_editor.sh
99 test_set_editor "$(pwd)/fake_editor.sh"
100 test_expect_success 'bad edit rejected' '
102 (echo e; echo n; echo d) | git add -p >output &&
103 grep "hunk does not apply" output
110 test_expect_success 'garbage edit rejected' '
112 (echo e; echo n; echo d) | git add -p >output &&
113 grep "hunk does not apply" output
124 diff --git a/file b/file
125 index b5dd6c9..f910ae9 100644
135 test_expect_success 'real edit works' '
136 (echo e; echo n; echo d) | git add -p &&
138 test_cmp expected output
141 test_expect_success 'skip files similarly as commit -a' '
143 echo file >.gitignore &&
144 echo changed >file &&
145 echo y | git add -p file &&
148 git commit -am commit &&
149 git diff >expected &&
150 test_cmp expected output &&
151 git reset --hard HEAD^
155 if test "$(git config --bool core.filemode)" = false
157 say '# skipping filemode tests (filesystem does not properly support modes)'
159 test_set_prereq FILEMODE
162 test_expect_success FILEMODE 'patch does not affect mode' '
164 echo content >>file &&
166 printf "n\\ny\\n" | git add -p &&
167 git show :file | grep content &&
168 git diff file | grep "new mode"
171 test_expect_success FILEMODE 'stage mode but not hunk' '
173 echo content >>file &&
175 printf "y\\nn\\n" | git add -p &&
176 git diff --cached file | grep "new mode" &&
177 git diff file | grep "+content"
181 test_expect_success FILEMODE 'stage mode and hunk' '
183 echo content >>file &&
185 printf "y\\ny\\n" | git add -p &&
186 git diff --cached file | grep "new mode" &&
187 git diff --cached file | grep "+content" &&
188 test -z "$(git diff file)"
191 # end of tests disabled when filemode is not usable
193 test_expect_success 'setup again' '
195 test_chmod +x file &&
199 # Write the patch file with a new line at the top and bottom
201 index 180b47c..b6f2c08 100644
210 # Expected output, similar to the patch but w/ diff at the top
212 diff --git a/file b/file
213 index b6f2c08..61b9053 100755
222 # Test splitting the first patch, then adding both
223 test_expect_success 'add first line works' '
224 git commit -am "clear local changes" &&
226 (echo s; echo y; echo y) | git add -p file &&
227 git diff --cached > diff &&
228 test_cmp expected diff
232 diff --git a/non-empty b/non-empty
233 deleted file mode 100644
234 index d95f3ad..0000000
240 test_expect_success 'deleting a non-empty file' '
242 echo content >non-empty &&
244 git commit -m non-empty &&
246 echo y | git add -p non-empty &&
247 git diff --cached >diff &&
248 test_cmp expected diff
252 diff --git a/empty b/empty
253 deleted file mode 100644
254 index e69de29..0000000
257 test_expect_success 'deleting an empty file' '
261 git commit -m empty &&
263 echo y | git add -p empty &&
264 git diff --cached >diff &&
265 test_cmp expected diff