-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (71 loc) · 1.47 KB
/
main.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
package main
import (
"errors"
"os"
"github.com/ktr0731/go-fuzzyfinder"
"github.com/urfave/cli/v2"
"github.com/wesleimp/checkout/internal/branch"
"github.com/wesleimp/checkout/internal/git"
)
var (
version = "v0.1.0"
)
func main() {
app := &cli.App{
Name: "checkout",
Usage: "Fuzzy-finding for checkouted branches in reflog",
UsageText: "checkout",
Version: version,
Authors: []*cli.Author{{
Name: "Weslei Juan Novaes Pereira",
Email: "wesleimsr@gmail.com",
}},
Action: func(c *cli.Context) error {
if !git.IsRepo() {
return errors.New("It's not a git repository")
}
current_branch, err := git.Run("rev-parse", "--abbrev-ref", "HEAD")
if err != nil {
return err
}
branches, err := branch.Checkouts(10000)
if err != nil {
return err
}
if !contains(branches, current_branch) {
branches = append(branches, current_branch)
}
idx, err := fuzzyfinder.Find(branches, func(i int) string {
return branches[i]
})
if err != nil {
if err.Error() != "abort" {
return err
}
return nil
}
branch := branches[idx]
out, err := git.Run("checkout", branch)
if err != nil {
return err
}
println(out)
println("switched to", branch)
return nil
},
}
err := app.Run(os.Args)
if err != nil {
println(err.Error())
os.Exit(1)
}
os.Exit(0)
}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}