From 9105940e006825696cd2a18d03f5a5896e0e3a85 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Sat, 5 Feb 2022 01:35:44 +0100 Subject: [PATCH] Push deletion is done before doing it locally to prevent deleting the local branch in case of error --- .../kotlin/app/git/RemoteOperationsManager.kt | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/app/git/RemoteOperationsManager.kt b/src/main/kotlin/app/git/RemoteOperationsManager.kt index 5f81b03..91065eb 100644 --- a/src/main/kotlin/app/git/RemoteOperationsManager.kt +++ b/src/main/kotlin/app/git/RemoteOperationsManager.kt @@ -85,11 +85,6 @@ class RemoteOperationsManager @Inject constructor( } suspend fun deleteBranch(git: Git, ref: Ref) = withContext(Dispatchers.IO) { - git - .branchDelete() - .setBranchNames(ref.name) - .call() - val branchSplit = ref.name.split("/").toMutableList() val remoteName = branchSplit[2] // Remote name repeat(3) { @@ -101,13 +96,33 @@ class RemoteOperationsManager @Inject constructor( val refSpec = RefSpec() .setSource(null) .setDestination(branchName) - git.push() + + val pushResult = git.push() .setTransportConfigCallback { handleTransportCredentials(it) } .setRefSpecs(refSpec) .setRemote(remoteName) .call() + + val results = + pushResult.map { it.remoteUpdates.filter { remoteRefUpdate -> remoteRefUpdate.status.isRejected } } + .flatten() + if (results.isNotEmpty()) { + val error = StringBuilder() + + results.forEach { result -> + error.append(result.statusMessage) + error.append("\n") + } + + throw Exception(error.toString()) + } + + git + .branchDelete() + .setBranchNames(ref.name) + .call() } private val RemoteRefUpdate.Status.isRejected: Boolean