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.merge;
44
45 import static org.junit.Assert.assertEquals;
46
47 import java.io.IOException;
48 import java.util.Arrays;
49
50 import org.eclipse.jgit.lib.ObjectId;
51 import org.eclipse.jgit.lib.ObjectIdRef;
52 import org.eclipse.jgit.lib.Ref;
53 import org.eclipse.jgit.lib.Ref.Storage;
54 import org.eclipse.jgit.lib.RefUpdate;
55 import org.eclipse.jgit.lib.SymbolicRef;
56 import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
57 import org.junit.Before;
58 import org.junit.Test;
59
60
61
62
63 public class MergeMessageFormatterTest extends SampleDataRepositoryTestCase {
64
65 private MergeMessageFormatter formatter;
66
67 @Override
68 @Before
69 public void setUp() throws Exception {
70 super.setUp();
71
72 RefUpdate createRemoteRefA = db
73 .updateRef("refs/remotes/origin/remote-a");
74 createRemoteRefA.setNewObjectId(db.resolve("refs/heads/a"));
75 createRemoteRefA.update();
76
77 RefUpdate createRemoteRefB = db
78 .updateRef("refs/remotes/origin/remote-b");
79 createRemoteRefB.setNewObjectId(db.resolve("refs/heads/b"));
80 createRemoteRefB.update();
81
82 formatter = new MergeMessageFormatter();
83 }
84
85 @Test
86 public void testOneBranch() throws IOException {
87 Ref a = db.exactRef("refs/heads/a");
88 Ref master = db.exactRef("refs/heads/master");
89 String message = formatter.format(Arrays.asList(a), master);
90 assertEquals("Merge branch 'a'", message);
91 }
92
93 @Test
94 public void testTwoBranches() throws IOException {
95 Ref a = db.exactRef("refs/heads/a");
96 Ref b = db.exactRef("refs/heads/b");
97 Ref master = db.exactRef("refs/heads/master");
98 String message = formatter.format(Arrays.asList(a, b), master);
99 assertEquals("Merge branches 'a' and 'b'", message);
100 }
101
102 @Test
103 public void testThreeBranches() throws IOException {
104 Ref c = db.exactRef("refs/heads/c");
105 Ref b = db.exactRef("refs/heads/b");
106 Ref a = db.exactRef("refs/heads/a");
107 Ref master = db.exactRef("refs/heads/master");
108 String message = formatter.format(Arrays.asList(c, b, a), master);
109 assertEquals("Merge branches 'c', 'b' and 'a'", message);
110 }
111
112 @Test
113 public void testRemoteBranch() throws Exception {
114 Ref remoteA = db.exactRef("refs/remotes/origin/remote-a");
115 Ref master = db.exactRef("refs/heads/master");
116 String message = formatter.format(Arrays.asList(remoteA), master);
117 assertEquals("Merge remote-tracking branch 'origin/remote-a'", message);
118 }
119
120 @Test
121 public void testMixed() throws IOException {
122 Ref c = db.exactRef("refs/heads/c");
123 Ref remoteA = db.exactRef("refs/remotes/origin/remote-a");
124 Ref master = db.exactRef("refs/heads/master");
125 String message = formatter.format(Arrays.asList(c, remoteA), master);
126 assertEquals("Merge branch 'c', remote-tracking branch 'origin/remote-a'",
127 message);
128 }
129
130 @Test
131 public void testTag() throws IOException {
132 Ref tagA = db.exactRef("refs/tags/A");
133 Ref master = db.exactRef("refs/heads/master");
134 String message = formatter.format(Arrays.asList(tagA), master);
135 assertEquals("Merge tag 'A'", message);
136 }
137
138 @Test
139 public void testCommit() throws IOException {
140 ObjectId objectId = ObjectId
141 .fromString("6db9c2ebf75590eef973081736730a9ea169a0c4");
142 Ref commit = new ObjectIdRef.Unpeeled(Storage.LOOSE,
143 objectId.getName(), objectId);
144 Ref master = db.exactRef("refs/heads/master");
145 String message = formatter.format(Arrays.asList(commit), master);
146 assertEquals("Merge commit '6db9c2ebf75590eef973081736730a9ea169a0c4'",
147 message);
148 }
149
150 @Test
151 public void testPullWithUri() throws IOException {
152 String name = "branch 'test' of http://egit.eclipse.org/jgit.git";
153 ObjectId objectId = ObjectId
154 .fromString("6db9c2ebf75590eef973081736730a9ea169a0c4");
155 Ref remoteBranch = new ObjectIdRef.Unpeeled(Storage.LOOSE, name,
156 objectId);
157 Ref master = db.exactRef("refs/heads/master");
158 String message = formatter.format(Arrays.asList(remoteBranch), master);
159 assertEquals("Merge branch 'test' of http://egit.eclipse.org/jgit.git",
160 message);
161 }
162
163 @Test
164 public void testIntoOtherThanMaster() throws IOException {
165 Ref a = db.exactRef("refs/heads/a");
166 Ref b = db.exactRef("refs/heads/b");
167 String message = formatter.format(Arrays.asList(a), b);
168 assertEquals("Merge branch 'a' into b", message);
169 }
170
171 @Test
172 public void testIntoHeadOtherThanMaster() throws IOException {
173 Ref a = db.exactRef("refs/heads/a");
174 Ref b = db.exactRef("refs/heads/b");
175 SymbolicRef head = new SymbolicRef("HEAD", b);
176 String message = formatter.format(Arrays.asList(a), head);
177 assertEquals("Merge branch 'a' into b", message);
178 }
179
180 @Test
181 public void testIntoSymbolicRefHeadPointingToMaster() throws IOException {
182 Ref a = db.exactRef("refs/heads/a");
183 Ref master = db.exactRef("refs/heads/master");
184 SymbolicRef head = new SymbolicRef("HEAD", master);
185 String message = formatter.format(Arrays.asList(a), head);
186 assertEquals("Merge branch 'a'", message);
187 }
188
189 @Test
190 public void testFormatWithConflictsNoFooter() {
191 String originalMessage = "Header Line\n\nCommit body\n";
192 String message = formatter.formatWithConflicts(originalMessage,
193 Arrays.asList(new String[] { "path1" }));
194 assertEquals("Header Line\n\nCommit body\n\nConflicts:\n\tpath1\n",
195 message);
196 }
197
198 @Test
199 public void testFormatWithConflictsNoFooterNoLineBreak() {
200 String originalMessage = "Header Line\n\nCommit body";
201 String message = formatter.formatWithConflicts(originalMessage,
202 Arrays.asList(new String[] { "path1" }));
203 assertEquals("Header Line\n\nCommit body\n\nConflicts:\n\tpath1\n",
204 message);
205 }
206
207 @Test
208 public void testFormatWithConflictsWithFooters() {
209 String originalMessage = "Header Line\n\nCommit body\n\nChangeId:"
210 + " I123456789123456789123456789123456789\nBug:1234567\n";
211 String message = formatter.formatWithConflicts(originalMessage,
212 Arrays.asList(new String[] { "path1" }));
213 assertEquals(
214 "Header Line\n\nCommit body\n\nConflicts:\n\tpath1\n\n"
215 + "ChangeId: I123456789123456789123456789123456789\nBug:1234567\n",
216 message);
217 }
218
219 @Test
220 public void testFormatWithConflictsWithFooterlikeLineInBody() {
221 String originalMessage = "Header Line\n\nCommit body\nBug:1234567\nMore Body\n\nChangeId:"
222 + " I123456789123456789123456789123456789\nBug:1234567\n";
223 String message = formatter.formatWithConflicts(originalMessage,
224 Arrays.asList(new String[] { "path1" }));
225 assertEquals(
226 "Header Line\n\nCommit body\nBug:1234567\nMore Body\n\nConflicts:\n\tpath1\n\n"
227 + "ChangeId: I123456789123456789123456789123456789\nBug:1234567\n",
228 message);
229 }
230 }