-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
71 lines (54 loc) · 1.36 KB
/
git.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
package main
import (
"fmt"
"log"
"os/exec"
"regexp"
"strings"
)
const mergedOption = "--merged"
const noMergedOption = "--no-merged"
func gitBranch(merged bool) (branches []*Branch) {
option := noMergedOption
if merged {
option = mergedOption
}
commit := fmt.Sprintf("%s/%s", remote, branch)
cmd := exec.Command("git", "branch", option, commit)
output, err := cmd.Output()
if err != nil {
log.Fatalf("Error running git branch %s: %v", option, err)
}
trimmedString := strings.ReplaceAll(string(output), " ", "")
branchNames := strings.Split(trimmedString, "\n")
for _, name := range branchNames {
if name == "" {
continue
}
current := isCurrentBranch(name)
strippedName := strings.ReplaceAll(name, "*", "")
branches = append(branches, newBranch(strippedName, merged, current))
}
return
}
func gitBranchDelete(branches []string) string {
args := []string{"branch", "-D"}
args = append(args, branches...)
cmd := exec.Command("git", args...)
output, err := cmd.Output()
if err != nil {
log.Fatalf("Error while deleting branches: %v", err)
}
return string(output)
}
func gitFetch() {
cmd := exec.Command("git", "fetch", remote)
err := cmd.Run()
if err != nil {
log.Fatalf("Error while fetching %s: %v", remote, err)
}
}
func isCurrentBranch(name string) bool {
re := regexp.MustCompile(`^\*\w+`)
return re.Match([]byte(name))
}