Skip to content

Commit 89d45ff

Browse files
authored
Merge pull request #81 from abless/ab/fixCommitSha
Fix wrong commit sha in version
2 parents a05ec5c + c43f4d4 commit 89d45ff

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ class GitVersionPlugin implements Plugin<Project> {
117117
if (tagRefs.contains(rev)) {
118118
String exactTag = runGitCmd("describe", "--tags", "--exact-match", "--match=${prefix}*", rev)
119119
if (exactTag != "") {
120-
return depth == 0 ? exactTag : String.format("%s-%s-g%s", exactTag, depth, abbrevHash(rev))
120+
return depth == 0 ?
121+
exactTag : String.format("%s-%s-g%s", exactTag, depth, abbrevHash(revs.get(0)))
121122
}
122123
}
123124
}

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

+72
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.palantir.gradle.gitversion
1818
import org.eclipse.jgit.api.Git
1919
import org.eclipse.jgit.api.MergeCommand
2020
import org.eclipse.jgit.lib.Ref
21+
import org.eclipse.jgit.revwalk.RevCommit
2122
import org.gradle.testkit.runner.BuildResult
2223
import org.gradle.testkit.runner.GradleRunner
2324
import org.junit.Rule
@@ -159,6 +160,27 @@ class GitVersionPluginTests extends Specification {
159160
buildResult.output.contains(":printVersion\n1.0.0\n")
160161
}
161162

163+
def 'git describe when lightweight tag is present' () {
164+
given:
165+
buildFile << '''
166+
plugins {
167+
id 'com.palantir.git-version'
168+
}
169+
version gitVersion()
170+
'''.stripIndent()
171+
gitIgnoreFile << 'build'
172+
Git git = Git.init().setDirectory(projectDir).call();
173+
git.add().addFilepattern('.').call()
174+
git.commit().setMessage('initial commit').call()
175+
git.tag().setAnnotated(false).setName('1.0.0').call()
176+
177+
when:
178+
BuildResult buildResult = with('printVersion').build()
179+
180+
then:
181+
buildResult.output.contains(":printVersion\n1.0.0\n")
182+
}
183+
162184
def 'git describe when annotated tag is present with merge commit' () {
163185
given:
164186
buildFile << '''
@@ -442,6 +464,56 @@ class GitVersionPluginTests extends Specification {
442464
buildResult.output =~ ":printVersionDetails\n1.0.0\n"
443465
}
444466

467+
def 'git describe with commit after annotated tag' () {
468+
given:
469+
buildFile << '''
470+
plugins {
471+
id 'com.palantir.git-version'
472+
}
473+
version gitVersion()
474+
'''.stripIndent()
475+
gitIgnoreFile << 'build'
476+
Git git = Git.init().setDirectory(projectDir).call();
477+
git.add().addFilepattern('.').call()
478+
git.commit().setMessage('initial commit').call()
479+
git.tag().setAnnotated(true).setMessage('1.0.0').setName('1.0.0').call()
480+
dirtyContentFile << 'dirty-content'
481+
git.add().addFilepattern('.').call()
482+
RevCommit latestCommit = git.commit().setMessage('added some stuff').call()
483+
484+
when:
485+
BuildResult buildResult = with('printVersion').build()
486+
String commitSha = latestCommit.getName()
487+
488+
then:
489+
buildResult.output.contains(":printVersion\n1.0.0-1-g${commitSha.substring(0, 7)}\n")
490+
}
491+
492+
def 'git describe with commit after lightweight tag' () {
493+
given:
494+
buildFile << '''
495+
plugins {
496+
id 'com.palantir.git-version'
497+
}
498+
version gitVersion()
499+
'''.stripIndent()
500+
gitIgnoreFile << 'build'
501+
Git git = Git.init().setDirectory(projectDir).call();
502+
git.add().addFilepattern('.').call()
503+
git.commit().setMessage('initial commit').call()
504+
git.tag().setAnnotated(false).setName('1.0.0').call()
505+
dirtyContentFile << 'dirty-content'
506+
git.add().addFilepattern('.').call()
507+
RevCommit latestCommit = git.commit().setMessage('added some stuff').call()
508+
509+
when:
510+
BuildResult buildResult = with('printVersion').build()
511+
String commitSha = latestCommit.getName()
512+
513+
then:
514+
buildResult.output.contains(":printVersion\n1.0.0-1-g${commitSha.substring(0, 7)}\n")
515+
}
516+
445517
def 'test valid prefixes' () {
446518
expect:
447519
GitVersionPlugin.verifyPrefix("@Product@")

0 commit comments

Comments
 (0)