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

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