]> git.frustrated-labs.net Git - gradle-changelog-plugin.git/commitdiff
feat: propagate shell errors if git commands fail
authorAlexander Goussas <[email protected]>
Fri, 24 Oct 2025 16:40:08 +0000 (11:40 -0500)
committerAlexander Goussas <[email protected]>
Fri, 24 Oct 2025 16:47:10 +0000 (11:47 -0500)
app/build.gradle.kts
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogTask.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GitService.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/AbstractGitCommand.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/GetCurrentBranchCommitsCommand.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/GitCommand.kt

index 5669953155717b037265232669ab15ec54052188..9c302500eb61309a10f7c7cc886aad1b6d6120d0 100644 (file)
@@ -9,7 +9,7 @@ changelog {
     fileName = "CHANGELOG.md"
 
     git {
-        baseBranch = "master"
+        baseBranch = "main"
     }
 }
 
index 7b1e4afff1ef142987867f8f90d0c8b0f73d29f2..9a88b7711e7c2589a3057941ec3d96d43c945d3e 100644 (file)
@@ -17,11 +17,13 @@ abstract class ChangelogTask : DefaultTask() {
     @Input
     lateinit var config: Config
 
-    private val gitService = GitService(
-        GetCurrentBranchCommand(),
-        GetCurrentBranchCommitsCommand(),
-        GetCurrentReleaseCommand(),
-    )
+    private val gitService by lazy {
+        GitService(
+            GetCurrentBranchCommand(),
+            GetCurrentBranchCommitsCommand(config.gitBranch),
+            GetCurrentReleaseCommand(),
+        )
+    }
 
     @TaskAction
     fun run() {
index 47b3d3889998df3fc09d67d2a42aebe0f82e690e..2b4bb078bc99345b1271051de8dea68be7ab4d3c 100644 (file)
@@ -10,8 +10,8 @@ class GitService(
 ) {
 
     fun getCurrrentBranchChanges(): ChangelogEntry {
-        val currentBranch = currentBranchCommand.execute()
-        val commits = currentBranchCommitsCommand.execute()
+        val currentBranch = currentBranchCommand.execute().getOrThrow()
+        val commits = currentBranchCommitsCommand.execute().getOrThrow()
         val release = Tag("Unreleased")
         return ChangelogEntry(
             release.name,
index ca5dda9dd6bd5da63a69588b5ecf18d40dc9fe2b..779f377902b32063f6565634f09f7033cebc0c12 100644 (file)
@@ -1,16 +1,30 @@
 package io.github.aloussase.changelog.git.commands
 
+import org.gradle.api.GradleException
+
 abstract class AbstractGitCommand<T> : GitCommand<T> {
 
     abstract fun transform(rawOutput: String): T
 
-    override fun execute(): T {
+    override fun execute(): Result<T> {
         val runtime = Runtime.getRuntime()
         val ary = arrayOf("/bin/sh", "-c", commandLine)
         val process = runtime.exec(ary)
         process.waitFor()
-        return process.inputReader().use { br ->
-            transform(br.readText().trim())
+        return if (process.exitValue() == 0) {
+            Result.success(
+                process.inputReader().use { br ->
+                    transform(br.readText().trim())
+                }
+            )
+        } else {
+            Result.failure(
+                process.errorReader().use { br ->
+                    GradleException(
+                        br.readText()
+                    )
+                }
+            )
         }
     }
 
index e2d440aa286cd907584a872b66cfcfb5549c8258..975b4481740341a7b65284ede97a1ca333872c83 100644 (file)
@@ -2,9 +2,12 @@ package io.github.aloussase.changelog.git.commands
 
 import io.github.aloussase.changelog.git.RawCommit
 
-class GetCurrentBranchCommitsCommand : AbstractGitCommand<List<RawCommit>>() {
+class GetCurrentBranchCommitsCommand(
+    val baseBranch: String
+) : AbstractGitCommand<List<RawCommit>>() {
+
     override val commandLine: String
-        get() = "git log main..HEAD --no-merges --oneline --pretty=format:\"%an|%s\""
+        get() = "git log $baseBranch..HEAD --no-merges --oneline --pretty=format:\"%an|%s\""
 
     override fun transform(rawOutput: String): List<RawCommit> =
         rawOutput
index 40d5b4c5b3b28bf2ed40ffa32135bab956aee02a..c04e285e6060843300485bf52e3882cd6b5cfaf8 100644 (file)
@@ -4,6 +4,6 @@ interface GitCommand<T> {
 
     val commandLine: String
 
-    fun execute(): T
+    fun execute(): Result<T>
 
 }