From f7e78664ef01465c79accaada5619d18bfadf707 Mon Sep 17 00:00:00 2001 From: Mike Glazer Date: Sun, 22 May 2016 15:28:27 +0200 Subject: [PATCH] Add gitHash attribute to VersionDetails Remove guava dependency because we can use the @Memoized annotation instead --- build.gradle | 1 - readme.md | 1 + .../gradle/gitversion/GitVersionPlugin.groovy | 62 ++++++++++++------- .../gradle/gitversion/VersionDetails.groovy | 11 ++-- .../gitversion/GitVersionPluginTests.groovy | 6 +- 5 files changed, 51 insertions(+), 30 deletions(-) diff --git a/build.gradle b/build.gradle index 10b5b1a3..e8f101ec 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,6 @@ repositories { dependencies { compile gradleApi() compile 'org.eclipse.jgit:org.eclipse.jgit:4.1.1.201511131810-r' - compile 'com.google.guava:guava:19.0' testCompile gradleTestKit() testCompile('org.spockframework:spock-core:1.0-groovy-2.4') { diff --git a/readme.md b/readme.md index 26fd9702..890ec6dc 100644 --- a/readme.md +++ b/readme.md @@ -29,6 +29,7 @@ You can get an object containing more detailed information by calling: def details = versionDetails() details.lastTag details.commitDistance + details.gitHash Tasks ----- diff --git a/src/main/groovy/com/palantir/gradle/gitversion/GitVersionPlugin.groovy b/src/main/groovy/com/palantir/gradle/gitversion/GitVersionPlugin.groovy index 2c77a756..218ee5bb 100644 --- a/src/main/groovy/com/palantir/gradle/gitversion/GitVersionPlugin.groovy +++ b/src/main/groovy/com/palantir/gradle/gitversion/GitVersionPlugin.groovy @@ -15,6 +15,8 @@ */ package com.palantir.gradle.gitversion +import groovy.transform.* + import java.util.regex.Matcher import org.eclipse.jgit.api.Git @@ -22,38 +24,52 @@ import org.eclipse.jgit.internal.storage.file.FileRepository import org.gradle.api.Plugin import org.gradle.api.Project -import com.google.common.base.Supplier -import com.google.common.base.Suppliers - class GitVersionPlugin implements Plugin { // Gradle returns 'unspecified' when no version is set private static final String UNSPECIFIED_VERSION = 'unspecified' + private static final int VERSION_ABBR_LENGTH = 10 - private Supplier gitDesc + @Memoized + private File gitDir(Project project) { + return getRootGitDir(project.rootDir) + } - void apply(Project project) { - gitDesc = Suppliers.memoize(new Supplier() { - @Override - public String get() { - File gitDir = getRootGitDir(project.rootDir) - try { - Git git = Git.wrap(new FileRepository(gitDir)) - String version = git.describe().call() ?: UNSPECIFIED_VERSION - boolean isClean = git.status().call().isClean() - return version + (isClean ? '' : '.dirty') - } catch (Throwable t) { - return UNSPECIFIED_VERSION - } - } - }) + @Memoized + private Git gitRepo(Project project) { + return Git.wrap(new FileRepository(gitDir(project))) + } + @Memoized + private String gitDesc(Project project) { + Git git = gitRepo(project) + try { + String version = git.describe().call() ?: UNSPECIFIED_VERSION + boolean isClean = git.status().call().isClean() + return version + (isClean ? '' : '.dirty') + } catch (Throwable t) { + return UNSPECIFIED_VERSION + } + } + + @Memoized + private String gitHash(Project project) { + Git git = gitRepo(project) + try { + return git.getRepository().getRef("HEAD").getObjectId().abbreviate(VERSION_ABBR_LENGTH).name() + } catch (Throwable t) { + return UNSPECIFIED_VERSION + } + } + + void apply(Project project) { project.ext.gitVersion = { - return gitDesc.get() + return gitDesc(project) } project.ext.versionDetails = { - String description = gitDesc.get() + String description = gitDesc(project) + String hash = gitHash(project) if (description.equals(UNSPECIFIED_VERSION)) { return null @@ -61,14 +77,14 @@ class GitVersionPlugin implements Plugin { if (!(description =~ /.*g.?[0-9a-fA-F]{3,}/)) { // Description has no git hash so it is just the tag name - return new VersionDetails(description, 0) + return new VersionDetails(description, 0, hash) } Matcher match = (description =~ /(.*)-([0-9]+)-g.?[0-9a-fA-F]{3,}/) String tagName = match[0][1] int commitCount = Integer.valueOf(match[0][2]) - return new VersionDetails(tagName, commitCount) + return new VersionDetails(tagName, commitCount, hash) } project.tasks.create('printVersion') { diff --git a/src/main/groovy/com/palantir/gradle/gitversion/VersionDetails.groovy b/src/main/groovy/com/palantir/gradle/gitversion/VersionDetails.groovy index 2f51d117..c67810cd 100644 --- a/src/main/groovy/com/palantir/gradle/gitversion/VersionDetails.groovy +++ b/src/main/groovy/com/palantir/gradle/gitversion/VersionDetails.groovy @@ -15,22 +15,25 @@ */ package com.palantir.gradle.gitversion; +import groovy.transform.* + /** * POGO containing the tag name and commit count that make * up the version string. */ +@ToString +@EqualsAndHashCode class VersionDetails implements Serializable { private static final long serialVersionUID = -7340444937169877612L; final String lastTag; final int commitDistance; + final String gitHash; - public VersionDetails(String lastTag, int commitDistance) { + public VersionDetails(String lastTag, int commitDistance, String gitHash) { this.lastTag = lastTag; this.commitDistance = commitDistance; + this.gitHash = gitHash; } - public String toString() { - return "VersionDetails[lastTag=${lastTag}, commitDistance=${commitDistance}]"; - } } diff --git a/src/test/groovy/com/palantir/gradle/gitversion/GitVersionPluginTests.groovy b/src/test/groovy/com/palantir/gradle/gitversion/GitVersionPluginTests.groovy index b47a2cdc..e88605ea 100644 --- a/src/test/groovy/com/palantir/gradle/gitversion/GitVersionPluginTests.groovy +++ b/src/test/groovy/com/palantir/gradle/gitversion/GitVersionPluginTests.groovy @@ -238,6 +238,7 @@ class GitVersionPluginTests extends Specification { task printVersionDetails() << { println versionDetails().lastTag println versionDetails().commitDistance + println versionDetails().gitHash } '''.stripIndent() gitIgnoreFile << 'build' @@ -250,7 +251,7 @@ class GitVersionPluginTests extends Specification { BuildResult buildResult = with('printVersionDetails').build() then: - buildResult.output.contains(":printVersionDetails\n1.0.0\n0\n") + buildResult.output =~ ":printVersionDetails\n1.0.0\n0\n[a-z0-9]{10}\n" } def 'version details when commit distance to tag is > 0' () { @@ -263,6 +264,7 @@ class GitVersionPluginTests extends Specification { task printVersionDetails() << { println versionDetails().lastTag println versionDetails().commitDistance + println versionDetails().gitHash } '''.stripIndent() @@ -277,7 +279,7 @@ class GitVersionPluginTests extends Specification { BuildResult buildResult = with('printVersionDetails').build() then: - buildResult.output.contains(":printVersionDetails\n1.0.0\n1\n") + buildResult.output =~ ":printVersionDetails\n1.0.0\n1\n[a-z0-9]{10}\n" } private GradleRunner with(String... tasks) {