Initial library implementations and tests (#1)
All checks were successful
Main Workflow / Lint and test library (push) Successful in 16m40s
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:
34
src/main/kotlin/tinyvm/Commit.kt
Normal file
34
src/main/kotlin/tinyvm/Commit.kt
Normal 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>"
|
||||
}
|
Reference in New Issue
Block a user