feat: add a helper to PasswordRepository to retrieve the current branch

This commit is contained in:
Harsh Shandilya 2022-11-27 12:57:18 +05:30
parent 2a3f78d43e
commit e8aabaf752
No known key found for this signature in database

View file

@ -16,6 +16,7 @@ import com.github.michaelbull.result.onFailure
import com.github.michaelbull.result.runCatching import com.github.michaelbull.result.runCatching
import java.io.File import java.io.File
import org.eclipse.jgit.api.Git import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.lib.Repository import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.storage.file.FileRepositoryBuilder import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import org.eclipse.jgit.transport.RefSpec import org.eclipse.jgit.transport.RefSpec
@ -125,6 +126,18 @@ object PasswordRepository {
return repository 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 * Gets the .gpg files in a directory
* *