Skip to content

Commit 245dc15

Browse files
committed
Merge pull request #27 from gillessed/develop
Add version details.
2 parents 1b311ea + fe68fe1 commit 245dc15

File tree

4 files changed

+154
-9
lines changed

4 files changed

+154
-9
lines changed

readme.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ Git-Version Gradle Plugin
33
[![Build Status](https://circleci.com/gh/palantir/gradle-git-version.svg?style=shield)](https://circleci.com/gh/palantir/gradle-git-version)
44
[![Gradle Plugins Release](https://img.shields.io/github/release/palantir/gradle-git-version.svg)](https://plugins.gradle.org/plugin/com.palantir.git-version)
55

6-
When applied, Git-Version adds a method to the target project called `gitVersion()` that
7-
runs the JGit equivalent of `git describe` to determine a version string. It behaves exactly
8-
as the JGit `git describe` method behaves, except that when the repository is in a dirty
6+
When applied, Git-Version adds two methods to the target project.
7+
8+
The first, called `gitVersion()`, runs the JGit equivalent of `git describe` to determine a version string.
9+
It behaves exactly as the JGit `git describe` method behaves, except that when the repository is in a dirty
910
state, appends `.dirty` to the version string.
1011

12+
The second, called `versionDetails()`, returns an object containing the specific details of the version string:
13+
the tag name, and the commit count since the tag.
14+
1115
Usage
1216
-----
1317
Apply the plugin using standard Gradle convention:
@@ -20,6 +24,12 @@ Set the version of a project by calling:
2024

2125
version gitVersion()
2226

27+
You can get an object containing more detailed information by calling:
28+
29+
def details = versionDetails()
30+
details.lastTag
31+
details.commitDistance
32+
2333
Tasks
2434
-----
2535
This plugin adds a `printVersion` task, which will echo the project's configured version

src/main/groovy/com/palantir/gradle/gitversion/GitVersionPlugin.groovy

+33-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.eclipse.jgit.api.Git
1919
import org.eclipse.jgit.internal.storage.file.FileRepository
2020
import org.gradle.api.Plugin
2121
import org.gradle.api.Project
22+
import com.palantir.gradle.gitversion.VersionDetails;
2223

2324
class GitVersionPlugin implements Plugin<Project> {
2425

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

2829
void apply(Project project) {
2930
project.ext.gitVersion = {
30-
File gitDir = findRootGitDir(project.rootDir)
31-
if (!gitDir.exists()) {
32-
throw new IllegalArgumentException('Cannot find \'.git\' directory')
33-
}
31+
File gitDir = getRootGitDir(project.rootDir)
3432

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

43+
project.ext.versionDetails = {
44+
File gitDir = getRootGitDir(project.rootDir)
45+
46+
try {
47+
Git git = Git.wrap(new FileRepository(gitDir))
48+
String description = git.describe().call();
49+
50+
// Description has no git hash so it is just the tag name
51+
if(!(description =~ /.*g.?[0-9a-fA-F]{3,}/)) {
52+
return new VersionDetails(description, 0);
53+
}
54+
def match = (description =~ /(.*)-([0-9]+)-g.?[0-9a-fA-F]{3,}/)
55+
String tagName = match[0][1]
56+
int commitCount = match[0][2].toInteger()
57+
58+
return new VersionDetails(tagName, commitCount)
59+
} catch (Throwable t) {
60+
return null
61+
}
62+
}
63+
4564
project.tasks.create('printVersion') << {
4665
println project.version
4766
}
4867
}
4968

50-
private File findRootGitDir(File currentRoot) {
69+
private File getRootGitDir(currentRoot) {
70+
File gitDir = scanForRootGitDir(currentRoot)
71+
if (!gitDir.exists()) {
72+
throw new IllegalArgumentException('Cannot find \'.git\' directory')
73+
}
74+
return gitDir
75+
}
76+
77+
private File scanForRootGitDir(File currentRoot) {
5178
File gitDir = new File(currentRoot, '.git')
5279

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

6289
// look in parent directory
63-
return findRootGitDir(currentRoot.parentFile)
90+
return scanForRootGitDir(currentRoot.parentFile)
6491
}
6592
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2016 Palantir Technologies
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* <http://www.apache.org/licenses/LICENSE-2.0>
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.palantir.gradle.gitversion;
17+
18+
/**
19+
* POGO containing the tag name and commit count that make
20+
* up the version string.
21+
*/
22+
class VersionDetails implements Serializable {
23+
private static final long serialVersionUID = -7340444937169877612L;
24+
25+
final String lastTag;
26+
final int commitDistance;
27+
28+
public VersionDetails(String lastTag, int commitDistance) {
29+
this.lastTag = lastTag;
30+
this.commitDistance = commitDistance;
31+
}
32+
33+
public String toString() {
34+
return "VersionDetails[lastTag=${lastTag}, commitDistance=${commitDistance}]";
35+
}
36+
}

src/test/groovy/com/palantir/gradle/gitversion/GitVersionPluginTests.groovy

+72
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,78 @@ class GitVersionPluginTests extends Specification {
208208
buildResult.output.contains(':printVersion\n1.0.0\n')
209209
}
210210

211+
def 'version details null when no tags are present' () {
212+
given:
213+
buildFile << '''
214+
plugins {
215+
id 'com.palantir.git-version'
216+
}
217+
version gitVersion()
218+
task printVersionDetails() << {
219+
println versionDetails()
220+
}
221+
'''.stripIndent()
222+
Git git = Git.init().setDirectory(projectDir).call();
223+
224+
when:
225+
BuildResult buildResult = with('printVersionDetails').build()
226+
227+
then:
228+
buildResult.output.contains(':printVersionDetails\nnull\n')
229+
}
230+
231+
def 'version details on commit with a tag' () {
232+
given:
233+
buildFile << '''
234+
plugins {
235+
id 'com.palantir.git-version'
236+
}
237+
version gitVersion()
238+
task printVersionDetails() << {
239+
println versionDetails().lastTag
240+
println versionDetails().commitDistance
241+
}
242+
'''.stripIndent()
243+
gitIgnoreFile << 'build'
244+
Git git = Git.init().setDirectory(projectDir).call();
245+
git.add().addFilepattern('.').call()
246+
git.commit().setMessage('initial commit').call()
247+
git.tag().setAnnotated(true).setMessage('1.0.0').setName('1.0.0').call()
248+
249+
when:
250+
BuildResult buildResult = with('printVersionDetails').build()
251+
252+
then:
253+
buildResult.output.contains(":printVersionDetails\n1.0.0\n0\n")
254+
}
255+
256+
def 'version details when commit distance to tag is > 0' () {
257+
given:
258+
buildFile << '''
259+
plugins {
260+
id 'com.palantir.git-version'
261+
}
262+
version gitVersion()
263+
task printVersionDetails() << {
264+
println versionDetails().lastTag
265+
println versionDetails().commitDistance
266+
}
267+
268+
'''.stripIndent()
269+
gitIgnoreFile << 'build'
270+
Git git = Git.init().setDirectory(projectDir).call();
271+
git.add().addFilepattern('.').call()
272+
git.commit().setMessage('initial commit').call()
273+
git.tag().setAnnotated(true).setMessage('1.0.0').setName('1.0.0').call()
274+
git.commit().setMessage('commit 2').call()
275+
276+
when:
277+
BuildResult buildResult = with('printVersionDetails').build()
278+
279+
then:
280+
buildResult.output.contains(":printVersionDetails\n1.0.0\n1\n")
281+
}
282+
211283
private GradleRunner with(String... tasks) {
212284
GradleRunner.create()
213285
.withPluginClasspath(pluginClasspath)

0 commit comments

Comments
 (0)