You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MergeMessageFormatterTest.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2010, Robin Stocker <robin@nibor.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.merge;
  44. import static org.junit.Assert.assertEquals;
  45. import java.io.IOException;
  46. import java.util.Arrays;
  47. import org.eclipse.jgit.lib.ObjectId;
  48. import org.eclipse.jgit.lib.ObjectIdRef;
  49. import org.eclipse.jgit.lib.Ref;
  50. import org.eclipse.jgit.lib.Ref.Storage;
  51. import org.eclipse.jgit.lib.RefUpdate;
  52. import org.eclipse.jgit.lib.SymbolicRef;
  53. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  54. import org.junit.Before;
  55. import org.junit.Test;
  56. /**
  57. * Test construction of merge message by {@link MergeMessageFormatter}.
  58. */
  59. public class MergeMessageFormatterTest extends SampleDataRepositoryTestCase {
  60. private MergeMessageFormatter formatter;
  61. @Override
  62. @Before
  63. public void setUp() throws Exception {
  64. super.setUp();
  65. RefUpdate createRemoteRefA = db
  66. .updateRef("refs/remotes/origin/remote-a");
  67. createRemoteRefA.setNewObjectId(db.resolve("refs/heads/a"));
  68. createRemoteRefA.update();
  69. RefUpdate createRemoteRefB = db
  70. .updateRef("refs/remotes/origin/remote-b");
  71. createRemoteRefB.setNewObjectId(db.resolve("refs/heads/b"));
  72. createRemoteRefB.update();
  73. formatter = new MergeMessageFormatter();
  74. }
  75. @Test
  76. public void testOneBranch() throws IOException {
  77. Ref a = db.exactRef("refs/heads/a");
  78. Ref master = db.exactRef("refs/heads/master");
  79. String message = formatter.format(Arrays.asList(a), master);
  80. assertEquals("Merge branch 'a'", message);
  81. }
  82. @Test
  83. public void testTwoBranches() throws IOException {
  84. Ref a = db.exactRef("refs/heads/a");
  85. Ref b = db.exactRef("refs/heads/b");
  86. Ref master = db.exactRef("refs/heads/master");
  87. String message = formatter.format(Arrays.asList(a, b), master);
  88. assertEquals("Merge branches 'a' and 'b'", message);
  89. }
  90. @Test
  91. public void testThreeBranches() throws IOException {
  92. Ref c = db.exactRef("refs/heads/c");
  93. Ref b = db.exactRef("refs/heads/b");
  94. Ref a = db.exactRef("refs/heads/a");
  95. Ref master = db.exactRef("refs/heads/master");
  96. String message = formatter.format(Arrays.asList(c, b, a), master);
  97. assertEquals("Merge branches 'c', 'b' and 'a'", message);
  98. }
  99. @Test
  100. public void testRemoteBranch() throws Exception {
  101. Ref remoteA = db.exactRef("refs/remotes/origin/remote-a");
  102. Ref master = db.exactRef("refs/heads/master");
  103. String message = formatter.format(Arrays.asList(remoteA), master);
  104. assertEquals("Merge remote-tracking branch 'origin/remote-a'", message);
  105. }
  106. @Test
  107. public void testMixed() throws IOException {
  108. Ref c = db.exactRef("refs/heads/c");
  109. Ref remoteA = db.exactRef("refs/remotes/origin/remote-a");
  110. Ref master = db.exactRef("refs/heads/master");
  111. String message = formatter.format(Arrays.asList(c, remoteA), master);
  112. assertEquals("Merge branch 'c', remote-tracking branch 'origin/remote-a'",
  113. message);
  114. }
  115. @Test
  116. public void testTag() throws IOException {
  117. Ref tagA = db.exactRef("refs/tags/A");
  118. Ref master = db.exactRef("refs/heads/master");
  119. String message = formatter.format(Arrays.asList(tagA), master);
  120. assertEquals("Merge tag 'A'", message);
  121. }
  122. @Test
  123. public void testCommit() throws IOException {
  124. ObjectId objectId = ObjectId
  125. .fromString("6db9c2ebf75590eef973081736730a9ea169a0c4");
  126. Ref commit = new ObjectIdRef.Unpeeled(Storage.LOOSE,
  127. objectId.getName(), objectId);
  128. Ref master = db.exactRef("refs/heads/master");
  129. String message = formatter.format(Arrays.asList(commit), master);
  130. assertEquals("Merge commit '6db9c2ebf75590eef973081736730a9ea169a0c4'",
  131. message);
  132. }
  133. @Test
  134. public void testPullWithUri() throws IOException {
  135. String name = "branch 'test' of http://egit.eclipse.org/jgit.git";
  136. ObjectId objectId = ObjectId
  137. .fromString("6db9c2ebf75590eef973081736730a9ea169a0c4");
  138. Ref remoteBranch = new ObjectIdRef.Unpeeled(Storage.LOOSE, name,
  139. objectId);
  140. Ref master = db.exactRef("refs/heads/master");
  141. String message = formatter.format(Arrays.asList(remoteBranch), master);
  142. assertEquals("Merge branch 'test' of http://egit.eclipse.org/jgit.git",
  143. message);
  144. }
  145. @Test
  146. public void testIntoOtherThanMaster() throws IOException {
  147. Ref a = db.exactRef("refs/heads/a");
  148. Ref b = db.exactRef("refs/heads/b");
  149. String message = formatter.format(Arrays.asList(a), b);
  150. assertEquals("Merge branch 'a' into b", message);
  151. }
  152. @Test
  153. public void testIntoHeadOtherThanMaster() throws IOException {
  154. Ref a = db.exactRef("refs/heads/a");
  155. Ref b = db.exactRef("refs/heads/b");
  156. SymbolicRef head = new SymbolicRef("HEAD", b);
  157. String message = formatter.format(Arrays.asList(a), head);
  158. assertEquals("Merge branch 'a' into b", message);
  159. }
  160. @Test
  161. public void testIntoSymbolicRefHeadPointingToMaster() throws IOException {
  162. Ref a = db.exactRef("refs/heads/a");
  163. Ref master = db.exactRef("refs/heads/master");
  164. SymbolicRef head = new SymbolicRef("HEAD", master);
  165. String message = formatter.format(Arrays.asList(a), head);
  166. assertEquals("Merge branch 'a'", message);
  167. }
  168. @Test
  169. public void testFormatWithConflictsNoFooter() {
  170. String originalMessage = "Header Line\n\nCommit body\n";
  171. String message = formatter.formatWithConflicts(originalMessage,
  172. Arrays.asList(new String[] { "path1" }));
  173. assertEquals("Header Line\n\nCommit body\n\nConflicts:\n\tpath1\n",
  174. message);
  175. }
  176. @Test
  177. public void testFormatWithConflictsNoFooterNoLineBreak() {
  178. String originalMessage = "Header Line\n\nCommit body";
  179. String message = formatter.formatWithConflicts(originalMessage,
  180. Arrays.asList(new String[] { "path1" }));
  181. assertEquals("Header Line\n\nCommit body\n\nConflicts:\n\tpath1\n",
  182. message);
  183. }
  184. @Test
  185. public void testFormatWithConflictsWithFooters() {
  186. String originalMessage = "Header Line\n\nCommit body\n\nChangeId:"
  187. + " I123456789123456789123456789123456789\nBug:1234567\n";
  188. String message = formatter.formatWithConflicts(originalMessage,
  189. Arrays.asList(new String[] { "path1" }));
  190. assertEquals(
  191. "Header Line\n\nCommit body\n\nConflicts:\n\tpath1\n\n"
  192. + "ChangeId: I123456789123456789123456789123456789\nBug:1234567\n",
  193. message);
  194. }
  195. @Test
  196. public void testFormatWithConflictsWithFooterlikeLineInBody() {
  197. String originalMessage = "Header Line\n\nCommit body\nBug:1234567\nMore Body\n\nChangeId:"
  198. + " I123456789123456789123456789123456789\nBug:1234567\n";
  199. String message = formatter.formatWithConflicts(originalMessage,
  200. Arrays.asList(new String[] { "path1" }));
  201. assertEquals(
  202. "Header Line\n\nCommit body\nBug:1234567\nMore Body\n\nConflicts:\n\tpath1\n\n"
  203. + "ChangeId: I123456789123456789123456789123456789\nBug:1234567\n",
  204. message);
  205. }
  206. }