-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbranch.go
41 lines (37 loc) · 820 Bytes
/
branch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"bytes"
"os/exec"
)
func getCurrentBranch() (string, error) {
out, err := getBranchesShell()
if err != nil {
return "", err
}
curBranchBytes, err := out.ReadBytes('\n')
if err != nil {
return "", err
}
return string(bytes.Trim(curBranchBytes, "* \n")), nil
}
func getAllBranches() ([]string, error) {
out, err := getBranchesShell()
if err != nil {
return nil, err
}
branchBytes := bytes.Split(out.Bytes(), []byte{'\n'})
var branches []string
for _, b := range branchBytes {
if s := string(bytes.Trim(b, "* ")); s != "" {
branches = append(branches, s)
}
}
return branches, nil
}
func getBranchesShell() (*bytes.Buffer, error) {
cmd := exec.Command("git", "branch", "--no-color")
out := bytes.NewBuffer(nil)
cmd.Stdout = out
err := cmd.Run()
return out, err
}