aboutsummaryrefslogtreecommitdiff
path: root/lib/git/git.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2018-09-14 04:08:19 +0700
committerShulhan <ms@kilabit.info>2018-09-14 04:09:00 +0700
commit4c6857d82b30d18afc24cbbdecdfc44a08ed3673 (patch)
treedf71a72c5f0aaa8e737dd2cecfb8aa50fe60a1f8 /lib/git/git.go
parent1702fcf4576b4fddbbc40ad120511971e764db4b (diff)
downloadpakakeh.go-4c6857d82b30d18afc24cbbdecdfc44a08ed3673.tar.xz
lib/git: add function to get list of remote branches
Diffstat (limited to 'lib/git/git.go')
-rw-r--r--lib/git/git.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/git/git.go b/lib/git/git.go
index 6bb113f9..632f6bb4 100644
--- a/lib/git/git.go
+++ b/lib/git/git.go
@@ -373,3 +373,49 @@ func RemoteChange(repoDir, oldName, newName, newURL string) error {
return err
}
+
+//
+// RemoteBranches return list of remote branches.
+//
+func RemoteBranches(repoDir string) ([]string, error) {
+ if len(repoDir) == 0 {
+ return nil, nil
+ }
+
+ cmd := exec.Command("git", "--no-pager", "branch", "-r", "--format", "%(refname:lstrip=3)")
+ cmd.Dir = repoDir
+ cmd.Stderr = _stderr
+
+ if debug.Value >= 1 {
+ fmt.Printf("= RemoteBranches %s %s\n", cmd.Dir, cmd.Args)
+ }
+
+ bout, err := cmd.Output()
+ if err != nil {
+ err = fmt.Errorf("RemoteBranches: %s", err)
+ return nil, err
+ }
+
+ bbranches := bytes.Split(bout, []byte{'\n'})
+ if len(bbranches) == 0 {
+ return nil, err
+ }
+
+ var branches []string
+ bHEAD := []byte("HEAD")
+ for x := 0; x < len(bbranches); x++ {
+ if len(bbranches[x]) == 0 {
+ continue
+ }
+ if bytes.Equal(bbranches[x], bHEAD) {
+ continue
+ }
+ branches = append(branches, string(bbranches[x]))
+ }
+
+ if debug.Value >= 1 {
+ fmt.Printf("= RemoteBranches: %s\n", branches)
+ }
+
+ return branches, nil
+}