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