Skip to content

Commit f23903e

Browse files
committed
fix(model-client): IModelClientV2.runWrite creates the repository if it doesn't exist
If the referenced branched doesn't exist on the server it is created during write.
1 parent 1ef1b58 commit f23903e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

model-client/src/commonMain/kotlin/org/modelix/model/client2/IModelClientV2.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ interface IModelClientV2 {
6060

6161
suspend fun pull(branch: BranchReference, lastKnownVersion: IVersion?): IVersion
6262

63+
suspend fun pullIfExists(branch: BranchReference): IVersion?
64+
6365
suspend fun pullHash(branch: BranchReference): String
6466

6567
/**

model-client/src/commonMain/kotlin/org/modelix/model/client2/ModelClientV2.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ import io.ktor.client.HttpClientConfig
1818
import io.ktor.client.call.body
1919
import io.ktor.client.plugins.HttpRequestRetry
2020
import io.ktor.client.plugins.HttpTimeout
21+
import io.ktor.client.plugins.ResponseException
2122
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
23+
import io.ktor.client.plugins.expectSuccess
2224
import io.ktor.client.request.get
2325
import io.ktor.client.request.post
2426
import io.ktor.client.request.setBody
2527
import io.ktor.client.statement.bodyAsText
2628
import io.ktor.http.ContentType
29+
import io.ktor.http.HttpStatusCode
2730
import io.ktor.http.URLBuilder
2831
import io.ktor.http.appendPathSegments
2932
import io.ktor.http.contentType
@@ -205,6 +208,24 @@ class ModelClientV2(
205208
return receivedVersion
206209
}
207210

211+
override suspend fun pullIfExists(branch: BranchReference): IVersion? {
212+
val response = httpClient.get {
213+
expectSuccess = false
214+
url {
215+
takeFrom(baseUrl)
216+
appendPathSegmentsEncodingSlash("repositories", branch.repositoryId.id, "branches", branch.branchName)
217+
}
218+
}
219+
220+
val receivedVersion = when (response.status) {
221+
HttpStatusCode.NotFound -> null
222+
HttpStatusCode.OK -> createVersion(null, response.body())
223+
else -> throw ResponseException(response, response.bodyAsText())
224+
}
225+
LOG.debug { "${clientId.toString(16)}.pullIfExists($branch) -> $receivedVersion" }
226+
return receivedVersion
227+
}
228+
208229
override suspend fun pullHash(branch: BranchReference): String {
209230
val response = httpClient.get {
210231
url {
@@ -376,7 +397,11 @@ fun VersionDelta.getAllObjects(): Map<String, String> = objectsMap + objects.ass
376397
*/
377398
suspend fun <T> IModelClientV2.runWrite(branchRef: BranchReference, body: (INode) -> T): T {
378399
val client = this
379-
val baseVersion = client.pull(branchRef, null)
400+
val baseVersion = client.pullIfExists(branchRef)
401+
?: branchRef.repositoryId.getBranchReference()
402+
.takeIf { it != branchRef }
403+
?.let { client.pullIfExists(it) } // master branch
404+
?: client.initRepository(branchRef.repositoryId)
380405
val branch = OTBranch(TreePointer(baseVersion.getTree(), client.getIdGenerator()), client.getIdGenerator(), (client as ModelClientV2).store)
381406
val result = branch.computeWrite {
382407
body(branch.getRootNode())

0 commit comments

Comments
 (0)