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.1KB

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