-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
239 lines (206 loc) · 4.89 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"os/exec"
"sort"
"strings"
)
var sortFlag string
var showAll bool
func init() {
flag.Usage = func() {
name := os.Args[0]
if name == "git-branch-status" {
name = "git branch-status"
}
fmt.Fprintf(os.Stderr, "usage: %s [<options>] [<branch>]\n\noptions:\n", name)
flag.PrintDefaults()
}
flag.StringVar(&sortFlag, "sort", "name", `How to sort branches (name | left | right)
"name": Sort by name of branch (left side branch)
"left": Sort by number of commits in left branch and not right
"right": Sort by number of commits in right branch and not left`)
flag.BoolVar(&showAll, "a", false, "Show all branches including branches with no commit differences")
}
func main() {
flag.Parse()
if flag.NArg() > 1 {
flag.Usage()
log.Fatalf("Too many arguments")
}
upstream := "%(upstream:short)"
if flag.NArg() == 1 {
upstream = flag.Arg(0)
}
sortT := SortName
if sortFlag != "" {
switch sortFlag {
case "left":
sortT = SortLeft
case "right":
sortT = SortRight
case "name":
default:
flag.Usage()
}
}
cmd := exec.Command("git", "for-each-ref", fmt.Sprintf("--format=%%(refname:short) %s", upstream), "refs/heads")
cmd.Stderr = os.Stderr
foreachOut, err := cmd.StdoutPipe()
if err != nil {
log.Fatalf("Error getting pipe: %s", err)
}
if err := cmd.Start(); err != nil {
log.Fatalf("Error starting process: %s", err)
}
// Prepare output records
var statuses []BranchStatus
var maxLeftLen int
scanner := bufio.NewScanner(foreachOut)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(scanner.Text())
if len(fields) > 2 {
log.Fatalf("Unexpected output line: %q", line)
}
if len(fields) == 1 {
// No branch to compare, skip
continue
}
status, err := GetBranchStatus(fields[0], fields[1])
if err != nil {
log.Fatalf("Error getting branch status: %s", err)
}
if len(status.Left) > maxLeftLen {
maxLeftLen = len(status.Left)
}
if !showAll && status.LeftCount == 0 && status.RightCount == 0 {
continue
}
statuses = append(statuses, status)
}
if err := scanner.Err(); err != nil {
log.Fatalf("Error starting process: %s", err)
}
if err := cmd.Wait(); err != nil {
log.Fatalf("Error running process: %s", err)
}
l := NewBranchStatusList(statuses, sortT)
sort.Sort(l)
fmtStr := fmt.Sprintf("%%-%ds %%5d|%%-5d %%s\n", maxLeftLen)
for _, status := range l.Statuses() {
fmt.Printf(fmtStr, status.Left, status.LeftCount, status.RightCount, status.Right)
}
}
type BranchStatus struct {
Left string
Right string
LeftCount int
RightCount int
}
func GetBranchStatus(left, right string) (BranchStatus, error) {
args := []string{
"rev-list",
"--left-right",
left + "..." + right,
"--",
}
cmd := exec.Command("git", args...)
cmd.Stderr = os.Stderr
p, err := cmd.StdoutPipe()
if err != nil {
return BranchStatus{}, err
}
if err := cmd.Start(); err != nil {
return BranchStatus{}, err
}
status := BranchStatus{
Left: left,
Right: right,
}
scanner := bufio.NewScanner(p)
for scanner.Scan() {
b := scanner.Bytes()
switch b[0] {
case '<':
status.LeftCount++
case '>':
status.RightCount++
default:
return BranchStatus{}, fmt.Errorf("Unexpected revision line: %q", scanner.Text())
}
}
if err := scanner.Err(); err != nil {
return BranchStatus{}, err
}
if err := cmd.Wait(); err != nil {
return BranchStatus{}, err
}
return status, nil
}
type SortType int
const (
SortName SortType = iota
SortLeft
SortRight
)
func LeftLess(s1, s2 BranchStatus) bool {
if s1.LeftCount == s2.LeftCount {
if s1.RightCount == s2.RightCount {
return s1.Left < s2.Left
}
return s1.RightCount < s2.RightCount
}
return s1.LeftCount < s2.LeftCount
}
func RightLess(s1, s2 BranchStatus) bool {
if s1.RightCount == s2.RightCount {
if s1.LeftCount == s2.LeftCount {
return s1.Left < s2.Left
}
return s1.LeftCount < s2.LeftCount
}
return s1.RightCount < s2.RightCount
}
func NameLess(s1, s2 BranchStatus) bool {
if s1.Left == s2.Left {
if s1.LeftCount == s2.LeftCount {
return s1.RightCount < s2.RightCount
}
return s1.LeftCount < s2.LeftCount
}
return s1.Left < s2.Left
}
type BranchStatusList struct {
statuses []BranchStatus
sort SortType
}
func NewBranchStatusList(statuses []BranchStatus, sort SortType) *BranchStatusList {
return &BranchStatusList{
statuses: statuses,
sort: sort,
}
}
func (l *BranchStatusList) Statuses() []BranchStatus {
return l.statuses
}
func (l *BranchStatusList) Len() int {
return len(l.statuses)
}
func (l *BranchStatusList) Less(i, j int) bool {
switch l.sort {
case SortLeft:
return LeftLess(l.statuses[i], l.statuses[j])
case SortRight:
return RightLess(l.statuses[i], l.statuses[j])
default:
return NameLess(l.statuses[i], l.statuses[j])
}
}
func (l *BranchStatusList) Swap(i, j int) {
l.statuses[i], l.statuses[j] = l.statuses[j], l.statuses[i]
}