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 8721c1a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 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 @@ -83,6 +83,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,6 +131,8 @@ class GitReadOnlyCommandUtil implements Serializable {
}
}



String describeHeadWithTags() {
try {
def describeTagInHeadProvider = providers.of(DescribeHeadWithTag.class) {
Expand All @@ -144,6 +146,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 @@ -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 8721c1a

Please sign in to comment.