diff options
| author | Harsh Shandilya <msfjarvis@gmail.com> | 2020-07-29 16:25:49 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-29 16:25:49 +0530 |
| commit | 017abd0f61663889e911fdb6328cbd793de960ac (patch) | |
| tree | 54b7066eed90bee1db232dbf5fa09044a81b0c57 /app/src/main/java/com/zeapo/pwdstore/git/ResetToRemoteOperation.kt | |
| parent | 729fc80255a2b9b8a2ff669530d189a4f4ab06f8 (diff) | |
| download | HardenedPass-017abd0f61663889e911fdb6328cbd793de960ac.tar.gz HardenedPass-017abd0f61663889e911fdb6328cbd793de960ac.zip | |
Add ability to change default branch (#977)
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/git/ResetToRemoteOperation.kt')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/git/ResetToRemoteOperation.kt | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/ResetToRemoteOperation.kt b/app/src/main/java/com/zeapo/pwdstore/git/ResetToRemoteOperation.kt index c570bf27..60a9fbf3 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/ResetToRemoteOperation.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/ResetToRemoteOperation.kt @@ -6,13 +6,15 @@ package com.zeapo.pwdstore.git import android.content.Intent import androidx.appcompat.app.AppCompatActivity +import androidx.preference.PreferenceManager import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.zeapo.pwdstore.R +import com.zeapo.pwdstore.utils.PreferenceKeys import java.io.File -import org.eclipse.jgit.api.AddCommand -import org.eclipse.jgit.api.FetchCommand import org.eclipse.jgit.api.Git +import org.eclipse.jgit.api.GitCommand import org.eclipse.jgit.api.ResetCommand +import org.eclipse.jgit.api.TransportCommand /** * Creates a new git operation @@ -22,9 +24,7 @@ import org.eclipse.jgit.api.ResetCommand */ class ResetToRemoteOperation(fileDir: File, callingActivity: AppCompatActivity) : GitOperation(fileDir, callingActivity) { - private var addCommand: AddCommand? = null - private var fetchCommand: FetchCommand? = null - private var resetCommand: ResetCommand? = null + private lateinit var commands: List<GitCommand<out Any>> /** * Sets the command @@ -32,17 +32,27 @@ class ResetToRemoteOperation(fileDir: File, callingActivity: AppCompatActivity) * @return the current object */ fun setCommands(): ResetToRemoteOperation { + val remoteBranch = PreferenceManager + .getDefaultSharedPreferences(callingActivity.applicationContext) + .getString(PreferenceKeys.GIT_BRANCH_NAME, "master") val git = Git(repository) - this.addCommand = git.add().addFilepattern(".") - this.fetchCommand = git.fetch().setRemote("origin") - this.resetCommand = git.reset().setRef("origin/master").setMode(ResetCommand.ResetType.HARD) + val cmds = arrayListOf( + git.add().addFilepattern("."), + git.fetch().setRemote("origin"), + git.reset().setRef("origin/$remoteBranch").setMode(ResetCommand.ResetType.HARD) + ) + if (git.branchList().call().none { it.name == remoteBranch }) { + cmds.add( + git.branchCreate().setName(remoteBranch).setForce(true) + ) + } + commands = cmds return this } override fun execute() { - this.fetchCommand?.setCredentialsProvider(this.provider) - GitAsyncTask(callingActivity, this, Intent()) - .execute(this.addCommand, this.fetchCommand, this.resetCommand) + commands.filterIsInstance<TransportCommand<*, *>>().map { it.setCredentialsProvider(provider) } + GitAsyncTask(callingActivity, this, Intent()).execute(*commands.toTypedArray()) } override fun onError(err: Exception) { |
