Add ability to change default branch (#977)

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-07-29 16:25:49 +05:30 committed by GitHub
parent 729fc80255
commit 017abd0f61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 80 additions and 28 deletions

View file

@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
## [Unreleased] ## [Unreleased]
### Added
- Allow changing the branch used for Git operations
### Changed ### Changed
- Slightly reduce APK size - Slightly reduce APK size

View file

@ -41,6 +41,7 @@ abstract class BaseGitActivity : AppCompatActivity() {
lateinit var serverPath: String lateinit var serverPath: String
lateinit var username: String lateinit var username: String
lateinit var email: String lateinit var email: String
lateinit var branch: String
private var identityBuilder: SshApiSessionFactory.IdentityBuilder? = null private var identityBuilder: SshApiSessionFactory.IdentityBuilder? = null
private var identity: SshApiSessionFactory.ApiIdentity? = null private var identity: SshApiSessionFactory.ApiIdentity? = null
lateinit var settings: SharedPreferences lateinit var settings: SharedPreferences
@ -61,6 +62,7 @@ abstract class BaseGitActivity : AppCompatActivity() {
serverPath = settings.getString(PreferenceKeys.GIT_REMOTE_LOCATION, null) ?: "" serverPath = settings.getString(PreferenceKeys.GIT_REMOTE_LOCATION, null) ?: ""
username = settings.getString(PreferenceKeys.GIT_CONFIG_USER_NAME, null) ?: "" username = settings.getString(PreferenceKeys.GIT_CONFIG_USER_NAME, null) ?: ""
email = settings.getString(PreferenceKeys.GIT_CONFIG_USER_EMAIL, null) ?: "" email = settings.getString(PreferenceKeys.GIT_CONFIG_USER_EMAIL, null) ?: ""
branch = settings.getString(PreferenceKeys.GIT_BRANCH_NAME, null) ?: "master"
updateUrl() updateUrl()
} }
@ -248,10 +250,9 @@ abstract class BaseGitActivity : AppCompatActivity() {
const val REQUEST_PULL = 101 const val REQUEST_PULL = 101
const val REQUEST_PUSH = 102 const val REQUEST_PUSH = 102
const val REQUEST_CLONE = 103 const val REQUEST_CLONE = 103
const val REQUEST_INIT = 104 const val REQUEST_SYNC = 104
const val REQUEST_SYNC = 105 const val BREAK_OUT_OF_DETACHED = 105
const val BREAK_OUT_OF_DETACHED = 106 const val REQUEST_RESET = 106
const val REQUEST_RESET = 107
const val TAG = "AbstractGitActivity" const val TAG = "AbstractGitActivity"
} }
} }

View file

