Sven Wollinger
02/01/2024, 12:01 PM@Serializable
data class Commit(
val hash: String = "%h",
val author: String = "%an",
val timestamp: String = "%at",
val subject: String = "%f"
)
fun getCommits(repoFolder: File, amount: Int = Integer.MAX_VALUE): List<Commit> {
val baseJson = jsonWithDefaults.encodeToString(Commit()).replace("\"", "\\\"") + "__split__"
val log = exec("git log -$amount --format=\"$baseJson\"", repoFolder)
return log
.split("__split__")
.filter { it.isNotEmpty() }
.map { Json.decodeFromString<Commit>(it) }
}
Ive seen other people serialize git log using the --format command, by manually writing json which was really ugly.
I thought this could be neater with kotlin serializationAdam S
02/02/2024, 3:52 PMinterface CommitSpec {
val hash: String
// ...
}
@Serializable
data class Commit(
override hash: String,
// ...
) : CommitSpec
@Serializable
class CommitTemplate : CommitSpec {
@EncodeDefault
override hash: String = "%h"
// ...
}
However, it would be much more verbose so if what you've got works then I'd stick with it :)