Refactor GitCommandExecutor (#1255)
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
0396bf92a9
commit
c34d08b094
3 changed files with 51 additions and 30 deletions
|
@ -5,17 +5,12 @@
|
|||
|
||||
package dev.msfjarvis.aps.util.git
|
||||
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import com.github.michaelbull.result.Result
|
||||
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 dev.msfjarvis.aps.util.settings.GitSettings
|
||||
import com.github.michaelbull.result.Result
|
||||
import com.github.michaelbull.result.runCatching
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.eclipse.jgit.api.CommitCommand
|
||||
|
@ -27,15 +22,10 @@ import org.eclipse.jgit.lib.PersonIdent
|
|||
import org.eclipse.jgit.transport.RemoteRefUpdate
|
||||
|
||||
class GitCommandExecutor(
|
||||
private val activity: FragmentActivity,
|
||||
private val operation: GitOperation,
|
||||
) {
|
||||
|
||||
suspend fun execute(): Result<Unit, Throwable> {
|
||||
val snackbar = activity.snackbar(
|
||||
message = activity.resources.getString(R.string.git_operation_running),
|
||||
length = Snackbar.LENGTH_INDEFINITE,
|
||||
)
|
||||
suspend fun execute(): Result<GitResult, Throwable> {
|
||||
// Count the number of uncommitted files
|
||||
var nbChanges = 0
|
||||
return runCatching {
|
||||
|
@ -89,13 +79,7 @@ class GitCommandExecutor(
|
|||
}
|
||||
}
|
||||
RemoteRefUpdate.Status.UP_TO_DATE -> {
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(
|
||||
activity.applicationContext,
|
||||
activity.applicationContext.getString(R.string.git_push_up_to_date),
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
return@runCatching GitResult.AlreadyUpToDate
|
||||
}
|
||||
else -> {
|
||||
}
|
||||
|
@ -110,8 +94,7 @@ class GitCommandExecutor(
|
|||
}
|
||||
}
|
||||
}
|
||||
}.also {
|
||||
snackbar.dismiss()
|
||||
GitResult.OK
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
22
app/src/main/java/dev/msfjarvis/aps/util/git/GitResult.kt
Normal file
22
app/src/main/java/dev/msfjarvis/aps/util/git/GitResult.kt
Normal file
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* 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()
|
||||
}
|
|
@ -25,6 +25,10 @@ import dev.msfjarvis.aps.util.git.sshj.SshKey
|
|||
import dev.msfjarvis.aps.util.git.sshj.SshjSessionFactory
|
||||
import dev.msfjarvis.aps.util.auth.BiometricAuthenticator
|
||||
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.suspendCoroutine
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
@ -119,12 +123,14 @@ abstract class GitOperation(protected val callingActivity: FragmentActivity) {
|
|||
if (!preExecute()) {
|
||||
return Ok(Unit)
|
||||
}
|
||||
val operationResult = GitCommandExecutor(
|
||||
callingActivity,
|
||||
this,
|
||||
).execute()
|
||||
postExecute()
|
||||
return operationResult
|
||||
val snackbar = callingActivity.snackbar(
|
||||
message = callingActivity.resources.getString(R.string.git_operation_running),
|
||||
length = Snackbar.LENGTH_INDEFINITE,
|
||||
)
|
||||
val operationResult = GitCommandExecutor(this).execute()
|
||||
snackbar.dismiss()
|
||||
postExecute(operationResult)
|
||||
return Ok(Unit)
|
||||
}
|
||||
|
||||
private fun onMissingSshKeyFile() {
|
||||
|
@ -200,7 +206,17 @@ abstract class GitOperation(protected val callingActivity: FragmentActivity) {
|
|||
*/
|
||||
open fun preExecute() = true
|
||||
|
||||
private suspend fun postExecute() {
|
||||
private suspend fun postExecute(result: Result<GitResult, Throwable>) {
|
||||
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) {
|
||||
sshSessionFactory?.close()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue