Skip to content

Commit

Permalink
NearestVersionLocator: use proper commit to find tags for a given commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalcolea committed Dec 1, 2023
1 parent 4b6705b commit 1a25aeb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ interface GitCommandParameters extends ValueSourceParameters {
Property<String> getGitConfigKey()
Property<String> getGitConfigValue()
Property<String> getCommit()
Property<String> getTag()
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abstract class GitReadCommand implements ValueSource<String, GitCommandParameter
commandLineArgs.addAll(args)
execOperations.exec {
it.setCommandLine(commandLineArgs)
it.ignoreExitValue = true
it.standardOutput = output
it.errorOutput = error
}
Expand Down Expand Up @@ -68,6 +69,17 @@ abstract class DescribeHeadWithTag extends GitReadCommand {
}
}
}

abstract class DescribeHeadWithTagWithExclude extends GitReadCommand {
@Override
String obtain() {
try {
return executeGitCommand( "describe", "HEAD", "--tags", "--long", "--exclude", "\"*-rc.*\"")
} catch (Exception e) {
return null
}
}
}
/**
* 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
Expand All @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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<String> getTagsPointingAt(String commit) {
try {
def tagsPointingAtProvider = providers.of(TagsPointingAt.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
}
Expand All @@ -93,20 +93,20 @@ 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<Version> allTagsForCommit = gitCommandUtil.getTagsPointingAt(commit).collect {
parseTag(it)
}.findAll {
it && excludePreReleases ? !it.preReleaseVersion : true
}

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 ->
Expand Down

0 comments on commit 1a25aeb

Please sign in to comment.