l0nax/changelog-go

View on GitHub
pkg/gut/branch.go

Summary

Maintainability
A
40 mins
Test Coverage
package gut

import (
    "gopkg.in/src-d/go-git.v4/plumbing"

    git "gopkg.in/src-d/go-git.v4"
)

// GetCurrentBranchFromRepository does return the current checked out branch from the repository.
//
// Deprecated: The gut package is marked as deprecated since we will drop the git dependency in the future.
func GetCurrentBranchFromRepository(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().Short()
            return nil
        }

        return nil
    })
    if err != nil {
        return "", err
    }

    return currentBranchName, nil
}