Revert "Refactor GitCommandExecutor (#1255)" (#1267)

This breaks propagation of errors to the UI layer

This reverts commit c34d08b094.
This commit is contained in:
Harsh Shandilya 2021-01-01 23:03:13 +05:30 committed by GitHub
parent 360f96306b
commit fa2279791f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 52 deletions

View file

@ -5,12 +5,17 @@
package dev.msfjarvis.aps.util.git package dev.msfjarvis.aps.util.git
import dev.msfjarvis.aps.util.git.GitException.PullException import android.widget.Toast
import dev.msfjarvis.aps.util.git.GitException.PushException import androidx.fragment.app.FragmentActivity
import dev.msfjarvis.aps.util.git.operation.GitOperation
import dev.msfjarvis.aps.util.settings.GitSettings
import com.github.michaelbull.result.Result import com.github.michaelbull.result.Result
import com.github.michaelbull.result.runCatching import com.github.michaelbull.result.runCatching
import com.google.android.material.snackbar.Snackbar
import dev.msfjarvis.aps.R
import dev.msfjarvis.aps.util.git.GitException.PullException
import dev.msfjarvis.aps.util.git.GitException.PushException
import dev.msfjarvis.aps.util.settings.GitSettings
import dev.msfjarvis.aps.util.git.operation.GitOperation
import dev.msfjarvis.aps.util.extensions.snackbar
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.CommitCommand import org.eclipse.jgit.api.CommitCommand
@ -22,10 +27,15 @@ import org.eclipse.jgit.lib.PersonIdent
import org.eclipse.jgit.transport.RemoteRefUpdate import org.eclipse.jgit.transport.RemoteRefUpdate
class GitCommandExecutor( class GitCommandExecutor(
private val activity: FragmentActivity,
private val operation: GitOperation, private val operation: GitOperation,
) { ) {
suspend fun execute(): Result<GitResult, Throwable> { suspend fun execute(): Result<Unit, Throwable> {
val snackbar = activity.snackbar(
message = activity.resources.getString(R.string.git_operation_running),
length = Snackbar.LENGTH_INDEFINITE,
)
// Count the number of uncommitted files // Count the number of uncommitted files
var nbChanges = 0 var nbChanges = 0
return runCatching { return runCatching {
@ -79,7 +89,13 @@ class GitCommandExecutor(
} }
} }
RemoteRefUpdate.Status.UP_TO_DATE -> { RemoteRefUpdate.Status.UP_TO_DATE -> {
return@runCatching GitResult.AlreadyUpToDate withContext(Dispatchers.Main) {
Toast.makeText(
activity.applicationContext,
activity.applicationContext.getString(R.string.git_push_up_to_date),
Toast.LENGTH_SHORT
).show()
}
} }
else -> { else -> {
} }
@ -94,7 +110,8 @@ class GitCommandExecutor(
} }
} }
} }
GitResult.OK }.also {
snackbar.dismiss()
} }
} }
} }

View file

@ -1,22 +0,0 @@
/*
* Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
package dev.msfjarvis.aps.util.git
/**
* Result of a Git operation executed by [GitCommandExecutor]
*/
sealed class GitResult {
/**
* All good!
*/
object OK : GitResult()
/**
* Push operation succeeded and HEAD is already in sync with remote.
*/
object AlreadyUpToDate : GitResult()
}

View file

@ -25,10 +25,6 @@ import dev.msfjarvis.aps.util.git.sshj.SshKey
import dev.msfjarvis.aps.util.git.sshj.SshjSessionFactory import dev.msfjarvis.aps.util.git.sshj.SshjSessionFactory
import dev.msfjarvis.aps.util.auth.BiometricAuthenticator import dev.msfjarvis.aps.util.auth.BiometricAuthenticator
import dev.msfjarvis.aps.data.repo.PasswordRepository import dev.msfjarvis.aps.data.repo.PasswordRepository
import dev.msfjarvis.aps.util.extensions.snackbar
import dev.msfjarvis.aps.util.git.GitResult
import com.github.michaelbull.result.get
import com.google.android.material.snackbar.Snackbar
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -123,14 +119,12 @@ abstract class GitOperation(protected val callingActivity: FragmentActivity) {
if (!preExecute()) { if (!preExecute()) {
return Ok(Unit) return Ok(Unit)
} }
val snackbar = callingActivity.snackbar( val operationResult = GitCommandExecutor(
message = callingActivity.resources.getString(R.string.git_operation_running), callingActivity,
length = Snackbar.LENGTH_INDEFINITE, this,
) ).execute()
val operationResult = GitCommandExecutor(this).execute() postExecute()
snackbar.dismiss() return operationResult
postExecute(operationResult)
return Ok(Unit)
} }
private fun onMissingSshKeyFile() { private fun onMissingSshKeyFile() {
@ -206,17 +200,7 @@ abstract class GitOperation(protected val callingActivity: FragmentActivity) {
*/ */
open fun preExecute() = true open fun preExecute() = true
private suspend fun postExecute(result: Result<GitResult, Throwable>) { private suspend fun postExecute() {
when (result.get()) {
GitResult.OK -> {}
GitResult.AlreadyUpToDate -> {
Toast.makeText(
callingActivity,
callingActivity.getString(R.string.git_push_up_to_date),
Toast.LENGTH_SHORT
).show()
}
}
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
sshSessionFactory?.close() sshSessionFactory?.close()
} }