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.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
110 assertEquals("t2-1-g3e563c5", describe(c4));
111 assertEquals("t2-1-g3e563c5", describe(c4, true));
112
113
114 assertEquals("t2-1-g3e563c5", git.describe().call());
115 }
116
117
118
119
120
121
122
123
124
125
126
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));
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
154
155
156
157
158
159
160
161
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));
179
180 assertNameStartsWith(c3, "0244e7f");
181 assertEquals("t1-1-g0244e7f", describe(c3));
182 }
183
184
185
186
187
188
189
190
191
192
193
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
214
215
216
217
218
219
220
221
222
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 }