refactor: make ResetToRemoteOperation actually work and use its own remoteBranch input

This commit is contained in:
Harsh Shandilya
2022-11-27 13:00:45 +05:30
parent e8aabaf752
commit 8bb61eca2d
4 changed files with 33 additions and 15 deletions

View File

@@ -57,6 +57,12 @@ abstract class BaseGitActivity : AppCompatActivity() {
@Inject lateinit var gitSettings: GitSettings
@GitPreferences @Inject lateinit var gitPrefs: SharedPreferences
/**
* Poor workaround to pass in a specified remote branch for [ResetToRemoteOperation]. Callers of
* [launchGitOperation] should set this before calling the method with [GitOp.RESET].
*/
protected var remoteBranch = ""
/**
* Attempt to launch the requested Git operation.
* @param operation The type of git operation to launch
@@ -77,7 +83,7 @@ abstract class BaseGitActivity : AppCompatActivity() {
GitOp.PUSH -> PushOperation(this)
GitOp.SYNC -> SyncOperation(this, gitSettings.rebaseOnPull)
GitOp.BREAK_OUT_OF_DETACHED -> BreakOutOfDetached(this)
GitOp.RESET -> ResetToRemoteOperation(this)
GitOp.RESET -> ResetToRemoteOperation(this, remoteBranch)
GitOp.GC -> GcOperation(this)
}
return (if (op.requiresAuth) {

View File

@@ -11,10 +11,12 @@ import android.os.Looper
import android.util.Patterns
import android.view.MenuItem
import androidx.core.os.postDelayed
import androidx.fragment.app.setFragmentResultListener
import androidx.lifecycle.lifecycleScope
import app.passwordstore.R
import app.passwordstore.data.repo.PasswordRepository
import app.passwordstore.databinding.ActivityGitConfigBinding
import app.passwordstore.ui.dialogs.TextInputDialog
import app.passwordstore.ui.git.base.BaseGitActivity
import app.passwordstore.ui.git.log.GitLogActivity
import app.passwordstore.util.extensions.viewBinding
@@ -115,12 +117,21 @@ class GitConfigActivity : BaseGitActivity() {
}
}
binding.gitResetToRemote.setOnClickListener {
lifecycleScope.launch {
launchGitOperation(GitOp.RESET)
.fold(
success = ::finishOnSuccessHandler,
failure = { err -> promptOnErrorHandler(err) { finish() } },
)
val dialog =
TextInputDialog.newInstance(getString(R.string.git_utils_reset_remote_branch_title))
dialog.show(supportFragmentManager, "BRANCH_INPUT_DIALOG")
dialog.setFragmentResultListener(TextInputDialog.REQUEST_KEY) { _, bundle ->
val result = bundle.getString(TextInputDialog.BUNDLE_KEY_TEXT)
if (!result.isNullOrEmpty()) {
remoteBranch = result
lifecycleScope.launch {
launchGitOperation(GitOp.RESET)
.fold(
success = ::finishOnSuccessHandler,
failure = { err -> promptOnErrorHandler(err) { finish() } },
)
}
}
}
}
binding.gitGc.setOnClickListener {

View File

@@ -5,22 +5,22 @@
package app.passwordstore.util.git.operation
import androidx.appcompat.app.AppCompatActivity
import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode.TRACK
import org.eclipse.jgit.api.ResetCommand
class ResetToRemoteOperation(callingActivity: AppCompatActivity) : GitOperation(callingActivity) {
class ResetToRemoteOperation(callingActivity: AppCompatActivity, remoteBranch: String) :
GitOperation(callingActivity) {
override val commands =
arrayOf(
// Stage all files
git.add().addFilepattern("."),
// Fetch everything from the origin remote
git.fetch().setRemote("origin"),
git.fetch().setRemote("origin").setRemoveDeletedRefs(true),
// Force-create $remoteBranch if it doesn't exist. This covers the case where a branch name is
// changed.
git.branchCreate().setName(remoteBranch).setForce(true),
git.checkout().setName(remoteBranch).setForce(true).setUpstreamMode(TRACK),
// Do a hard reset to the remote branch. Equivalent to git reset --hard
// origin/$remoteBranch
git.reset().setRef("origin/$remoteBranch").setMode(ResetCommand.ResetType.HARD),
// Force-create $remoteBranch if it doesn't exist. This covers the case where you
// switched
// branches from 'master' to anything else.
git.branchCreate().setName(remoteBranch).setForce(true),
)
}

View File

@@ -368,4 +368,5 @@
<string name="git_run_gc_job">Run garbage collection job</string>
<string name="activity_label_pgp_key_manager">PGP Key Manager</string>
<string name="pgp_key_manager_delete_confirmation_dialog_title">Delete key?</string>
<string name="git_utils_reset_remote_branch_title">Remote branch name</string>
</resources>