1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.pgm;
44
45 import static org.junit.Assert.assertArrayEquals;
46 import static org.junit.Assert.assertEquals;
47
48 import java.util.Iterator;
49
50 import org.eclipse.jgit.api.Git;
51 import org.eclipse.jgit.lib.CLIRepositoryTestCase;
52 import org.eclipse.jgit.merge.MergeStrategy;
53 import org.eclipse.jgit.pgm.internal.CLIText;
54 import org.eclipse.jgit.revwalk.RevCommit;
55 import org.junit.Before;
56 import org.junit.Test;
57
58 public class MergeTest extends CLIRepositoryTestCase {
59
60 private Git git;
61
62 @Override
63 @Before
64 public void setUp() throws Exception {
65 super.setUp();
66 git = new Git(db);
67 git.commit().setMessage("initial commit").call();
68 }
69
70 @Test
71 public void testMergeSelf() throws Exception {
72 assertEquals("Already up-to-date.", execute("git merge master")[0]);
73 }
74
75 @Test
76 public void testSquashSelf() throws Exception {
77 assertEquals(" (nothing to squash)Already up-to-date.",
78 execute("git merge master --squash")[0]);
79 }
80
81 @Test
82 public void testFastForward() throws Exception {
83 git.branchCreate().setName("side").call();
84 writeTrashFile("file", "master");
85 git.add().addFilepattern("file").call();
86 git.commit().setMessage("commit").call();
87 git.checkout().setName("side").call();
88
89 assertArrayEquals(new String[] { "Updating 6fd41be..26a81a1",
90 "Fast-forward", "" }, execute("git merge master"));
91 }
92
93 @Test
94 public void testMerge() throws Exception {
95 git.branchCreate().setName("side").call();
96 writeTrashFile("master", "content");
97 git.add().addFilepattern("master").call();
98 git.commit().setMessage("master commit").call();
99 git.checkout().setName("side").call();
100 writeTrashFile("side", "content");
101 git.add().addFilepattern("side").call();
102 git.commit().setMessage("side commit").call();
103
104 assertEquals("Merge made by the '" + MergeStrategy.RECURSIVE.getName()
105 + "' strategy.", execute("git merge master")[0]);
106 }
107
108 @Test
109 public void testMergeNoCommit() throws Exception {
110 git.branchCreate().setName("side").call();
111 writeTrashFile("master", "content");
112 git.add().addFilepattern("master").call();
113 git.commit().setMessage("master commit").call();
114 git.checkout().setName("side").call();
115 writeTrashFile("side", "content");
116 git.add().addFilepattern("side").call();
117 git.commit().setMessage("side commit").call();
118
119 assertEquals(
120 "Automatic merge went well; stopped before committing as requested",
121 execute("git merge --no-commit master")[0]);
122 }
123
124 @Test
125 public void testMergeNoCommitSquash() throws Exception {
126 git.branchCreate().setName("side").call();
127 writeTrashFile("master", "content");
128 git.add().addFilepattern("master").call();
129 git.commit().setMessage("master commit").call();
130 git.checkout().setName("side").call();
131 writeTrashFile("side", "content");
132 git.add().addFilepattern("side").call();
133 git.commit().setMessage("side commit").call();
134
135 assertArrayEquals(
136 new String[] {
137 "Squash commit -- not updating HEAD",
138 "Automatic merge went well; stopped before committing as requested",
139 "" }, execute("git merge --no-commit --squash master"));
140 }
141
142 @Test
143 public void testSquash() throws Exception {
144 git.branchCreate().setName("side").call();
145 writeTrashFile("file1", "content1");
146 git.add().addFilepattern("file1").call();
147 git.commit().setMessage("file1 commit").call();
148 writeTrashFile("file2", "content2");
149 git.add().addFilepattern("file2").call();
150 git.commit().setMessage("file2 commit").call();
151 git.checkout().setName("side").call();
152 writeTrashFile("side", "content");
153 git.add().addFilepattern("side").call();
154 git.commit().setMessage("side commit").call();
155
156 assertArrayEquals(
157 new String[] { "Squash commit -- not updating HEAD",
158 "Automatic merge went well; stopped before committing as requested",
159 "" },
160 execute("git merge master --squash"));
161 }
162
163 @Test
164 public void testNoFastForward() throws Exception {
165 git.branchCreate().setName("side").call();
166 writeTrashFile("file", "master");
167 git.add().addFilepattern("file").call();
168 git.commit().setMessage("commit").call();
169 git.checkout().setName("side").call();
170
171 assertEquals("Merge made by the 'recursive' strategy.",
172 execute("git merge master --no-ff")[0]);
173 assertArrayEquals(new String[] {
174 "commit 6db23724012376e8407fc24b5da4277a9601be81",
175 "Author: GIT_COMMITTER_NAME <GIT_COMMITTER_EMAIL>",
176 "Date: Sat Aug 15 20:12:58 2009 -0330",
177 "",
178 " Merge branch 'master' into side",
179 "",
180 "commit 6fd41be26b7ee41584dd997f665deb92b6c4c004",
181 "Author: GIT_COMMITTER_NAME <GIT_COMMITTER_EMAIL>",
182 "Date: Sat Aug 15 20:12:58 2009 -0330",
183 "",
184 " initial commit",
185 "",
186 "commit 26a81a1c6a105551ba703a8b6afc23994cacbae1",
187 "Author: GIT_COMMITTER_NAME <GIT_COMMITTER_EMAIL>",
188 "Date: Sat Aug 15 20:12:58 2009 -0330",
189 "",
190 " commit",
191 "",
192 ""
193 }, execute("git log"));
194 }
195
196 @Test
197 public void testNoFastForwardAndSquash() throws Exception {
198 assertEquals(
199 CLIText.fatalError(CLIText.get().cannotCombineSquashWithNoff),
200 executeUnchecked("git merge master --no-ff --squash")[0]);
201 }
202
203 @Test
204 public void testFastForwardOnly() throws Exception {
205 git.branchCreate().setName("side").call();
206 writeTrashFile("file", "master");
207 git.add().addFilepattern("file").call();
208 git.commit().setMessage("commit#1").call();
209 git.checkout().setName("side").call();
210 writeTrashFile("file", "side");
211 git.add().addFilepattern("file").call();
212 git.commit().setMessage("commit#2").call();
213
214 assertEquals(CLIText.fatalError(CLIText.get().ffNotPossibleAborting),
215 executeUnchecked("git merge master --ff-only")[0]);
216 }
217
218 @Test
219 public void testMergeWithUserMessage() throws Exception {
220 git.branchCreate().setName("side").call();
221 writeTrashFile("master", "content");
222 git.add().addFilepattern("master").call();
223 git.commit().setMessage("master commit").call();
224 git.checkout().setName("side").call();
225 writeTrashFile("side", "content");
226 git.add().addFilepattern("side").call();
227 git.commit().setMessage("side commit").call();
228
229 assertEquals("Merge made by the '" + MergeStrategy.RECURSIVE.getName()
230 + "' strategy.",
231 execute("git merge master -m \"user message\"")[0]);
232
233 Iterator<RevCommit> it = git.log().call().iterator();
234 RevCommit newHead = it.next();
235 assertEquals("user message", newHead.getFullMessage());
236 }
237 }