diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2019-11-17 10:04:06 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2019-11-21 22:07:14 +1100 |
commit | b7f2d0366b2c9b8ac034e426661db4ea72a7bb14 (patch) | |
tree | 49d13e31a03c97c9bc8674ce68eeee12531a739e | |
parent | 6bd0979b4a31c64ef813c581e5571e3d730bffda (diff) |
get branches with git for-each-ref
-rw-r--r-- | pkg/commands/branch.go | 1 | ||||
-rw-r--r-- | pkg/commands/loading_remotes.go | 51 |
2 files changed, 12 insertions, 40 deletions
diff --git a/pkg/commands/branch.go b/pkg/commands/branch.go index b6821c8a7..d1cafdfb4 100644 --- a/pkg/commands/branch.go +++ b/pkg/commands/branch.go @@ -17,7 +17,6 @@ type Branch struct { Recency string Pushables string Pullables string - Hash string Selected bool } diff --git a/pkg/commands/loading_remotes.go b/pkg/commands/loading_remotes.go index d9903a5da..8b6b47d57 100644 --- a/pkg/commands/loading_remotes.go +++ b/pkg/commands/loading_remotes.go @@ -1,46 +1,17 @@ package commands import ( - "io/ioutil" - "os" - "path/filepath" - "strings" + "fmt" + "regexp" ) -func (c *GitCommand) GetBranchesFromDir(dirPath string) ([]*Branch, error) { - branches := []*Branch{} - err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { - if err != nil { - // it's possible that go-git is referencing a remote we don't have locally - // in which case we'll just swallow this error - c.Log.Warn(err) - return nil - } - - if info.IsDir() { - return nil - } - - // it's a file: we need to get the path and work out the branch name from that - fileContents, err := ioutil.ReadFile(path) - if err != nil { - return err - } - - branches = append(branches, &Branch{ - Name: strings.TrimPrefix(path, dirPath)[1:], // stripping prefix slash - Hash: strings.TrimSpace(string(fileContents)), - }) - - return nil - }) +func (c *GitCommand) GetRemotes() ([]*Remote, error) { + // get remote branches + remoteBranchesStr, err := c.OSCommand.RunCommandWithOutput("git for-each-ref --format='%(refname:strip=2)' refs/remotes") if err != nil { return nil, err } - return branches, nil -} -func (c *GitCommand) GetRemotes() ([]*Remote, error) { goGitRemotes, err := c.Repo.Remotes() if err != nil { return nil, err @@ -51,11 +22,13 @@ func (c *GitCommand) GetRemotes() ([]*Remote, error) { for i, goGitRemote := range goGitRemotes { name := goGitRemote.Config().Name - // can't seem to easily get the branches of the remotes from go-git so we'll - // traverse the directory recursively - branches, err := c.GetBranchesFromDir(filepath.Join(".git", "refs", "remotes", name)) - if err != nil { - return nil, err + re := regexp.MustCompile(fmt.Sprintf("%s\\/(.*)", name)) + matches := re.FindAllStringSubmatch(remoteBranchesStr, -1) + branches := make([]*Branch, len(matches)) + for j, match := range matches { + branches[j] = &Branch{ + Name: match[1], + } } remotes[i] = &Remote{ |