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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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.assertNotNull;
  47. import static org.junit.Assert.assertTrue;
  48. import java.io.File;
  49. import java.io.FileWriter;
  50. import java.io.IOException;
  51. import java.util.Arrays;
  52. import java.util.Collection;
  53. import org.eclipse.jgit.api.errors.GitAPIException;
  54. import org.eclipse.jgit.api.errors.RefNotFoundException;
  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(0)
  66. public boolean useAnnotatedTags;
  67. @Parameter(1)
  68. public boolean describeUseAllTags;
  69. @Parameters(name = "git tag -a {0}?-a: with git describe {1}?--tags:")
  70. public static Collection<Boolean[]> getUseAnnotatedTagsValues() {
  71. return Arrays.asList(new Boolean[][] { { Boolean.TRUE, Boolean.FALSE },
  72. { Boolean.FALSE, Boolean.FALSE },
  73. { Boolean.TRUE, Boolean.TRUE },
  74. { Boolean.FALSE, Boolean.TRUE } });
  75. }
  76. @Override
  77. public void setUp() throws Exception {
  78. super.setUp();
  79. git = new Git(db);
  80. }
  81. @Test(expected = RefNotFoundException.class)
  82. public void noTargetSet() throws Exception {
  83. git.describe().call();
  84. }
  85. @Test
  86. public void testDescribe() throws Exception {
  87. ObjectId c1 = modify("aaa");
  88. ObjectId c2 = modify("bbb");
  89. tag("alice-t1");
  90. ObjectId c3 = modify("ccc");
  91. tag("bob-t2");
  92. ObjectId c4 = modify("ddd");
  93. assertNameStartsWith(c4, "3e563c5");
  94. assertNull(describe(c1));
  95. assertNull(describe(c1, true));
  96. assertNull(describe(c1, "a*", "b*", "c*"));
  97. assertNull(describe(c2, "bob*"));
  98. assertNull(describe(c2, "?ob*"));
  99. if (useAnnotatedTags || describeUseAllTags) {
  100. assertEquals("alice-t1", describe(c2));
  101. assertEquals("alice-t1", describe(c2, "alice*"));
  102. assertEquals("alice-t1", describe(c2, "a*", "b*", "c*"));
  103. assertEquals("bob-t2", describe(c3));
  104. assertEquals("bob-t2-0-g44579eb", describe(c3, true));
  105. assertEquals("alice-t1-1-g44579eb", describe(c3, "alice*"));
  106. assertEquals("alice-t1-1-g44579eb", describe(c3, "a??c?-t*"));
  107. assertEquals("bob-t2", describe(c3, "bob*"));
  108. assertEquals("bob-t2", describe(c3, "?ob*"));
  109. assertEquals("bob-t2", describe(c3, "a*", "b*", "c*"));
  110. // the value verified with git-describe(1)
  111. assertEquals("bob-t2-1-g3e563c5", describe(c4));
  112. assertEquals("bob-t2-1-g3e563c5", describe(c4, true));
  113. assertEquals("alice-t1-2-g3e563c5", describe(c4, "alice*"));
  114. assertEquals("bob-t2-1-g3e563c5", describe(c4, "bob*"));
  115. assertEquals("bob-t2-1-g3e563c5", describe(c4, "a*", "b*", "c*"));
  116. } else {
  117. assertEquals(null, describe(c2));
  118. assertEquals(null, describe(c3));
  119. assertEquals(null, describe(c4));
  120. }
  121. // test default target
  122. if (useAnnotatedTags) {
  123. assertEquals("bob-t2-1-g3e563c5", git.describe().call());
  124. assertEquals("bob-t2-1-g3e563c5",
  125. git.describe().setTags(false).call());
  126. assertEquals("bob-t2-1-g3e563c5",
  127. git.describe().setTags(true).call());
  128. } else {
  129. assertEquals(null, git.describe().call());
  130. assertEquals(null, git.describe().setTags(false).call());
  131. assertEquals("bob-t2-1-g3e563c5",
  132. git.describe().setTags(true).call());
  133. }
  134. }
  135. @Test
  136. public void testDescribeMultiMatch() throws Exception {
  137. ObjectId c1 = modify("aaa");
  138. tag("v1.0.0");
  139. tick();
  140. tag("v1.0.1");
  141. tick();
  142. tag("v1.1.0");
  143. tick();
  144. tag("v1.1.1");
  145. ObjectId c2 = modify("bbb");
  146. if (!useAnnotatedTags && !describeUseAllTags) {
  147. assertEquals(null, describe(c1));
  148. assertEquals(null, describe(c2));
  149. return;
  150. }
  151. // Ensure that if we're interested in any tags, we get the most recent tag
  152. // as per Git behaviour since 1.7.1.1
  153. if (useAnnotatedTags) {
  154. assertEquals("v1.1.1", describe(c1));
  155. assertEquals("v1.1.1-1-gb89dead", describe(c2));
  156. // Ensure that if we're only interested in one of multiple tags, we get the right match
  157. assertEquals("v1.0.1", describe(c1, "v1.0*"));
  158. assertEquals("v1.1.1", describe(c1, "v1.1*"));
  159. assertEquals("v1.0.1-1-gb89dead", describe(c2, "v1.0*"));
  160. assertEquals("v1.1.1-1-gb89dead", describe(c2, "v1.1*"));
  161. // Ensure that ordering of match precedence is preserved as per Git behaviour
  162. assertEquals("v1.1.1", describe(c1, "v1.0*", "v1.1*"));
  163. assertEquals("v1.1.1", describe(c1, "v1.1*", "v1.0*"));
  164. assertEquals("v1.1.1-1-gb89dead", describe(c2, "v1.0*", "v1.1*"));
  165. assertEquals("v1.1.1-1-gb89dead", describe(c2, "v1.1*", "v1.0*"));
  166. } else {
  167. // no timestamps so no guarantees on which tag is chosen
  168. assertNotNull(describe(c1));
  169. assertNotNull(describe(c2));
  170. assertNotNull(describe(c1, "v1.0*"));
  171. assertNotNull(describe(c1, "v1.1*"));
  172. assertNotNull(describe(c2, "v1.0*"));
  173. assertNotNull(describe(c2, "v1.1*"));
  174. // Ensure that ordering of match precedence is preserved as per Git behaviour
  175. assertNotNull(describe(c1, "v1.0*", "v1.1*"));
  176. assertNotNull(describe(c1, "v1.1*", "v1.0*"));
  177. assertNotNull(describe(c2, "v1.0*", "v1.1*"));
  178. assertNotNull(describe(c2, "v1.1*", "v1.0*"));
  179. }
  180. }
  181. /**
  182. * Make sure it finds a tag when not all ancestries include a tag.
  183. *
  184. * <pre>
  185. * c1 -+-&gt; T -
  186. * | |
  187. * +-&gt; c3 -+-&gt; c4
  188. * </pre>
  189. *
  190. * @throws Exception
  191. */
  192. @Test
  193. public void testDescribeBranch() throws Exception {
  194. ObjectId c1 = modify("aaa");
  195. ObjectId c2 = modify("bbb");
  196. tag("t");
  197. branch("b", c1);
  198. ObjectId c3 = modify("ccc");
  199. ObjectId c4 = merge(c2);
  200. assertNameStartsWith(c4, "119892b");
  201. if (useAnnotatedTags || describeUseAllTags) {
  202. assertEquals("2 commits: c4 and c3", "t-2-g119892b", describe(c4));
  203. } else {
  204. assertEquals(null, describe(c4));
  205. }
  206. assertNull(describe(c3));
  207. assertNull(describe(c3, true));
  208. }
  209. private void branch(String name, ObjectId base) throws GitAPIException {
  210. git.checkout().setCreateBranch(true).setName(name)
  211. .setStartPoint(base.name()).call();
  212. }
  213. /**
  214. * When t2 dominates t1, it's clearly preferable to describe by using t2.
  215. *
  216. * <pre>
  217. * t1 -+-&gt; t2 -
  218. * | |
  219. * +-&gt; c3 -+-&gt; c4
  220. * </pre>
  221. *
  222. * @throws Exception
  223. */
  224. @Test
  225. public void t1DominatesT2() throws Exception {
  226. ObjectId c1 = modify("aaa");
  227. tag("t1");
  228. ObjectId c2 = modify("bbb");
  229. tag("t2");
  230. branch("b", c1);
  231. ObjectId c3 = modify("ccc");
  232. assertNameStartsWith(c3, "0244e7f");
  233. ObjectId c4 = merge(c2);
  234. assertNameStartsWith(c4, "119892b");
  235. if (useAnnotatedTags || describeUseAllTags) {
  236. assertEquals("t2-2-g119892b", describe(c4)); // 2 commits: c4 and c3
  237. assertEquals("t1-1-g0244e7f", describe(c3));
  238. } else {
  239. assertEquals(null, describe(c4));
  240. assertEquals(null, describe(c3));
  241. }
  242. }
  243. /**
  244. * When t1 annotated dominates t2 lightweight tag
  245. *
  246. * <pre>
  247. * t1 -+-> t2 -
  248. * | |
  249. * +-> c3 -+-> c4
  250. * </pre>
  251. *
  252. * @throws Exception
  253. */
  254. @Test
  255. public void t1AnnotatedDominatesT2lightweight() throws Exception {
  256. ObjectId c1 = modify("aaa");
  257. tag("t1", useAnnotatedTags);
  258. ObjectId c2 = modify("bbb");
  259. tag("t2", false);
  260. assertNameStartsWith(c2, "3747db3");
  261. if (useAnnotatedTags && !describeUseAllTags) {
  262. assertEquals(
  263. "only annotated tag t1 expected to be used for describe",
  264. "t1-1-g3747db3", describe(c2)); // 1 commits: t2 overridden
  265. // by t1
  266. } else if (!useAnnotatedTags && !describeUseAllTags) {
  267. assertEquals("no commits to describe expected", null, describe(c2));
  268. } else {
  269. assertEquals("lightweight tag t2 expected in describe", "t2",
  270. describe(c2));
  271. }
  272. branch("b", c1);
  273. ObjectId c3 = modify("ccc");
  274. assertNameStartsWith(c3, "0244e7f");
  275. if (useAnnotatedTags || describeUseAllTags) {
  276. assertEquals("t1-1-g0244e7f", describe(c3));
  277. }
  278. ObjectId c4 = merge(c2);
  279. assertNameStartsWith(c4, "119892b");
  280. if (describeUseAllTags) {
  281. assertEquals(
  282. "2 commits for describe commit increment expected since lightweight tag: c4 and c3",
  283. "t2-2-g119892b", describe(c4)); // 2 commits: c4 and c3
  284. } else if (!useAnnotatedTags && !describeUseAllTags) {
  285. assertEquals("no matching commits expected", null, describe(c4));
  286. } else {
  287. assertEquals(
  288. "3 commits for describe commit increment expected since annotated tag: c4 and c3 and c2",
  289. "t1-3-g119892b", describe(c4)); //
  290. }
  291. }
  292. /**
  293. * When t1 is nearer than t2, t2 should be found
  294. *
  295. * <pre>
  296. * c1 -+-&gt; c2 -&gt; t1 -+
  297. * | |
  298. * +-&gt; t2 -&gt; c3 -+-&gt; c4
  299. * </pre>
  300. *
  301. * @throws Exception
  302. */
  303. @Test
  304. public void t1nearerT2() throws Exception {
  305. ObjectId c1 = modify("aaa");
  306. modify("bbb");
  307. ObjectId t1 = modify("ccc");
  308. tag("t1");
  309. branch("b", c1);
  310. modify("ddd");
  311. tag("t2");
  312. modify("eee");
  313. ObjectId c4 = merge(t1);
  314. assertNameStartsWith(c4, "bb389a4");
  315. if (useAnnotatedTags || describeUseAllTags) {
  316. assertEquals("t1-3-gbb389a4", describe(c4));
  317. } else {
  318. assertEquals(null, describe(c4));
  319. }
  320. }
  321. /**
  322. * When t1 and t2 have same depth native git seems to add the depths of both
  323. * paths
  324. *
  325. * <pre>
  326. * c1 -+-&gt; t1 -&gt; c2 -+
  327. * | |
  328. * +-&gt; t2 -&gt; c3 -+-&gt; c4
  329. * </pre>
  330. *
  331. * @throws Exception
  332. */
  333. @Test
  334. public void t1sameDepthT2() throws Exception {
  335. ObjectId c1 = modify("aaa");
  336. modify("bbb");
  337. tag("t1");
  338. ObjectId c2 = modify("ccc");
  339. branch("b", c1);
  340. modify("ddd");
  341. tag("t2");
  342. modify("eee");
  343. ObjectId c4 = merge(c2);
  344. assertNameStartsWith(c4, "bb389a4");
  345. if (useAnnotatedTags || describeUseAllTags) {
  346. assertEquals("t2-4-gbb389a4", describe(c4));
  347. } else {
  348. assertEquals(null, describe(c4));
  349. }
  350. }
  351. private ObjectId merge(ObjectId c2) throws GitAPIException {
  352. return git.merge().include(c2).call().getNewHead();
  353. }
  354. private ObjectId modify(String content) throws Exception {
  355. File a = new File(db.getWorkTree(), "a.txt");
  356. touch(a, content);
  357. return git.commit().setAll(true).setMessage(content).call().getId();
  358. }
  359. private void tag(String tag) throws GitAPIException {
  360. tag(tag, this.useAnnotatedTags);
  361. }
  362. private void tag(String tag, boolean annotatedTag) throws GitAPIException {
  363. TagCommand tagCommand = git.tag().setName(tag)
  364. .setAnnotated(annotatedTag);
  365. if (annotatedTag) {
  366. tagCommand.setMessage(tag);
  367. }
  368. tagCommand.call();
  369. }
  370. private static void touch(File f, String contents) throws Exception {
  371. try (FileWriter w = new FileWriter(f)) {
  372. w.write(contents);
  373. }
  374. }
  375. private String describe(ObjectId c1, boolean longDesc)
  376. throws GitAPIException, IOException {
  377. return git.describe().setTarget(c1).setTags(describeUseAllTags)
  378. .setLong(longDesc).call();
  379. }
  380. private String describe(ObjectId c1) throws GitAPIException, IOException {
  381. return describe(c1, false);
  382. }
  383. private String describe(ObjectId c1, String... patterns) throws Exception {
  384. return git.describe().setTarget(c1).setTags(describeUseAllTags)
  385. .setMatch(patterns).call();
  386. }
  387. private static void assertNameStartsWith(ObjectId c4, String prefix) {
  388. assertTrue(c4.name(), c4.name().startsWith(prefix));
  389. }
  390. }