forked from toolshed/abra
fix: get branch is now more robust
This commit is contained in:
35
pkg/git/branch.go
Normal file
35
pkg/git/branch.go
Normal file
@ -0,0 +1,35 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
)
|
||||
|
||||
// GetCurrentBranch retrieves the current branch of a repository
|
||||
func GetCurrentBranch(repository *git.Repository) (string, error) {
|
||||
branchRefs, err := repository.Branches()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
headRef, err := repository.Head()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var currentBranchName string
|
||||
err = branchRefs.ForEach(func(branchRef *plumbing.Reference) error {
|
||||
if branchRef.Hash() == headRef.Hash() {
|
||||
currentBranchName = branchRef.Name().String()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return currentBranchName, nil
|
||||
}
|
Reference in New Issue
Block a user