]> git.frustrated-labs.net Git - gradle-changelog-plugin.git/commitdiff
feat: add git command to get commits from current branch only
authorAlexander Goussas <[email protected]>
Fri, 24 Oct 2025 15:38:26 +0000 (10:38 -0500)
committerAlexander Goussas <[email protected]>
Fri, 24 Oct 2025 16:47:10 +0000 (11:47 -0500)
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogPlugin.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogTask.kt [new file with mode: 0644]
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/config/Config.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/AbstractGitCommand.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GetCurrentBranchCommits.kt [new file with mode: 0644]
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GetCurrentReleaseCommand.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GitCommand.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/parser/MarkdownChangelogParser.kt

index 955e9845332c04955859f4e3338e242cd8f25b26..594d2686bd9e33cceb3e6b0340d7e2bab880eb0e 100644 (file)
@@ -2,11 +2,8 @@ package io.github.aloussase.changelog
 
 import io.github.aloussase.changelog.config.ChangelogPluginExtension
 import io.github.aloussase.changelog.config.Config
-import io.github.aloussase.changelog.formatter.ChangelogFormatterFactory
-import io.github.aloussase.changelog.parser.ChangelogParserFactory
 import org.gradle.api.Plugin
 import org.gradle.api.Project
-import java.io.File
 
 class ChangelogPlugin : Plugin<Project> {
     override fun apply(project: Project) {
@@ -16,20 +13,8 @@ class ChangelogPlugin : Plugin<Project> {
         extension.format.convention("markdown")
         extension.fileName.convention("CHANGELOG.md")
 
-        project.tasks.register("changelog") {
-            val config = Config.from(extension)
-            val parser = ChangelogParserFactory.createParser(config.documentFormat)
-            val changelogFile = File(config.fileName)
-            if (!changelogFile.exists()) {
-                changelogFile.createNewFile()
-            }
-            val document = changelogFile.readText()
-            val changelog = parser.parse(document).getOrThrow()
-
-            // TODO: Add commits from current branch here.
-
-            val formatter = ChangelogFormatterFactory.create(config.documentFormat)
-            changelogFile.writeText(formatter.format(changelog))
+        project.tasks.register("changelog", ChangelogTask::class.java) {
+            it.config = Config.configure(extension)
         }
     }
 }
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
new file mode 100644 (file)
index 0000000..aafc507
--- /dev/null
@@ -0,0 +1,34 @@
+package io.github.aloussase.changelog
+
+import io.github.aloussase.changelog.config.Config
+import io.github.aloussase.changelog.formatter.ChangelogFormatterFactory
+import io.github.aloussase.changelog.git.GetCurrentBranchCommits
+import io.github.aloussase.changelog.parser.ChangelogParserFactory
+import org.gradle.api.DefaultTask
+import org.gradle.api.tasks.Input
+import org.gradle.api.tasks.TaskAction
+import java.io.File
+
+abstract class ChangelogTask : DefaultTask() {
+
+    @Input
+    lateinit var config: Config
+
+    @TaskAction
+    fun run() {
+        val parser = ChangelogParserFactory.createParser(config.documentFormat)
+        val changelogFile = File(config.fileName)
+        if (!changelogFile.exists()) {
+            changelogFile.createNewFile()
+        }
+        val document = changelogFile.readText()
+        val changelog = parser.parse(document).getOrThrow()
+
+        // TODO: Add commits from current branch here.
+        println(GetCurrentBranchCommits().execute())
+
+        val formatter = ChangelogFormatterFactory.create(config.documentFormat)
+        changelogFile.writeText(formatter.format(changelog))
+    }
+
+}
index 927eda3d1f8db4b1eaff8cc27a47ce6c3bae532e..ffd61da03c445a020c7dac032b204c707ca1e647 100644 (file)
@@ -3,13 +3,14 @@ package io.github.aloussase.changelog.config
 import io.github.aloussase.changelog.formats.DocumentFormat
 import org.gradle.api.GradleException
 
-data class Config(
+class Config private constructor(
     val fileName: String,
     val gitBranch: String,
     val documentFormat: DocumentFormat,
 ) {
+
     companion object {
-        fun from(extension: ChangelogPluginExtension): Config {
+        fun configure(extension: ChangelogPluginExtension): Config {
             val format = DocumentFormat.parse(extension.format.get())
             if (format == null) {
                 throw GradleException("Invalid document format: cannot make a changelog from ${extension.format.get()}")
index 13ceb33bbad11b8d6fd09567fb0092ed8b10aaee..1a4a9ed301bfaf6461441ec396f9b10108e8da47 100644 (file)
@@ -1,14 +1,16 @@
 package io.github.aloussase.changelog.git
 
-abstract class AbstractGitCommand : GitCommand {
+abstract class AbstractGitCommand<T> : GitCommand<T> {
 
-    override fun execute(): String {
+    abstract fun transform(rawOutput: String): T
+
+    override fun execute(): T {
         val runtime = Runtime.getRuntime()
         val ary = arrayOf("/bin/sh", "-c", commandLine)
         val process = runtime.exec(ary)
         process.waitFor()
         return process.inputReader().use { br ->
-            br.readText().trim()
+            transform(br.readText().trim())
         }
     }
 
diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GetCurrentBranchCommits.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/GetCurrentBranchCommits.kt
new file mode 100644 (file)
index 0000000..7dae1da
--- /dev/null
@@ -0,0 +1,12 @@
+package io.github.aloussase.changelog.git
+
+class GetCurrentBranchCommits : AbstractGitCommand<List<Pair<String, String>>>() {
+    override val commandLine: String
+        get() = "git log main..HEAD --no-merges --oneline --pretty=format:\"%an|%s\""
+
+    override fun transform(rawOutput: String): List<Pair<String, String>> =
+        rawOutput
+            .split("\n")
+            .map { it.split("|") }
+            .map { Pair(it[0], it[1]) }
+}
index 3bea08dfce52f01457e1a139267fe75053e24208..e9a52c3792c67b1091beaa4fe993e6c20d563eed 100644 (file)
@@ -1,6 +1,8 @@
 package io.github.aloussase.changelog.git
 
-class GetCurrentReleaseCommand : AbstractGitCommand() {
+class GetCurrentReleaseCommand : AbstractGitCommand<String>() {
     override val commandLine: String
         get() = "git tag | tail -n1"
+
+    override fun transform(rawOutput: String): String = rawOutput
 }
index eb18e172cd0af071fc6da40747510aed8db38f3e..1603d5f9f2711d559562f661aef66a68372e9634 100644 (file)
@@ -1,9 +1,9 @@
 package io.github.aloussase.changelog.git
 
-interface GitCommand {
+interface GitCommand<T> {
 
     val commandLine: String
 
-    fun execute(): String
+    fun execute(): T
 
 }
index b3e2a1b3f463072409775226cb5fe41c29ffe3b4..e15e413a26bdf16522469da01e8d777d032b3c82 100644 (file)
@@ -23,7 +23,7 @@ class MarkdownChangelogParser : ChangelogParser {
 
         val entries = arrayListOf<ChangelogEntry>()
 
-        for (blk in doc.split("\n\n")) {
+        for (blk in doc.trim().split("\n\n")) {
             if (DOC_TITLE_REGEX.matches(blk)) continue
 
             val lines = blk.split("\n")