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 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 java.util.Arrays;
  51. import java.util.Collection;
  52. import org.eclipse.jgit.api.errors.GitAPIException;
  53. import org.eclipse.jgit.api.errors.RefNotFoundException;
  54. import org.eclipse.jgit.errors.InvalidPatternException;
  55. import org.eclipse.jgit.junit.RepositoryTestCase;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.junit.Test;
  58. import org.junit.runner.RunWith;
  59. import org.junit.runners.Parameterized;
  60. import org.junit.runners.Parameterized.Parameter;
  61. import org.junit.runners.Parameterized.Parameters;
  62. @RunWith(Parameterized.class)
  63. public class DescribeCommandTest extends RepositoryTestCase {
  64. private Git git;
  65. @Parameter
  66. public boolean useAnnotatedTags;
  67. @Parameters
  68. public static Collection<Boolean[]> getUseAnnotatedTagsValues() {
  69. return Arrays.asList(new Boolean[][] { { Boolean.TRUE },
  70. { Boolean.FALSE } });
  71. }
  72. @Override
  73. public void setUp() throws Exception {
  74. super.setUp();
  75. git = new Git(db);
  76. }
  77. @Test(expected = RefNotFoundException.class)
  78. public void noTargetSet() throws Exception {
  79. git.describe().call();
  80. }
  81. @Test
  82. public void testDescribe() throws Exception {
  83. ObjectId c1 = modify("aaa");
  84. ObjectId c2 = modify("bbb");
  85. tag("alice-t1");
  86. ObjectId c3 = modify("ccc");
  87. tag("bob-t2");
  88. ObjectId c4 = modify("ddd");
  89. assertNull(describe(c1));
  90. assertNull(describe(c1, true));
  91. assertNull(describe(c1, "a*", "b*", "c*"));
  92. assertEquals("alice-t1", describe(c2));
  93. assertEquals("alice-t1", describe(c2, "alice*"));
  94. assertNull(describe(c2, "bob*"));
  95. assertNull(describe(c2, "?ob*"));
  96. assertEquals("alice-t1", describe(c2, "a*", "b*", "c*"));
  97. assertEquals("bob-t2", describe(c3));
  98. assertEquals("bob-t2-0-g44579eb", describe(c3, true));
  99. assertEquals("alice-t1-1-g44579eb", describe(c3, "alice*"));
  100. assertEquals("alice-t1-1-g44579eb", describe(c3, "a??c?-t*"));
  101. assertEquals("bob-t2", describe(c3, "bob*"));
  102. assertEquals("bob-t2", describe(c3, "?ob*"));
  103. assertEquals("bob-t2", describe(c3, "a*", "b*", "c*"));
  104. assertNameStartsWith(c4, "3e563c5");
  105. // the value verified with git-describe(1)
  106. assertEquals("bob-t2-1-g3e563c5", describe(c4));
  107. assertEquals("bob-t2-1-g3e563c5", describe(c4, true));
  108. assertEquals("alice-t1-2-g3e563c5", describe(c4, "alice*"));
  109. assertEquals("bob-t2-1-g3e563c5", describe(c4, "bob*"));
  110. assertEquals("bob-t2-1-g3e563c5", describe(c4, "a*", "b*", "c*"));
  111. // test default target
  112. assertEquals("bob-t2-1-g3e563c5", git.describe().call());
  113. }
  114. @Test
  115. public void testDescribeMultiMatch() throws Exception {
  116. ObjectId c1 = modify("aaa");
  117. tag("v1.0.0");
  118. tag("v1.1.1");
  119. ObjectId c2 = modify("bbb");
  120. // Ensure that if we're interested in any tags, we get the first match as per Git behaviour
  121. assertEquals("v1.0.0", describe(c1));
  122. assertEquals("v1.0.0-1-g3747db3", describe(c2));
  123. // Ensure that if we're only interested in one of multiple tags, we get the right match
  124. assertEquals("v1.0.0", describe(c1, "v1.0*"));
  125. assertEquals("v1.1.1", describe(c1, "v1.1*"));
  126. assertEquals("v1.0.0-1-g3747db3", describe(c2, "v1.0*"));
  127. assertEquals("v1.1.1-1-g3747db3", describe(c2, "v1.1*"));
  128. // Ensure that ordering of match precedence is preserved as per Git behaviour
  129. assertEquals("v1.0.0", describe(c1, "v1.0*", "v1.1*"));
  130. assertEquals("v1.1.1", describe(c1, "v1.1*", "v1.0*"));
  131. assertEquals("v1.0.0-1-g3747db3", describe(c2, "v1.0*", "v1.1*"));
  132. assertEquals("v1.1.1-1-g3747db3", describe(c2, "v1.1*", "v1.0*"));
  133. }
  134. /**
  135. * Make sure it finds a tag when not all ancestries include a tag.
  136. *
  137. * <pre>
  138. * c1 -+-&gt; T -
  139. * | |
  140. * +-&gt; c3 -+-&gt; c4
  141. * </pre>
  142. *
  143. * @throws Exception
  144. */
  145. @Test
  146. public void testDescribeBranch() throws Exception {
  147. ObjectId c1 = modify("aaa");
  148. ObjectId c2 = modify("bbb");
  149. tag("t");
  150. branch("b", c1);
  151. ObjectId c3 = modify("ccc");
  152. ObjectId c4 = merge(c2);
  153. assertNameStartsWith(c4, "119892b");
  154. assertEquals("t-2-g119892b", describe(c4)); // 2 commits: c4 and c3
  155. assertNull(describe(c3));
  156. assertNull(describe(c3, true));
  157. }
  158. private void branch(String name, ObjectId base) throws GitAPIException {
  159. git.checkout().setCreateBranch(true).setName(name)
  160. .setStartPoint(base.name()).call();
  161. }
  162. /**
  163. * When t2 dominates t1, it's clearly preferable to describe by using t2.
  164. *
  165. * <pre>
  166. * t1 -+-&gt; t2 -
  167. * | |
  168. * +-&gt; c3 -+-&gt; c4
  169. * </pre>
  170. *
  171. * @throws Exception
  172. */
  173. @Test
  174. public void t1DominatesT2() throws Exception {
  175. ObjectId c1 = modify("aaa");
  176. tag("t1");
  177. ObjectId c2 = modify("bbb");
  178. tag("t2");
  179. branch("b", c1);
  180. ObjectId c3 = modify("ccc");
  181. ObjectId c4 = merge(c2);
  182. assertNameStartsWith(c4, "119892b");
  183. assertEquals("t2-2-g119892b", describe(c4)); // 2 commits: c4 and c3
  184. assertNameStartsWith(c3, "0244e7f");
  185. assertEquals("t1-1-g0244e7f", describe(c3));
  186. }
  187. /**
  188. * When t1 is nearer than t2, t2 should be found
  189. *
  190. * <pre>
  191. * c1 -+-&gt; c2 -&gt; t1 -+
  192. * | |
  193. * +-&gt; t2 -&gt; c3 -+-&gt; c4
  194. * </pre>
  195. *
  196. * @throws Exception
  197. */
  198. @Test
  199. public void t1nearerT2() throws Exception {
  200. ObjectId c1 = modify("aaa");
  201. modify("bbb");
  202. ObjectId t1 = modify("ccc");
  203. tag("t1");
  204. branch("b", c1);
  205. modify("ddd");
  206. tag("t2");
  207. modify("eee");
  208. ObjectId c4 = merge(t1);
  209. assertNameStartsWith(c4, "bb389a4");
  210. assertEquals("t1-3-gbb389a4", describe(c4));
  211. }
  212. /**
  213. * When t1 and t2 have same depth native git seems to add the depths of both
  214. * paths
  215. *
  216. * <pre>
  217. * c1 -+-&gt; t1 -&gt; c2 -+
  218. * | |
  219. * +-&gt; t2 -&gt; c3 -+-&gt; c4
  220. * </pre>
  221. *
  222. * @throws Exception
  223. */
  224. @Test
  225. public void t1sameDepthT2() throws Exception {
  226. ObjectId c1 = modify("aaa");
  227. modify("bbb");
  228. tag("t1");
  229. ObjectId c2 = modify("ccc");
  230. branch("b", c1);
  231. modify("ddd");
  232. tag("t2");
  233. modify("eee");
  234. ObjectId c4 = merge(c2);
  235. assertNameStartsWith(c4, "bb389a4");
  236. assertEquals("t2-4-gbb389a4", describe(c4));
  237. }
  238. private ObjectId merge(ObjectId c2) throws GitAPIException {
  239. return git.merge().include(c2).call().getNewHead();
  240. }
  241. private ObjectId modify(String content) throws Exception {
  242. File a = new File(db.getWorkTree(), "a.txt");
  243. touch(a, content);
  244. return git.commit().setAll(true).setMessage(content).call().getId();
  245. }
  246. private void tag(String tag) throws GitAPIException {
  247. TagCommand tagCommand = git.tag().setName(tag)
  248. .setAnnotated(useAnnotatedTags);
  249. if (useAnnotatedTags)
  250. tagCommand.setMessage(tag);
  251. tagCommand.call();
  252. }
  253. private static void touch(File f, String contents) throws Exception {
  254. FileWriter w = new FileWriter(f);
  255. w.write(contents);
  256. w.close();
  257. }
  258. private String describe(ObjectId c1, boolean longDesc)
  259. throws GitAPIException, IOException {
  260. return git.describe().setTarget(c1).setLong(longDesc).call();
  261. }
  262. private String describe(ObjectId c1) throws GitAPIException, IOException {
  263. return describe(c1, false);
  264. }
  265. private String describe(ObjectId c1, String... patterns) throws GitAPIException, IOException, InvalidPatternException {
  266. return git.describe().setTarget(c1).setMatch(patterns).call();
  267. }
  268. private static void assertNameStartsWith(ObjectId c4, String prefix) {
  269. assertTrue(c4.name(), c4.name().startsWith(prefix));
  270. }
  271. }