From 017abd0f61663889e911fdb6328cbd793de960ac Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 29 Jul 2020 16:25:49 +0530 Subject: Add ability to change default branch (#977) Signed-off-by: Harsh Shandilya --- .../zeapo/pwdstore/git/ResetToRemoteOperation.kt | 32 ++++++++++++++-------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/git/ResetToRemoteOperation.kt') 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> /** * 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>().map { it.setCredentialsProvider(provider) } + GitAsyncTask(callingActivity, this, Intent()).execute(*commands.toTypedArray()) } override fun onError(err: Exception) { -- cgit v1.2.3