Skip to content

Commit

Permalink
Fix commit number not being null
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierSegoviaCordoba committed Sep 26, 2024
1 parent 3cdbb33 commit 6b42543
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

### Fixed

- commit number not being null

### Removed

- unnecessary call to `Git` in `GitCache`
Expand Down
1 change: 1 addition & 0 deletions gradle-version/api/gradle-version.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("+")
Expand Down Expand Up @@ -92,6 +91,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific
metadata = metadata,
)
}

number == null && stageName.isBlank() -> {
invoke(
major = major,
Expand All @@ -104,6 +104,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific
metadata = metadata,
)
}

number == null && stageName.isNotBlank() && stage?.name.isNullOrBlank() -> {
invoke(
major = major,
Expand All @@ -116,6 +117,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific
metadata = metadata,
)
}

number == null && stageName.isNotBlank() && stageName == stage?.name -> {
invoke(
major = major,
Expand All @@ -128,6 +130,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific
metadata = metadata,
)
}

number == null && stageName.isNotBlank() && stageName != stage?.name -> {
invoke(
major = major,
Expand All @@ -140,6 +143,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific
metadata = metadata,
)
}

number is Increase.Major && stageName.isBlank() -> {
invoke(
major = major.inc(),
Expand All @@ -152,6 +156,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific
metadata = metadata,
)
}

number is Increase.Minor && stageName.isBlank() -> {
invoke(
major = major,
Expand All @@ -164,6 +169,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific
metadata = metadata,
)
}

number is Increase.Patch && stageName.isBlank() -> {
invoke(
major = major,
Expand All @@ -176,6 +182,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific
metadata = metadata,
)
}

number is Increase.Major && stageName.isNotBlank() -> {
invoke(
major = major.inc(),
Expand All @@ -188,6 +195,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific
metadata = metadata,
)
}

number is Increase.Minor && stageName.isNotBlank() -> {
invoke(
major = major,
Expand All @@ -200,6 +208,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific
metadata = metadata,
)
}

number is Increase.Patch && stageName.isNotBlank() -> {
invoke(
major = major,
Expand All @@ -212,6 +221,7 @@ private constructor(private val value: String, checkMode: CheckMode = Insignific
metadata = metadata,
)
}

else -> null
} ?: gradleVersionError("There were an error configuring the version")

Expand Down Expand Up @@ -282,6 +292,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)""")
Expand Down Expand Up @@ -439,6 +451,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,27 @@ internal class GradleVersionTest {
Version(major, minor, patch, stageName, num)
}
}

stageName.equals("snapshot", true) && num != null -> {
shouldThrow<GradleVersionException> {
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<GradleVersionException> {
Version(major, minor, patch, stageName, num)
}
}

else -> Version(major, minor, patch, stageName, num)
}
}
Expand Down Expand Up @@ -400,6 +405,7 @@ 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")
}

@Test
Expand Down

0 comments on commit 6b42543

Please sign in to comment.