-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (131 loc) · 2.76 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
package main
import (
"fmt"
"os"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
)
func die(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}
func main() {
o, err := parseOpts(os.Args[1:])
die(err)
r, err := git.PlainOpen("")
die(err)
head, err := r.Head()
die(err)
var base *plumbing.Reference
if o.base == "" {
base = head
} else {
branch, err := r.Branch(o.base)
die(err)
base, err = r.Reference(plumbing.ReferenceName(branch.Name), true)
die(err)
}
baseComit, err := r.CommitObject(base.Hash())
die(err)
refs, _ := r.References()
var report []item
refs.ForEach(func(ref *plumbing.Reference) error {
if (ref.Name().IsBranch() && !o.heads) || (ref.Name().IsRemote() && !o.remotes) || (!ref.Name().IsBranch() && !ref.Name().IsRemote()) {
return nil
}
commit, _ := r.CommitObject(ref.Hash())
ancestors, _ := commit.MergeBase(baseComit)
ahead, _ := walk(commit, ancestors)
behind, _ := walk(baseComit, ancestors)
var name string
if o.heads && ref.Name().IsRemote() {
name = fmt.Sprintf("remotes/%s", ref.Name().Short())
} else {
name = ref.Name().Short()
}
report = append(report, item{
name: name,
h: ref.Hash(),
ahead: ahead, behind: behind,
current: *ref == *head,
})
return nil
})
if len(report) == 0 {
return
}
var nameW int
for _, i := range report {
if len(i.name) > nameW {
nameW = len(i.name)
}
}
for _, i := range report {
if i.current {
fmt.Print("* ")
} else {
fmt.Print(" ")
}
fmt.Printf("%-*s %s", nameW, i.name, i.h.String()[:8])
fmt.Printf(" %3d", i.behind)
if i.behind > 100 {
fmt.Print("━")
} else if i.behind > 10 {
fmt.Print("╺")
} else {
fmt.Print(" ")
}
if i.behind > 0 && i.ahead > 0 {
fmt.Print("┿")
} else if i.behind > 0 {
fmt.Print("┥")
} else if i.ahead > 0 {
fmt.Print("┝")
} else {
fmt.Print("│")
}
if i.ahead > 100 {
fmt.Print("━")
} else if i.ahead > 10 {
fmt.Print("╸")
} else {
fmt.Print(" ")
}
fmt.Printf("%-3d", i.ahead)
fmt.Println()
}
}
type item struct {
name string
h plumbing.Hash
ahead, behind uint
current bool
}
func walk(leaf *object.Commit, roots []*object.Commit) (uint, error) {
var count uint
todo := []*object.Commit{leaf}
cache := make(map[plumbing.Hash]bool, len(roots))
for _, c := range roots {
cache[c.Hash] = true
}
for len(todo) > 0 {
c := todo[0]
todo = todo[1:]
if cache[c.Hash] {
continue
}
count += 1
c.Parents().ForEach(func(p *object.Commit) error {
todo = append(todo, p)
return nil
})
cache[c.Hash] = true
}
return count, nil
}
type walker struct {
seen map[plumbing.Hash]bool
}