From 6a66b2b1fed3e5eb39c40e0a5e3203a54694cb1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Segovia=20C=C3=B3rdoba?= Date: Thu, 26 Sep 2024 17:18:56 +0200 Subject: [PATCH] Fix commit number not being null --- CHANGELOG.md | 2 ++ gradle-version/api/gradle-version.api | 1 + .../javiersc/gradle/version/GradleVersion.kt | 33 +++++++++++++++---- .../gradle/version/GradleVersionTest.kt | 22 ++++++++++--- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 656156e0..92e09474 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ ### Fixed +- commit number not being null + ### Removed - unnecessary call to `Git` in `GitCache` diff --git a/gradle-version/api/gradle-version.api b/gradle-version/api/gradle-version.api index dc146683..db6bb5f7 100644 --- a/gradle-version/api/gradle-version.api +++ b/gradle-version/api/gradle-version.api @@ -32,6 +32,7 @@ public final class com/javiersc/gradle/version/GradleVersion$CheckMode : java/la } public final class com/javiersc/gradle/version/GradleVersion$Companion { + public final fun getCommitNumberRegex ()Lkotlin/text/Regex; public final fun getCommitsHashRegex ()Lkotlin/text/Regex; public final fun getCommitsPlusMetadataRegex ()Lkotlin/text/Regex; public final fun getDotCommitsHashRegex ()Lkotlin/text/Regex; diff --git a/gradle-version/main/kotlin/com/javiersc/gradle/version/GradleVersion.kt b/gradle-version/main/kotlin/com/javiersc/gradle/version/GradleVersion.kt index a113b587..53dc2d98 100644 --- a/gradle-version/main/kotlin/com/javiersc/gradle/version/GradleVersion.kt +++ b/gradle-version/main/kotlin/com/javiersc/gradle/version/GradleVersion.kt @@ -27,9 +27,8 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific public val stage: Stage? = hyphenStageRegex.find(value)?.value?.remove("-")?.let(Stage::invoke) public val commits: Int? = run { - val commitsAndHashOrDirty: String? = - dotCommitsHashRegex.find(value)?.value ?: dotCommitsPlusMetadataRegex.find(value)?.value - commitsAndHashOrDirty?.drop(1)?.takeWhile(Char::isDigit)?.toIntOrNull() + val commitsNumber: String? = commitNumberRegex.find(value)?.groupValues?.get(1) + commitsNumber?.toInt() } public val hash: String? = dotCommitsHashRegex.find(value)?.value?.substringAfter("+") @@ -65,7 +64,15 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific patch < other.patch -> -1 stage == null && other.stage != null -> 1 stage != null && other.stage == null -> -1 - stage != null && other.stage != null -> stage.compareTo(other.stage) + stage != null && other.stage != null -> { + val stageComparison: Int = stage.compareTo(other.stage) + if (stageComparison == 0) this.compareAfterStage(other) else stageComparison + } + else -> this.compareAfterStage(other) + } + + private fun GradleVersion.compareAfterStage(other: GradleVersion): Int = + when { commits != null && other.commits == null -> 1 commits == null && other.commits != null -> -1 commits != null && other.commits != null -> commits.compareTo(other.commits) @@ -92,6 +99,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific metadata = metadata, ) } + number == null && stageName.isBlank() -> { invoke( major = major, @@ -104,6 +112,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific metadata = metadata, ) } + number == null && stageName.isNotBlank() && stage?.name.isNullOrBlank() -> { invoke( major = major, @@ -116,6 +125,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific metadata = metadata, ) } + number == null && stageName.isNotBlank() && stageName == stage?.name -> { invoke( major = major, @@ -128,6 +138,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific metadata = metadata, ) } + number == null && stageName.isNotBlank() && stageName != stage?.name -> { invoke( major = major, @@ -140,6 +151,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific metadata = metadata, ) } + number is Increase.Major && stageName.isBlank() -> { invoke( major = major.inc(), @@ -152,6 +164,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific metadata = metadata, ) } + number is Increase.Minor && stageName.isBlank() -> { invoke( major = major, @@ -164,6 +177,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific metadata = metadata, ) } + number is Increase.Patch && stageName.isBlank() -> { invoke( major = major, @@ -176,6 +190,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific metadata = metadata, ) } + number is Increase.Major && stageName.isNotBlank() -> { invoke( major = major.inc(), @@ -188,6 +203,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific metadata = metadata, ) } + number is Increase.Minor && stageName.isNotBlank() -> { invoke( major = major, @@ -200,6 +216,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific metadata = metadata, ) } + number is Increase.Patch && stageName.isNotBlank() -> { invoke( major = major, @@ -212,6 +229,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific metadata = metadata, ) } + else -> null } ?: gradleVersionError("There were an error configuring the version") @@ -282,6 +300,8 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific public val hashRegex: Regex = Regex("""([A-Za-z0-9]{7})""") + public val commitNumberRegex: Regex = Regex("""^\d+\.\d+\.\d+(?:-\w+\.\d+)?\.(\d+)\+""") + public val commitsHashRegex: Regex = Regex("""($numRegex)\+($hashRegex)""") public val dotCommitsHashRegex: Regex = Regex("""(\.$commitsHashRegex)""") @@ -421,7 +441,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific val special = SpecialStage(this) val otherSpecial = SpecialStage(other) - val specialComparison = + val specialComparison: Int? = when { special != null && otherSpecial == null -> 1 special == null && otherSpecial != null -> -1 @@ -429,7 +449,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific else -> null } - val devComparison = + val devComparison: Int? = when { name.lowercase() == dev && other.name.lowercase() == dev -> { when { @@ -439,6 +459,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific else -> gradleVersionError("`dev` version stage must contain a `num`") } } + name.lowercase() != dev && other.name.lowercase() == dev -> 1 name.lowercase() == dev && other.name.lowercase() != dev -> -1 else -> null diff --git a/gradle-version/test/kotlin/com/javiersc/gradle/version/GradleVersionTest.kt b/gradle-version/test/kotlin/com/javiersc/gradle/version/GradleVersionTest.kt index 4ddce0b4..b812e818 100644 --- a/gradle-version/test/kotlin/com/javiersc/gradle/version/GradleVersionTest.kt +++ b/gradle-version/test/kotlin/com/javiersc/gradle/version/GradleVersionTest.kt @@ -200,7 +200,7 @@ internal class GradleVersionTest { (this.patch == other.patch) && (this.stage?.name != null) && (other.stage?.name != null) && - (this.stage!!.name > other.stage!!.name) + (this.stage.name > other.stage.name) @Test fun stage_num_comparator() = runTestNoTimeout { @@ -215,10 +215,10 @@ internal class GradleVersionTest { (this.patch == other.patch) && (this.stage?.name != null) && (other.stage?.name != null) && - (this.stage!!.name == other.stage!!.name) && - (this.stage?.num != null) && - (other.stage?.num != null) && - (this.stage!!.num!! > other.stage!!.num!!) + (this.stage.name == other.stage.name) && + (this.stage.num != null) && + (other.stage.num != null) && + (this.stage.num > other.stage.num) @Test fun wrong_versions() = runTestNoTimeout { @@ -229,22 +229,27 @@ internal class GradleVersionTest { Version(major, minor, patch, stageName, num) } } + stageName.equals("snapshot", true) && num != null -> { shouldThrow { Version(major, minor, patch, stageName, num) } } + stageName.equals("SNAPSHOT", true) && num == null -> { Version(major, minor, patch, stageName, num) } + stageName.equals("snapshot", true) && num == null -> { Version(major, minor, patch, stageName, num) } + stageName != null && num == null -> { shouldThrow { Version(major, minor, patch, stageName, num) } } + else -> Version(major, minor, patch, stageName, num) } } @@ -400,6 +405,13 @@ internal class GradleVersionTest { Version("5.9.10-SNAPSHOT") shouldBeGreaterThan Version("5.9.10-zasca.5") Version("10.4.2-snapshot") shouldBeGreaterThan Version("10.4.2-zasca.5") Version("1.0.0") shouldBeGreaterThan Version("1.0.0-rc.1") + Version("0.4.3.1+1cbf00b+2.0.20") shouldBeGreaterThan Version("0.4.3+2.0.20") + Version("0.4.3.13+1cbf00b+2.0.20") shouldBeGreaterThan Version("0.4.3+2.0.20") + Version("10.40.30.13+1cbf00b+2.0.20") shouldBeGreaterThan Version("10.40.30+2.0.20") + Version("0.4.3-rc.1.1+1cbf00b+2.0.20") shouldBeGreaterThan Version("0.4.3-rc.1+2.0.20") + Version("0.4.13-rc.1.13+1cbf00b+2.0.20") shouldBeGreaterThan Version("0.4.13-rc.1+2.0.20") + Version("10.40.30-rc.1.13+1cbf00b+2.0.20") shouldBeGreaterThan + Version("10.40.30-rc.1+2.0.20") } @Test