Skip to content

Commit fd6eb23

Browse files
jared2501markelliot
jared2501
authored andcommitted
Add VersionDetails#branchName for current branch of HEAD (#43)
* Add VersionDetails#branchName for current branch of HEAD * address comments
1 parent 9f47cb7 commit fd6eb23

File tree

4 files changed

+73
-24
lines changed

4 files changed

+73
-24
lines changed

readme.md

+16-9
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,33 @@ It behaves exactly as the JGit `git describe` method behaves, except that when t
1010
state, appends `.dirty` to the version string.
1111

1212
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.
13+
the tag name, the commit count since the tag, the current commit hash of HEAD, and an optional branch name of HEAD.
1414

1515
Usage
1616
-----
1717
Apply the plugin using standard Gradle convention:
1818

19-
plugins {
20-
id 'com.palantir.git-version' version '<current version>'
21-
}
19+
```groovy
20+
plugins {
21+
id 'com.palantir.git-version' version '<current version>'
22+
}
23+
```
2224

2325
Set the version of a project by calling:
2426

25-
version gitVersion()
27+
```groovy
28+
version gitVersion()
29+
```
2630

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

29-
def details = versionDetails()
30-
details.lastTag
31-
details.commitDistance
32-
details.gitHash
33+
```groovy
34+
def details = versionDetails()
35+
details.lastTag
36+
details.commitDistance
37+
details.gitHash
38+
details.branchName // is null if the repository in detached HEAD mode
39+
```
3340

3441
Tasks
3542
-----

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

+20-12
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
*/
1616
package com.palantir.gradle.gitversion
1717

18-
import groovy.transform.*
19-
20-
import java.util.regex.Matcher
21-
18+
import groovy.transform.Memoized
2219
import org.eclipse.jgit.api.Git
2320
import org.eclipse.jgit.internal.storage.file.FileRepository
21+
import org.eclipse.jgit.lib.Constants
2422
import org.gradle.api.Plugin
2523
import org.gradle.api.Project
2624

25+
import java.util.regex.Matcher
26+
2727
class GitVersionPlugin implements Plugin<Project> {
2828

2929
// Gradle returns 'unspecified' when no version is set
@@ -55,10 +55,17 @@ class GitVersionPlugin implements Plugin<Project> {
5555
@Memoized
5656
private String gitHash(Project project) {
5757
Git git = gitRepo(project)
58-
try {
59-
return git.getRepository().getRef("HEAD").getObjectId().abbreviate(VERSION_ABBR_LENGTH).name()
60-
} catch (Throwable t) {
61-
return UNSPECIFIED_VERSION
58+
return git.getRepository().getRef("HEAD").getObjectId().abbreviate(VERSION_ABBR_LENGTH).name()
59+
}
60+
61+
@Memoized
62+
private String gitBranchName(Project project) {
63+
Git git = gitRepo(project)
64+
def ref = git.repository.getRef(git.repository.branch)
65+
if (ref == null) {
66+
return null
67+
} else {
68+
return ref.getName().substring(Constants.R_HEADS.length())
6269
}
6370
}
6471

@@ -69,22 +76,23 @@ class GitVersionPlugin implements Plugin<Project> {
6976

7077
project.ext.versionDetails = {
7178
String description = gitDesc(project)
72-
String hash = gitHash(project)
73-
7479
if (description.equals(UNSPECIFIED_VERSION)) {
7580
return null
7681
}
7782

83+
String hash = gitHash(project)
84+
String branchName = gitBranchName(project)
85+
7886
if (!(description =~ /.*g.?[0-9a-fA-F]{3,}/)) {
7987
// Description has no git hash so it is just the tag name
80-
return new VersionDetails(description, 0, hash)
88+
return new VersionDetails(description, 0, hash, branchName)
8189
}
8290

8391
Matcher match = (description =~ /(.*)-([0-9]+)-g.?[0-9a-fA-F]{3,}/)
8492
String tagName = match[0][1]
8593
int commitCount = Integer.valueOf(match[0][2])
8694

87-
return new VersionDetails(tagName, commitCount, hash)
95+
return new VersionDetails(tagName, commitCount, hash, branchName)
8896
}
8997

9098
project.tasks.create('printVersion') {

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ class VersionDetails implements Serializable {
2929
final String lastTag;
3030
final int commitDistance;
3131
final String gitHash;
32+
final String branchName;
3233

33-
public VersionDetails(String lastTag, int commitDistance, String gitHash) {
34+
public VersionDetails(String lastTag, int commitDistance, String gitHash, String branchName) {
3435
this.lastTag = lastTag;
3536
this.commitDistance = commitDistance;
3637
this.gitHash = gitHash;
38+
this.branchName = branchName
3739
}
3840

3941
}

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

+34-2
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ class GitVersionPluginTests extends Specification {
238238
println versionDetails().lastTag
239239
println versionDetails().commitDistance
240240
println versionDetails().gitHash
241+
println versionDetails().branchName
241242
}
242243
'''.stripIndent()
243244
gitIgnoreFile << 'build'
@@ -250,7 +251,7 @@ class GitVersionPluginTests extends Specification {
250251
BuildResult buildResult = with('printVersionDetails').build()
251252

252253
then:
253-
buildResult.output =~ ":printVersionDetails\n1.0.0\n0\n[a-z0-9]{10}\n"
254+
buildResult.output =~ ":printVersionDetails\n1.0.0\n0\n[a-z0-9]{10}\nmaster\n"
254255
}
255256

256257
def 'version details when commit distance to tag is > 0' () {
@@ -264,6 +265,7 @@ class GitVersionPluginTests extends Specification {
264265
println versionDetails().lastTag
265266
println versionDetails().commitDistance
266267
println versionDetails().gitHash
268+
println versionDetails().branchName
267269
}
268270
269271
'''.stripIndent()
@@ -278,7 +280,37 @@ class GitVersionPluginTests extends Specification {
278280
BuildResult buildResult = with('printVersionDetails').build()
279281

280282
then:
281-
buildResult.output =~ ":printVersionDetails\n1.0.0\n1\n[a-z0-9]{10}\n"
283+
buildResult.output =~ ":printVersionDetails\n1.0.0\n1\n[a-z0-9]{10}\nmaster\n"
284+
}
285+
286+
def 'version details when detached HEAD mode' () {
287+
given:
288+
buildFile << '''
289+
plugins {
290+
id 'com.palantir.git-version'
291+
}
292+
version gitVersion()
293+
task printVersionDetails() << {
294+
println versionDetails().lastTag
295+
println versionDetails().commitDistance
296+
println versionDetails().gitHash
297+
println versionDetails().branchName
298+
}
299+
300+
'''.stripIndent()
301+
gitIgnoreFile << 'build'
302+
Git git = Git.init().setDirectory(projectDir).call();
303+
git.add().addFilepattern('.').call()
304+
def commit1 = git.commit().setMessage('initial commit').call()
305+
git.tag().setAnnotated(true).setMessage('1.0.0').setName('1.0.0').call()
306+
git.commit().setMessage('commit 2').call()
307+
git.checkout().setName(commit1.getId().getName()).call()
308+
309+
when:
310+
BuildResult buildResult = with('printVersionDetails').build()
311+
312+
then:
313+
buildResult.output =~ ":printVersionDetails\n1.0.0\n0\n[a-z0-9]{10}\nnull\n"
282314
}
283315

284316
private GradleRunner with(String... tasks) {

0 commit comments

Comments
 (0)