Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/59 conventional commits dirty tree #67

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Branch specific snapshot suffixes.
- Add task to create a properties file with all version information.

## [2.3.3]
### Fixed
- Fix dirty working tree in the conventional commits incrementer.

## [2.3.2]
### Fixed
- Fix conventional commits incrementer for BREAKING CHANGES marked by a ! after the scope.
Expand Down
12 changes: 6 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ plugins {
`maven-publish`
`java-gradle-plugin`
`jacoco`
kotlin("jvm") version "1.3.21"
kotlin("jvm") version "1.4.31"
id("com.gradle.plugin-publish") version "0.11.0"
}

group = "io.wusa"
version = "3.0.0"

dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("stdlib-jdk8:1.4.31"))
implementation("org.koin:koin-gradle-plugin:2.1.6")
testImplementation("org.junit.jupiter:junit-jupiter:5.4.0")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.0")
testImplementation("io.mockk:mockk:1.9")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.0")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.1")
testImplementation(kotlin("test-junit5"))
testImplementation("io.mockk:mockk:1.10.6")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.1")
}

repositories {
Expand Down
14 changes: 8 additions & 6 deletions src/main/kotlin/io/wusa/GitService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class GitService(private val gitCommandRunner: GitCommandRunner) {

@Throws(NoLastTagFoundException::class)
fun lastTag(tagPrefix: String = "", tagType: TagType = TagType.ANNOTATED): String {
var cmdArgs = arrayOf("describe", "--dirty", "--abbrev=7", "--match", "$tagPrefix*")
var cmdArgs = arrayOf("describe", "--abbrev=7", "--match", "$tagPrefix*")
if (tagType == TagType.LIGHTWEIGHT) {
cmdArgs = arrayOf("describe", "--tags", "--dirty", "--abbrev=7", "--match", "$tagPrefix*")
cmdArgs = arrayOf("describe", "--tags", "--abbrev=7", "--match", "$tagPrefix*")
}
return try {
gitCommandRunner.execute(cmdArgs)
Expand All @@ -67,9 +67,9 @@ class GitService(private val gitCommandRunner: GitCommandRunner) {
}

fun getCommitsSinceLastTag(tagPrefix: String = "", tagType: TagType = TagType.ANNOTATED): List<String> {
var cmdArgs = arrayOf("describe", "--dirty", "--abbrev=0", "--match", "$tagPrefix*")
var cmdArgs = arrayOf("describe", "--abbrev=0", "--match", "$tagPrefix*")
if (tagType == TagType.LIGHTWEIGHT) {
cmdArgs = arrayOf("describe", "--tags", "--dirty", "--abbrev=0", "--match", "$tagPrefix*")
cmdArgs = arrayOf("describe", "--tags", "--abbrev=0", "--match", "$tagPrefix*")
}
return try {
val lastTag = gitCommandRunner.execute(cmdArgs)
Expand All @@ -79,8 +79,10 @@ class GitService(private val gitCommandRunner: GitCommandRunner) {
}
}

private fun isGitDifferent() =
gitCommandRunner.execute(arrayOf("diff", "--stat")) != ""

private fun isGitDifferent(): Boolean {
return gitCommandRunner.execute(arrayOf("status", "-s")).isNotBlank()
}

private fun getCurrentCommit(): String {
return try {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/wusa/Info.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ data class Info(var semverGitPluginExtension: SemverGitPluginExtension) {
get() = versionService.getVersion()

override fun toString(): String {
return SemanticVersionFormatter.format(this, semverGitPluginExtension.branches, semverGitPluginExtension.snapshotSuffix)
return SemanticVersionFormatter.format(this, semverGitPluginExtension.branches, semverGitPluginExtension.dirtyMarker)
}
}
22 changes: 20 additions & 2 deletions src/main/kotlin/io/wusa/VersionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package io.wusa
import io.wusa.exception.*
import io.wusa.extension.SemverGitPluginExtension
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.koin.java.KoinJavaComponent.inject

class VersionService(private val semverGitPluginExtension: SemverGitPluginExtension, private val gitService: GitService) {

Expand All @@ -18,6 +16,22 @@ class VersionService(private val semverGitPluginExtension: SemverGitPluginExtens
throw GradleException("The current tag is not a semantic version.")
} catch (ex: NoCurrentTagFoundException) {
handleNoCurrentTagFound(versionFactory)
} catch (ex: DirtyWorkingTreeException) {
handleDirtyWorkingTree(versionFactory)
}
}

@Throws(GradleException::class)
private fun handleDirtyWorkingTree(versionFactory: IVersionFactory): Version {
return try {
val lastVersion = getLastVersion(versionFactory)
incrementVersion(lastVersion)
} catch (ex: NoValidSemverTagFoundException) {
throw GradleException(ex.localizedMessage)
} catch (ex: NoIncrementerFoundException) {
throw GradleException(ex.localizedMessage)
} catch (ex: NoLastTagFoundException) {
buildInitialVersion(versionFactory)
}
}

Expand Down Expand Up @@ -70,6 +84,10 @@ class VersionService(private val semverGitPluginExtension: SemverGitPluginExtens
if (!curTag.startsWith(tagPrefix)) {
throw NoCurrentTagFoundException("$curTag doesn't match $tagPrefix")
}
val isDirty = gitService.isDirty()
if (isDirty) {
throw DirtyWorkingTreeException("The current working tree is dirty.")
}

return versionFactory.createFromString(curTag.substring(tagPrefix.length))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.wusa.exception

class DirtyWorkingTreeException: Exception {
constructor(message: String, ex: Throwable?): super(message, ex) {}
constructor(message: String): super(message) {}
constructor(ex: Throwable): super(ex) {}
}
28 changes: 15 additions & 13 deletions src/main/kotlin/io/wusa/formatter/SemanticVersionFormatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.wusa.formatter

import io.wusa.Info
import io.wusa.RegexResolver
import io.wusa.Suffix
import io.wusa.extension.Branches
import io.wusa.extension.SemverGitPluginExtension
import org.gradle.api.Transformer
Expand All @@ -12,12 +11,12 @@ class SemanticVersionFormatter {
fun format(info: Info, branches: Branches, dirtyMarker: String): String {
if (!hasFirstCommit(info)) return appendSuffix(buildVersionString(info), branches, info)

if (hasTag(info)) {
if (hasTag(info) && !isDirty(info)) {
return formatVersionWithTag(info)
}

val formattedVersion = formatVersionWithoutTag(branches, info, dirtyMarker)
if (!hasTag(info)) {
val formattedVersion = formatVersion(branches, info, dirtyMarker)
if (!hasTag(info) || hasTag(info) && isDirty(info)) {
return appendSuffix(formattedVersion, branches, info)
}
return formattedVersion
Expand Down Expand Up @@ -48,19 +47,25 @@ class SemanticVersionFormatter {

private fun buildVersionString(info: Info) = "${info.version.major}.${info.version.minor}.${info.version.patch}"

private fun formatVersionWithoutTag(branches: Branches, info: Info, dirtyMarker: String): String {
private fun formatVersion(branches: Branches, info: Info, dirtyMarker: String): String {
val regexFormatterPair = RegexResolver.findMatchingRegex(branches, info.branch.name)
var formattedVersion = transform(SemverGitPluginExtension.DEFAULT_FORMATTER, info)
formattedVersion = appendDirtyMarker(formattedVersion, info.version.suffix, dirtyMarker)
if (isDirty(info)) {
formattedVersion = appendDirtyMarker(formattedVersion, dirtyMarker)
}
regexFormatterPair?.let {
formattedVersion = transform(regexFormatterPair.formatter, info)
formattedVersion = appendDirtyMarker(formattedVersion, info.version.suffix, dirtyMarker)
if (isDirty(info)) {
formattedVersion = appendDirtyMarker(formattedVersion, dirtyMarker)
}
}
return formattedVersion
}

private fun hasTag(info: Info) = info.version.suffix == null

private fun isDirty(info: Info) = info.dirty

private fun hasVersionBuildInformation(info: Info) = info.version.build != ""

private fun hasVersionPrerelease(info: Info) = info.version.prerelease != ""
Expand All @@ -83,12 +88,9 @@ class SemanticVersionFormatter {
return true
}

private fun appendDirtyMarker(version: String, suffix: Suffix?, dirtyMarker: String): String {
if (suffix != null && suffix.dirty) {
if (dirtyMarker != "") {
return "$version-$dirtyMarker"
}
return version
private fun appendDirtyMarker(version: String, dirtyMarker: String): String {
if (dirtyMarker != "") {
return "$version-$dirtyMarker"
}
return version
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ object ConventionalCommitsVersionIncrementer : Transformer<Version, Version> {
val fix = "^fix$optionalScope"
val breakingChange = "\\bBREAKING CHANGE\\b:"

if (semverGitPluginExtension.info.dirty) patch = 1

listOfCommits.forEach {
when {
it.contains("$feat!:".toRegex()) -> major += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class ConventionalCommitsVersionIncrementerTest {
semverGitPluginExtension = mockkClass(SemverGitPluginExtension::class)
every { semverGitPluginExtension.tagPrefix } returns ""
every { semverGitPluginExtension.tagType } returns TagType.ANNOTATED
val info = Info(semverGitPluginExtension)
every { semverGitPluginExtension.info } returns info
every { gitService.isDirty() } returns false
startKoin {
modules(modules)
}
Expand Down
45 changes: 25 additions & 20 deletions src/test/kotlin/io/wusa/FunctionalBaseTest.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
package io.wusa

import org.gradle.internal.impldep.org.eclipse.jgit.api.Git
import org.gradle.internal.impldep.org.eclipse.jgit.lib.Repository
import java.io.File

abstract class FunctionalBaseTest {

fun initializeGitWithBranch(directory: File, tag: String = "0.1.0", branch: String = "develop"): Git {
val git = Git.init().setDirectory(directory).call()
val commit = git.commit().setMessage("").call()
git.checkout().setCreateBranch(true).setName(branch).call()
git.tag().setName(tag).setObjectId(commit).call()
return git
fun initializeGitWithBranch(repository: Repository, directory: File, tag: String = "0.1.0", branch: String = "develop") {
val gitIgnore = File(directory, ".gitignore")
gitIgnore.writeText(".gradle")
Git(repository).add().addFilepattern(".").call()
val commit = Git(repository).commit().setMessage("").call()
Git(repository).checkout().setCreateBranch(true).setName(branch).call()
Git(repository).tag().setName(tag).setObjectId(commit).call()
}

fun initializeGitWithoutBranchAnnotated(directory: File, tag: String = "0.1.0"): Git {
val git = Git.init().setDirectory(directory).call()
val commit = git.commit().setMessage("").call()
git.tag().setName(tag).setMessage(tag).setAnnotated(true).setObjectId(commit).call()
return git
fun initializeGitWithoutBranchAnnotated(repository: Repository, directory: File, tag: String = "0.1.0") {
val gitIgnore = File(directory, ".gitignore")
gitIgnore.writeText(".gradle")
Git(repository).add().addFilepattern(".").call()
val commit = Git(repository).commit().setMessage("").call()
Git(repository).tag().setName(tag).setMessage(tag).setAnnotated(true).setObjectId(commit).call()
}

fun initializeGitWithoutBranchLightweight(directory: File, tag: String = "0.1.0"): Git {
val git = Git.init().setDirectory(directory).call()
val commit = git.commit().setMessage("").call()
git.tag().setName(tag).setObjectId(commit).setAnnotated(false).call()
return git
fun initializeGitWithoutBranchLightweight(repository: Repository, directory: File, tag: String = "0.1.0") {
val gitIgnore = File(directory, ".gitignore")
gitIgnore.writeText(".gradle")
Git(repository).add().addFilepattern(".").call()
val commit = Git(repository).commit().setMessage("").call()
Git(repository).tag().setName(tag).setObjectId(commit).setAnnotated(false).call()
}

fun initializeGitWithoutBranchAndWithoutTag(directory: File): Git {
val git = Git.init().setDirectory(directory).call()
git.commit().setMessage("").call()
return git
fun initializeGitWithoutBranchAndWithoutTag(repository: Repository, directory: File) {
val gitIgnore = File(directory, ".gitignore")
gitIgnore.writeText(".gradle")
Git(repository).add().addFilepattern(".").call()
Git(repository).commit().setMessage("").call()
}
}
4 changes: 4 additions & 0 deletions src/test/kotlin/io/wusa/InfoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class InfoTest {
fun `get version`() {
val info = Info(semverGitPluginExtension)
every { gitService.currentTag() } returns "0.1.0"
every { gitService.isDirty() } returns false
Assertions.assertEquals("Version(major=0, minor=1, patch=0, prerelease=, build=, suffix=null)", info.version.toString())
}

Expand All @@ -118,6 +119,7 @@ class InfoTest {
semverGitPluginExtension.tagType = TagType.LIGHTWEIGHT
val info = Info(semverGitPluginExtension)
every { gitService.currentTag(any(), tagType = TagType.LIGHTWEIGHT) } returns "0.1.0"
every { gitService.isDirty() } returns false
Assertions.assertEquals("Version(major=0, minor=1, patch=0, prerelease=, build=, suffix=null)", info.version.toString())
}

Expand All @@ -126,6 +128,7 @@ class InfoTest {
semverGitPluginExtension.tagPrefix = "prj_"
val info = Info(semverGitPluginExtension)
every { gitService.currentTag(any(), any()) } returns "prj_0.1.0"
every { gitService.isDirty() } returns false
Assertions.assertEquals("Version(major=0, minor=1, patch=0, prerelease=, build=, suffix=null)", info.version.toString())
}

Expand All @@ -143,6 +146,7 @@ class InfoTest {
fun `current version has not tag with tag prefix`() {
val info = Info(semverGitPluginExtension)
every { gitService.currentTag() } returns "prj_0.1.0"
every { gitService.isDirty() } returns false
Assertions.assertThrows(NoValidSemverTagFoundException::class.java ) {
info.version
}
Expand Down
Loading