@@ -18,12 +18,15 @@ import io.ktor.client.HttpClientConfig
18
18
import io.ktor.client.call.body
19
19
import io.ktor.client.plugins.HttpRequestRetry
20
20
import io.ktor.client.plugins.HttpTimeout
21
+ import io.ktor.client.plugins.ResponseException
21
22
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
23
+ import io.ktor.client.plugins.expectSuccess
22
24
import io.ktor.client.request.get
23
25
import io.ktor.client.request.post
24
26
import io.ktor.client.request.setBody
25
27
import io.ktor.client.statement.bodyAsText
26
28
import io.ktor.http.ContentType
29
+ import io.ktor.http.HttpStatusCode
27
30
import io.ktor.http.URLBuilder
28
31
import io.ktor.http.appendPathSegments
29
32
import io.ktor.http.contentType
@@ -205,6 +208,24 @@ class ModelClientV2(
205
208
return receivedVersion
206
209
}
207
210
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
+
208
229
override suspend fun pullHash (branch : BranchReference ): String {
209
230
val response = httpClient.get {
210
231
url {
@@ -376,7 +397,11 @@ fun VersionDelta.getAllObjects(): Map<String, String> = objectsMap + objects.ass
376
397
*/
377
398
suspend fun <T > IModelClientV2.runWrite (branchRef : BranchReference , body : (INode ) -> T ): T {
378
399
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)
380
405
val branch = OTBranch (TreePointer (baseVersion.getTree(), client.getIdGenerator()), client.getIdGenerator(), (client as ModelClientV2 ).store)
381
406
val result = branch.computeWrite {
382
407
body(branch.getRootNode())
0 commit comments