3 # Copyright (c) 2009 Christian Couder
6 test_description='Tests for "git reset --merge"'
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: 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 &&
55 echo "line 5" >> file1 &&
57 git reset --merge HEAD^ &&
61 test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
62 test -z "$(git diff --cached)"
65 test_expect_success 'reset --merge is ok again when switching back (1)' '
66 git reset --hard initial &&
67 echo "line 5" >> file2 &&
69 git reset --merge second &&
73 test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
74 test -z "$(git diff --cached)"
77 # The next test will test the following:
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 &&
87 git reset --merge HEAD^ &&
89 test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
90 test -z "$(git diff)" &&
91 test -z "$(git diff --cached)"
94 test_expect_success 'reset --merge is ok again when switching back (2)' '
95 git reset --hard initial &&
96 git reset --merge second &&
99 test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
100 test -z "$(git diff --cached)"
103 # The next test will test the following:
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 &&
112 git commit -m "add line 5" file1 &&
113 sed -e "s/line 1/changed line 1/" <file1 >file3 &&
115 test_must_fail git reset --merge HEAD^ 2>err.log &&
116 grep file1 err.log | grep "not uptodate"
119 test_expect_success 'setup 3 different branches' '
120 git reset --hard second &&
121 git branch branch1 &&
122 git branch branch2 &&
123 git branch branch3 &&
124 git checkout branch1 &&
125 echo "line 5 in branch1" >> file1 &&
127 git commit -a -m "change in branch1" &&
128 git checkout branch2 &&
129 echo "line 5 in branch2" >> file1 &&
131 git commit -a -m "change in branch2" &&
133 git checkout branch3 &&
134 echo a new file >file3 &&
138 git commit -a -m "change in branch3"
141 # The next test will test the following:
143 # working index HEAD target working index HEAD
144 # ----------------------------------------------------
145 # file1: X U B C --merge C C C
146 test_expect_success '"reset --merge HEAD^" is ok with pending merge' '
147 git checkout third &&
148 test_must_fail git merge branch1 &&
149 git reset --merge HEAD^ &&
150 test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
151 test -z "$(git diff --cached)" &&
152 test -z "$(git diff)"
155 # The next test will test the following:
157 # working index HEAD target working index HEAD
158 # ----------------------------------------------------
159 # file1: X U B B --merge B B B
160 test_expect_success '"reset --merge HEAD" is ok with pending merge' '
161 git reset --hard third &&
162 test_must_fail git merge branch1 &&
163 git reset --merge HEAD &&
164 test "$(git rev-parse HEAD)" = "$(git rev-parse third)" &&
165 test -z "$(git diff --cached)" &&
166 test -z "$(git diff)"
169 test_expect_success '--merge with added/deleted' '
170 git reset --hard third &&
172 test_must_fail git merge branch3 &&
175 git diff --exit-code file3 &&
176 git diff --exit-code branch3 file3 &&
177 git reset --merge HEAD &&
180 git diff --exit-code --cached