-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitwrapper.go
More file actions
83 lines (67 loc) · 1.36 KB
/
gitwrapper.go
File metadata and controls
83 lines (67 loc) · 1.36 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package gitwrapper
import (
"bytes"
"io"
"log"
"os"
"os/exec"
"strings"
)
var (
logger = log.New(os.Stdout, "[gw] ", 0)
)
func green(s string) string {
return "\033[0;32m" + s + "\033[0m"
}
func runGitCmd(trace bool, gitCmd string) (string, error) {
if trace {
logger.Println(green(gitCmd))
}
cmd := exec.Command("bash", "-c", gitCmd)
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
// skip git error if std output exists
if err != nil && stdout.Len() == 0 {
return "", err
}
if trace {
buffCopy := stdout
_, _ = io.Copy(os.Stdout, &buffCopy)
}
return strings.TrimSpace(stdout.String()), nil
}
type Branch struct {
Name string
IsCurrent, IsRemote bool
}
func getAllBranches() ([]Branch, error) {
o, err := runGitCmd(false, "git branch -a")
if err != nil {
return nil, err
}
var bb []Branch
for _, row := range strings.Split(o, "\n") {
row = strings.TrimSpace(row)
if len(row) == 0 {
continue
}
b := Branch{Name: row}
if strings.HasPrefix(b.Name, "* ") {
b.IsCurrent = true
b.Name = b.Name[2:]
}
if strings.HasPrefix(b.Name, "remotes/") {
b.Name = b.Name[8:]
b.IsRemote = true
// ignore remote branches not from origin
if !strings.HasPrefix(b.Name, "origin/") {
continue
}
b.Name = b.Name[7:]
}
bb = append(bb, b)
}
return bb, nil
}