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