-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathDescribeCommand.java
More file actions
651 lines (555 loc) · 21.9 KB
/
DescribeCommand.java
File metadata and controls
651 lines (555 loc) · 21.9 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
* This file is part of git-commit-id-plugin by Konrad Malawski <konrad.malawski@java.pl>
*
* git-commit-id-plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* git-commit-id-plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
*/
package pl.project13.jgit;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.newLinkedList;
import static com.google.common.collect.Maps.newHashMap;
import static com.google.common.collect.Sets.newHashSet;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.revwalk.RevWalk;
import pl.project13.jgit.dummy.DatedRevTag;
import pl.project13.maven.git.GitDescribeConfig;
import pl.project13.maven.git.log.LoggerBridge;
import pl.project13.maven.git.log.StdOutLoggerBridge;
import pl.project13.maven.git.util.Pair;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
/**
* Implements git's <pre>describe</pre> command.
* <p/>
* <code><pre>
* usage: git describe [options] <committish>*
* or: git describe [options] --dirty
* <p/>
* --contains find the tag that comes after the commit
* --debug debug search strategy on stderr
* --all use any ref in .git/refs
* --tags use any tag in .git/refs/tags
* --long always use long format
* --abbrev[=<n>] use <n> digits to display SHA-1s
* --exact-match only output exact matches
* --candidates <n> consider <n> most recent tags (default: 10)
* --match <pattern> only consider tags matching <pattern>
* --always show abbreviated commit object as fallback
* --dirty[=<mark>] append <mark> on dirty working tree (default: "-dirty")
* </pre></code>
*
* @author <a href="mailto:konrad.malawski@java.pl">Konrad 'ktoso' Malawski</a>
*/
public class DescribeCommand extends GitCommand<DescribeResult> {
private LoggerBridge loggerBridge;
// todo not yet implemented options:
// private boolean containsFlag = false;
// private boolean allFlag = false;
// private boolean tagsFlag = false;
// private Optional<Integer> candidatesOption = Optional.of(10);
// private boolean exactMatchFlag = false;
private Optional<String> matchOption = Optional.absent();
/**
* How many chars of the commit hash should be displayed? 7 is the default used by git.
*/
private int abbrev = 7;
/**
* Skipping lightweight tags by default - that's how git-describe works by default.
* {@link DescribeCommand#tags(Boolean)} for more details.
*/
private boolean tagsFlag = false;
private boolean alwaysFlag = true;
/**
* Corresponds to <pre>--long</pre>. Always use the <pre>TAG-N-HASH</pre> format, even when ON a tag.
*/
private boolean forceLongFormat = false;
/**
* The string marker (such as "DEV") to be suffixed to the describe result when the working directory is dirty
*/
private Optional<String> dirtyOption = Optional.absent();
/**
* Creates a new describe command which interacts with a single repository
*
* @param repo the {@link org.eclipse.jgit.lib.Repository} this command should interact with
*/
@Nonnull
public static DescribeCommand on(Repository repo) {
return new DescribeCommand(repo);
}
/**
* Creates a new describe command which interacts with a single repository
*
* @param repo the {@link org.eclipse.jgit.lib.Repository} this command should interact with
*/
private DescribeCommand(Repository repo) {
this(repo, true);
}
private DescribeCommand(Repository repo, boolean verbose) {
super(repo);
initDefaultLoggerBridge(verbose);
setVerbose(verbose);
}
private void initDefaultLoggerBridge(boolean verbose) {
loggerBridge = new StdOutLoggerBridge(verbose);
}
@Nonnull
public DescribeCommand setVerbose(boolean verbose) {
loggerBridge.setVerbose(verbose);
return this;
}
@Nonnull
public DescribeCommand withLoggerBridge(LoggerBridge bridge) {
this.loggerBridge = bridge;
return this;
}
/**
* <pre>--always</pre>
* <p/>
* Show uniquely abbreviated commit object as fallback.
* <p/>
* <pre>true</pre> by default.
*/
@Nonnull
public DescribeCommand always(boolean always) {
this.alwaysFlag = always;
log("--always =", always);
return this;
}
/**
* <pre>--long</pre>
* <p/>
* Always output the long format (the tag, the number of commits and the abbreviated commit name)
* even when it matches a tag. This is useful when you want to see parts of the commit object name
* in "describe" output, even when the commit in question happens to be a tagged version. Instead
* of just emitting the tag name, it will describe such a commit as v1.2-0-gdeadbee (0th commit
* since tag v1.2 that points at object deadbee....).
* <p/>
* <pre>false</pre> by default.
*/
@Nonnull
public DescribeCommand forceLongFormat(@Nullable Boolean forceLongFormat) {
if (forceLongFormat != null) {
this.forceLongFormat = forceLongFormat;
log("--long = %s", forceLongFormat);
}
return this;
}
/**
* <pre>--abbrev=N</pre>
* <p/>
* Instead of using the default <em>7 hexadecimal digits</em> as the abbreviated object name,
* use <b>N</b> digits, or as many digits as needed to form a unique object name.
* <p/>
* An <n> of 0 will suppress long format, only showing the closest tag.
*/
@Nonnull
public DescribeCommand abbrev(@Nullable Integer n) {
if (n != null) {
Preconditions.checkArgument(n < 41, String.format("N (commit abbres length) must be < 41. (Was:[%s])", n));
Preconditions.checkArgument(n >= 0, String.format("N (commit abbrev length) must be positive! (Was [%s])", n));
log("--abbrev =", n);
abbrev = n;
}
return this;
}
/**
* <pre>--tags</pre>
* <p>
* Instead of using only the annotated tags, use any tag found in .git/refs/tags.
* This option enables matching a lightweight (non-annotated) tag.
* </p>
* <p/>
* <p>Searching for lightweight tags is <b>false</b> by default.</p>
* <p/>
* Example:
* <pre>
* b6a73ed - (HEAD, master)
* d37a598 - (v1.0-fixed-stuff) - a lightweight tag (with no message)
* 9597545 - (v1.0) - an annotated tag
*
* > git describe
* annotated-tag-2-gb6a73ed # the nearest "annotated" tag is found
*
* > git describe --tags
* lightweight-tag-1-gb6a73ed # the nearest tag (including lightweights) is found
* </pre>
* <p/>
* <p>
* Using only annotated tags to mark builds may be useful if you're using tags to help yourself with annotating
* things like "i'll get back to that" etc - you don't need such tags to be exposed. But if you want lightweight
* tags to be included in the search, enable this option.
* </p>
*/
@Nonnull
public DescribeCommand tags(@Nullable Boolean includeLightweightTagsInSearch) {
if (includeLightweightTagsInSearch != null) {
tagsFlag = includeLightweightTagsInSearch;
log("--tags =", includeLightweightTagsInSearch);
}
return this;
}
/**
* Alias for {@link DescribeCommand#tags(Boolean)} with <b>true</b> value
*/
public DescribeCommand tags() {
return tags(true);
}
/**
* Apply all configuration options passed in with {@param config}.
* If a setting is null, it will not be applied - so for abbrev for example, the default 7 would be used.
*
* @return itself, after applying the settings
*/
@Nonnull
public DescribeCommand apply(@Nullable GitDescribeConfig config) {
if (config != null) {
always(config.isAlways());
dirty(config.getDirty());
abbrev(config.getAbbrev());
forceLongFormat(config.getForceLongFormat());
tags(config.getTags());
match(config.getMatch());
}
return this;
}
/**
* <pre>--dirty[=mark]</pre>
* Describe the working tree. It means describe HEAD and appends mark (<pre>-dirty</pre> by default) if the
* working tree is dirty.
*
* @param dirtyMarker the marker name to be appended to the describe output when the workspace is dirty
* @return itself, to allow fluent configuration
*/
@Nonnull
public DescribeCommand dirty(@Nullable String dirtyMarker) {
Optional<String> option = Optional.fromNullable(dirtyMarker);
log("--dirty =", option.or(""));
this.dirtyOption = option;
return this;
}
/**
* <pre>--match glob-pattern</pre>
* Consider only those tags which match the given glob pattern.
*
* @param pattern the glob style pattern to match against the tag names
* @return itself, to allow fluent configuration
*/
@Nonnull
public DescribeCommand match(@Nullable String pattern) {
matchOption = Optional.fromNullable(pattern);
log("--match =", matchOption.or(""));
return this;
}
@Override
public DescribeResult call() throws GitAPIException {
// needed for abbrev id's calculation
ObjectReader objectReader = repo.newObjectReader();
// get tags
Map<ObjectId, List<String>> tagObjectIdToName = findTagObjectIds(repo, tagsFlag);
// get current commit
RevCommit headCommit = findHeadObjectId(repo);
ObjectId headCommitId = headCommit.getId();
// check if dirty
boolean dirty = findDirtyState(repo);
if (hasTags(headCommit, tagObjectIdToName)) {
String tagName = tagObjectIdToName.get(headCommit).iterator().next();
log("The commit we're on is a Tag ([",tagName,"]), returning.");
return new DescribeResult(tagName, dirty, dirtyOption);
}
// get commits, up until the nearest tag
List<RevCommit> commits = findCommitsUntilSomeTag(repo, headCommit, tagObjectIdToName);
// if there is no tags or any tag is not on that branch then return generic describe
if (foundZeroTags(tagObjectIdToName) || commits.isEmpty()) {
return new DescribeResult(objectReader, headCommitId, dirty, dirtyOption)
.withCommitIdAbbrev(abbrev);
}
// check how far away from a tag we are
int distance = distanceBetween(repo, headCommit, commits.get(0));
String tagName = tagObjectIdToName.get(commits.get(0)).iterator().next();
Pair<Integer, String> howFarFromWhichTag = Pair.of(distance, tagName);
// if it's null, no tag's were found etc, so let's return just the commit-id
return createDescribeResult(objectReader, headCommitId, dirty, howFarFromWhichTag);
}
/**
* Prepares the final result of this command.
* It tries to put as much information as possible into the result,
* and will fallback to a plain commit hash if nothing better is returnable.
* <p/>
* The exact logic is following what <pre>git-describe</pre> would do.
*/
private DescribeResult createDescribeResult(ObjectReader objectReader, ObjectId headCommitId, boolean dirty, @Nullable Pair<Integer, String> howFarFromWhichTag) {
if (howFarFromWhichTag == null) {
return new DescribeResult(objectReader, headCommitId, dirty, dirtyOption)
.withCommitIdAbbrev(abbrev);
} else if (howFarFromWhichTag.first > 0 || forceLongFormat) {
return new DescribeResult(objectReader, howFarFromWhichTag.second, howFarFromWhichTag.first, headCommitId, dirty, dirtyOption)
.withCommitIdAbbrev(abbrev); // we're a bit away from a tag
} else if (howFarFromWhichTag.first == 0) {
return new DescribeResult(howFarFromWhichTag.second)
.withCommitIdAbbrev(abbrev); // we're ON a tag
} else if (alwaysFlag) {
return new DescribeResult(objectReader, headCommitId)
.withCommitIdAbbrev(abbrev); // we have no tags! display the commit
} else {
return DescribeResult.EMPTY;
}
}
private static boolean foundZeroTags(@Nonnull Map<ObjectId, List<String>> tags) {
return tags.isEmpty();
}
@VisibleForTesting
boolean findDirtyState(Repository repo) throws GitAPIException {
Git git = Git.wrap(repo);
Status status = git.status().call();
// Git describe doesn't mind about untracked files when checking if
// repo is dirty. JGit does this, so we cannot use the isClean method
// to get the same behaviour. Instead check dirty state without
// status.getUntracked().isEmpty()
boolean isDirty = !(status.getAdded().isEmpty()
&& status.getChanged().isEmpty()
&& status.getRemoved().isEmpty()
&& status.getMissing().isEmpty()
&& status.getModified().isEmpty()
&& status.getConflicting().isEmpty());
log("Repo is in dirty state [", isDirty, "]");
return isDirty;
}
@VisibleForTesting
static boolean hasTags(ObjectId headCommit, @Nonnull Map<ObjectId, List<String>> tagObjectIdToName) {
return tagObjectIdToName.containsKey(headCommit);
}
RevCommit findHeadObjectId(@Nonnull Repository repo) throws RuntimeException {
try {
ObjectId headId = repo.resolve("HEAD");
RevWalk walk = new RevWalk(repo);
RevCommit headCommit = walk.lookupCommit(headId);
walk.dispose();
log("HEAD is [",headCommit.getName(),"] ");
return headCommit;
} catch (IOException ex) {
throw new RuntimeException("Unable to obtain HEAD commit!", ex);
}
}
private List<RevCommit> findCommitsUntilSomeTag(Repository repo, RevCommit head, @Nonnull Map<ObjectId, List<String>> tagObjectIdToName) {
RevWalk revWalk = new RevWalk(repo);
try {
revWalk.markStart(head);
for (RevCommit commit : revWalk) {
ObjectId objId = commit.getId();
if (tagObjectIdToName.size() > 0) {
List<String> maybeList = tagObjectIdToName.get(objId);
if (maybeList != null && maybeList.get(0) != null) {
return Collections.singletonList(commit);
}
}
}
return Collections.emptyList();
} catch (Exception e) {
throw new RuntimeException("Unable to find commits until some tag", e);
}
}
/**
* Calculates the distance (number of commits) between the given parent and child commits.
*
* @return distance (number of commits) between the given commits
* @see <a href="https://github.com/mdonoughe/jgit-describe/blob/master/src/org/mdonoughe/JGitDescribeTask.java">mdonoughe/jgit-describe/blob/master/src/org/mdonoughe/JGitDescribeTask.java</a>
*/
private int distanceBetween(@Nonnull Repository repo, @Nonnull RevCommit child, @Nonnull RevCommit parent) {
RevWalk revWalk = new RevWalk(repo);
try {
revWalk.markStart(child);
Set<ObjectId> seena = newHashSet();
Set<ObjectId> seenb = newHashSet();
Queue<RevCommit> q = newLinkedList();
q.add(revWalk.parseCommit(child));
int distance = 0;
ObjectId parentId = parent.getId();
while (q.size() > 0) {
RevCommit commit = q.remove();
ObjectId commitId = commit.getId();
if (seena.contains(commitId)) {
continue;
}
seena.add(commitId);
if (parentId.equals(commitId)) {
// don't consider commits that are included in this commit
seeAllParents(revWalk, commit, seenb);
// remove things we shouldn't have included
for (ObjectId oid : seenb) {
if (seena.contains(oid)) {
distance--;
}
}
seena.addAll(seenb);
continue;
}
for (ObjectId oid : commit.getParents()) {
if (!seena.contains(oid)) {
q.add(revWalk.parseCommit(oid));
}
}
distance++;
}
return distance;
} catch (Exception e) {
throw new RuntimeException(String.format("Unable to calculate distance between [%s] and [%s]", child, parent), e);
} finally {
revWalk.dispose();
}
}
private static void seeAllParents(@Nonnull RevWalk revWalk, RevCommit child, @Nonnull Set<ObjectId> seen) throws IOException {
Queue<RevCommit> q = newLinkedList();
q.add(child);
while (q.size() > 0) {
RevCommit commit = q.remove();
for (ObjectId oid : commit.getParents()) {
if (seen.contains(oid)) {
continue;
}
seen.add(oid);
q.add(revWalk.parseCommit(oid));
}
}
}
// git commit id -> its tag (or tags)
private Map<ObjectId, List<String>> findTagObjectIds(@Nonnull Repository repo, boolean tagsFlag) {
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = newHashMap();
RevWalk walk = new RevWalk(repo);
try {
walk.markStart(walk.parseCommit(repo.resolve("HEAD")));
List<Ref> tagRefs = Git.wrap(repo).tagList().call();
String matchPattern = createMatchPattern();
Pattern regex = Pattern.compile(matchPattern);
log("Tag refs [", tagRefs, "]");
for (Ref tagRef : tagRefs) {
walk.reset();
String name = tagRef.getName();
if (!regex.matcher(name).matches()) {
log("Skipping tagRef with name [", name, "] as it doesn't match [", matchPattern, "]");
continue;
}
ObjectId resolvedCommitId = repo.resolve(name);
// todo that's a bit of a hack...
try {
final RevTag revTag = walk.parseTag(resolvedCommitId);
ObjectId taggedCommitId = revTag.getObject().getId();
log("Resolved tag [",revTag.getTagName(),"] [",revTag.getTaggerIdent(),"], points at [",taggedCommitId,"] ");
// sometimes a tag, may point to another tag, so we need to unpack it
while (isTagId(taggedCommitId)) {
taggedCommitId = walk.parseTag(taggedCommitId).getObject().getId();
}
if (commitIdsToTags.containsKey(taggedCommitId)) {
commitIdsToTags.get(taggedCommitId).add(new DatedRevTag(revTag));
} else {
commitIdsToTags.put(taggedCommitId, newArrayList(new DatedRevTag(revTag)));
}
} catch (IncorrectObjectTypeException ex) {
// it's an lightweight tag! (yeah, really)
if (tagsFlag) {
// --tags means "include lightweight tags"
log("Including lightweight tag [", name, "]");
DatedRevTag datedRevTag = new DatedRevTag(resolvedCommitId, name);
if (commitIdsToTags.containsKey(resolvedCommitId)) {
commitIdsToTags.get(resolvedCommitId).add(datedRevTag);
} else {
commitIdsToTags.put(resolvedCommitId, newArrayList(datedRevTag));
}
}
} catch (Exception ignored) {
error("Failed while parsing [",tagRef,"] -- ", Throwables.getStackTraceAsString(ignored));
}
}
for (Map.Entry<ObjectId, List<DatedRevTag>> entry : commitIdsToTags.entrySet()) {
log("key [",entry.getKey(),"], tags => [",entry.getValue(),"] ");
}
Map<ObjectId, List<String>> commitIdsToTagNames = transformRevTagsMapToDateSortedTagNames(commitIdsToTags);
log("Created map: [",commitIdsToTagNames,"] ");
return commitIdsToTagNames;
} catch (Exception e) {
log("Unable to locate tags\n[",Throwables.getStackTraceAsString(e),"]");
} finally {
walk.release();
}
return Collections.emptyMap();
}
/** Checks if the given object id resolved to a tag object */
private boolean isTagId(ObjectId objectId) {
return objectId.toString().startsWith("tag ");
}
private HashMap<ObjectId, List<String>> transformRevTagsMapToDateSortedTagNames(Map<ObjectId, List<DatedRevTag>> commitIdsToTags) {
HashMap<ObjectId, List<String>> commitIdsToTagNames = newHashMap();
for (Map.Entry<ObjectId, List<DatedRevTag>> objectIdListEntry : commitIdsToTags.entrySet()) {
List<DatedRevTag> tags = objectIdListEntry.getValue();
List<DatedRevTag> newTags = newArrayList(tags);
Collections.sort(newTags, new Comparator<DatedRevTag>() {
@Override
public int compare(DatedRevTag revTag, DatedRevTag revTag2) {
return revTag2.date.compareTo(revTag.date);
}
});
List<String> tagNames = Lists.transform(newTags, new Function<DatedRevTag, String>() {
@Override
public String apply(DatedRevTag input) {
return trimFullTagName(input.tagName);
}
});
commitIdsToTagNames.put(objectIdListEntry.getKey(), tagNames);
}
return commitIdsToTagNames;
}
private String createMatchPattern() {
if (!matchOption.isPresent()) {
return ".*";
}
StringBuffer buf = new StringBuffer();
buf.append("^refs/tags/\\Q");
buf.append(matchOption.get().replace("*", "\\E.*\\Q").replace("?", "\\E.\\Q"));
buf.append("\\E$");
return buf.toString();
}
@VisibleForTesting
static String trimFullTagName(@Nonnull String tagName) {
return tagName.replaceFirst("refs/tags/", "");
}
private void log(Object... parts) {
loggerBridge.log(parts);
}
private void error(Object... parts) {
loggerBridge.error(parts);
}
}