summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2019-11-17 10:04:06 +1100
committerJesse Duffield <jessedduffield@gmail.com>2019-11-21 21:15:27 +1100
commit089c55a0b34de269e60ee85f5cff407f5286e332 (patch)
tree49d13e31a03c97c9bc8674ce68eeee12531a739e
parent620e49b8e3a02df9020eb7fa753862020193cd09 (diff)
get branches with git for-each-ref
-rw-r--r--pkg/commands/branch.go1
-rw-r--r--pkg/commands/loading_remotes.go51
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{