fileName = "CHANGELOG.md"
git {
- baseBranch = "master"
+ baseBranch = "main"
}
}
@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() {
) {
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,
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()
+ )
+ }
+ )
}
}
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
val commandLine: String
- fun execute(): T
+ fun execute(): Result<T>
}