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.

NameRevCommandTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (C) 2013, Google Inc.
  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.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import java.util.Map;
  47. import org.eclipse.jgit.junit.RepositoryTestCase;
  48. import org.eclipse.jgit.junit.TestRepository;
  49. import org.eclipse.jgit.lib.ObjectId;
  50. import org.eclipse.jgit.lib.Repository;
  51. import org.eclipse.jgit.revwalk.RevCommit;
  52. import org.junit.Before;
  53. import org.junit.Test;
  54. public class NameRevCommandTest extends RepositoryTestCase {
  55. private TestRepository<Repository> tr;
  56. private Git git;
  57. @Override
  58. @Before
  59. public void setUp() throws Exception {
  60. super.setUp();
  61. tr = new TestRepository<>(db);
  62. git = new Git(db);
  63. }
  64. @Test
  65. public void nameExact() throws Exception {
  66. RevCommit c = tr.commit().create();
  67. tr.update("master", c);
  68. assertOneResult("master", c);
  69. }
  70. @Test
  71. public void prefix() throws Exception {
  72. RevCommit c = tr.commit().create();
  73. tr.update("refs/heads/master", c);
  74. tr.update("refs/tags/tag", c);
  75. assertOneResult("master", c);
  76. assertOneResult("master",
  77. git.nameRev().addPrefix("refs/heads/").addPrefix("refs/tags/"),
  78. c);
  79. assertOneResult("tag",
  80. git.nameRev().addPrefix("refs/tags/").addPrefix("refs/heads/"),
  81. c);
  82. }
  83. @Test
  84. public void ref() throws Exception {
  85. RevCommit c = tr.commit().create();
  86. tr.update("refs/heads/master", c);
  87. tr.update("refs/tags/tag", c);
  88. assertOneResult("master",
  89. git.nameRev().addRef(db.exactRef("refs/heads/master")), c);
  90. assertOneResult("tag",
  91. git.nameRev().addRef(db.exactRef("refs/tags/tag")), c);
  92. }
  93. @Test
  94. public void annotatedTags() throws Exception {
  95. RevCommit c = tr.commit().create();
  96. tr.update("refs/heads/master", c);
  97. tr.update("refs/tags/tag1", c);
  98. tr.update("refs/tags/tag2", tr.tag("tag2", c));
  99. assertOneResult("tag2", git.nameRev().addAnnotatedTags(), c);
  100. }
  101. @Test
  102. public void annotatedTagsNoResult() throws Exception {
  103. RevCommit c = tr.commit().create();
  104. tr.update("refs/heads/master", c);
  105. tr.update("refs/tags/tag1", c);
  106. tr.update("refs/tags/tag2", c);
  107. Map<ObjectId, String> result = git.nameRev()
  108. .add(c)
  109. .addAnnotatedTags()
  110. .call();
  111. assertTrue(result.toString(), result.isEmpty());
  112. }
  113. @Test
  114. public void simpleAncestor() throws Exception {
  115. // 0--1--2
  116. RevCommit c0 = tr.commit().create();
  117. RevCommit c1 = tr.commit().parent(c0).create();
  118. RevCommit c2 = tr.commit().parent(c1).create();
  119. tr.update("master", c2);
  120. Map<ObjectId, String> result = git.nameRev().add(c0).add(c1).add(c2).call();
  121. assertEquals(3, result.size());
  122. assertEquals("master~2", result.get(c0));
  123. assertEquals("master~1", result.get(c1));
  124. assertEquals("master", result.get(c2));
  125. }
  126. @Test
  127. public void multiplePathsNoMerge() throws Exception {
  128. // 0--1 <- master
  129. // \-2--3 <- branch
  130. RevCommit c0 = tr.commit().create();
  131. RevCommit c1 = tr.commit().parent(c0).create();
  132. RevCommit c2 = tr.commit().parent(c0).create();
  133. RevCommit c3 = tr.commit().parent(c2).create();
  134. tr.update("master", c1);
  135. tr.update("branch", c3);
  136. assertOneResult("master~1", c0);
  137. }
  138. @Test
  139. public void onePathMerge() throws Exception {
  140. // 0--1--3
  141. // \-2-/
  142. RevCommit c0 = tr.commit().create();
  143. RevCommit c1 = tr.commit().parent(c0).create();
  144. RevCommit c2 = tr.commit().parent(c0).create();
  145. RevCommit c3 = tr.commit().parent(c1).parent(c2).create();
  146. tr.update("master", c3);
  147. assertOneResult("master~2", c0);
  148. }
  149. @Test
  150. public void onePathMergeSecondParent() throws Exception {
  151. // 0--1-----4
  152. // \-2--3-/
  153. RevCommit c0 = tr.commit().create();
  154. RevCommit c1 = tr.commit().parent(c0).create();
  155. RevCommit c2 = tr.commit().parent(c0).create();
  156. RevCommit c3 = tr.commit().parent(c2).create();
  157. RevCommit c4 = tr.commit().parent(c1).parent(c3).create();
  158. tr.update("master", c4);
  159. assertOneResult("master^2", c3);
  160. assertOneResult("master^2~1", c2);
  161. }
  162. @Test
  163. public void onePathMergeLongerFirstParentPath() throws Exception {
  164. // 0--1--2--4
  165. // \--3---/
  166. RevCommit c0 = tr.commit().create();
  167. RevCommit c1 = tr.commit().parent(c0).create();
  168. RevCommit c2 = tr.commit().parent(c1).create();
  169. RevCommit c3 = tr.commit().parent(c0).create();
  170. RevCommit c4 = tr.commit().parent(c2).parent(c3).create();
  171. tr.update("master", c4);
  172. assertOneResult("master^2", c3);
  173. assertOneResult("master~3", c0);
  174. }
  175. @Test
  176. public void multiplePathsSecondParent() throws Exception {
  177. // 0--...--2
  178. // \--1--/
  179. RevCommit c0 = tr.commit().create();
  180. RevCommit c1 = tr.commit().parent(c0).create();
  181. RevCommit c = c0;
  182. int mergeCost = 5;
  183. for (int i = 0; i < mergeCost; i++) {
  184. c = tr.commit().parent(c).create();
  185. }
  186. RevCommit c2 = tr.commit().parent(c).parent(c1).create();
  187. tr.update("master", c2);
  188. assertOneResult("master^2~1", git.nameRev().setMergeCost(mergeCost), c0);
  189. }
  190. private static void assertOneResult(String expected, NameRevCommand nameRev,
  191. ObjectId id) throws Exception {
  192. Map<ObjectId, String> result = nameRev.add(id).call();
  193. assertEquals(1, result.size());
  194. assertEquals(expected, result.get(id));
  195. }
  196. private void assertOneResult(String expected, ObjectId id) throws Exception {
  197. assertOneResult(expected, git.nameRev(), id);
  198. }
  199. }