2 Commits

Author SHA1 Message Date
7233a7e8d1 Cleanup and privatise commit comparator
All checks were successful
Publish Workflow / Publish library (push) Successful in 8m28s
Test Workflow / Lint and test library (push) Successful in 16m51s
2023-12-05 02:00:35 +00:00
6cb4f82fce Cleanup comments in README and Commit.kt
All checks were successful
Test Workflow / Lint and test library (push) Successful in 16m49s
2023-12-02 20:51:34 +00:00
5 changed files with 24 additions and 23 deletions

View File

@@ -20,13 +20,14 @@ repositories {
}
dependencies {
implementation("net.koval.teamcity-gitea-test-task:tinyvm:0.1.0")
// other dependencies
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

View File

@@ -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.
@@ -28,7 +12,6 @@ class Commit(
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"

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 CommitTimeComparator : Comparator<Commit> {
private class CommitTimeComparator : Comparator<Commit> {
override fun compare(
o1: Commit,
o2: Commit,

View File

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