Initial library implementations and tests (#1)
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
This commit is contained in:
2023-12-01 20:42:07 +00:00
parent d5ad54bea5
commit e2764a5473
10 changed files with 305 additions and 2 deletions

View File

@@ -0,0 +1,34 @@
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>"
}