This repository has been archived on 2024-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
teamcity-gitea-test-task/src/main/kotlin/tinyvm/Commit.kt
Gleb Koval e2764a5473
All checks were successful
Main Workflow / Lint and test library (push) Successful in 16m40s
Initial library implementations and tests (#1)
- [x] Implements library according to instructions, with tests.
- [x] GitHub Actions workflow to lint and test library.

Reviewed-on: #1
2023-12-01 20:42:07 +00:00

34 lines
816 B
Kotlin

package tinyvm
import java.security.MessageDigest
import java.time.Instant
abstract class Object(
val type: String,
) {
abstract val data: String
fun hash(): String =
MessageDigest
.getInstance("SHA-1")
.digest("$type ${data.length}\u0000$data".toByteArray())
.toHex()
}
class Commit(
val tree: Tree,
val author: Author,
val message: String,
val timestamp: Instant,
) : Object("commit") {
// Use \n\n for end of header in-case additional metadata is implemented in the future.
override val data: String
get() = "tree ${tree.hash()}\nauthor $author\ntimestamp ${timestamp.epochSecond}\n\n$message"
}
data class Author(
val name: String,
val email: String,
) {
override fun toString(): String = "$name <$email>"
}