From e8aabaf752c4f8ddffc8189ce84ddfa8258704cf Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 27 Nov 2022 12:57:18 +0530 Subject: [PATCH] feat: add a helper to `PasswordRepository` to retrieve the current branch --- .../passwordstore/data/repo/PasswordRepository.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/src/main/java/app/passwordstore/data/repo/PasswordRepository.kt b/app/src/main/java/app/passwordstore/data/repo/PasswordRepository.kt index b42b7892..2144cf1e 100644 --- a/app/src/main/java/app/passwordstore/data/repo/PasswordRepository.kt +++ b/app/src/main/java/app/passwordstore/data/repo/PasswordRepository.kt @@ -16,6 +16,7 @@ import com.github.michaelbull.result.onFailure import com.github.michaelbull.result.runCatching import java.io.File import org.eclipse.jgit.api.Git +import org.eclipse.jgit.lib.Constants import org.eclipse.jgit.lib.Repository import org.eclipse.jgit.storage.file.FileRepositoryBuilder import org.eclipse.jgit.transport.RefSpec @@ -125,6 +126,18 @@ object PasswordRepository { return repository } + /** Get the currently checked out branch. */ + fun getCurrentBranch(): String? { + val repository = repository ?: return null + val headRef = repository.getRef(Constants.HEAD) ?: return null + return if (headRef.isSymbolic) { + val branchName = headRef.target.name + Repository.shortenRefName(branchName) + } else { + null + } + } + /** * Gets the .gpg files in a directory *