-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlocal.go
173 lines (139 loc) · 3.48 KB
/
local.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package local
import (
"fmt"
"log"
"regexp"
"sort"
"strconv"
"strings"
"unicode"
"github.com/ldez/go-git-cmd-wrapper/v2/branch"
"github.com/ldez/go-git-cmd-wrapper/v2/git"
"github.com/ldez/go-git-cmd-wrapper/v2/remote"
"github.com/ldez/go-git-cmd-wrapper/v2/revparse"
)
// Remote Git remote model.
type Remote struct {
Name string
URL string
}
// Remotes a list of Remote.
type Remotes []Remote
// Find a remote by name.
func (r Remotes) Find(remoteName string) (*Remote, error) {
for _, rmt := range r {
if rmt.Name == remoteName {
return &rmt, nil
}
}
return nil, fmt.Errorf("unable to find remote %q in %v", remoteName, r)
}
// ByRemoteName sorts remote by name.
type ByRemoteName Remotes
func (r ByRemoteName) Len() int { return len(r) }
func (r ByRemoteName) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r ByRemoteName) Less(i, j int) bool { return r[i].Name < r[j].Name }
// GetCurrentPRNumber gets the current PR number.
func GetCurrentPRNumber(manualNumber int) (int, error) {
if manualNumber == 0 {
return GetCurrentBranchPRNumber()
}
return manualNumber, nil
}
// GetCurrentBranchPRNumber gets the current branch PR number.
func GetCurrentBranchPRNumber() (int, error) {
output, err := GetCurrentBranchName()
if err != nil {
log.Print(output)
return 0, err
}
return parsePRNumber(output)
}
// GetCurrentBranchName gets the current branch name.
func GetCurrentBranchName() (string, error) {
output, err := git.RevParse(revparse.AbbrevRef(""), revparse.Args("HEAD"))
if err != nil {
log.Print(output)
return "", err
}
return strings.TrimSpace(output), nil
}
func parsePRNumber(out string) (int, error) {
exp := regexp.MustCompile(`(\d+)--.+`)
parts := exp.FindStringSubmatch(out)
if len(parts) == 2 {
number, err := strconv.ParseInt(parts[1], 10, 32)
if err != nil {
return 0, err
}
return int(number), nil
}
return 0, fmt.Errorf("unable to parse: %s", out)
}
// GetGitRepoRoot gets the root of the git repository.
func GetGitRepoRoot() (string, error) {
output, err := git.RevParse(revparse.ShowToplevel)
if err != nil {
return "", err
}
return strings.TrimSpace(output), nil
}
// GetRemotes gets git remotes.
func GetRemotes() (Remotes, error) {
output, err := git.Remote(remote.Verbose, git.Debug)
if err != nil {
log.Print(output)
return nil, err
}
return parseRemotes(output), nil
}
// GetBranches gets git branches.
func GetBranches() ([]string, error) {
output, err := git.Branch(branch.Format("%(refname:short)"), branch.Sort("refname"), git.Debug)
if err != nil {
log.Print(output)
return nil, err
}
return parseBranches(output), nil
}
func parseBranches(output string) []string {
var branches []string
for _, name := range strings.Split(output, "\n") {
b := strings.TrimSpace(name)
if b == "" {
continue
}
branches = append(branches, b)
}
sort.Slice(branches, func(i, _ int) bool {
if branches[i] == "main" {
return true
}
if branches[i] == "master" {
return true
}
return false
})
return branches
}
func parseRemotes(output string) Remotes {
lines := strings.Split(output, "\n")
remoteMap := make(map[string]Remote)
for _, line := range lines {
if line != "" {
elt := strings.FieldsFunc(line, unicode.IsSpace)
name := elt[0]
rmt := Remote{
Name: name,
URL: elt[1],
}
remoteMap[name] = rmt
}
}
var remotes Remotes
for _, entry := range remoteMap {
remotes = append(remotes, entry)
}
sort.Sort(ByRemoteName(remotes))
return remotes
}