@ -5,8 +5,10 @@
package com.zeapo.pwdstore.git package com.zeapo.pwdstore.git
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.zeapo.pwdstore.R import com.zeapo.pwdstore.R
import com.zeapo.pwdstore.utils.PreferenceKeys
import java.io.File import java.io.File
import org.eclipse.jgit.api.Git import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.GitCommand import org.eclipse.jgit.api.GitCommand
@ -16,6 +18,9 @@ import org.eclipse.jgit.api.RebaseCommand
class BreakOutOfDetached(fileDir: File, callingActivity: AppCompatActivity) : GitOperation(fileDir, callingActivity) { class BreakOutOfDetached(fileDir: File, callingActivity: AppCompatActivity) : GitOperation(fileDir, callingActivity) {
private lateinit var commands: List<GitCommand<out Any>> private lateinit var commands: List<GitCommand<out Any>>
private val gitBranch = PreferenceManager
.getDefaultSharedPreferences(callingActivity.applicationContext)
.getString(PreferenceKeys.GIT_BRANCH_NAME, "master")
/** /**
* Sets the command * Sets the command
@ -24,7 +29,7 @@ class BreakOutOfDetached(fileDir: File, callingActivity: AppCompatActivity) : Gi
*/ */
fun setCommands(): BreakOutOfDetached { fun setCommands(): BreakOutOfDetached {
val git = Git(repository) val git = Git(repository)
val branchName = "conflicting-master-${System.currentTimeMillis()}" val branchName = "conflicting-$gitBranch-${System.currentTimeMillis()}"
this.commands = listOf( this.commands = listOf(
// abort the rebase // abort the rebase
@ -33,8 +38,8 @@ class BreakOutOfDetached(fileDir: File, callingActivity: AppCompatActivity) : Gi
git.checkout().setCreateBranch(true).setName(branchName), git.checkout().setCreateBranch(true).setName(branchName),
// push the changes // push the changes
git.push().setRemote("origin"), git.push().setRemote("origin"),
// switch back to master // switch back to ${gitBranch}
git.checkout().setName("master") git.checkout().setName(gitBranch)
) )
return this return this
} }
@ -76,7 +81,7 @@ class BreakOutOfDetached(fileDir: File, callingActivity: AppCompatActivity) : Gi
MaterialAlertDialogBuilder(callingActivity) MaterialAlertDialogBuilder(callingActivity)
.setTitle(callingActivity.resources.getString(R.string.git_abort_and_push_title)) .setTitle(callingActivity.resources.getString(R.string.git_abort_and_push_title))
.setMessage("There was a conflict when trying to rebase. " + .setMessage("There was a conflict when trying to rebase. " +
"Your local master branch was pushed to another branch named conflicting-master-....\n" + "Your local $gitBranch branch was pushed to another branch named conflicting-$gitBranch-....\n" +
"Use this branch to resolve conflict on your computer") "Use this branch to resolve conflict on your computer")
.setPositiveButton(callingActivity.resources.getString(R.string.dialog_ok)) { _, _ -> .setPositiveButton(callingActivity.resources.getString(R.string.dialog_ok)) { _, _ ->
callingActivity.finish() callingActivity.finish()

View file

@ -36,7 +36,7 @@ class GitConfigActivity : BaseGitActivity() {
if (repo != null) { if (repo != null) {
try { try {
val objectId = repo.resolve(Constants.HEAD) val objectId = repo.resolve(Constants.HEAD)
val ref = repo.getRef("refs/heads/master") val ref = repo.getRef("refs/heads/$branch")
val head = if (ref.objectId.equals(objectId)) ref.name else "DETACHED" val head = if (ref.objectId.equals(objectId)) ref.name else "DETACHED"
binding.gitCommitHash.text = String.format("%s (%s)", objectId.abbreviate(8).name(), head) binding.gitCommitHash.text = String.format("%s (%s)", objectId.abbreviate(8).name(), head)

View file

@ -102,6 +102,13 @@ class GitServerConfigActivity : BaseGitActivity() {
} }
} }
binding.serverBranch.apply {
setText(branch)
doOnTextChanged { text, _, _, _ ->
branch = text.toString().trim()
}
}
binding.saveButton.setOnClickListener { binding.saveButton.setOnClickListener {
if (isClone && PasswordRepository.getRepository(null) == null) if (isClone && PasswordRepository.getRepository(null) == null)
PasswordRepository.initialize(this) PasswordRepository.initialize(this)
@ -114,12 +121,14 @@ class GitServerConfigActivity : BaseGitActivity() {
putString(PreferenceKeys.GIT_REMOTE_PORT, serverPort) putString(PreferenceKeys.GIT_REMOTE_PORT, serverPort)
putString(PreferenceKeys.GIT_REMOTE_USERNAME, serverUser) putString(PreferenceKeys.GIT_REMOTE_USERNAME, serverUser)
putString(PreferenceKeys.GIT_REMOTE_LOCATION, serverPath) putString(PreferenceKeys.GIT_REMOTE_LOCATION, serverPath)
putString(PreferenceKeys.GIT_BRANCH_NAME, branch)
} }
if (!isClone) { if (!isClone) {
Snackbar.make(binding.root, getString(R.string.git_server_config_save_success), Snackbar.LENGTH_SHORT).show() Snackbar.make(binding.root, getString(R.string.git_server_config_save_success), Snackbar.LENGTH_SHORT).show()
Handler().postDelayed(500) { finish() } Handler().postDelayed(500) { finish() }
} else } else {
cloneRepository() cloneRepository()
}
} }
else -> Snackbar.make(binding.root, getString(R.string.git_server_config_save_error_prefix, getString(result.textRes)), Snackbar.LENGTH_LONG).show() else -> Snackbar.make(binding.root, getString(R.string.git_server_config_save_error_prefix, getString(result.textRes)), Snackbar.LENGTH_LONG).show()
} }

View file

@ -6,13 +6,15 @@ package com.zeapo.pwdstore.git
import android.content.Intent import android.content.Intent
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.zeapo.pwdstore.R import com.zeapo.pwdstore.R
import com.zeapo.pwdstore.utils.PreferenceKeys
import java.io.File 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.Git
import org.eclipse.jgit.api.GitCommand
import org.eclipse.jgit.api.ResetCommand import org.eclipse.jgit.api.ResetCommand
import org.eclipse.jgit.api.TransportCommand
/** /**
* Creates a new git operation * Creates a new git operation
@ -22,9 +24,7 @@ import org.eclipse.jgit.api.ResetCommand
*/ */
class ResetToRemoteOperation(fileDir: File, callingActivity: AppCompatActivity) : GitOperation(fileDir, callingActivity) { class ResetToRemoteOperation(fileDir: File, callingActivity: AppCompatActivity) : GitOperation(fileDir, callingActivity) {
private var addCommand: AddCommand? = null private lateinit var commands: List<GitCommand<out Any>>
private var fetchCommand: FetchCommand? = null
private var resetCommand: ResetCommand? = null
/** /**
* Sets the command * Sets the command
@ -32,17 +32,27 @@ class ResetToRemoteOperation(fileDir: File, callingActivity: AppCompatActivity)
* @return the current object * @return the current object
*/ */
fun setCommands(): ResetToRemoteOperation { fun setCommands(): ResetToRemoteOperation {
val remoteBranch = PreferenceManager
.getDefaultSharedPreferences(callingActivity.applicationContext)
.getString(PreferenceKeys.GIT_BRANCH_NAME, "master")
val git = Git(repository) val git = Git(repository)
this.addCommand = git.add().addFilepattern(".") val cmds = arrayListOf(
this.fetchCommand = git.fetch().setRemote("origin") git.add().addFilepattern("."),
this.resetCommand = git.reset().setRef("origin/master").setMode(ResetCommand.ResetType.HARD) 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 return this
} }
override fun execute() { override fun execute() {
this.fetchCommand?.setCredentialsProvider(this.provider) commands.filterIsInstance<TransportCommand<*, *>>().map { it.setCredentialsProvider(provider) }
GitAsyncTask(callingActivity, this, Intent()) GitAsyncTask(callingActivity, this, Intent()).execute(*commands.toTypedArray())
.execute(this.addCommand, this.fetchCommand, this.resetCommand)
} }
override fun onError(err: Exception) { override fun onError(err: Exception) {

View file

@ -35,6 +35,7 @@ object PreferenceKeys {
const val GIT_REMOTE_SERVER = "git_remote_server" const val GIT_REMOTE_SERVER = "git_remote_server"
const val GIT_REMOTE_USERNAME = "git_remote_username" const val GIT_REMOTE_USERNAME = "git_remote_username"
const val GIT_SERVER_INFO = "git_server_info" const val GIT_SERVER_INFO = "git_server_info"
const val GIT_BRANCH_NAME = "git_branch"
const val HTTPS_PASSWORD = "https_password" const val HTTPS_PASSWORD = "https_password"
const val LENGTH = "length" const val LENGTH = "length"
const val OREO_AUTOFILL_CUSTOM_PUBLIC_SUFFIXES = "oreo_autofill_custom_public_suffixes" const val OREO_AUTOFILL_CUSTOM_PUBLIC_SUFFIXES = "oreo_autofill_custom_public_suffixes"

View file

@ -20,7 +20,7 @@
<androidx.appcompat.widget.AppCompatTextView <androidx.appcompat.widget.AppCompatTextView
android:id="@+id/server_label" android:id="@+id/server_label"
style="@style/TextAppearance.MaterialComponents.Headline5" style="@style/TextAppearance.MaterialComponents.Headline5"
android:layout_width="match_parent" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="8dp" android:layout_margin="8dp"
android:text="@string/server_name" android:text="@string/server_name"
@ -32,7 +32,7 @@
<androidx.appcompat.widget.AppCompatTextView <androidx.appcompat.widget.AppCompatTextView
android:id="@+id/label_server_protocol" android:id="@+id/label_server_protocol"
style="@style/TextAppearance.MaterialComponents.Headline6" style="@style/TextAppearance.MaterialComponents.Headline6"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="8dp" android:layout_margin="8dp"
android:text="@string/server_protocol" android:text="@string/server_protocol"
@ -42,7 +42,7 @@
<com.google.android.material.button.MaterialButtonToggleGroup <com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/clone_protocol_group" android:id="@+id/clone_protocol_group"
style="@style/TextAppearance.MaterialComponents.Headline1" style="@style/TextAppearance.MaterialComponents.Headline1"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="8dp" android:layout_margin="8dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
@ -67,10 +67,12 @@
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:id="@+id/server_user_layout" android:id="@+id/server_user_layout"
android:layout_width="match_parent" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="8dp" android:layout_margin="8dp"
android:hint="@string/server_user" android:hint="@string/server_user"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/clone_protocol_group"> app:layout_constraintTop_toBottomOf="@id/clone_protocol_group">
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
@ -125,10 +127,11 @@
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:id="@+id/label_server_path" android:id="@+id/label_server_path"
android:layout_width="match_parent" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="8dp" android:layout_margin="8dp"
android:hint="@string/server_path" android:hint="@string/server_path"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/label_server_url"> app:layout_constraintTop_toBottomOf="@id/label_server_url">
@ -136,11 +139,31 @@
android:id="@+id/server_path" android:id="@+id/server_path"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:imeOptions="actionDone" android:imeOptions="actionNext"
android:nextFocusForward="@id/server_branch"
android:inputType="textWebEmailAddress" /> android:inputType="textWebEmailAddress" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/label_server_branch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="Branch"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/label_server_path">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/server_branch"
android:imeOptions="actionDone"
android:layout_width="match_parent"
android:inputType="textNoSuggestions"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<androidx.appcompat.widget.AppCompatTextView <androidx.appcompat.widget.AppCompatTextView
android:id="@+id/label_connection_mode" android:id="@+id/label_connection_mode"
style="@style/TextAppearance.MaterialComponents.Headline6" style="@style/TextAppearance.MaterialComponents.Headline6"
@ -151,7 +174,7 @@
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
android:text="@string/connection_mode" android:text="@string/connection_mode"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/label_server_path" /> app:layout_constraintTop_toBottomOf="@id/label_server_branch" />
<com.google.android.material.button.MaterialButtonToggleGroup <com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/connection_mode_group" android:id="@+id/connection_mode_group"