View Javadoc
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  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertNull;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.File;
50  import java.io.FileWriter;
51  import java.io.IOException;
52  import java.util.Arrays;
53  import java.util.Collection;
54  
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  
65  @RunWith(Parameterized.class)
66  public class DescribeCommandTest extends RepositoryTestCase {
67  
68  	private Git git;
69  
70  	@Parameter
71  	public boolean useAnnotatedTags;
72  
73  	@Parameters
74  	public static Collection<Boolean[]> getUseAnnotatedTagsValues() {
75  		return Arrays.asList(new Boolean[][] { { Boolean.TRUE },
76  				{ Boolean.FALSE } });
77  	}
78  
79  	@Override
80  	public void setUp() throws Exception {
81  		super.setUp();
82  		git = new Git(db);
83  	}
84  
85  	@Test(expected = RefNotFoundException.class)
86  	public void noTargetSet() throws Exception {
87  		git.describe().call();
88  	}
89  
90  	@Test
91  	public void testDescribe() throws Exception {
92  		ObjectId c1 = modify("aaa");
93  
94  		ObjectId c2 = modify("bbb");
95  		tag("t1");
96  
97  		ObjectId c3 = modify("ccc");
98  		tag("t2");
99  
100 		ObjectId c4 = modify("ddd");
101 
102 		assertNull(describe(c1));
103 		assertNull(describe(c1, true));
104 		assertEquals("t1", describe(c2));
105 		assertEquals("t2", describe(c3));
106 		assertEquals("t2-0-g44579eb", describe(c3, true));
107 
108 		assertNameStartsWith(c4, "3e563c5");
109 		// the value verified with git-describe(1)
110 		assertEquals("t2-1-g3e563c5", describe(c4));
111 		assertEquals("t2-1-g3e563c5", describe(c4, true));
112 
113 		// test default target
114 		assertEquals("t2-1-g3e563c5", git.describe().call());
115 	}
116 
117 	/**
118 	 * Make sure it finds a tag when not all ancestries include a tag.
119 	 *
120 	 * <pre>
121 	 * c1 -+-> T  -
122 	 *     |       |
123 	 *     +-> c3 -+-> c4
124 	 * </pre>
125 	 *
126 	 * @throws Exception
127 	 */
128 	@Test
129 	public void testDescribeBranch() throws Exception {
130 		ObjectId c1 = modify("aaa");
131 
132 		ObjectId c2 = modify("bbb");
133 		tag("t");
134 
135 		branch("b", c1);
136 
137 		ObjectId c3 = modify("ccc");
138 
139 		ObjectId c4 = merge(c2);
140 
141 		assertNameStartsWith(c4, "119892b");
142 		assertEquals("t-2-g119892b", describe(c4)); // 2 commits: c4 and c3
143 		assertNull(describe(c3));
144 		assertNull(describe(c3, true));
145 	}
146 
147 	private void branch(String name, ObjectId base) throws GitAPIException {
148 		git.checkout().setCreateBranch(true).setName(name)
149 				.setStartPoint(base.name()).call();
150 	}
151 
152 	/**
153 	 * When t2 dominates t1, it's clearly preferable to describe by using t2.
154 	 *
155 	 * <pre>
156 	 * t1 -+-> t2  -
157 	 *     |       |
158 	 *     +-> c3 -+-> c4
159 	 * </pre>
160 	 *
161 	 * @throws Exception
162 	 */
163 	@Test
164 	public void t1DominatesT2() throws Exception {
165 		ObjectId c1 = modify("aaa");
166 		tag("t1");
167 
168 		ObjectId c2 = modify("bbb");
169 		tag("t2");
170 
171 		branch("b", c1);
172 
173 		ObjectId c3 = modify("ccc");
174 
175 		ObjectId c4 = merge(c2);
176 
177 		assertNameStartsWith(c4, "119892b");
178 		assertEquals("t2-2-g119892b", describe(c4)); // 2 commits: c4 and c3
179 
180 		assertNameStartsWith(c3, "0244e7f");
181 		assertEquals("t1-1-g0244e7f", describe(c3));
182 	}
183 
184 	/**
185 	 * When t1 is nearer than t2, t2 should be found
186 	 *
187 	 * <pre>
188 	 * c1 -+-> c2 -> t1 -+
189 	 *     |             |
190 	 *     +-> t2 -> c3 -+-> c4
191 	 * </pre>
192 	 *
193 	 * @throws Exception
194 	 */
195 	@Test
196 	public void t1nearerT2() throws Exception {
197 		ObjectId c1 = modify("aaa");
198 		modify("bbb");
199 		ObjectId t1 = modify("ccc");
200 		tag("t1");
201 
202 		branch("b", c1);
203 		modify("ddd");
204 		tag("t2");
205 		modify("eee");
206 		ObjectId c4 = merge(t1);
207 
208 		assertNameStartsWith(c4, "bb389a4");
209 		assertEquals("t1-3-gbb389a4", describe(c4));
210 	}
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 -+-> t1 -> c2 -+
218 	 *     |             |
219 	 *     +-> t2 -> c3 -+-> 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 
231 		branch("b", c1);
232 		modify("ddd");
233 		tag("t2");
234 		modify("eee");
235 		ObjectId c4 = merge(c2);
236 
237 		assertNameStartsWith(c4, "bb389a4");
238 		assertEquals("t2-4-gbb389a4", describe(c4));
239 	}
240 
241 	private ObjectId merge(ObjectId c2) throws GitAPIException {
242 		return git.merge().include(c2).call().getNewHead();
243 	}
244 
245 	private ObjectId modify(String content) throws Exception {
246 		File a = new File(db.getWorkTree(), "a.txt");
247 		touch(a, content);
248 		return git.commit().setAll(true).setMessage(content).call().getId();
249 	}
250 
251 	private void tag(String tag) throws GitAPIException {
252 		TagCommand tagCommand = git.tag().setName(tag)
253 				.setAnnotated(useAnnotatedTags);
254 		if (useAnnotatedTags)
255 			tagCommand.setMessage(tag);
256 		tagCommand.call();
257 	}
258 
259 	private static void touch(File f, String contents) throws Exception {
260 		FileWriter w = new FileWriter(f);
261 		w.write(contents);
262 		w.close();
263 	}
264 
265 	private String describe(ObjectId c1, boolean longDesc)
266 			throws GitAPIException, IOException {
267 		return git.describe().setTarget(c1).setLong(longDesc).call();
268 	}
269 
270 	private String describe(ObjectId c1) throws GitAPIException, IOException {
271 		return describe(c1, false);
272 	}
273 
274 	private static void assertNameStartsWith(ObjectId c4, String prefix) {
275 		assertTrue(c4.name(), c4.name().startsWith(prefix));
276 	}
277 }