Skip to content

Commit 42d3d04

Browse files
MatthewMacleanRobert Fink
authored and
Robert Fink
committed
Allow definition of a tag prefix and if it's included in version (#61)
1 parent 1f60eda commit 42d3d04

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

readme.md

+14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ details.branchName // is null if the repository in detached HEAD mode
3939
details.isCleanTag
4040
```
4141

42+
You can optionally search a subset of tags with `prefix`. Example when the tag is [email protected]:
43+
44+
```groovy
45+
gitVersion(prefix:'my-product@') // -> 2.15.0
46+
```
47+
48+
Valid prefixes are defined by the regex `[/@]?([A-Za-z]+[/@-])+`.
49+
```
50+
/Abc/
51+
Abc@
52+
foo-bar@
53+
foo/bar@
54+
```
55+
4256
Tasks
4357
-----
4458
This plugin adds a `printVersion` task, which will echo the project's configured version
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.palantir.gradle.gitversion
2+
3+
class GitVersionArgs {
4+
String prefix = ''
5+
}

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

+20-6
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ import org.gradle.api.Project
2828
class GitVersionPlugin implements Plugin<Project> {
2929

3030
private static final int VERSION_ABBR_LENGTH = 10
31+
private static final String PREFIX_REGEX = "[/@]?([A-Za-z]+[/@-])+"
3132

3233
void apply(Project project) {
3334
project.ext.gitVersion = {
34-
return versionDetails(project).version
35+
args = [:] ->
36+
return versionDetails(project, args as GitVersionArgs).version
3537
}
3638

3739
project.ext.versionDetails = {
38-
return versionDetails(project)
40+
args = [:] ->
41+
return versionDetails(project, args as GitVersionArgs)
3942
}
4043

4144
project.tasks.create('printVersion') {
@@ -47,9 +50,19 @@ class GitVersionPlugin implements Plugin<Project> {
4750
}
4851
}
4952

53+
static void verifyPrefix(String prefix) {
54+
assert prefix != null && (prefix == "" || prefix.matches(PREFIX_REGEX)),
55+
"Specified prefix `${prefix}` does not match the allowed format regex `${PREFIX_REGEX}`."
56+
}
57+
58+
private static String stripPrefix(String description, String prefix) {
59+
return !description ? description : description.replaceFirst("^${prefix}", "")
60+
}
61+
5062
@Memoized
51-
private VersionDetails versionDetails(Project project) {
52-
String description = gitDescribe(project)
63+
private VersionDetails versionDetails(Project project, GitVersionArgs args) {
64+
verifyPrefix(args.prefix)
65+
String description = stripPrefix(gitDescribe(project, args.prefix), args.prefix)
5366
String hash = gitHash(project)
5467
String branchName = gitBranchName(project)
5568
boolean isClean = isClean(project)
@@ -64,7 +77,7 @@ class GitVersionPlugin implements Plugin<Project> {
6477
}
6578

6679
@Memoized
67-
private String gitDescribe(Project project) {
80+
private String gitDescribe(Project project, String prefix) {
6881
// verify that "git" command exists (throws exception if it does not)
6982
GitCli.verifyGitCommandExists()
7083

@@ -74,7 +87,8 @@ class GitVersionPlugin implements Plugin<Project> {
7487
// first to preserve this behavior in cases where this call would fail but native "git" call does not.
7588
new DescribeCommand(git.getRepository()).call()
7689

77-
return GitCli.runGitCommand(project.rootDir, "describe", "--tags", "--always", "--first-parent")
90+
return GitCli.runGitCommand(project.rootDir, "describe", "--tags", "--always", "--first-parent",
91+
"--match=${prefix}*")
7892
} catch (Throwable t) {
7993
return null
8094
}

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

+50-5
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@
1515
*/
1616
package com.palantir.gradle.gitversion
1717

18+
import org.eclipse.jgit.api.Git
1819
import org.eclipse.jgit.api.MergeCommand
1920
import org.eclipse.jgit.lib.Ref
20-
21-
import java.nio.file.Files
22-
23-
import org.eclipse.jgit.api.Git
2421
import org.gradle.testkit.runner.BuildResult
2522
import org.gradle.testkit.runner.GradleRunner
2623
import org.junit.Rule
2724
import org.junit.rules.TemporaryFolder
28-
2925
import spock.lang.Specification
3026

27+
import java.nio.file.Files
28+
3129
class GitVersionPluginTests extends Specification {
3230
@Rule
3331
TemporaryFolder temporaryFolder = new TemporaryFolder()
@@ -416,6 +414,53 @@ class GitVersionPluginTests extends Specification {
416414
buildResult.output =~ ":printVersionDetails\n1.0.0\n0\n[a-z0-9]{10}\nnull\n"
417415
}
418416

417+
def 'version filters out tags not matching prefix and strips prefix' () {
418+
given:
419+
buildFile << '''
420+
plugins {
421+
id 'com.palantir.git-version'
422+
}
423+
version gitVersion(prefix:"my-product@")
424+
task printVersionDetails() << {
425+
println versionDetails(prefix:"my-product@").lastTag
426+
}
427+
'''.stripIndent()
428+
gitIgnoreFile << 'build'
429+
Git git = Git.init().setDirectory(projectDir).call();
430+
git.add().addFilepattern('.').call()
431+
git.commit().setMessage('initial commit').call()
432+
git.tag().setAnnotated(true).setMessage('[email protected]').setName('[email protected]').call()
433+
git.commit().setMessage('commit 2').call()
434+
git.tag().setAnnotated(true).setMessage('1.1.0').setName('1.1.0').call()
435+
436+
when:
437+
BuildResult buildResult = with('printVersionDetails').build()
438+
439+
then:
440+
buildResult.output =~ ":printVersionDetails\n1.0.0\n"
441+
}
442+
443+
def 'test valid prefixes' () {
444+
expect:
445+
GitVersionPlugin.verifyPrefix("@Product@")
446+
GitVersionPlugin.verifyPrefix("abc@")
447+
GitVersionPlugin.verifyPrefix("abc@test@")
448+
GitVersionPlugin.verifyPrefix("Abc-aBc-abC@")
449+
GitVersionPlugin.verifyPrefix("foo-bar@")
450+
GitVersionPlugin.verifyPrefix("foo-bar/")
451+
GitVersionPlugin.verifyPrefix("foo-bar-")
452+
GitVersionPlugin.verifyPrefix("foo/bar@")
453+
GitVersionPlugin.verifyPrefix("Foo/Bar@")
454+
}
455+
456+
def 'test requires @ or / or - between prefix and version' () {
457+
when:
458+
GitVersionPlugin.verifyPrefix("v")
459+
460+
then:
461+
thrown AssertionError
462+
}
463+
419464
private GradleRunner with(String... tasks) {
420465
GradleRunner.create()
421466
.withPluginClasspath()

0 commit comments

Comments
 (0)