Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

NameRevCommandTest.java 6.6KB

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