diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2022-11-27 13:00:45 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2022-11-27 13:28:45 +0530 |
commit | 8bb61eca2dca583d8fc032cfc085bc899d3fa929 (patch) | |
tree | 1d39cfb6470b1f040790bee87e76efd0c0f0d902 /app/src | |
parent | e8aabaf752c4f8ddffc8189ce84ddfa8258704cf (diff) |
refactor: make `ResetToRemoteOperation` actually work and use its own `remoteBranch` input
Diffstat (limited to 'app/src')
4 files changed, 33 insertions, 15 deletions
diff --git a/app/src/main/java/app/passwordstore/ui/git/base/BaseGitActivity.kt b/app/src/main/java/app/passwordstore/ui/git/base/BaseGitActivity.kt index b79a1425..70532702 100644 --- a/app/src/main/java/app/passwordstore/ui/git/base/BaseGitActivity.kt +++ b/app/src/main/java/app/passwordstore/ui/git/base/BaseGitActivity.kt @@ -58,6 +58,12 @@ abstract class BaseGitActivity : AppCompatActivity() { @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) { diff --git a/app/src/main/java/app/passwordstore/ui/git/config/GitConfigActivity.kt b/app/src/main/java/app/passwordstore/ui/git/config/GitConfigActivity.kt index 5b9a04be..2603dfdf 100644 --- a/app/src/main/java/app/passwordstore/ui/git/config/GitConfigActivity.kt +++ b/app/src/main/java/app/passwordstore/ui/git/config/GitConfigActivity.kt @@ -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 { diff --git a/app/src/main/java/app/passwordstore/util/git/operation/ResetToRemoteOperation.kt b/app/src/main/java/app/passwordstore/util/git/operation/ResetToRemoteOperation.kt index ddbdc807..08cc195a 100644 --- a/app/src/main/java/app/passwordstore/util/git/operation/ResetToRemoteOperation.kt +++ b/app/src/main/java/app/passwordstore/util/git/operation/ResetToRemoteOperation.kt @@ -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), ) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ad60e177..2711f2c3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -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> |