summaryrefslogtreecommitdiffstats
path: root/pkg/commands/loading_remotes.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2019-11-16 16:00:47 +1100
committerJesse Duffield <jessedduffield@gmail.com>2019-11-21 22:07:14 +1100
commit325408d0e3db8e1b7d0b6915986f1a684ee8b257 (patch)
treedbc0ca0823c745950d113678b0379662e914827b /pkg/commands/loading_remotes.go
parenteeb667954fd2df5faf1df227a823dd5720bb3171 (diff)
get remote branches when getting remotes
Diffstat (limited to 'pkg/commands/loading_remotes.go')
-rw-r--r--pkg/commands/loading_remotes.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/pkg/commands/loading_remotes.go b/pkg/commands/loading_remotes.go
new file mode 100644
index 000000000..a90319f47
--- /dev/null
+++ b/pkg/commands/loading_remotes.go
@@ -0,0 +1,70 @@
+package commands
+
+import (
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+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.Error(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
+ })
+ 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
+ }
+
+ // first step is to get our remotes from go-git
+ remotes := make([]*Remote, len(goGitRemotes))
+ for i, goGitRemote := range goGitRemotes {
+ name := goGitRemote.Config().Name
+ c.Log.Warn(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
+ }
+
+ remotes[i] = &Remote{
+ Name: goGitRemote.Config().Name,
+ Urls: goGitRemote.Config().URLs,
+ Branches: branches,
+ }
+ }
+
+ return remotes, nil
+}