@@ -31,6 +31,7 @@ import org.modelix.model.mutable.withAutoTransactions
31
31
import org.modelix.model.persistent.MapBasedStore
32
32
import org.modelix.model.server.api.BranchInfo
33
33
import org.modelix.mps.multiplatform.model.MPSIdGenerator
34
+ import kotlin.coroutines.cancellation.CancellationException
34
35
import kotlin.js.Date
35
36
import kotlin.js.Promise
36
37
import kotlin.time.Duration.Companion.seconds
@@ -150,8 +151,26 @@ interface ClientJS {
150
151
repositoryId : String ,
151
152
): Promise <Array <BranchInfo >>
152
153
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 >
154
164
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
+ */
155
174
fun deleteBranch (
156
175
repositoryId : String ,
157
176
branchId : String ,
@@ -217,19 +236,14 @@ internal class ClientJSImpl(private val modelClient: ModelClientV2) : ClientJS {
217
236
repositoryId : String ,
218
237
branchId : String ,
219
238
versionHash : String ,
220
- failIfExists : Boolean ,
221
- ): Promise <Boolean > {
239
+ ): Promise <Unit > {
222
240
return GlobalScope .promise {
223
241
RepositoryId (repositoryId).let { repositoryId ->
224
242
val branchReference = repositoryId.getBranchReference(branchId)
225
243
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 " )
233
247
}
234
248
}
235
249
}
0 commit comments