diff --git a/context/context.go b/context/context.go index e5d74634c..c71e6d905 100644 --- a/context/context.go +++ b/context/context.go @@ -90,12 +90,12 @@ func (c *fsContext) Branch() (string, error) { return c.branch, nil } - currentBranch, err := git.Head() + currentBranch, err := git.CurrentBranch() if err != nil { return "", err } - c.branch = strings.Replace(currentBranch, "refs/heads/", "", 1) + c.branch = currentBranch return c.branch, nil } diff --git a/git/git.go b/git/git.go index 4dff19ed2..184308387 100644 --- a/git/git.go +++ b/git/git.go @@ -2,70 +2,30 @@ package git import ( "bytes" + "errors" "fmt" - "io/ioutil" "net/url" "os/exec" - "path/filepath" "regexp" "strings" "github.com/github/gh-cli/utils" ) -func Dir() (string, error) { - dirCmd := exec.Command("git", "rev-parse", "-q", "--git-dir") - output, err := utils.PrepareCmd(dirCmd).Output() - if err != nil { - return "", fmt.Errorf("Not a git repository (or any of the parent directories): .git") - } - - gitDir := firstLine(output) - if !filepath.IsAbs(gitDir) { - gitDir, err = filepath.Abs(gitDir) - if err != nil { - return "", err - } - gitDir = filepath.Clean(gitDir) - } - - return gitDir, nil -} - func VerifyRef(ref string) bool { showRef := exec.Command("git", "show-ref", "--verify", "--quiet", ref) err := utils.PrepareCmd(showRef).Run() return err == nil } -func BranchAtRef(paths ...string) (name string, err error) { - dir, err := Dir() - if err != nil { - return +func CurrentBranch() (string, error) { + branchCmd := exec.Command("git", "branch", "--show-current") + output, err := utils.PrepareCmd(branchCmd).Output() + branchName := firstLine(output) + if err == nil && branchName == "" { + return "", errors.New("git: not on any branch") } - - segments := []string{dir} - segments = append(segments, paths...) - path := filepath.Join(segments...) - b, err := ioutil.ReadFile(path) - if err != nil { - return - } - - n := string(b) - refPrefix := "ref: " - if strings.HasPrefix(n, refPrefix) { - name = strings.TrimPrefix(n, refPrefix) - name = strings.TrimSpace(name) - } else { - err = fmt.Errorf("No branch info in %s: %s", path, n) - } - - return -} - -func Head() (string, error) { - return BranchAtRef("HEAD") + return branchName, err } func listRemotes() ([]string, error) {