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.
Gleb Koval 14950b2f5f
All checks were successful
Publish Workflow / Publish library (push) Successful in 8m18s
Test Workflow / Lint and test library (push) Successful in 17m2s
Documentation generation (Dokka HTML) (#3)
Generate documentation HTML and `javadoc.jar`.

Reviewed-on: #3
2023-12-02 02:05:55 +00:00

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>"
}