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