-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrbs.go
89 lines (72 loc) · 2.2 KB
/
rbs.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
85
86
87
88
89
package rbs
import (
"fmt"
"os/exec"
"strings"
"github.com/manifoldco/promptui"
)
type branch struct {
ObjectName string
Name string
RelativeTime string
Track string
Subject string
}
func Run() {
// command - git branch --sort=-committerdate
// --format="%(committerdate:unix)|%(committerdate:relative)|%(refname:short)|%(authorname)|%(contents:subject)|%(objectname:short)"
resFormat := `%(objectname:short)|%(refname:short)|%(committerdate:relative)|%(upstream:track)|%(contents:subject)`
cmd := exec.Command("git", "branch",
"--sort=-committerdate",
"--format="+resFormat)
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return
}
out := strings.Split(string(stdout), "\n")
branches := []branch{}
var branchNameMaxLen int
for _, v := range out {
v := strings.TrimSpace(v)
if v == "" {
continue
}
p := strings.Split(v, "|")
branches = append(branches, branch{p[0], p[1], p[2], p[3], p[4]})
if len(p[1]) > branchNameMaxLen {
branchNameMaxLen = len(p[1])
}
}
templates := &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "\U0000279C [{{ .ObjectName | yellow }}] {{ .Name | green }} ({{ .RelativeTime | cyan }}) -{{if ne .Track \"\" }} {{ .Track | magenta }}{{end}} {{ .Subject }}",
Inactive: " [{{ .ObjectName | yellow | faint }}] {{ .Name | green | faint }} ({{ .RelativeTime | cyan | faint }}) -{{if ne .Track \"\" }} {{ .Track | magenta | faint }}{{end}} {{ .Subject | faint }}",
Selected: "git checkout {{ .Name | green | cyan }}",
}
searcher := func(input string, index int) bool {
branch := branches[index]
name := strings.Replace(strings.ToLower(branch.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
prompt := promptui.Select{
Label: "Select git branch:",
Items: branches,
Templates: templates,
Size: 10,
Searcher: searcher,
}
i, _, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
cmd = exec.Command("git", "checkout", branches[i].Name)
stdout, err = cmd.Output()
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fmt.Print(string(stdout))
}
}