-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathhist.go
233 lines (210 loc) · 4.27 KB
/
hist.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
package main
import (
"context"
"fmt"
"math"
"os"
"runtime"
"strings"
"time"
"github.com/sinclairtarget/git-who/internal/concurrent"
"github.com/sinclairtarget/git-who/internal/format"
"github.com/sinclairtarget/git-who/internal/git"
"github.com/sinclairtarget/git-who/internal/pretty"
"github.com/sinclairtarget/git-who/internal/tally"
)
const barWidth = 36
func hist(
revs []string,
paths []string,
mode tally.TallyMode,
showEmail bool,
countMerges bool,
since string,
until string,
authors []string,
nauthors []string,
) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("error running \"hist\": %w", err)
}
}()
logger().Debug(
"called hist()",
"revs",
revs,
"paths",
paths,
"mode",
mode,
"showEmail",
showEmail,
"countMerges",
countMerges,
"since",
since,
"until",
until,
"authors",
authors,
"nauthors",
nauthors,
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tallyOpts := tally.TallyOpts{Mode: mode, CountMerges: countMerges}
if showEmail {
tallyOpts.Key = func(c git.Commit) string { return c.AuthorEmail }
} else {
tallyOpts.Key = func(c git.Commit) string { return c.AuthorName }
}
populateDiffs := tallyOpts.IsDiffMode()
filters := git.LogFilters{
Since: since,
Until: until,
Authors: authors,
Nauthors: nauthors,
}
var end time.Time // Default is zero time, meaning use last commit
if len(revs) == 1 && revs[0] == "HEAD" && len(until) == 0 {
// If no revs or --until given, end timeline at current time
end = time.Now()
}
var buckets []tally.TimeBucket
if populateDiffs && runtime.GOMAXPROCS(0) > 1 {
buckets, err = concurrent.TallyCommitsTimeline(
ctx,
revs,
paths,
filters,
tallyOpts,
end,
getCache(),
pretty.AllowDynamic(os.Stdout),
)
if err != nil {
return err
}
} else {
commits, closer, err := git.CommitsWithOpts(
ctx,
revs,
paths,
filters,
populateDiffs,
)
if err != nil {
return err
}
buckets, err = tally.TallyCommitsTimeline(
commits,
tallyOpts,
end,
)
if err != nil {
return err
}
err = closer()
if err != nil {
return err
}
}
// -- Pick winner in each bucket --
for i, bucket := range buckets {
buckets[i] = bucket.Rank(mode)
}
// -- Draw bar plot --
maxVal := barWidth
for _, bucket := range buckets {
if bucket.TotalValue(mode) > maxVal {
maxVal = bucket.TotalValue(mode)
}
}
drawPlot(buckets, maxVal, mode, showEmail)
return nil
}
func drawPlot(
buckets []tally.TimeBucket,
maxVal int,
mode tally.TallyMode,
showEmail bool,
) {
var lastAuthor string
for _, bucket := range buckets {
value := bucket.Value(mode)
clampedValue := int(math.Ceil(
(float64(value) / float64(maxVal)) * float64(barWidth),
))
total := bucket.TotalValue(mode)
clampedTotal := int(math.Ceil(
(float64(total) / float64(maxVal)) * float64(barWidth),
))
valueBar := strings.Repeat("#", clampedValue)
totalBar := strings.Repeat("-", clampedTotal-clampedValue)
if value > 0 {
tallyPart := fmtHistTally(
bucket.Tally,
mode,
showEmail,
bucket.Tally.AuthorName == lastAuthor,
)
fmt.Printf(
"%s ┤ %s%s%-*s%s %s\n",
bucket.Name,
valueBar,
pretty.Dim,
barWidth-clampedValue,
totalBar,
pretty.Reset,
tallyPart,
)
lastAuthor = bucket.Tally.AuthorName
} else {
fmt.Printf("%s ┤ \n", bucket.Name)
}
}
}
func fmtHistTally(
t tally.FinalTally,
mode tally.TallyMode,
showEmail bool,
fade bool,
) string {
var metric string
switch mode {
case tally.CommitMode:
metric = fmt.Sprintf("(%s)", format.Number(t.Commits))
case tally.FilesMode:
metric = fmt.Sprintf("(%s)", format.Number(t.FileCount))
case tally.LinesMode:
metric = fmt.Sprintf(
"(%s%s%s / %s%s%s)",
pretty.Green,
format.Number(t.LinesAdded),
pretty.DefaultColor,
pretty.Red,
format.Number(t.LinesRemoved),
pretty.DefaultColor,
)
default:
panic("unrecognized tally mode in switch")
}
var author string
if showEmail {
author = format.Abbrev(format.GitEmail(t.AuthorEmail), 25)
} else {
author = format.Abbrev(t.AuthorName, 25)
}
if fade {
return fmt.Sprintf(
"%s%s %s%s",
pretty.Dim,
author,
metric,
pretty.Reset,
)
} else {
return fmt.Sprintf("%s %s", author, metric)
}
}