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.eclipse.jgit.lib.Constants.R_TAGS;
46
47 import java.io.IOException;
48 import java.text.MessageFormat;
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.Collections;
52 import java.util.Comparator;
53 import java.util.Date;
54 import java.util.List;
55 import java.util.Map;
56 import java.util.Optional;
57 import java.util.stream.Collectors;
58 import java.util.stream.Stream;
59
60 import org.eclipse.jgit.api.errors.GitAPIException;
61 import org.eclipse.jgit.api.errors.JGitInternalException;
62 import org.eclipse.jgit.api.errors.RefNotFoundException;
63 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
64 import org.eclipse.jgit.errors.InvalidPatternException;
65 import org.eclipse.jgit.errors.MissingObjectException;
66 import org.eclipse.jgit.ignore.internal.IMatcher;
67 import org.eclipse.jgit.ignore.internal.PathMatcher;
68 import org.eclipse.jgit.internal.JGitText;
69 import org.eclipse.jgit.lib.Constants;
70 import org.eclipse.jgit.lib.ObjectId;
71 import org.eclipse.jgit.lib.Ref;
72 import org.eclipse.jgit.lib.Repository;
73 import org.eclipse.jgit.revwalk.RevCommit;
74 import org.eclipse.jgit.revwalk.RevFlag;
75 import org.eclipse.jgit.revwalk.RevFlagSet;
76 import org.eclipse.jgit.revwalk.RevTag;
77 import org.eclipse.jgit.revwalk.RevWalk;
78
79
80
81
82
83
84 public class DescribeCommand extends GitCommand<String> {
85 private final RevWalk w;
86
87
88
89
90 private RevCommit target;
91
92
93
94
95
96
97 private int maxCandidates = 10;
98
99
100
101
102 private boolean longDesc;
103
104
105
106
107 private List<IMatcher> matchers = new ArrayList<>();
108
109
110
111
112 private boolean useTags = false;
113
114
115
116
117
118
119
120 protected DescribeCommand(Repository repo) {
121 super(repo);
122 w = new RevWalk(repo);
123 w.setRetainBody(false);
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public DescribeCommand setTarget(ObjectId target) throws IOException {
140 this.target = w.parseCommit(target);
141 return this;
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 public DescribeCommand setTarget(String rev) throws IOException,
160 RefNotFoundException {
161 ObjectId id = repo.resolve(rev);
162 if (id == null)
163 throw new RefNotFoundException(MessageFormat.format(JGitText.get().refNotResolved, rev));
164 return setTarget(id);
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179 public DescribeCommand setLong(boolean longDesc) {
180 this.longDesc = longDesc;
181 return this;
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195 public DescribeCommand setTags(boolean tags) {
196 this.useTags = tags;
197 return this;
198 }
199
200 private String longDescription(Ref tag, int depth, ObjectId tip)
201 throws IOException {
202 return String.format(
203 "%s-%d-g%s", tag.getName().substring(R_TAGS.length()),
204 Integer.valueOf(depth), w.getObjectReader().abbreviate(tip)
205 .name());
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223 public DescribeCommand setMatch(String... patterns) throws InvalidPatternException {
224 for (String p : patterns) {
225 matchers.add(PathMatcher.createPathMatcher(p, null, false));
226 }
227 return this;
228 }
229
230 private final Comparator<Ref> TAG_TIE_BREAKER = new Comparator<Ref>() {
231
232 @Override
233 public int compare(Reff" href="../../../../org/eclipse/jgit/lib/Ref.html#Ref">Ref o1, Ref o2) {
234 try {
235 return tagDate(o2).compareTo(tagDate(o1));
236 } catch (IOException e) {
237 return 0;
238 }
239 }
240
241 private Date tagDate(Ref tag) throws IOException {
242 RevTag t = w.parseTag(tag.getObjectId());
243 w.parseBody(t);
244 return t.getTaggerIdent().getWhen();
245 }
246 };
247
248 private Optional<Ref> getBestMatch(List<Ref> tags) {
249 if (tags == null || tags.size() == 0) {
250 return Optional.empty();
251 } else if (matchers.size() == 0) {
252 Collections.sort(tags, TAG_TIE_BREAKER);
253 return Optional.of(tags.get(0));
254 } else {
255
256
257 Stream<Ref> matchingTags = Stream.empty();
258 for (IMatcher matcher : matchers) {
259 Stream<Ref> m = tags.stream().filter(
260 tag -> matcher.matches(tag.getName(), false, false));
261 matchingTags = Stream.of(matchingTags, m).flatMap(i -> i);
262 }
263 return matchingTags.sorted(TAG_TIE_BREAKER).findFirst();
264 }
265 }
266
267 private ObjectId getObjectIdFromRef(Ref r) throws JGitInternalException {
268 try {
269 ObjectId key = repo.getRefDatabase().peel(r).getPeeledObjectId();
270 if (key == null) {
271 key = r.getObjectId();
272 }
273 return key;
274 } catch (IOException e) {
275 throw new JGitInternalException(e.getMessage(), e);
276 }
277 }
278
279
280
281
282
283
284
285 @Override
286 public String call() throws GitAPIException {
287 try {
288 checkCallable();
289 if (target == null) {
290 setTarget(Constants.HEAD);
291 }
292
293 Collection<Ref> tagList = repo.getRefDatabase()
294 .getRefsByPrefix(R_TAGS);
295 Map<ObjectId, List<Ref>> tags = tagList.stream()
296 .filter(this::filterLightweightTags)
297 .collect(Collectors.groupingBy(this::getObjectIdFromRef));
298
299
300 final RevFlagSett.html#RevFlagSet">RevFlagSet allFlags = new RevFlagSet();
301
302
303
304
305 class Candidate {
306 final Ref tag;
307 final RevFlag flag;
308
309
310
311
312
313 int depth;
314
315 Candidate(RevCommit commit, Ref tag) {
316 this.tag = tag;
317 this.flag = w.newFlag(tag.getName());
318
319 allFlags.add(flag);
320 w.carry(flag);
321 commit.add(flag);
322
323
324
325
326 commit.carry(flag);
327 }
328
329
330
331
332 boolean reaches(RevCommit c) {
333 return c.has(flag);
334 }
335
336 String describe(ObjectId tip) throws IOException {
337 return longDescription(tag, depth, tip);
338 }
339
340 }
341 List<Candidate> candidates = new ArrayList<>();
342
343
344 Optional<Ref> bestMatch = getBestMatch(tags.get(target));
345 if (bestMatch.isPresent()) {
346 return longDesc ? longDescription(bestMatch.get(), 0, target) :
347 bestMatch.get().getName().substring(R_TAGS.length());
348 }
349
350 w.markStart(target);
351
352 int seen = 0;
353 RevCommit c;
354 while ((c = w.next()) != null) {
355 if (!c.hasAny(allFlags)) {
356
357
358
359 bestMatch = getBestMatch(tags.get(c));
360 if (bestMatch.isPresent()) {
361 Candidate cd = new Candidate(c, bestMatch.get());
362 candidates.add(cd);
363 cd.depth = seen;
364 }
365 }
366
367
368
369 for (Candidate cd : candidates) {
370 if (!cd.reaches(c))
371 cd.depth++;
372 }
373
374
375
376
377 if (candidates.size() >= maxCandidates)
378 break;
379
380
381
382
383 seen++;
384 }
385
386
387
388 while ((c = w.next()) != null) {
389 if (c.hasAll(allFlags)) {
390
391 for (RevCommit p : c.getParents())
392 p.add(RevFlag.SEEN);
393 } else {
394 for (Candidate cd : candidates) {
395 if (!cd.reaches(c))
396 cd.depth++;
397 }
398 }
399 }
400
401
402 if (candidates.isEmpty())
403 return null;
404
405 Candidate best = Collections.min(candidates, new Comparator<Candidate>() {
406 @Override
407 public int compare(Candidate o1, Candidate o2) {
408 return o1.depth - o2.depth;
409 }
410 });
411
412 return best.describe(target);
413 } catch (IOException e) {
414 throw new JGitInternalException(e.getMessage(), e);
415 } finally {
416 setCallable(false);
417 w.close();
418 }
419 }
420
421
422
423
424
425
426
427
428
429 @SuppressWarnings("null")
430 private boolean filterLightweightTags(Ref ref) {
431 ObjectId id = ref.getObjectId();
432 try {
433 return this.useTags || (id != null && (w.parseTag(id) != null));
434 } catch (IOException e) {
435 return false;
436 }
437 }
438 }