1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
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
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
176
177 if (useAnnotatedTags) {
178 assertEquals("v1.1.1", describe(c1));
179 assertEquals("v1.1.1-1-gb89dead", describe(c2));
180
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
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
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
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
212
213
214
215
216
217
218
219
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
251
252
253
254
255
256
257
258
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));
279 assertEquals("t1-1-g0244e7f", describe(c3));
280 } else {
281 assertEquals(null, describe(c4));
282 assertEquals(null, describe(c3));
283 }
284 }
285
286
287
288
289
290
291
292
293
294
295
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));
310
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));
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
345
346
347
348
349
350
351
352
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
377
378
379
380
381
382
383
384
385
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 }