Skip to content

Commit

Permalink
Merge pull request #27 from gillessed/develop
Browse files Browse the repository at this point in the history
 Add version details.
  • Loading branch information
markelliot committed Mar 30, 2016
2 parents 1b311ea + fe68fe1 commit 245dc15
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 9 deletions.
16 changes: 13 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ Git-Version Gradle Plugin
[![Build Status](https://circleci.com/gh/palantir/gradle-git-version.svg?style=shield)](https://circleci.com/gh/palantir/gradle-git-version)
[![Gradle Plugins Release](https://img.shields.io/github/release/palantir/gradle-git-version.svg)](https://plugins.gradle.org/plugin/com.palantir.git-version)

When applied, Git-Version adds a method to the target project called `gitVersion()` that
runs the JGit equivalent of `git describe` to determine a version string. It behaves exactly
as the JGit `git describe` method behaves, except that when the repository is in a dirty
When applied, Git-Version adds two methods to the target project.

The first, called `gitVersion()`, runs the JGit equivalent of `git describe` to determine a version string.
It behaves exactly as the JGit `git describe` method behaves, except that when the repository is in a dirty
state, appends `.dirty` to the version string.

The second, called `versionDetails()`, returns an object containing the specific details of the version string:
the tag name, and the commit count since the tag.

Usage
-----
Apply the plugin using standard Gradle convention:
Expand All @@ -20,6 +24,12 @@ Set the version of a project by calling:

version gitVersion()

You can get an object containing more detailed information by calling:

def details = versionDetails()
details.lastTag
details.commitDistance

Tasks
-----
This plugin adds a `printVersion` task, which will echo the project's configured version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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.palantir.gradle.gitversion.VersionDetails;

class GitVersionPlugin implements Plugin<Project> {

Expand All @@ -27,10 +28,7 @@ class GitVersionPlugin implements Plugin<Project> {

void apply(Project project) {
project.ext.gitVersion = {
File gitDir = findRootGitDir(project.rootDir)
if (!gitDir.exists()) {
throw new IllegalArgumentException('Cannot find \'.git\' directory')
}
File gitDir = getRootGitDir(project.rootDir)

try {
Git git = Git.wrap(new FileRepository(gitDir))
Expand All @@ -42,12 +40,41 @@ class GitVersionPlugin implements Plugin<Project> {
}
}

project.ext.versionDetails = {
File gitDir = getRootGitDir(project.rootDir)

try {
Git git = Git.wrap(new FileRepository(gitDir))
String description = git.describe().call();

// Description has no git hash so it is just the tag name
if(!(description =~ /.*g.?[0-9a-fA-F]{3,}/)) {
return new VersionDetails(description, 0);
}
def match = (description =~ /(.*)-([0-9]+)-g.?[0-9a-fA-F]{3,}/)
String tagName = match[0][1]
int commitCount = match[0][2].toInteger()

return new VersionDetails(tagName, commitCount)
} catch (Throwable t) {
return null
}
}

project.tasks.create('printVersion') << {
println project.version
}
}

private File findRootGitDir(File currentRoot) {
private File getRootGitDir(currentRoot) {
File gitDir = scanForRootGitDir(currentRoot)
if (!gitDir.exists()) {
throw new IllegalArgumentException('Cannot find \'.git\' directory')
}
return gitDir
}

private File scanForRootGitDir(File currentRoot) {
File gitDir = new File(currentRoot, '.git')

if (gitDir.exists()) {
Expand All @@ -60,6 +87,6 @@ class GitVersionPlugin implements Plugin<Project> {
}

// look in parent directory
return findRootGitDir(currentRoot.parentFile)
return scanForRootGitDir(currentRoot.parentFile)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2016 Palantir Technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* <http://www.apache.org/licenses/LICENSE-2.0>
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.palantir.gradle.gitversion;

/**
* POGO containing the tag name and commit count that make
* up the version string.
*/
class VersionDetails implements Serializable {
private static final long serialVersionUID = -7340444937169877612L;

final String lastTag;
final int commitDistance;

public VersionDetails(String lastTag, int commitDistance) {
this.lastTag = lastTag;
this.commitDistance = commitDistance;
}

public String toString() {
return "VersionDetails[lastTag=${lastTag}, commitDistance=${commitDistance}]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,78 @@ class GitVersionPluginTests extends Specification {
buildResult.output.contains(':printVersion\n1.0.0\n')
}

def 'version details null when no tags are present' () {
given:
buildFile << '''
plugins {
id 'com.palantir.git-version'
}
version gitVersion()
task printVersionDetails() << {
println versionDetails()
}
'''.stripIndent()
Git git = Git.init().setDirectory(projectDir).call();

when:
BuildResult buildResult = with('printVersionDetails').build()

then:
buildResult.output.contains(':printVersionDetails\nnull\n')
}

def 'version details on commit with a tag' () {
given:
buildFile << '''
plugins {
id 'com.palantir.git-version'
}
version gitVersion()
task printVersionDetails() << {
println versionDetails().lastTag
println versionDetails().commitDistance
}
'''.stripIndent()
gitIgnoreFile << 'build'
Git git = Git.init().setDirectory(projectDir).call();
git.add().addFilepattern('.').call()
git.commit().setMessage('initial commit').call()
git.tag().setAnnotated(true).setMessage('1.0.0').setName('1.0.0').call()

when:
BuildResult buildResult = with('printVersionDetails').build()

then:
buildResult.output.contains(":printVersionDetails\n1.0.0\n0\n")
}

def 'version details when commit distance to tag is > 0' () {
given:
buildFile << '''
plugins {
id 'com.palantir.git-version'
}
version gitVersion()
task printVersionDetails() << {
println versionDetails().lastTag
println versionDetails().commitDistance
}
'''.stripIndent()
gitIgnoreFile << 'build'
Git git = Git.init().setDirectory(projectDir).call();
git.add().addFilepattern('.').call()
git.commit().setMessage('initial commit').call()
git.tag().setAnnotated(true).setMessage('1.0.0').setName('1.0.0').call()
git.commit().setMessage('commit 2').call()

when:
BuildResult buildResult = with('printVersionDetails').build()

then:
buildResult.output.contains(":printVersionDetails\n1.0.0\n1\n")
}

private GradleRunner with(String... tasks) {
GradleRunner.create()
.withPluginClasspath(pluginClasspath)
Expand Down

0 comments on commit 245dc15

Please sign in to comment.