Skip to content

Commit

Permalink
Change pre-tag check logic
Browse files Browse the repository at this point in the history
Instead of checking if HEAD has a tag, just make sure tag does not exist.
  • Loading branch information
serpro69 committed Oct 26, 2023
1 parent e0b174e commit e9c741e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class SemverKtPluginFT : DescribeSpec({
it.add().addFilepattern("text.txt").call()
it.commit().setMessage("New commit\n\n[minor]").call()
}
Builder.build(project = project, args = arrayOf("tag", "-Pincrement=minor")) // release a version
Builder.build(project = project, args = arrayOf("tag")) // release a version
// act
val args = if (dryRun) arrayOf("tag", "-PdryRun") else arrayOf("tag")
val result = Builder.build(project = project, args = args)
Expand Down Expand Up @@ -346,48 +346,54 @@ class SemverKtPluginFT : DescribeSpec({
}

describe("multi-module project") {
it("should set next version via commit message") {
val project = SemverKtTestProject(multiModule = true)
// arrange
Git.open(project.projectDir.toFile()).use {
it.tag().setName("v0.1.0").call() // set initial version
project.projectDir.resolve("text.txt").createFile().writeText("Hello")
it.add().addFilepattern("text.txt").call()
it.commit().setMessage("New commit\n\n[minor]").call()
listOf(true, false).forEach { dryRun ->
it("should set next version via commit${if (dryRun) " and dryRun" else ""}") {
val project = SemverKtTestProject(multiModule = true)
// arrange
Git.open(project.projectDir.toFile()).use {
it.tag().setName("v0.1.0").call() // set initial version
project.projectDir.resolve("text.txt").createFile().writeText("Hello")
it.add().addFilepattern("text.txt").call()
it.commit().setMessage("New commit\n\n[minor]").call()
}
// act
val args = if (dryRun) arrayOf("tag", "-PdryRun") else arrayOf("tag")
val result = Builder.build(project = project, args = args)
// assert
result.task(":tag")?.outcome shouldBe TaskOutcome.SUCCESS
result.output shouldContain """
> Task :tag
Calculated next version: 0.2.0
> Task :submodule:tag
Calculated next version: 0.2.0
${if (!dryRun) "Tag v0.2.0 already exists in project" else ""}
""".trimIndent().trim()
}
// act
val result = Builder.build(project = project, args = arrayOf("tag", "-PdryRun"))
// assert
result.task(":tag")?.outcome shouldBe TaskOutcome.SUCCESS
result.output shouldContain """
> Task :tag
Calculated next version: 0.2.0

> Task :submodule:tag
Calculated next version: 0.2.0
""".trimIndent()
}
it("should set next version via gradle property${if (dryRun) " and dryRun" else ""}") {
val project = SemverKtTestProject(multiModule = true)
// arrange
Git.open(project.projectDir.toFile()).use {
it.tag().setName("v0.1.0").call() // set initial version
project.projectDir.resolve("text.txt").createFile().writeText("Hello")
it.add().addFilepattern("text.txt").call()
it.commit().setMessage("New commit").call()
}
// act
val args = if (dryRun) arrayOf("tag", "-PdryRun", "-Pincrement=minor") else arrayOf("tag", "-Pincrement=minor")
val result = Builder.build(project = project, args = args)
// assert
result.task(":tag")?.outcome shouldBe TaskOutcome.SUCCESS
result.output shouldContain """
> Task :tag
Calculated next version: 0.2.0
it("should set next version via gradle property") {
val project = SemverKtTestProject(multiModule = true)
// arrange
Git.open(project.projectDir.toFile()).use {
it.tag().setName("v0.1.0").call() // set initial version
project.projectDir.resolve("text.txt").createFile().writeText("Hello")
it.add().addFilepattern("text.txt").call()
it.commit().setMessage("New commit").call()
> Task :submodule:tag
Calculated next version: 0.2.0
${if (!dryRun) "Tag v0.2.0 already exists in project" else ""}
""".trimIndent().trim()
}
// act
val result = Builder.build(project = project, args = arrayOf("tag", "-PdryRun", "-Pincrement=minor"))
// assert
result.task(":tag")?.outcome shouldBe TaskOutcome.SUCCESS
result.output shouldContain """
> Task :tag
Calculated next version: 0.2.0
> Task :submodule:tag
Calculated next version: 0.2.0
""".trimIndent()
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ abstract class TagTask : SemverReleaseTask() {

private fun setTag(nextVer: Semver, config: SemverKtPluginConfig) {
if (!dryRun.get()) run {
// TODO is there a better way to handle multi-module projects?
// tag might already exist (was set by root project or another sub-module)
val isHeadOnTag = GitRepository(config).use { repo ->
repo.tags().let { tags ->
tags.isNotEmpty()
&& tags.last().let { t -> repo.head() == t.peeledObjectId || repo.head() == t.objectId }
// check if tag exists, don't try to create a duplicate
val tagExists = GitRepository(config).use { repo ->
repo.tags().any {
Semver(it.name.replace(Regex("""^refs/tags/${config.git.tag.prefix}"""), "")) == nextVer
}
}
if (!isHeadOnTag) {
if (!tagExists) {
logger.log(DEBUG, "Open repo at: $this")
FileRepositoryBuilder()
.setWorkTree(project.projectDir)
Expand All @@ -73,7 +71,7 @@ abstract class TagTask : SemverReleaseTask() {
logger.log(DEBUG, "Set tag to: v$nextVer")
Git(it).use { git -> git.setTag("v$nextVer") }
}
} else logger.log(INFO, "Tag v$nextVer already exists in project")
} else logger.log(LIFECYCLE, "Tag v$nextVer already exists in project")
}
}
}
Expand Down

0 comments on commit e9c741e

Please sign in to comment.