42 lines
1.0 KiB
Kotlin
42 lines
1.0 KiB
Kotlin
package tinyvm
|
|
|
|
import java.security.MessageDigest
|
|
import java.time.Instant
|
|
import java.util.HexFormat
|
|
|
|
/**
|
|
* Represents an arbitrary version manager object.
|
|
*/
|
|
abstract class Object(val type: String) {
|
|
abstract val data: String
|
|
|
|
fun hash(): String =
|
|
HexFormat.of().formatHex(
|
|
MessageDigest
|
|
.getInstance("SHA-1")
|
|
.digest("$type ${data.length}\u0000$data".toByteArray()),
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Commits are a pointer to a 'head' tree with some metadata.
|
|
*/
|
|
class Commit(
|
|
val tree: Tree,
|
|
val author: Author,
|
|
val message: String,
|
|
val timestamp: Instant,
|
|
) : Object("commit") {
|
|
override val data: String
|
|
// Use \n\n for end of header in-case additional metadata is implemented in the future.
|
|
get() = "tree ${tree.hash()}\nauthor $author\ntimestamp ${timestamp.epochSecond}\n\n$message"
|
|
|
|
override fun toString(): String = "commit ${hash()}\n$data"
|
|
}
|
|
|
|
data class Author(
|
|
val name: String,
|
|
val email: String,
|
|
) {
|
|
override fun toString(): String = "$name <$email>"
|
|
} |