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

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