builtin-mailinfo.c: check error status from rewind and ftruncate
[git/spearce.git] / t / t3701-add-interactive.sh
blob62fd65e18d434711176ddb1c22ef057b879e992a
1 #!/bin/sh
3 test_description='add -i basic tests'
4 . ./test-lib.sh
6 if ! test_have_prereq PERL; then
7 say 'skipping git add -i tests, perl not available'
8 test_done
9 fi
11 test_expect_success 'setup (initial)' '
12 echo content >file &&
13 git add file &&
14 echo more >>file &&
15 echo lines >>file
17 test_expect_success 'status works (initial)' '
18 git add -i </dev/null >output &&
19 grep "+1/-0 *+2/-0 file" output
21 cat >expected <<EOF
22 new file mode 100644
23 index 0000000..d95f3ad
24 --- /dev/null
25 +++ b/file
26 @@ -0,0 +1 @@
27 +content
28 EOF
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)' '
35 git add file &&
36 (echo r; echo 1) | git add -i &&
37 git ls-files >output &&
38 ! grep . output
41 test_expect_success '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
50 test_expect_success 'status works (commit)' '
51 git add -i </dev/null >output &&
52 grep "+1/-0 *+2/-0 file" output
54 cat >expected <<EOF
55 index 180b47c..b6f2c08 100644
56 --- a/file
57 +++ b/file
58 @@ -1 +1,2 @@
59 baseline
60 +content
61 EOF
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)' '
68 git add file &&
69 (echo r; echo 1) | git add -i &&
70 git add -i </dev/null >output &&
71 grep "unchanged *+3/-0 file" output
74 cat >expected <<EOF
75 EOF
76 cat >fake_editor.sh <<EOF
77 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 &&
82 git diff > diff &&
83 test_cmp expected diff
86 cat >patch <<EOF
87 @@ -1,1 +1,4 @@
88 this
89 +patch
90 -doesn't
91 apply
92 EOF
93 echo "#!$SHELL_PATH" >fake_editor.sh
94 cat >>fake_editor.sh <<\EOF
95 mv -f "$1" oldpatch &&
96 mv -f patch "$1"
97 EOF
98 chmod a+x fake_editor.sh
99 test_set_editor "$(pwd)/fake_editor.sh"
100 test_expect_success 'bad edit rejected' '
101 git reset &&
102 (echo e; echo n; echo d) | git add -p >output &&
103 grep "hunk does not apply" output
106 cat >patch <<EOF
107 this patch
108 is garbage
110 test_expect_success 'garbage edit rejected' '
111 git reset &&
112 (echo e; echo n; echo d) | git add -p >output &&
113 grep "hunk does not apply" output
116 cat >patch <<EOF
117 @@ -1,0 +1,0 @@
118 baseline
119 +content
120 +newcontent
121 +lines
123 cat >expected <<EOF
124 diff --git a/file b/file
125 index b5dd6c9..f910ae9 100644
126 --- a/file
127 +++ b/file
128 @@ -1,4 +1,4 @@
129 baseline
130 content
131 -newcontent
132 +more
133 lines
135 test_expect_success 'real edit works' '
136 (echo e; echo n; echo d) | git add -p &&
137 git diff >output &&
138 test_cmp expected output
141 if test "$(git config --bool core.filemode)" = false
142 then
143 say 'skipping filemode tests (filesystem does not properly support modes)'
144 else
145 test_set_prereq FILEMODE
148 test_expect_success FILEMODE 'patch does not affect mode' '
149 git reset --hard &&
150 echo content >>file &&
151 chmod +x file &&
152 printf "n\\ny\\n" | git add -p &&
153 git show :file | grep content &&
154 git diff file | grep "new mode"
157 test_expect_success FILEMODE 'stage mode but not hunk' '
158 git reset --hard &&
159 echo content >>file &&
160 chmod +x file &&
161 printf "y\\nn\\n" | git add -p &&
162 git diff --cached file | grep "new mode" &&
163 git diff file | grep "+content"
167 test_expect_success FILEMODE 'stage mode and hunk' '
168 git reset --hard &&
169 echo content >>file &&
170 chmod +x file &&
171 printf "y\\ny\\n" | git add -p &&
172 git diff --cached file | grep "new mode" &&
173 git diff --cached file | grep "+content" &&
174 test -z "$(git diff file)"
177 # end of tests disabled when filemode is not usable
179 test_expect_success 'setup again' '
180 git reset --hard &&
181 test_chmod +x file &&
182 echo content >>file
185 # Write the patch file with a new line at the top and bottom
186 cat >patch <<EOF
187 index 180b47c..b6f2c08 100644
188 --- a/file
189 +++ b/file
190 @@ -1,2 +1,4 @@
191 +firstline
192 baseline
193 content
194 +lastline
196 # Expected output, similar to the patch but w/ diff at the top
197 cat >expected <<EOF
198 diff --git a/file b/file
199 index b6f2c08..61b9053 100755
200 --- a/file
201 +++ b/file
202 @@ -1,2 +1,4 @@
203 +firstline
204 baseline
205 content
206 +lastline
208 # Test splitting the first patch, then adding both
209 test_expect_success 'add first line works' '
210 git commit -am "clear local changes" &&
211 git apply patch &&
212 (echo s; echo y; echo y) | git add -p file &&
213 git diff --cached > diff &&
214 test_cmp expected diff
217 test_done