4
4
import org .eclipse .jgit .lib .Constants ;
5
5
import org .eclipse .jgit .lib .ObjectId ;
6
6
import org .eclipse .jgit .lib .Ref ;
7
+ import org .eclipse .jgit .lib .Repository ;
7
8
import org .eclipse .jgit .revwalk .RevCommit ;
8
9
import org .eclipse .jgit .revwalk .RevWalk ;
9
10
import org .slf4j .Logger ;
10
11
import org .slf4j .LoggerFactory ;
11
12
13
+ import java .io .IOException ;
12
14
import java .util .ArrayList ;
13
15
import java .util .HashMap ;
14
16
import java .util .List ;
@@ -33,22 +35,12 @@ public String describe(String prefix) {
33
35
return null ;
34
36
}
35
37
36
- RevCommit headCommit ;
37
- RefWithTagNameComparator comparator ;
38
38
try {
39
39
ObjectId headObjectId = git .getRepository ().resolve (Constants .HEAD );
40
- RevWalk walk = new RevWalk (git .getRepository ());
41
- headCommit = walk .parseCommit (headObjectId );
42
- comparator = new RefWithTagNameComparator (walk );
43
- } catch (Exception e ) {
44
- log .debug ("HEAD not found: {}" , e );
45
- return null ;
46
- }
47
40
48
- try {
49
- List <String > revs = revList (headCommit );
41
+ List <String > revs = revList (headObjectId );
50
42
51
- Map <String , RefWithTagName > commitHashToTag = mapCommitsToTags (git , comparator );
43
+ Map <String , RefWithTagName > commitHashToTag = mapCommitsToTags (git );
52
44
53
45
// Walk back commit ancestors looking for tagged one
54
46
for (int depth = 0 ; depth < revs .size (); depth ++) {
@@ -64,39 +56,52 @@ public String describe(String prefix) {
64
56
}
65
57
66
58
// No tags found, so return commit hash of HEAD
67
- return GitUtils .abbrevHash (headCommit . toObjectId () .getName ());
59
+ return GitUtils .abbrevHash (headObjectId .getName ());
68
60
} catch (Exception e ) {
69
61
log .debug ("JGit describe failed with {}" , e );
70
62
return null ;
71
63
}
72
64
}
73
65
74
66
// Mimics 'git rev-list --first-parent <commit>'
75
- private List <String > revList (RevCommit commit ) {
76
- List <String > revs = new ArrayList <>();
77
- while (commit != null ) {
78
- revs .add (commit .getName ());
79
- try {
80
- // There is no way to check if this exists without failing
81
- commit = commit .getParent (0 );
82
- } catch (Exception ignored ) {
83
- break ;
67
+ private List <String > revList (ObjectId initialObjectId ) throws IOException {
68
+ ArrayList <String > revs = new ArrayList <>();
69
+
70
+ Repository repo = git .getRepository ();
71
+ try (RevWalk walk = new RevWalk (repo )) {
72
+ RevCommit head = walk .parseCommit (initialObjectId );
73
+
74
+ while (true ) {
75
+ revs .add (head .getName ());
76
+
77
+ RevCommit [] parents = head .getParents ();
78
+ if (parents == null || parents .length == 0 ) {
79
+ break ;
80
+ }
81
+
82
+ head = walk .parseCommit (parents [0 ]);
84
83
}
85
84
}
85
+
86
86
return revs ;
87
87
}
88
88
89
89
// Maps all commits returned by 'git show-ref --tags -d' to output of 'git describe --tags --exact-match <commit>'
90
- private Map <String , RefWithTagName > mapCommitsToTags (Git git , RefWithTagNameComparator comparator ) {
90
+ private Map <String , RefWithTagName > mapCommitsToTags (Git git ) {
91
+ RefWithTagNameComparator comparator = new RefWithTagNameComparator (git );
92
+
91
93
// Maps commit hash to list of all refs pointing to given commit hash.
92
94
// All keys in this map should be same as commit hashes in 'git show-ref --tags -d'
93
95
Map <String , RefWithTagName > commitHashToTag = new HashMap <>();
94
96
for (Map .Entry <String , Ref > entry : git .getRepository ().getTags ().entrySet ()) {
95
97
RefWithTagName refWithTagName = new RefWithTagName (entry .getValue (), entry .getKey ());
96
- updateCommitHashMap (commitHashToTag , comparator , entry .getValue ().getObjectId (), refWithTagName );
97
- // Also add dereferenced commit hash if exists
98
+
98
99
ObjectId peeledRef = refWithTagName .getRef ().getPeeledObjectId ();
99
- if (peeledRef != null ) {
100
+ if (peeledRef == null ) {
101
+ // lightweight tag (commit object)
102
+ updateCommitHashMap (commitHashToTag , comparator , entry .getValue ().getObjectId (), refWithTagName );
103
+ } else {
104
+ // annotated tag (tag object)
100
105
updateCommitHashMap (commitHashToTag , comparator , peeledRef , refWithTagName );
101
106
}
102
107
}
0 commit comments