diff options
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt | 68 |
1 files changed, 51 insertions, 17 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt index 9205e7fc..89376bfd 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitConfigActivity.kt @@ -4,20 +4,24 @@ */ package com.zeapo.pwdstore.git +import android.content.Intent import android.os.Bundle import android.os.Handler import android.util.Patterns import androidx.core.os.postDelayed import androidx.lifecycle.lifecycleScope +import com.github.ajalt.timberkt.e import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.snackbar.Snackbar import com.zeapo.pwdstore.R import com.zeapo.pwdstore.databinding.ActivityGitConfigBinding import com.zeapo.pwdstore.git.config.GitSettings +import com.zeapo.pwdstore.git.log.GitLogActivity import com.zeapo.pwdstore.utils.PasswordRepository import com.zeapo.pwdstore.utils.viewBinding import kotlinx.coroutines.launch import org.eclipse.jgit.lib.Constants +import org.eclipse.jgit.lib.Repository class GitConfigActivity : BaseGitActivity() { @@ -33,23 +37,7 @@ class GitConfigActivity : BaseGitActivity() { else binding.gitUserName.setText(GitSettings.authorName) binding.gitUserEmail.setText(GitSettings.authorEmail) - val repo = PasswordRepository.getRepository(PasswordRepository.getRepositoryDirectory()) - if (repo != null) { - try { - val objectId = repo.resolve(Constants.HEAD) - val ref = repo.getRef("refs/heads/${GitSettings.branch}") - val head = if (ref.objectId.equals(objectId)) ref.name else "DETACHED" - binding.gitCommitHash.text = String.format("%s (%s)", objectId.abbreviate(8).name(), head) - - // 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 - } catch (ignored: Exception) { - } - } - binding.gitAbortRebase.setOnClickListener { lifecycleScope.launch { launchGitOperation(BREAK_OUT_OF_DETACHED) } } - binding.gitResetToRemote.setOnClickListener { lifecycleScope.launch { launchGitOperation(REQUEST_RESET) } } + setupTools() binding.saveButton.setOnClickListener { val email = binding.gitUserEmail.text.toString().trim() val name = binding.gitUserName.text.toString().trim() @@ -66,4 +54,50 @@ class GitConfigActivity : BaseGitActivity() { } } } + + /** + * Sets up the UI components of the tools section. + */ + private fun setupTools() { + 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 + } + binding.gitLog.setOnClickListener { + try { + intent = Intent(this, GitLogActivity::class.java) + startActivity(intent) + } catch (ex: Exception) { + e(ex) { "Failed to start GitLogActivity" } + } + } + binding.gitAbortRebase.setOnClickListener { lifecycleScope.launch { launchGitOperation(BREAK_OUT_OF_DETACHED) } } + binding.gitResetToRemote.setOnClickListener { lifecycleScope.launch { launchGitOperation(REQUEST_RESET) } } + } + + /** + * Returns a user-friendly message about the current state of HEAD. + * + * The state is recognized to be either pointing to a branch or detached. + */ + private fun headStatusMsg(repo: Repository): String { + return try { + val headRef = repo.getRef(Constants.HEAD) + if (headRef.isSymbolic) { + val branchName = headRef.target.name + val shortBranchName = Repository.shortenRefName(branchName) + getString(R.string.git_head_on_branch, shortBranchName) + } else { + val commitHash = headRef.objectId.abbreviate(8).name() + getString(R.string.git_head_detached, commitHash) + } + } catch (ex: Exception) { + e(ex) { "Error getting HEAD reference" } + getString(R.string.git_head_missing) + } + } } |
