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

reset: add a few tests for "git reset --merge"

Commit 9e8eceab ("Add 'merge' mode to 'git reset'", 2008-12-01), added the --merge option to git reset, but there were no test cases for it. This was not a big problem because "git reset" was just forking and execing "git read-tree", but this will change in a following patch. So let's add a few test cases to make sure that there will be no regression. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Dec 30, 2009 at 06:54 UTC c93966906f5d578d06eee0ead8745e608a6e18cf
1 file changed +159
t/t7110-reset-merge.sh new
+159
@@ -0,0 +1,159 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2009 Christian Couder
4 +#
5 +
6 +test_description='Tests for "git reset --merge"'
7 +
8 +. ./test-lib.sh
9 +
10 +test_expect_success setup '
11 + for i in 1 2 3; do echo line $i; done >file1 &&
12 + cat file1 >file2 &&
13 + git add file1 file2 &&
14 + test_tick &&
15 + git commit -m "Initial commit" &&
16 + git tag initial &&
17 + echo line 4 >>file1 &&
18 + cat file1 >file2 &&
19 + test_tick &&
20 + git commit -m "add line 4 to file1" file1 &&
21 + git tag second
22 +'
23 +
24 +# The next test will test the following:
25 +#
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^ &&
32 + ! grep 4 file1 &&
33 + grep 4 file2 &&
34 + test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
35 + test -z "$(git diff --cached)"
36 +'
37 +
38 +test_expect_success 'reset --merge is ok when switching back' '
39 + git reset --merge second &&
40 + grep 4 file1 &&
41 + grep 4 file2 &&
42 + test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
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: B B C D --merge D D D
51 +# file2: C D D D --merge C D D
52 +test_expect_success 'reset --merge discards changes added to index (1)' '
53 + git reset --hard second &&
54 + cat file1 >file2 &&
55 + echo "line 5" >> file1 &&
56 + git add file1 &&
57 + git reset --merge HEAD^ &&
58 + ! grep 4 file1 &&
59 + ! grep 5 file1 &&
60 + grep 4 file2 &&
61 + test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
62 + test -z "$(git diff --cached)"
63 +'
64 +
65 +test_expect_success 'reset --merge is ok again when switching back (1)' '
66 + git reset --hard initial &&
67 + echo "line 5" >> file2 &&
68 + git add file2 &&
69 + git reset --merge second &&
70 + ! grep 4 file2 &&
71 + ! grep 5 file1 &&
72 + grep 4 file1 &&
73 + test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
74 + test -z "$(git diff --cached)"
75 +'
76 +
77 +# The next test will test the following:
78 +#
79 +# working index HEAD target working index HEAD
80 +# ----------------------------------------------------
81 +# file1: C C C D --merge D D D
82 +# file2: C C D D --merge D D D
83 +test_expect_success 'reset --merge discards changes added to index (2)' '
84 + git reset --hard second &&
85 + echo "line 4" >> file2 &&
86 + git add file2 &&
87 + git reset --merge HEAD^ &&
88 + ! grep 4 file2 &&
89 + test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
90 + test -z "$(git diff)" &&
91 + test -z "$(git diff --cached)"
92 +'
93 +
94 +test_expect_success 'reset --merge is ok again when switching back (2)' '
95 + git reset --hard initial &&
96 + git reset --merge second &&
97 + ! grep 4 file2 &&
98 + grep 4 file1 &&
99 + test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
100 + test -z "$(git diff --cached)"
101 +'
102 +
103 +# The next test will test the following:
104 +#
105 +# working index HEAD target working index HEAD
106 +# ----------------------------------------------------
107 +# file1: A B B C --merge (disallowed)
108 +test_expect_success 'reset --merge fails with changes in file it touches' '
109 + git reset --hard second &&
110 + echo "line 5" >> file1 &&
111 + test_tick &&
112 + git commit -m "add line 5" file1 &&
113 + sed -e "s/line 1/changed line 1/" <file1 >file3 &&
114 + mv file3 file1 &&
115 + test_must_fail git reset --merge HEAD^ 2>err.log &&
116 + grep file1 err.log | grep "not uptodate"
117 +'
118 +
119 +test_expect_success 'setup 2 different branches' '
120 + git reset --hard second &&
121 + git branch branch1 &&
122 + git branch branch2 &&
123 + git checkout branch1 &&
124 + echo "line 5 in branch1" >> file1 &&
125 + test_tick &&
126 + git commit -a -m "change in branch1" &&
127 + git checkout branch2 &&
128 + echo "line 5 in branch2" >> file1 &&
129 + test_tick &&
130 + git commit -a -m "change in branch2" &&
131 + git tag third
132 +'
133 +
134 +# The next test will test the following:
135 +#
136 +# working index HEAD target working index HEAD
137 +# ----------------------------------------------------
138 +# file1: X U B C --merge (disallowed)
139 +test_expect_success '"reset --merge HEAD^" fails with pending merge' '
140 + test_must_fail git merge branch1 &&
141 + test_must_fail git reset --merge HEAD^ &&
142 + test "$(git rev-parse HEAD)" = "$(git rev-parse third)" &&
143 + test -n "$(git diff --cached)"
144 +'
145 +
146 +# The next test will test the following:
147 +#
148 +# working index HEAD target working index HEAD
149 +# ----------------------------------------------------
150 +# file1: X U B B --merge (disallowed)
151 +test_expect_success '"reset --merge HEAD" fails with pending merge' '
152 + git reset --hard third &&
153 + test_must_fail git merge branch1 &&
154 + test_must_fail git reset --merge HEAD &&
155 + test "$(git rev-parse HEAD)" = "$(git rev-parse third)" &&
156 + test -n "$(git diff --cached)"
157 +'
158 +
159 +test_done