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