diff --git a/src/main/groovy/nebula/plugin/release/git/command/GitCommandParameters.groovy b/src/main/groovy/nebula/plugin/release/git/command/GitCommandParameters.groovy index ba2dfb4..15dbaf2 100644 --- a/src/main/groovy/nebula/plugin/release/git/command/GitCommandParameters.groovy +++ b/src/main/groovy/nebula/plugin/release/git/command/GitCommandParameters.groovy @@ -9,4 +9,5 @@ interface GitCommandParameters extends ValueSourceParameters { Property getGitConfigKey() Property getGitConfigValue() Property getCommit() + Property getTag() } diff --git a/src/main/groovy/nebula/plugin/release/git/command/GitReadCommand.groovy b/src/main/groovy/nebula/plugin/release/git/command/GitReadCommand.groovy index 07c65d5..63b8fc6 100644 --- a/src/main/groovy/nebula/plugin/release/git/command/GitReadCommand.groovy +++ b/src/main/groovy/nebula/plugin/release/git/command/GitReadCommand.groovy @@ -27,6 +27,7 @@ abstract class GitReadCommand implements ValueSource v10.0.0-220-ga00baaa @@ -83,6 +95,21 @@ abstract class TagsPointingAt extends GitReadCommand { } } +/** + * Uses git describe to find a given tag in the history of the current branch + * ex. git describe HEAD --tags --match v10.0.0 -> v10.0.0-220-ga00baaa + */ +abstract class CommitFromTag extends GitReadCommand { + @Override + String obtain() { + try { + return executeGitCommand( "rev-list", "-n", '1', parameters.tag.get()) + } catch (Exception e) { + return null + } + } +} + /** * Uses to determine if a given repo has any commit */ diff --git a/src/main/groovy/nebula/plugin/release/git/command/GitReadOnlyCommandUtil.groovy b/src/main/groovy/nebula/plugin/release/git/command/GitReadOnlyCommandUtil.groovy index 52331ce..66886c3 100644 --- a/src/main/groovy/nebula/plugin/release/git/command/GitReadOnlyCommandUtil.groovy +++ b/src/main/groovy/nebula/plugin/release/git/command/GitReadOnlyCommandUtil.groovy @@ -131,9 +131,13 @@ class GitReadOnlyCommandUtil implements Serializable { } } - String describeHeadWithTags() { + + + String describeHeadWithTags(boolean excludePreReleases) { try { - def describeTagInHeadProvider = providers.of(DescribeHeadWithTag.class) { + def describeTagInHeadProvider = excludePreReleases ? providers.of(DescribeHeadWithTagWithExclude.class) { + it.parameters.rootDir.set(rootDir) + } : providers.of(DescribeHeadWithTag.class) { it.parameters.rootDir.set(rootDir) } return describeTagInHeadProvider.get().toString() @@ -144,6 +148,20 @@ class GitReadOnlyCommandUtil implements Serializable { } } + String findCommitForTag(String tag) { + try { + def commitForTag = providers.of(CommitFromTag.class) { + it.parameters.rootDir.set(rootDir) + it.parameters.tag.set(tag) + } + return commitForTag.get().toString() + .split("\n") + .first()?.replaceAll("\n", "")?.toString() + } catch(Exception e) { + return null + } + } + List getTagsPointingAt(String commit) { try { def tagsPointingAtProvider = providers.of(TagsPointingAt.class) { diff --git a/src/main/groovy/nebula/plugin/release/git/semver/NearestVersionLocator.groovy b/src/main/groovy/nebula/plugin/release/git/semver/NearestVersionLocator.groovy index 9c145b4..bd7e5c1 100644 --- a/src/main/groovy/nebula/plugin/release/git/semver/NearestVersionLocator.groovy +++ b/src/main/groovy/nebula/plugin/release/git/semver/NearestVersionLocator.groovy @@ -83,7 +83,7 @@ class NearestVersionLocator { private getLatestTagWithDistance(boolean excludePreReleases) { try { - String result = gitCommandUtil.describeHeadWithTags() + String result = gitCommandUtil.describeHeadWithTags(excludePreReleases) if(!result) { return [version: UNKNOWN, distance: gitCommandUtil.getCommitCountForHead()] } @@ -93,7 +93,8 @@ class NearestVersionLocator { return [version: parseTag(parts[0], true), distance: 0] } - String commit = parts[parts.size() -1].drop(1) + String foundTag = parts.size() == 4 ? parts[0..1].join('-') : parts[0] + String commit = gitCommandUtil.findCommitForTag(foundTag) List allTagsForCommit = gitCommandUtil.getTagsPointingAt(commit).collect { parseTag(it) }.findAll { @@ -101,12 +102,11 @@ class NearestVersionLocator { } if(!allTagsForCommit || allTagsForCommit.every { !it }) { - String tag = parts.size() == 4 ? parts[0..1].join('-') : parts[0] - Version version = parseTag(tag, true) + Version version = parseTag(foundTag, true) if(version.preReleaseVersion && excludePreReleases) { return [version: UNKNOWN, distance: gitCommandUtil.getCommitCountForHead()] } - return [version: parseTag(tag, true), distance: parts[parts.size() - 2]?.toInteger()] + return [version: parseTag(foundTag, true), distance: parts[parts.size() - 2]?.toInteger()] } def highest = allTagsForCommit.min { a, b ->