Skip to content

Commit 24ca28a

Browse files
delta003iamdanfox
authored andcommitted
Use only jgit (#94)
* drop native git * to string * remove walk.close
1 parent eb78d5e commit 24ca28a

File tree

4 files changed

+253
-134
lines changed

4 files changed

+253
-134
lines changed

src/main/java/com/palantir/gradle/gitversion/JGitDescribe.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ private List<String> revList(ObjectId initialObjectId) throws IOException {
6464

6565
Repository repo = git.getRepository();
6666
try (RevWalk walk = new RevWalk(repo)) {
67+
walk.setRetainBody(false);
6768
RevCommit head = walk.parseCommit(initialObjectId);
6869

6970
while (true) {
@@ -88,16 +89,19 @@ private Map<String, RefWithTagName> mapCommitsToTags(Git git) {
8889
// Maps commit hash to list of all refs pointing to given commit hash.
8990
// All keys in this map should be same as commit hashes in 'git show-ref --tags -d'
9091
Map<String, RefWithTagName> commitHashToTag = new HashMap<>();
91-
for (Map.Entry<String, Ref> entry : git.getRepository().getTags().entrySet()) {
92-
RefWithTagName refWithTagName = new RefWithTagName(entry.getValue(), entry.getKey());
93-
94-
ObjectId peeledRef = refWithTagName.getRef().getPeeledObjectId();
95-
if (peeledRef == null) {
96-
// lightweight tag (commit object)
97-
updateCommitHashMap(commitHashToTag, comparator, entry.getValue().getObjectId(), refWithTagName);
92+
Repository repository = git.getRepository();
93+
for (Map.Entry<String, Ref> entry : repository.getTags().entrySet()) {
94+
Ref peeledRef = repository.peel(entry.getValue());
95+
RefWithTagName refWithTagName = new RefWithTagName(peeledRef, entry.getKey());
96+
97+
// Peel ref object
98+
ObjectId peeledObjectId = peeledRef.getPeeledObjectId();
99+
if (peeledObjectId == null) {
100+
// Lightweight tag (commit object)
101+
updateCommitHashMap(commitHashToTag, comparator, peeledRef.getObjectId(), refWithTagName);
98102
} else {
99-
// annotated tag (tag object)
100-
updateCommitHashMap(commitHashToTag, comparator, peeledRef, refWithTagName);
103+
// Annotated tag (tag object)
104+
updateCommitHashMap(commitHashToTag, comparator, peeledObjectId, refWithTagName);
101105
}
102106
}
103107
return commitHashToTag;

src/main/java/com/palantir/gradle/gitversion/NativeGitDescribe.java

-107
This file was deleted.

src/main/java/com/palantir/gradle/gitversion/VersionDetails.java

+17-18
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private String description() {
4949

5050
String rawDescription = expensiveComputeRawDescription();
5151
maybeCachedDescription = rawDescription == null ?
52-
rawDescription : rawDescription.replaceFirst("^" + args.getPrefix(), "");
52+
null : rawDescription.replaceFirst("^" + args.getPrefix(), "");
5353
return maybeCachedDescription;
5454
}
5555

@@ -59,23 +59,7 @@ private String expensiveComputeRawDescription() {
5959
return null;
6060
}
6161

62-
String nativeGitDescribe = new NativeGitDescribe(git.getRepository().getDirectory())
63-
.describe(args.getPrefix());
64-
String jgitDescribe = new JGitDescribe(git)
65-
.describe(args.getPrefix());
66-
67-
// If native failed, return JGit one
68-
if (nativeGitDescribe == null) {
69-
return jgitDescribe;
70-
}
71-
72-
// If native succeeded, make sure it's same as JGit one
73-
if (!nativeGitDescribe.equals(jgitDescribe)) {
74-
throw new IllegalStateException(String.format("Inconsistent git describe: native was %s and jgit was %s. " +
75-
"Please report this on github.com/palantir/gradle-git-version", nativeGitDescribe, jgitDescribe));
76-
}
77-
78-
return jgitDescribe;
62+
return new JGitDescribe(git).describe(args.getPrefix());
7963
}
8064

8165
private boolean isRepoEmpty() {
@@ -141,4 +125,19 @@ public String getBranchName() throws IOException {
141125

142126
return ref.getName().substring(Constants.R_HEADS.length());
143127
}
128+
129+
@Override
130+
public String toString() {
131+
try {
132+
return String.format("VersionDetails(%s, %s, %s, %s, %s)",
133+
getVersion(),
134+
getGitHash(),
135+
getGitHashFull(),
136+
getBranchName(),
137+
getIsCleanTag()
138+
);
139+
} catch (IOException e) {
140+
return null;
141+
}
142+
}
144143
}

0 commit comments

Comments
 (0)