@matheusbsbs / matheus-bagliano / commits / ffbc5dc2d0
Fork 0

reset: add test cases for "--keep" option

This shows that with the "--keep" option, changes that are both in the work tree and the index are kept in the work tree after the reset (but discarded in the index). In the case of unmerged entries, we can see that "git reset --keep" works only when the target state is the same as HEAD. And then the work tree is not reset. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Jan 19, 2010 at 05:25 UTC ffbc5dc2d0cbdbd63a4ae04dc2cc1ebf385fcc25
2 files changed +126 -1
t/t7110-reset-merge.sh
+118 -1
@@ -3,7 +3,7 @@
3 # Copyright (c) 2009 Christian Couder
4 #
5
6 -test_description='Tests for "git reset --merge"'
6 +test_description='Tests for "git reset" with "--merge" and "--keep" options'
7
8 . ./test-lib.sh
9
@@ -43,6 +43,30 @@ test_expect_success 'reset --merge is ok when switching back' '
43 test -z "$(git diff --cached)"
44 '
45
46 +# The next test will test the following:
47 +#
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 &&
54 + cat file1 >file2 &&
55 + git reset --keep HEAD^ &&
56 + ! grep 4 file1 &&
57 + grep 4 file2 &&
58 + test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
59 + test -z "$(git diff --cached)"
60 +'
61 +
62 +test_expect_success 'reset --keep is ok when switching back' '
63 + git reset --keep second &&
64 + grep 4 file1 &&
65 + grep 4 file2 &&
66 + test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
67 + test -z "$(git diff --cached)"
68 +'
69 +
70 # The next test will test the following:
71 #
72 # working index HEAD target working index HEAD
@@ -74,6 +98,18 @@ test_expect_success 'reset --merge is ok again when switching back (1)' '
98 test -z "$(git diff --cached)"
99 '
100
101 +# The next test will test the following:
102 +#
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 &&
109 + git add file1 &&
110 + test_must_fail git reset --keep HEAD^
111 +'
112 +
113 # The next test will test the following:
114 #
115 # working index HEAD target working index HEAD
@@ -100,6 +136,30 @@ test_expect_success 'reset --merge is ok again when switching back (2)' '
136 test -z "$(git diff --cached)"
137 '
138
139 +# The next test will test the following:
140 +#
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 &&
148 + git add file2 &&
149 + git reset --keep HEAD^ &&
150 + grep 4 file2 &&
151 + test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
152 + test -z "$(git diff --cached)"
153 +'
154 +
155 +test_expect_success 'reset --keep keeps changes when switching back' '
156 + git reset --keep second &&
157 + grep 4 file2 &&
158 + grep 4 file1 &&
159 + test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
160 + test -z "$(git diff --cached)"
161 +'
162 +
163 # The next test will test the following:
164 #
165 # working index HEAD target working index HEAD
@@ -116,6 +176,22 @@ test_expect_success 'reset --merge fails with changes in file it touches' '
176 grep file1 err.log | grep "not uptodate"
177 '
178
179 +# The next test will test the following:
180 +#
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 &&
187 + test_tick &&
188 + git commit -m "add line 5" file1 &&
189 + sed -e "s/line 1/changed line 1/" <file1 >file3 &&
190 + mv file3 file1 &&
191 + test_must_fail git reset --keep HEAD^ 2>err.log &&
192 + grep file1 err.log | grep "not uptodate"
193 +'
194 +
195 test_expect_success 'setup 3 different branches' '
196 git reset --hard second &&
197 git branch branch1 &&
@@ -152,6 +228,18 @@ test_expect_success '"reset --merge HEAD^" is ok with pending merge' '
228 test -z "$(git diff)"
229 '
230
231 +# The next test will test the following:
232 +#
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 + grep file1 err.log | grep "overwritten by merge"
241 +'
242 +
243 # The next test will test the following:
244 #
245 # working index HEAD target working index HEAD
@@ -166,6 +254,21 @@ test_expect_success '"reset --merge HEAD" is ok with pending merge' '
254 test -z "$(git diff)"
255 '
256
257 +# The next test will test the following:
258 +#
259 +# working index HEAD target working index HEAD
260 +# ----------------------------------------------------
261 +# file1: X U B B --keep X B B
262 +test_expect_success '"reset --keep HEAD" is ok with pending merge' '
263 + git reset --hard third &&
264 + test_must_fail git merge branch1 &&
265 + cat file1 >orig_file1 &&
266 + git reset --keep HEAD &&
267 + test "$(git rev-parse HEAD)" = "$(git rev-parse third)" &&
268 + test -z "$(git diff --cached)" &&
269 + test_cmp file1 orig_file1
270 +'
271 +
272 test_expect_success '--merge with added/deleted' '
273 git reset --hard third &&
274 rm -f file2 &&
@@ -180,4 +283,18 @@ test_expect_success '--merge with added/deleted' '
283 git diff --exit-code --cached
284 '
285
286 +test_expect_success '--keep with added/deleted' '
287 + git reset --hard third &&
288 + rm -f file2 &&
289 + test_must_fail git merge branch3 &&
290 + ! test -f file2 &&
291 + test -f file3 &&
292 + git diff --exit-code file3 &&
293 + git diff --exit-code branch3 file3 &&
294 + git reset --keep HEAD &&
295 + test -f file3 &&
296 + ! test -f file2 &&
297 + git diff --exit-code --cached
298 +'
299 +
300 test_done
t/t7111-reset-table.sh
+8
@@ -44,26 +44,32 @@ A B C D soft A B D
44 A B C D mixed A D D
45 A B C D hard D D D
46 A B C D merge XXXXX
47 +A B C D keep XXXXX
48 A B C C soft A B C
49 A B C C mixed A C C
50 A B C C hard C C C
51 A B C C merge XXXXX
52 +A B C C keep A C C
53 B B C D soft B B D
54 B B C D mixed B D D
55 B B C D hard D D D
56 B B C D merge D D D
57 +B B C D keep XXXXX
58 B B C C soft B B C
59 B B C C mixed B C C
60 B B C C hard C C C
61 B B C C merge C C C
62 +B B C C keep B C C
63 B C C D soft B C D
64 B C C D mixed B D D
65 B C C D hard D D D
66 B C C D merge XXXXX
67 +B C C D keep XXXXX
68 B C C C soft B C C
69 B C C C mixed B C C
70 B C C C hard C C C
71 B C C C merge B C C
72 +B C C C keep B C C
73 EOF
74
75 test_expect_success 'setting up branches to test with unmerged entries' '
@@ -104,10 +110,12 @@ X U B C soft XXXXX
110 X U B C mixed X C C
111 X U B C hard C C C
112 X U B C merge C C C
113 +X U B C keep XXXXX
114 X U B B soft XXXXX
115 X U B B mixed X B B
116 X U B B hard B B B
117 X U B B merge B B B
118 +X U B B keep X B B
119 EOF
120
121 test_done