Simplify reading current branch from git
This commit is contained in:
parent
2c94616969
commit
f0801b2deb
2 changed files with 10 additions and 50 deletions
|
|
@ -90,12 +90,12 @@ func (c *fsContext) Branch() (string, error) {
|
||||||
return c.branch, nil
|
return c.branch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
currentBranch, err := git.Head()
|
currentBranch, err := git.CurrentBranch()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.branch = strings.Replace(currentBranch, "refs/heads/", "", 1)
|
c.branch = currentBranch
|
||||||
return c.branch, nil
|
return c.branch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
56
git/git.go
56
git/git.go
|
|
@ -2,70 +2,30 @@ package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/github/gh-cli/utils"
|
"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 {
|
func VerifyRef(ref string) bool {
|
||||||
showRef := exec.Command("git", "show-ref", "--verify", "--quiet", ref)
|
showRef := exec.Command("git", "show-ref", "--verify", "--quiet", ref)
|
||||||
err := utils.PrepareCmd(showRef).Run()
|
err := utils.PrepareCmd(showRef).Run()
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func BranchAtRef(paths ...string) (name string, err error) {
|
func CurrentBranch() (string, error) {
|
||||||
dir, err := Dir()
|
branchCmd := exec.Command("git", "branch", "--show-current")
|
||||||
if err != nil {
|
output, err := utils.PrepareCmd(branchCmd).Output()
|
||||||
return
|
branchName := firstLine(output)
|
||||||
|
if err == nil && branchName == "" {
|
||||||
|
return "", errors.New("git: not on any branch")
|
||||||
}
|
}
|
||||||
|
return branchName, err
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func listRemotes() ([]string, error) {
|
func listRemotes() ([]string, error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue