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