/
git-shell.go
84 lines (77 loc) · 1.69 KB
/
git-shell.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
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
84
package main
import (
"fmt"
"os/exec"
"regexp"
"strings"
)
func getTopLevelPath() (string, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
bytes, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(bytes)), nil
}
func getSHA() ([]string, error) {
cmd := exec.Command("git", "log", "--pretty=%H", "HEAD", "-20")
bytes, err := cmd.Output()
if err != nil {
return nil, err
}
shas := strings.Split(strings.TrimSpace(string(bytes)), "\n")
return shas, nil
}
func getBranch() (string, error) {
cmd := exec.Command("git", "branch")
bytes, err := cmd.Output()
if err != nil {
return "", err
}
branches := strings.Split(strings.TrimSpace(string(bytes)), "\n")
for _, b := range branches {
if strings.HasPrefix(b, "* ") {
return b[2:], nil
}
}
return "", fmt.Errorf("Not on a branch")
}
type Remote struct {
LocalName string
User string
Repo string
}
func getRemotes() ([]*Remote, error) {
var out []*Remote
cmd := exec.Command("git", "remote", "-v")
bytes, err := cmd.Output()
if err != nil {
return nil, err
}
lines := strings.Split(strings.TrimSpace(string(bytes)), "\n")
re, err := regexp.Compile("github.com[:/](.*?)/(.*?).git")
if err != nil {
fmt.Println(err)
return nil, err
}
for _, line := range lines {
record := strings.Split(line, "\t")
lname := record[0]
record = strings.Split(record[1], " ")
path := record[0]
fetchpush := record[1]
if fetchpush != "(fetch)" {
continue
}
submatch := re.FindStringSubmatch(path)
if submatch == nil {
continue
}
out = append(out, &Remote{
LocalName: lname,
User: submatch[1],
Repo: submatch[2],
})
}
return out, nil
}