Added error display when a push has failed

This commit is contained in:
Abdelilah El Aissaoui 2021-12-21 02:26:52 +01:00
parent 3ed5f0cc1e
commit 908696735b
4 changed files with 45 additions and 9 deletions

View File

@ -207,10 +207,13 @@ class GitManager @Inject constructor(
fun push() = managerScope.launch {
safeProcessing {
try {
remoteOperationsManager.push(safeGit)
} finally {
coLoadLog()
}
}
}
private suspend fun refreshRepositoryInfo() {
statusManager.loadRepositoryStatus(safeGit)

View File

@ -9,10 +9,7 @@ import kotlinx.coroutines.isActive
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.ProgressMonitor
import org.eclipse.jgit.transport.CredentialsProvider
import org.eclipse.jgit.transport.HttpTransport
import org.eclipse.jgit.transport.RefSpec
import org.eclipse.jgit.transport.SshTransport
import org.eclipse.jgit.transport.*
import java.io.File
import javax.inject.Inject
@ -40,7 +37,7 @@ class RemoteOperationsManager @Inject constructor(
suspend fun push(git: Git) = withContext(Dispatchers.IO) {
val currentBranchRefSpec = git.repository.fullBranch
git
val pushResult = git
.push()
.setRefSpecs(RefSpec(currentBranchRefSpec))
.setPushTags()
@ -52,6 +49,41 @@ class RemoteOperationsManager @Inject constructor(
}
}
.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())
}
}
private val RemoteRefUpdate.Status.isRejected: Boolean
get() {
return this == RemoteRefUpdate.Status.REJECTED_NONFASTFORWARD ||
this == RemoteRefUpdate.Status.REJECTED_NODELETE ||
this == RemoteRefUpdate.Status.REJECTED_REMOTE_CHANGED ||
this == RemoteRefUpdate.Status.REJECTED_OTHER_REASON
}
private val RemoteRefUpdate.statusMessage: String
get() {
return when (this.status) {
RemoteRefUpdate.Status.REJECTED_NONFASTFORWARD -> "Failed to push some refs to ${this.remoteName}. " +
"Updates were rejected because the remote contains work that you do not have locally. Pulling changes from remote may help."
RemoteRefUpdate.Status.REJECTED_NODELETE -> "Could not delete ref because the remote doesn't support deleting refs."
RemoteRefUpdate.Status.REJECTED_REMOTE_CHANGED -> "Ref rejected, old object id in remote has changed."
RemoteRefUpdate.Status.REJECTED_OTHER_REASON -> this.message ?: "Push rejected for unknown reasons."
else -> ""
}
}
suspend fun clone(directory: File, url: String) = withContext(Dispatchers.IO) {

View File

@ -65,7 +65,7 @@ class StatusManager @Inject constructor(
val entries = it.value
if(entries.count() > 1)
entries.filter { it.oldPath != "/dev/null" }
entries.filter { entry -> entry.oldPath != "/dev/null" }
else
entries
}.flatten()
@ -80,7 +80,7 @@ class StatusManager @Inject constructor(
val entries = it.value
if(entries.count() > 1)
entries.filter { it.newPath != "/dev/null" }
entries.filter { entry -> entry.newPath != "/dev/null" }
else
entries
}.flatten()

View File

@ -148,6 +148,7 @@ fun AppTab(
text = safeLastError.message,
modifier = Modifier
.padding(top = 8.dp, bottom = 16.dp)
.widthIn(max = 600.dp)
)
}
}