Cleanup and privatise commit comparator
Publish Workflow / Publish library (push) Successful in 8m28s Details
Test Workflow / Lint and test library (push) Successful in 16m51s Details

This commit is contained in:
Gleb Koval 2023-12-05 02:00:35 +00:00
parent 6cb4f82fce
commit 7233a7e8d1
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
5 changed files with 23 additions and 22 deletions

View File

@ -21,13 +21,13 @@ repositories {
dependencies { dependencies {
// other dependencies // other dependencies
implementation("net.koval.teamcity-gitea-test-task:tinyvm:0.1.0") implementation("net.koval.teamcity-gitea-test-task:tinyvm:0.1.1")
} }
``` ```
### Documentation ### Documentation
Use autocompletion and hover menus in your IDE, or download the Use autocompletion and hover menus in your IDE, or download the
[generated HTML documentation](https://git.koval.net/cyclane/teamcity-gitea-test-task/releases/download/v0.1.0/tinyvm-0.1.0-javadoc.zip) [generated HTML documentation](https://git.koval.net/cyclane/teamcity-gitea-test-task/releases/download/v0.1.1/tinyvm-0.1.1-javadoc.zip)
from the [latest release](https://git.koval.net/cyclane/teamcity-gitea-test-task/releases). from the [latest release](https://git.koval.net/cyclane/teamcity-gitea-test-task/releases).
## Instructions ## Instructions

View File

@ -1,22 +1,6 @@
package tinyvm package tinyvm
import java.security.MessageDigest
import java.time.Instant 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. * Commits are a pointer to a 'head' tree with some metadata.

View File

@ -0,0 +1,17 @@
package tinyvm
import java.security.MessageDigest
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()),
)
}

View File

@ -2,7 +2,7 @@ package tinyvm
class HashCollisionException(hash: String) : Exception("Different object types with identical hash '$hash'") class HashCollisionException(hash: String) : Exception("Different object types with identical hash '$hash'")
class CommitTimeComparator : Comparator<Commit> { private class CommitTimeComparator : Comparator<Commit> {
override fun compare( override fun compare(
o1: Commit, o1: Commit,
o2: Commit, o2: Commit,

View File

@ -22,7 +22,7 @@ class Tree(val nodes: Map<String, Node>) : Node("tree") {
when (node) { when (node) {
is Tree -> "+$name/\n" is Tree -> "+$name/\n"
is Blob -> "+$name\n" is Blob -> "+$name\n"
} + node.toString().leftMargin() } + node.toString().insertLeftMargin()
}.sorted().joinToString("\n") }.sorted().joinToString("\n")
} }
@ -30,7 +30,7 @@ class Tree(val nodes: Map<String, Node>) : Node("tree") {
* A blob is a data container. * A blob is a data container.
*/ */
class Blob(override val data: String) : Node("blob") { class Blob(override val data: String) : Node("blob") {
override fun toString(): String = "blob ${hash()}\n${data.leftMargin()}" override fun toString(): String = "blob ${hash()}\n${data.insertLeftMargin()}"
} }
private fun String.leftMargin(): String = split('\n').joinToString("\n") { "| $it" } private fun String.insertLeftMargin(): String = split('\n').joinToString("\n") { "| $it" }