aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2021-01-12 21:42:53 +0530
committerGitHub <noreply@github.com>2021-01-12 21:42:53 +0530
commit1e9e5686afc2a6515f871c5f2ad4dc2ddc4731c4 (patch)
tree3cce3972dd01f6c5cc4884e5c7b99c5e2db7eae5 /app/src/main/java
parent8bd156dea6e87eb667f76f4738fde992323ce0cf (diff)
Allow configuring pull behaviour (#1276)
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/ui/git/base/BaseGitActivity.kt4
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/ui/git/config/GitConfigActivity.kt9
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/ui/settings/RepositorySettings.kt6
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/util/git/ErrorMessages.kt3
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/util/git/GitCommandExecutor.kt12
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/util/git/operation/BreakOutOfDetached.kt28
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/util/git/operation/PullOperation.kt7
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/util/git/operation/SyncOperation.kt7
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/util/settings/GitSettings.kt8
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/util/settings/PreferenceKeys.kt2
10 files changed, 67 insertions, 19 deletions
diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/git/base/BaseGitActivity.kt b/app/src/main/java/dev/msfjarvis/aps/ui/git/base/BaseGitActivity.kt
index 8b288d00..e5737227 100644
--- a/app/src/main/java/dev/msfjarvis/aps/ui/git/base/BaseGitActivity.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/ui/git/base/BaseGitActivity.kt
@@ -66,9 +66,9 @@ abstract class BaseGitActivity : ContinuationContainerActivity() {
}
val op = when (operation) {
GitOp.CLONE -> CloneOperation(this, GitSettings.url!!)
- GitOp.PULL -> PullOperation(this)
+ GitOp.PULL -> PullOperation(this, GitSettings.rebaseOnPull)
GitOp.PUSH -> PushOperation(this)
- GitOp.SYNC -> SyncOperation(this)
+ GitOp.SYNC -> SyncOperation(this, GitSettings.rebaseOnPull)
GitOp.BREAK_OUT_OF_DETACHED -> BreakOutOfDetached(this)
GitOp.RESET -> ResetToRemoteOperation(this)
}
diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/git/config/GitConfigActivity.kt b/app/src/main/java/dev/msfjarvis/aps/ui/git/config/GitConfigActivity.kt
index d354ce4e..2a31aeee 100644
--- a/app/src/main/java/dev/msfjarvis/aps/ui/git/config/GitConfigActivity.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/ui/git/config/GitConfigActivity.kt
@@ -29,6 +29,7 @@ import dev.msfjarvis.aps.util.extensions.viewBinding
import kotlinx.coroutines.launch
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.lib.Repository
+import org.eclipse.jgit.lib.RepositoryState
class GitConfigActivity : BaseGitActivity() {
@@ -79,10 +80,10 @@ class GitConfigActivity : BaseGitActivity() {
val repo = PasswordRepository.getRepository(null)
if (repo != null) {
binding.gitHeadStatus.text = headStatusMsg(repo)
- // enable the abort button only if we're rebasing
- val isRebasing = repo.repositoryState.isRebasing
- binding.gitAbortRebase.isEnabled = isRebasing
- binding.gitAbortRebase.alpha = if (isRebasing) 1.0f else 0.5f
+ // enable the abort button only if we're rebasing or merging
+ val needsAbort = repo.repositoryState.isRebasing || repo.repositoryState == RepositoryState.MERGING
+ binding.gitAbortRebase.isEnabled = needsAbort
+ binding.gitAbortRebase.alpha = if (needsAbort) 1.0f else 0.5f
}
binding.gitLog.setOnClickListener {
runCatching {
diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/settings/RepositorySettings.kt b/app/src/main/java/dev/msfjarvis/aps/ui/settings/RepositorySettings.kt
index 32d04c9b..22ac899a 100644
--- a/app/src/main/java/dev/msfjarvis/aps/ui/settings/RepositorySettings.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/ui/settings/RepositorySettings.kt
@@ -56,6 +56,12 @@ class RepositorySettings(private val activity: FragmentActivity) : SettingsProvi
override fun provideSettings(builder: PreferenceScreen.Builder) {
builder.apply {
+ checkBox(PreferenceKeys.REBASE_ON_PULL) {
+ titleRes = R.string.pref_rebase_on_pull_title
+ summaryRes = R.string.pref_rebase_on_pull_summary
+ summaryOnRes = R.string.pref_rebase_on_pull_summary_on
+ defaultValue = true
+ }
pref(PreferenceKeys.GIT_SERVER_INFO) {
titleRes = R.string.pref_edit_git_server_settings
visible = PasswordRepository.isGitRepo()
diff --git a/app/src/main/java/dev/msfjarvis/aps/util/git/ErrorMessages.kt b/app/src/main/java/dev/msfjarvis/aps/util/git/ErrorMessages.kt
index 44eb11e1..e03ebe0e 100644
--- a/app/src/main/java/dev/msfjarvis/aps/util/git/ErrorMessages.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/util/git/ErrorMessages.kt
@@ -28,7 +28,8 @@ sealed class GitException(@StringRes res: Int, vararg fmt: String) : Exception(b
*/
sealed class PullException(@StringRes res: Int, vararg fmt: String) : GitException(res, *fmt) {
- object PullRebaseFailed : PullException(R.string.git_pull_fail_error)
+ object PullRebaseFailed : PullException(R.string.git_pull_rebase_fail_error)
+ object PullMergeFailed : PullException(R.string.git_pull_merge_fail_error)
}
/**
diff --git a/app/src/main/java/dev/msfjarvis/aps/util/git/GitCommandExecutor.kt b/app/src/main/java/dev/msfjarvis/aps/util/git/GitCommandExecutor.kt
index 429ea2c5..eca74762 100644
--- a/app/src/main/java/dev/msfjarvis/aps/util/git/GitCommandExecutor.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/util/git/GitCommandExecutor.kt
@@ -21,7 +21,6 @@ import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.CommitCommand
import org.eclipse.jgit.api.PullCommand
import org.eclipse.jgit.api.PushCommand
-import org.eclipse.jgit.api.RebaseResult
import org.eclipse.jgit.api.StatusCommand
import org.eclipse.jgit.lib.PersonIdent
import org.eclipse.jgit.transport.RemoteRefUpdate
@@ -62,9 +61,14 @@ class GitCommandExecutor(
val result = withContext(Dispatchers.IO) {
command.call()
}
- val rr = result.rebaseResult
- if (rr.status == RebaseResult.Status.STOPPED) {
- throw PullException.PullRebaseFailed
+ if (result.rebaseResult != null) {
+ if (!result.rebaseResult.status.isSuccessful) {
+ throw PullException.PullRebaseFailed
+ }
+ } else if (result.mergeResult != null) {
+ if (!result.mergeResult.mergeStatus.isSuccessful) {
+ throw PullException.PullMergeFailed
+ }
}
}
is PushCommand -> {
diff --git a/app/src/main/java/dev/msfjarvis/aps/util/git/operation/BreakOutOfDetached.kt b/app/src/main/java/dev/msfjarvis/aps/util/git/operation/BreakOutOfDetached.kt
index 1aff34de..635813e7 100644
--- a/app/src/main/java/dev/msfjarvis/aps/util/git/operation/BreakOutOfDetached.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/util/git/operation/BreakOutOfDetached.kt
@@ -8,12 +8,13 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dev.msfjarvis.aps.R
import dev.msfjarvis.aps.util.git.sshj.ContinuationContainerActivity
import org.eclipse.jgit.api.RebaseCommand
+import org.eclipse.jgit.api.ResetCommand
+import org.eclipse.jgit.lib.RepositoryState
class BreakOutOfDetached(callingActivity: ContinuationContainerActivity) : GitOperation(callingActivity) {
- override val commands = arrayOf(
- // abort the rebase
- git.rebase().setOperation(RebaseCommand.Operation.ABORT),
+ private val merging = repository.repositoryState == RepositoryState.MERGING
+ private val resetCommands = arrayOf(
// git checkout -b conflict-branch
git.checkout().setCreateBranch(true).setName("conflicting-$remoteBranch-${System.currentTimeMillis()}"),
// push the changes
@@ -22,7 +23,26 @@ class BreakOutOfDetached(callingActivity: ContinuationContainerActivity) : GitOp
git.checkout().setName(remoteBranch),
)
- override fun preExecute() = if (!git.repository.repositoryState.isRebasing) {
+ override val commands by lazy(LazyThreadSafetyMode.NONE) {
+ if (merging) {
+ // We need to run some non-command operations first
+ repository.writeMergeCommitMsg(null)
+ repository.writeMergeHeads(null)
+ arrayOf(
+ // reset hard back to our local HEAD
+ git.reset().setMode(ResetCommand.ResetType.HARD),
+ *resetCommands,
+ )
+ } else {
+ arrayOf(
+ // abort the rebase
+ git.rebase().setOperation(RebaseCommand.Operation.ABORT),
+ *resetCommands,
+ )
+ }
+ }
+
+ override fun preExecute() = if (!git.repository.repositoryState.isRebasing && !merging) {
MaterialAlertDialogBuilder(callingActivity)
.setTitle(callingActivity.resources.getString(R.string.git_abort_and_push_title))
.setMessage(callingActivity.resources.getString(R.string.git_break_out_of_detached_unneeded))
diff --git a/app/src/main/java/dev/msfjarvis/aps/util/git/operation/PullOperation.kt b/app/src/main/java/dev/msfjarvis/aps/util/git/operation/PullOperation.kt
index 7bee775a..938bc146 100644
--- a/app/src/main/java/dev/msfjarvis/aps/util/git/operation/PullOperation.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/util/git/operation/PullOperation.kt
@@ -7,7 +7,10 @@ package dev.msfjarvis.aps.util.git.operation
import dev.msfjarvis.aps.util.git.sshj.ContinuationContainerActivity
import org.eclipse.jgit.api.GitCommand
-class PullOperation(callingActivity: ContinuationContainerActivity) : GitOperation(callingActivity) {
+class PullOperation(
+ callingActivity: ContinuationContainerActivity,
+ rebase: Boolean,
+) : GitOperation(callingActivity) {
/**
* The story of why the pull operation is committing files goes like this: Once upon a time when
@@ -26,6 +29,6 @@ class PullOperation(callingActivity: ContinuationContainerActivity) : GitOperati
// Commit everything! If needed, obviously.
git.commit().setAll(true).setMessage("[Android Password Store] Sync"),
// Pull and rebase on top of the remote branch
- git.pull().setRebase(true).setRemote("origin"),
+ git.pull().setRebase(rebase).setRemote("origin"),
)
}
diff --git a/app/src/main/java/dev/msfjarvis/aps/util/git/operation/SyncOperation.kt b/app/src/main/java/dev/msfjarvis/aps/util/git/operation/SyncOperation.kt
index 512d6b48..e6a61f60 100644
--- a/app/src/main/java/dev/msfjarvis/aps/util/git/operation/SyncOperation.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/util/git/operation/SyncOperation.kt
@@ -6,7 +6,10 @@ package dev.msfjarvis.aps.util.git.operation
import dev.msfjarvis.aps.util.git.sshj.ContinuationContainerActivity
-class SyncOperation(callingActivity: ContinuationContainerActivity) : GitOperation(callingActivity) {
+class SyncOperation(
+ callingActivity: ContinuationContainerActivity,
+ rebase: Boolean,
+) : GitOperation(callingActivity) {
override val commands = arrayOf(
// Stage all files
@@ -16,7 +19,7 @@ class SyncOperation(callingActivity: ContinuationContainerActivity) : GitOperati
// Commit everything! If needed, obviously.
git.commit().setAll(true).setMessage("[Android Password Store] Sync"),
// Pull and rebase on top of the remote branch
- git.pull().setRebase(true).setRemote("origin"),
+ git.pull().setRebase(rebase).setRemote("origin"),
// Push it all back
git.push().setPushAll().setRemote("origin"),
)
diff --git a/app/src/main/java/dev/msfjarvis/aps/util/settings/GitSettings.kt b/app/src/main/java/dev/msfjarvis/aps/util/settings/GitSettings.kt
index 82af814b..0b5f0270 100644
--- a/app/src/main/java/dev/msfjarvis/aps/util/settings/GitSettings.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/util/settings/GitSettings.kt
@@ -147,6 +147,14 @@ object GitSettings {
}
}
+ var rebaseOnPull
+ get() = settings.getBoolean(PreferenceKeys.REBASE_ON_PULL, true)
+ set(value) {
+ settings.edit {
+ putBoolean(PreferenceKeys.REBASE_ON_PULL, value)
+ }
+ }
+
sealed class UpdateConnectionSettingsResult {
class MissingUsername(val newProtocol: Protocol) : UpdateConnectionSettingsResult()
class AuthModeMismatch(val newProtocol: Protocol, val validModes: List<AuthMode>) : UpdateConnectionSettingsResult()
diff --git a/app/src/main/java/dev/msfjarvis/aps/util/settings/PreferenceKeys.kt b/app/src/main/java/dev/msfjarvis/aps/util/settings/PreferenceKeys.kt
index 198be889..930a5427 100644
--- a/app/src/main/java/dev/msfjarvis/aps/util/settings/PreferenceKeys.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/util/settings/PreferenceKeys.kt
@@ -82,4 +82,6 @@ object PreferenceKeys {
const val PROXY_PORT = "proxy_port"
const val PROXY_USERNAME = "proxy_username"
const val PROXY_PASSWORD = "proxy_password"
+
+ const val REBASE_ON_PULL = "rebase_on_pull"
}