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.

DescribeCommandTest.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (C) 2013, CloudBees, 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.assertNull;
  46. import static org.junit.Assert.assertTrue;
  47. import java.io.File;
  48. import java.io.FileWriter;
  49. import java.io.IOException;
  50. import org.eclipse.jgit.api.errors.GitAPIException;
  51. import org.eclipse.jgit.api.errors.RefNotFoundException;
  52. import org.eclipse.jgit.junit.RepositoryTestCase;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.junit.Test;
  55. public class DescribeCommandTest extends RepositoryTestCase {
  56. private Git git;
  57. @Override
  58. public void setUp() throws Exception {
  59. super.setUp();
  60. git = new Git(db);
  61. }
  62. @Test(expected = RefNotFoundException.class)
  63. public void noTargetSet() throws Exception {
  64. git.describe().call();
  65. }
  66. @Test
  67. public void testDescribe() throws Exception {
  68. ObjectId c1 = modify("aaa");
  69. ObjectId c2 = modify("bbb");
  70. tag("t1");
  71. ObjectId c3 = modify("ccc");
  72. tag("t2");
  73. ObjectId c4 = modify("ddd");
  74. assertNull(describe(c1));
  75. assertEquals("t1", describe(c2));
  76. assertEquals("t2", describe(c3));
  77. assertNameStartsWith(c4, "3e563c5");
  78. // the value verified with git-describe(1)
  79. assertEquals("t2-1-g3e563c5", describe(c4));
  80. // test default target
  81. assertEquals("t2-1-g3e563c5", git.describe().call());
  82. }
  83. /**
  84. * Make sure it finds a tag when not all ancestries include a tag.
  85. *
  86. * <pre>
  87. * c1 -+-> T -
  88. * | |
  89. * +-> c3 -+-> c4
  90. * </pre>
  91. *
  92. * @throws Exception
  93. */
  94. @Test
  95. public void testDescribeBranch() throws Exception {
  96. ObjectId c1 = modify("aaa");
  97. ObjectId c2 = modify("bbb");
  98. tag("t");
  99. branch("b", c1);
  100. ObjectId c3 = modify("ccc");
  101. ObjectId c4 = merge(c2);
  102. assertNameStartsWith(c4, "119892b");
  103. assertEquals("t-2-g119892b", describe(c4)); // 2 commits: c4 and c3
  104. assertNull(describe(c3));
  105. }
  106. private void branch(String name, ObjectId base) throws GitAPIException {
  107. git.checkout().setCreateBranch(true).setName(name)
  108. .setStartPoint(base.name()).call();
  109. }
  110. /**
  111. * When t2 dominates t1, it's clearly preferable to describe by using t2.
  112. *
  113. * <pre>
  114. * t1 -+-> t2 -
  115. * | |
  116. * +-> c3 -+-> c4
  117. * </pre>
  118. *
  119. * @throws Exception
  120. */
  121. @Test
  122. public void t1DominatesT2() throws Exception {
  123. ObjectId c1 = modify("aaa");
  124. tag("t1");
  125. ObjectId c2 = modify("bbb");
  126. tag("t2");
  127. branch("b", c1);
  128. ObjectId c3 = modify("ccc");
  129. ObjectId c4 = merge(c2);
  130. assertNameStartsWith(c4, "119892b");
  131. assertEquals("t2-2-g119892b", describe(c4)); // 2 commits: c4 and c3
  132. assertNameStartsWith(c3, "0244e7f");
  133. assertEquals("t1-1-g0244e7f", describe(c3));
  134. }
  135. /**
  136. * When t1 is nearer than t2, t2 should be found
  137. *
  138. * <pre>
  139. * c1 -+-> c2 -> t1 -+
  140. * | |
  141. * +-> t2 -> c3 -+-> c4
  142. * </pre>
  143. *
  144. * @throws Exception
  145. */
  146. @Test
  147. public void t1nearerT2() throws Exception {
  148. ObjectId c1 = modify("aaa");
  149. modify("bbb");
  150. ObjectId t1 = modify("ccc");
  151. tag("t1");
  152. branch("b", c1);
  153. modify("ddd");
  154. tag("t2");
  155. modify("eee");
  156. ObjectId c4 = merge(t1);
  157. assertNameStartsWith(c4, "bb389a4");
  158. assertEquals("t1-3-gbb389a4", describe(c4));
  159. }
  160. /**
  161. * When t1 and t2 have same depth native git seems to add the depths of both
  162. * paths
  163. *
  164. * <pre>
  165. * c1 -+-> t1 -> c2 -+
  166. * | |
  167. * +-> t2 -> c3 -+-> c4
  168. * </pre>
  169. *
  170. * @throws Exception
  171. */
  172. @Test
  173. public void t1sameDepthT2() throws Exception {
  174. ObjectId c1 = modify("aaa");
  175. modify("bbb");
  176. tag("t1");
  177. ObjectId c2 = modify("ccc");
  178. branch("b", c1);
  179. modify("ddd");
  180. tag("t2");
  181. modify("eee");
  182. ObjectId c4 = merge(c2);
  183. assertNameStartsWith(c4, "bb389a4");
  184. assertEquals("t2-4-gbb389a4", describe(c4));
  185. }
  186. private ObjectId merge(ObjectId c2) throws GitAPIException {
  187. return git.merge().include(c2).call().getNewHead();
  188. }
  189. private ObjectId modify(String content) throws Exception {
  190. File a = new File(db.getWorkTree(), "a.txt");
  191. touch(a, content);
  192. return git.commit().setAll(true).setMessage(content).call().getId();
  193. }
  194. private void tag(String tag) throws GitAPIException {
  195. git.tag().setName(tag).setMessage(tag).call();
  196. }
  197. private static void touch(File f, String contents) throws Exception {
  198. FileWriter w = new FileWriter(f);
  199. w.write(contents);
  200. w.close();
  201. }
  202. private String describe(ObjectId c1) throws GitAPIException, IOException {
  203. return git.describe().setTarget(c1).call();
  204. }
  205. private static void assertNameStartsWith(ObjectId c4, String prefix) {
  206. assertTrue(c4.name(), c4.name().startsWith(prefix));
  207. }
  208. }