Skip to content

Commit

Permalink
Merge pull request #32 from mglazer/feature/add-githash
Browse files Browse the repository at this point in the history
Add gitHash attribute to VersionDetails
  • Loading branch information
markelliot committed May 23, 2016
2 parents a54ca79 + f7e7866 commit 116efaf
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,76 @@
*/
package com.palantir.gradle.gitversion

import groovy.transform.*

import java.util.regex.Matcher

import org.eclipse.jgit.api.Git
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<Project> {

// 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<String> gitDesc
@Memoized
private File gitDir(Project project) {
return getRootGitDir(project.rootDir)
}

void apply(Project project) {
gitDesc = Suppliers.memoize(new Supplier<String>() {
@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
}

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') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ class GitVersionPluginTests extends Specification {
task printVersionDetails() << {
println versionDetails().lastTag
println versionDetails().commitDistance
println versionDetails().gitHash
}
'''.stripIndent()
gitIgnoreFile << 'build'
Expand All @@ -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' () {
Expand All @@ -263,6 +264,7 @@ class GitVersionPluginTests extends Specification {
task printVersionDetails() << {
println versionDetails().lastTag
println versionDetails().commitDistance
println versionDetails().gitHash
}
'''.stripIndent()
Expand All @@ -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) {
Expand Down

0 comments on commit 116efaf

Please sign in to comment.