3 # Copyright (c) 2009 Christian Couder
6 test_description
='Tests for "git reset" with "--merge" and "--keep" options'
10 test_expect_success setup
'
11 for i in 1 2 3; do echo line $i; done >file1 &&
13 git add file1 file2 &&
15 git commit -m "Initial commit" &&
17 echo line 4 >>file1 &&
20 git commit -m "add line 4 to file1" file1 &&
24 # The next test will test the following:
26 # working index HEAD target working index HEAD
27 # ----------------------------------------------------
28 # file1: C C C D --merge D D D
29 # file2: C D D D --merge C D D
30 test_expect_success
'reset --merge is ok with changes in file it does not touch' '
31 git reset --merge HEAD^ &&
34 test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
35 test -z "$(git diff --cached)"
38 test_expect_success
'reset --merge is ok when switching back' '
39 git reset --merge second &&
42 test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
43 test -z "$(git diff --cached)"
46 # The next test will test the following:
48 # working index HEAD target working index HEAD
49 # ----------------------------------------------------
50 # file1: C C C D --keep D D D
51 # file2: C D D D --keep C D D
52 test_expect_success
'reset --keep is ok with changes in file it does not touch' '
53 git reset --hard second &&
55 git reset --keep HEAD^ &&
58 test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
59 test -z "$(git diff --cached)"
62 test_expect_success
'reset --keep is ok when switching back' '
63 git reset --keep second &&
66 test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
67 test -z "$(git diff --cached)"
70 # The next test will test the following:
72 # working index HEAD target working index HEAD
73 # ----------------------------------------------------
74 # file1: B B C D --merge D D D
75 # file2: C D D D --merge C D D
76 test_expect_success
'reset --merge discards changes added to index (1)' '
77 git reset --hard second &&
79 echo "line 5" >> file1 &&
81 git reset --merge HEAD^ &&
85 test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
86 test -z "$(git diff --cached)"
89 test_expect_success
'reset --merge is ok again when switching back (1)' '
90 git reset --hard initial &&
91 echo "line 5" >> file2 &&
93 git reset --merge second &&
97 test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
98 test -z "$(git diff --cached)"
101 # The next test will test the following:
103 # working index HEAD target working index HEAD
104 # ----------------------------------------------------
105 # file1: B B C D --keep (disallowed)
106 test_expect_success
'reset --keep fails with changes in index in files it touches' '
107 git reset --hard second &&
108 echo "line 5" >> file1 &&
110 test_must_fail git reset --keep HEAD^
113 # The next test will test the following:
115 # working index HEAD target working index HEAD
116 # ----------------------------------------------------
117 # file1: C C C D --merge D D D
118 # file2: C C D D --merge D D D
119 test_expect_success
'reset --merge discards changes added to index (2)' '
120 git reset --hard second &&
121 echo "line 4" >> file2 &&
123 git reset --merge HEAD^ &&
125 test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
126 test -z "$(git diff)" &&
127 test -z "$(git diff --cached)"
130 test_expect_success
'reset --merge is ok again when switching back (2)' '
131 git reset --hard initial &&
132 git reset --merge second &&
135 test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
136 test -z "$(git diff --cached)"
139 # The next test will test the following:
141 # working index HEAD target working index HEAD
142 # ----------------------------------------------------
143 # file1: C C C D --keep D D D
144 # file2: C C D D --keep C D D
145 test_expect_success
'reset --keep keeps changes it does not touch' '
146 git reset --hard second &&
147 echo "line 4" >> file2 &&
149 git reset --keep HEAD^ &&
151 test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
152 test -z "$(git diff --cached)"
155 test_expect_success
'reset --keep keeps changes when switching back' '
156 git reset --keep second &&
159 test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
160 test -z "$(git diff --cached)"
163 # The next test will test the following:
165 # working index HEAD target working index HEAD
166 # ----------------------------------------------------
167 # file1: A B B C --merge (disallowed)
168 test_expect_success
'reset --merge fails with changes in file it touches' '
169 git reset --hard second &&
170 echo "line 5" >> file1 &&
172 git commit -m "add line 5" file1 &&
173 sed -e "s/line 1/changed line 1/" <file1 >file3 &&
175 test_must_fail git reset --merge HEAD^ 2>err.log &&
176 grep file1 err.log | grep "not uptodate"
179 # The next test will test the following:
181 # working index HEAD target working index HEAD
182 # ----------------------------------------------------
183 # file1: A B B C --keep (disallowed)
184 test_expect_success
'reset --keep fails with changes in file it touches' '
185 git reset --hard second &&
186 echo "line 5" >> file1 &&
188 git commit -m "add line 5" file1 &&
189 sed -e "s/line 1/changed line 1/" <file1 >file3 &&
191 test_must_fail git reset --keep HEAD^ 2>err.log &&
192 grep file1 err.log | grep "not uptodate"
195 test_expect_success
'setup 3 different branches' '
196 git reset --hard second &&
197 git branch branch1 &&
198 git branch branch2 &&
199 git branch branch3 &&
200 git checkout branch1 &&
201 echo "line 5 in branch1" >> file1 &&
203 git commit -a -m "change in branch1" &&
204 git checkout branch2 &&
205 echo "line 5 in branch2" >> file1 &&
207 git commit -a -m "change in branch2" &&
209 git checkout branch3 &&
210 echo a new file >file3 &&
214 git commit -a -m "change in branch3"
217 # The next test will test the following:
219 # working index HEAD target working index HEAD
220 # ----------------------------------------------------
221 # file1: X U B C --merge C C C
222 test_expect_success
'"reset --merge HEAD^" is ok with pending merge' '
223 git checkout third &&
224 test_must_fail git merge branch1 &&
225 git reset --merge HEAD^ &&
226 test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
227 test -z "$(git diff --cached)" &&
228 test -z "$(git diff)"
231 # The next test will test the following:
233 # working index HEAD target working index HEAD
234 # ----------------------------------------------------
235 # file1: X U B C --keep (disallowed)
236 test_expect_success
'"reset --keep HEAD^" fails with pending merge' '
237 git reset --hard third &&
238 test_must_fail git merge branch1 &&
239 test_must_fail git reset --keep HEAD^ 2>err.log &&
240 test_i18ngrep "middle of a merge" err.log
243 # The next test will test the following:
245 # working index HEAD target working index HEAD
246 # ----------------------------------------------------
247 # file1: X U B B --merge B B B
248 test_expect_success
'"reset --merge HEAD" is ok with pending merge' '
249 git reset --hard third &&
250 test_must_fail git merge branch1 &&
251 git reset --merge HEAD &&
252 test "$(git rev-parse HEAD)" = "$(git rev-parse third)" &&
253 test -z "$(git diff --cached)" &&
254 test -z "$(git diff)"
257 # The next test will test the following:
259 # working index HEAD target working index HEAD
260 # ----------------------------------------------------
261 # file1: X U B B --keep (disallowed)
262 test_expect_success
'"reset --keep HEAD" fails with pending merge' '
263 git reset --hard third &&
264 test_must_fail git merge branch1 &&
265 test_must_fail git reset --keep HEAD 2>err.log &&
266 test_i18ngrep "middle of a merge" err.log
269 test_expect_success
'--merge is ok with added/deleted merge' '
270 git reset --hard third &&
272 test_must_fail git merge branch3 &&
275 git diff --exit-code file3 &&
276 git diff --exit-code branch3 file3 &&
277 git reset --merge HEAD &&
280 git diff --exit-code --cached
283 test_expect_success
'--keep fails with added/deleted merge' '
284 git reset --hard third &&
286 test_must_fail git merge branch3 &&
289 git diff --exit-code file3 &&
290 git diff --exit-code branch3 file3 &&
291 test_must_fail git reset --keep HEAD 2>err.log &&
292 test_i18ngrep "middle of a merge" err.log