From: Alexander Goussas Date: Fri, 24 Oct 2025 16:40:08 +0000 (-0500) Subject: feat: propagate shell errors if git commands fail X-Git-Url: http://git.frustrated-labs.net/?a=commitdiff_plain;h=4cce4e7c11b9d362c37dafd71d28f171245da309;p=gradle-changelog-plugin.git feat: propagate shell errors if git commands fail --- diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 5669953..9c30250 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -9,7 +9,7 @@ changelog { fileName = "CHANGELOG.md" git { - baseBranch = "master" + baseBranch = "main" } } diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogTask.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogTask.kt index 7b1e4af..9a88b77 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogTask.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogTask.kt @@ -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() { diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GitService.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GitService.kt index 47b3d38..2b4bb07 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GitService.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GitService.kt @@ -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, diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/AbstractGitCommand.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/AbstractGitCommand.kt index ca5dda9..779f377 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/AbstractGitCommand.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/AbstractGitCommand.kt @@ -1,16 +1,30 @@ package io.github.aloussase.changelog.git.commands +import org.gradle.api.GradleException + abstract class AbstractGitCommand : GitCommand { abstract fun transform(rawOutput: String): T - override fun execute(): T { + override fun execute(): Result { 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() + ) + } + ) } } diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/GetCurrentBranchCommitsCommand.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/GetCurrentBranchCommitsCommand.kt index e2d440a..975b448 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/GetCurrentBranchCommitsCommand.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/GetCurrentBranchCommitsCommand.kt @@ -2,9 +2,12 @@ package io.github.aloussase.changelog.git.commands import io.github.aloussase.changelog.git.RawCommit -class GetCurrentBranchCommitsCommand : AbstractGitCommand>() { +class GetCurrentBranchCommitsCommand( + val baseBranch: String +) : AbstractGitCommand>() { + 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 = rawOutput diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/GitCommand.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/GitCommand.kt index 40d5b4c..c04e285 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/GitCommand.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/commands/GitCommand.kt @@ -4,6 +4,6 @@ interface GitCommand { val commandLine: String - fun execute(): T + fun execute(): Result }