Skip to content

Commit

Permalink
Use only jgit (#94)
Browse files Browse the repository at this point in the history
* drop native git

* to string

* remove walk.close
  • Loading branch information
delta003 authored and iamdanfox committed Apr 25, 2018
1 parent eb78d5e commit 24ca28a
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 134 deletions.
22 changes: 13 additions & 9 deletions src/main/java/com/palantir/gradle/gitversion/JGitDescribe.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private List<String> revList(ObjectId initialObjectId) throws IOException {

Repository repo = git.getRepository();
try (RevWalk walk = new RevWalk(repo)) {
walk.setRetainBody(false);
RevCommit head = walk.parseCommit(initialObjectId);

while (true) {
Expand All @@ -88,16 +89,19 @@ private Map<String, RefWithTagName> mapCommitsToTags(Git git) {
// Maps commit hash to list of all refs pointing to given commit hash.
// All keys in this map should be same as commit hashes in 'git show-ref --tags -d'
Map<String, RefWithTagName> commitHashToTag = new HashMap<>();
for (Map.Entry<String, Ref> entry : git.getRepository().getTags().entrySet()) {
RefWithTagName refWithTagName = new RefWithTagName(entry.getValue(), entry.getKey());

ObjectId peeledRef = refWithTagName.getRef().getPeeledObjectId();
if (peeledRef == null) {
// lightweight tag (commit object)
updateCommitHashMap(commitHashToTag, comparator, entry.getValue().getObjectId(), refWithTagName);
Repository repository = git.getRepository();
for (Map.Entry<String, Ref> entry : repository.getTags().entrySet()) {
Ref peeledRef = repository.peel(entry.getValue());
RefWithTagName refWithTagName = new RefWithTagName(peeledRef, entry.getKey());

// Peel ref object
ObjectId peeledObjectId = peeledRef.getPeeledObjectId();
if (peeledObjectId == null) {
// Lightweight tag (commit object)
updateCommitHashMap(commitHashToTag, comparator, peeledRef.getObjectId(), refWithTagName);
} else {
// annotated tag (tag object)
updateCommitHashMap(commitHashToTag, comparator, peeledRef, refWithTagName);
// Annotated tag (tag object)
updateCommitHashMap(commitHashToTag, comparator, peeledObjectId, refWithTagName);
}
}
return commitHashToTag;
Expand Down
107 changes: 0 additions & 107 deletions src/main/java/com/palantir/gradle/gitversion/NativeGitDescribe.java

This file was deleted.

35 changes: 17 additions & 18 deletions src/main/java/com/palantir/gradle/gitversion/VersionDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private String description() {

String rawDescription = expensiveComputeRawDescription();
maybeCachedDescription = rawDescription == null ?
rawDescription : rawDescription.replaceFirst("^" + args.getPrefix(), "");
null : rawDescription.replaceFirst("^" + args.getPrefix(), "");
return maybeCachedDescription;
}

Expand All @@ -59,23 +59,7 @@ private String expensiveComputeRawDescription() {
return null;
}

String nativeGitDescribe = new NativeGitDescribe(git.getRepository().getDirectory())
.describe(args.getPrefix());
String jgitDescribe = new JGitDescribe(git)
.describe(args.getPrefix());

// If native failed, return JGit one
if (nativeGitDescribe == null) {
return jgitDescribe;
}

// If native succeeded, make sure it's same as JGit one
if (!nativeGitDescribe.equals(jgitDescribe)) {
throw new IllegalStateException(String.format("Inconsistent git describe: native was %s and jgit was %s. " +
"Please report this on github.com/palantir/gradle-git-version", nativeGitDescribe, jgitDescribe));
}

return jgitDescribe;
return new JGitDescribe(git).describe(args.getPrefix());
}

private boolean isRepoEmpty() {
Expand Down Expand Up @@ -141,4 +125,19 @@ public String getBranchName() throws IOException {

return ref.getName().substring(Constants.R_HEADS.length());
}

@Override
public String toString() {
try {
return String.format("VersionDetails(%s, %s, %s, %s, %s)",
getVersion(),
getGitHash(),
getGitHashFull(),
getBranchName(),
getIsCleanTag()
);
} catch (IOException e) {
return null;
}
}
}
Loading

0 comments on commit 24ca28a

Please sign in to comment.