Cleanup and privatise commit comparator
This commit is contained in:
parent
6cb4f82fce
commit
7233a7e8d1
|
@ -21,13 +21,13 @@ repositories {
|
|||
|
||||
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
|
||||
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).
|
||||
|
||||
## Instructions
|
||||
|
|
|
@ -1,22 +1,6 @@
|
|||
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.
|
||||
|
|
|
@ -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()),
|
||||
)
|
||||
}
|
|
@ -2,7 +2,7 @@ package tinyvm
|
|||
|
||||
class HashCollisionException(hash: String) : Exception("Different object types with identical hash '$hash'")
|
||||
|
||||
class CommitTimeComparator : Comparator<Commit> {
|
||||
private class CommitTimeComparator : Comparator<Commit> {
|
||||
override fun compare(
|
||||
o1: Commit,
|
||||
o2: Commit,
|
||||
|
|
|
@ -22,7 +22,7 @@ class Tree(val nodes: Map<String, Node>) : Node("tree") {
|
|||
when (node) {
|
||||
is Tree -> "+$name/\n"
|
||||
is Blob -> "+$name\n"
|
||||
} + node.toString().leftMargin()
|
||||
} + node.toString().insertLeftMargin()
|
||||
}.sorted().joinToString("\n")
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ class Tree(val nodes: Map<String, Node>) : Node("tree") {
|
|||
* A blob is a data container.
|
||||
*/
|
||||
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" }
|
Reference in New Issue