Skip to content

Commit 7d2c86e

Browse files
abstraktorslisson
authored andcommitted
feat(model-client): implement createBranch(…) properly
1 parent 0c51480 commit 7d2c86e

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ interface IModelClientV2 {
8484
suspend fun push(branch: BranchReference, version: IVersion, baseVersion: IVersion?, force: Boolean = false): IVersion
8585
suspend fun push(branch: BranchReference, version: IVersion, baseVersions: List<IVersion>, force: Boolean = false): IVersion
8686

87+
/**
88+
* The pushed version is merged automatically by the server with the current head.
89+
*
90+
* If [failIfExists] is true, the push fails if the version already exists on the server.
91+
* @return The resulting version on the server or null iff the version already exists and [failIfExists] is true.
92+
* @param baseVersion Some version that is known to exist on the server.
93+
* Is used for optimizing the amount of data sent to the server.
94+
*/
95+
suspend fun push(branch: BranchReference, version: IVersion, baseVersion: IVersion?, force: Boolean = false, failIfExists: Boolean): IVersion?
96+
suspend fun push(branch: BranchReference, version: IVersion, baseVersions: List<IVersion>, force: Boolean = false, failIfExists: Boolean): IVersion?
97+
8798
suspend fun pull(
8899
branch: BranchReference,
89100
lastKnownVersion: IVersion?,

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,20 @@ class ModelClientV2(
518518
}
519519

520520
override suspend fun push(branch: BranchReference, version: IVersion, baseVersion: IVersion?, force: Boolean): IVersion {
521-
return push(branch, version, listOfNotNull(baseVersion), force)
521+
// safe to ignore null, because push may only return null if failIfExists is true
522+
return push(branch, version, baseVersion, force, failIfExists = false)!!
522523
}
523524

524525
override suspend fun push(branch: BranchReference, version: IVersion, knownVersions: List<IVersion>, force: Boolean): IVersion {
526+
// safe to ignore null, because push may only return null if failIfExists is true
527+
return push(branch, version, knownVersions, force, failIfExists = false)!!
528+
}
529+
530+
override suspend fun push(branch: BranchReference, version: IVersion, baseVersion: IVersion?, force: Boolean, failIfExists: Boolean): IVersion? {
531+
return push(branch, version, listOfNotNull(baseVersion), force)
532+
}
533+
534+
override suspend fun push(branch: BranchReference, version: IVersion, knownVersions: List<IVersion>, force: Boolean, failIfExists: Boolean): IVersion? {
525535
LOG.debug { "${clientId.toString(16)}.push($branch, $version, ${knownVersions.joinToString(", ").take(200)})" }
526536
require(version is CLVersion)
527537
val delta = if (knownVersions.map { it.getObjectHash() }.contains(version.getObjectHash())) {
@@ -546,12 +556,19 @@ class ModelClientV2(
546556
if (force) {
547557
parameters["force"] = force.toString()
548558
}
559+
if (failIfExists) {
560+
parameters["failIfExists"] = failIfExists.toString()
561+
}
549562
}
550563
useVersionStreamFormat()
551564
contentType(ContentType.Application.Json)
552565
setBody(delta)
553566
}.execute { response ->
554-
createVersion(branch.repositoryId, version, response.readVersionDelta())
567+
if (response.status == HttpStatusCode.Conflict) {
568+
null
569+
} else {
570+
createVersion(branch.repositoryId, version, response.readVersionDelta())
571+
}
555572
}
556573
}
557574

model-client/src/jsMain/kotlin/org/modelix/model/client2/ClientJS.kt

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.modelix.model.mutable.withAutoTransactions
3131
import org.modelix.model.persistent.MapBasedStore
3232
import org.modelix.model.server.api.BranchInfo
3333
import org.modelix.mps.multiplatform.model.MPSIdGenerator
34+
import kotlin.coroutines.cancellation.CancellationException
3435
import kotlin.js.Date
3536
import kotlin.js.Promise
3637
import kotlin.time.Duration.Companion.seconds
@@ -150,8 +151,26 @@ interface ClientJS {
150151
repositoryId: String,
151152
): Promise<Array<BranchInfo>>
152153

153-
fun createBranch(repositoryId: String, branchId: String, versionHash: String, failIfExists: Boolean = false): Promise<Boolean>
154+
/**
155+
* Create a new branch in the given repository on the model server.
156+
* The new branch will point to the given version.
157+
* If the branch already exists, the promise will be rejected.
158+
* See also [deleteBranch] to delete an existing branch.
159+
* @param repositoryId Repository ID to create the branch in.
160+
* @param branchId ID of the new branch to create.
161+
* @param versionHash Hash of the version the new branch should point to.
162+
*/
163+
fun createBranch(repositoryId: String, branchId: String, versionHash: String): Promise<Unit>
154164

165+
/**
166+
* Delete an existing branch from the given repository on the model server.
167+
* If the branch does not exist, the promise will be resolved with `false`.
168+
* See also [createBranch] to create a new branch.
169+
*
170+
* @param repositoryId Repository ID to delete the branch from.
171+
* @param branchId ID of the branch to delete.
172+
* @return Promise that resolves to `true` if the branch existed and was deleted, else `false`.
173+
*/
155174
fun deleteBranch(
156175
repositoryId: String,
157176
branchId: String,
@@ -217,19 +236,14 @@ internal class ClientJSImpl(private val modelClient: ModelClientV2) : ClientJS {
217236
repositoryId: String,
218237
branchId: String,
219238
versionHash: String,
220-
failIfExists: Boolean,
221-
): Promise<Boolean> {
239+
): Promise<Unit> {
222240
return GlobalScope.promise {
223241
RepositoryId(repositoryId).let { repositoryId ->
224242
val branchReference = repositoryId.getBranchReference(branchId)
225243

226-
if (failIfExists) {
227-
throw TODO("implement server endpoint for this")
228-
} else {
229-
val version = modelClient.lazyLoadVersion(repositoryId, versionHash)
230-
modelClient.push(branchReference, version, version, true)
231-
return@promise true
232-
}
244+
val version = modelClient.lazyLoadVersion(repositoryId, versionHash)
245+
modelClient.push(branchReference, version, version, force = false, failIfExists = true)
246+
?: throw CancellationException("Branch $branchId already exists in repository $repositoryId")
233247
}
234248
}
235249
}

0 commit comments

Comments
 (0)