All checks were successful
Main Workflow / Lint and test library (push) Successful in 16m40s
- [x] Implements library according to instructions, with tests. - [x] GitHub Actions workflow to lint and test library. Reviewed-on: #1
34 lines
816 B
Kotlin
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>"
|
|
} |