-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtable.go
343 lines (302 loc) · 6.55 KB
/
table.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package main
import (
"context"
"encoding/csv"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"time"
runewidth "github.com/mattn/go-runewidth"
"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 narrowWidth = 55
const wideWidth = 80
func pickWidth(mode tally.TallyMode, showEmail bool) int {
wideMode := mode == tally.FilesMode || mode == tally.LinesMode
if wideMode || showEmail {
return wideWidth
}
return narrowWidth
}
// The "table" subcommand summarizes the authorship history of the given
// commits and paths in a table printed to stdout.
func table(
revs []string,
paths []string,
mode tally.TallyMode,
useCsv bool,
showEmail bool,
countMerges bool,
limit int,
since string,
until string,
authors []string,
nauthors []string,
) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("error running \"table\": %w", err)
}
}()
logger().Debug(
"called table()",
"revs",
revs,
"paths",
paths,
"mode",
mode,
"useCsv",
useCsv,
"showEmail",
showEmail,
"countMerges",
countMerges,
"limit",
limit,
"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 tallies map[string]tally.Tally
if populateDiffs && runtime.GOMAXPROCS(0) > 1 {
tallies, err = concurrent.TallyCommits(
ctx,
revs,
paths,
filters,
tallyOpts,
getCache(),
pretty.AllowDynamic(os.Stdout),
)
if err != nil {
return err
}
} else {
// This is fast in the no-diff case even if we don't parallelize it
commits, closer, err := git.CommitsWithOpts(
ctx,
revs,
paths,
filters,
populateDiffs,
)
if err != nil {
return err
}
tallies, err = tally.TallyCommits(commits, tallyOpts)
if err != nil {
return fmt.Errorf("failed to tally commits: %w", err)
}
err = closer()
if err != nil {
return err
}
}
rankedTallies := tally.Rank(tallies, mode)
numFilteredOut := 0
if limit > 0 && limit < len(rankedTallies) {
numFilteredOut = len(rankedTallies) - limit
rankedTallies = rankedTallies[:limit]
}
if useCsv {
err := writeCsv(rankedTallies, tallyOpts, showEmail)
if err != nil {
return err
}
} else {
colwidth := pickWidth(mode, showEmail)
writeTable(rankedTallies, colwidth, showEmail, mode, numFilteredOut)
}
return nil
}
func toRecord(
t tally.FinalTally,
opts tally.TallyOpts,
showEmail bool,
) []string {
record := []string{t.AuthorName}
if showEmail {
record = append(record, t.AuthorEmail)
}
record = append(record, strconv.Itoa(t.Commits))
if opts.IsDiffMode() {
record = append(
record,
strconv.Itoa(t.LinesAdded),
strconv.Itoa(t.LinesRemoved),
strconv.Itoa(t.FileCount),
)
}
return append(
record,
t.LastCommitTime.Format(time.RFC3339),
t.FirstCommitTime.Format(time.RFC3339),
)
}
func writeCsv(
tallies []tally.FinalTally,
opts tally.TallyOpts,
showEmail bool,
) error {
w := csv.NewWriter(os.Stdout)
// Write header
columnHeaders := []string{"name"}
if showEmail {
columnHeaders = append(columnHeaders, "email")
}
columnHeaders = append(columnHeaders, "commits")
if opts.IsDiffMode() {
columnHeaders = append(
columnHeaders,
"lines added",
"lines removed",
"files",
)
}
columnHeaders = append(columnHeaders, "last commit time", "first commit time")
w.Write(columnHeaders)
for _, tally := range tallies {
record := toRecord(tally, opts, showEmail)
if err := w.Write(record); err != nil {
return fmt.Errorf("error writing CSV record to stdout: %w", err)
}
}
w.Flush()
if err := w.Error(); err != nil {
return fmt.Errorf("error flushing CSV writer: %w", err)
}
return nil
}
// Returns a string matching the given width describing the author
func formatAuthor(
t tally.FinalTally,
showEmail bool,
width int,
) string {
var author string
if showEmail {
author = fmt.Sprintf(
"%s %s",
t.AuthorName,
format.GitEmail(t.AuthorEmail),
)
} else {
author = t.AuthorName
}
author = format.Abbrev(author, width)
return runewidth.FillRight(author, width)
}
func writeTable(
tallies []tally.FinalTally,
colwidth int,
showEmail bool,
mode tally.TallyMode,
numFilteredOut int,
) {
if len(tallies) == 0 {
return
}
var build strings.Builder
for _ = range colwidth - 2 {
build.WriteRune('─')
}
rule := build.String()
// -- Write header --
fmt.Printf("┌%s┐\n", rule)
if mode == tally.LinesMode || mode == tally.FilesMode {
fmt.Printf(
"│%-*s %-11s %7s %7s %17s│\n",
colwidth-36-13,
"Author",
"Last Edit",
"Commits",
"Files",
"Lines (+/-)",
)
} else if mode == tally.FirstModifiedMode {
fmt.Printf(
"│%-*s %-11s %7s│\n",
colwidth-22,
"Author",
"First Edit",
"Commits",
)
} else {
fmt.Printf(
"│%-*s %-11s %7s│\n",
colwidth-22,
"Author",
"Last Edit",
"Commits",
)
}
fmt.Printf("├%s┤\n", rule)
// -- Write table rows --
for _, t := range tallies {
lines := fmt.Sprintf(
"%s%7s%s / %s%7s%s",
pretty.Green,
format.Number(t.LinesAdded),
pretty.Reset,
pretty.Red,
format.Number(t.LinesRemoved),
pretty.Reset,
)
if mode == tally.LinesMode || mode == tally.FilesMode {
fmt.Printf(
"│%s %-11s %7s %7s %17s│\n",
formatAuthor(t, showEmail, colwidth-36-13),
format.RelativeTime(progStart, t.LastCommitTime),
format.Number(t.Commits),
format.Number(t.FileCount),
lines,
)
} else if mode == tally.FirstModifiedMode {
fmt.Printf(
"│%s %-11s %7s│\n",
formatAuthor(t, showEmail, colwidth-22),
format.RelativeTime(progStart, t.FirstCommitTime),
format.Number(t.Commits),
)
} else {
fmt.Printf(
"│%s %-11s %7s│\n",
formatAuthor(t, showEmail, colwidth-22),
format.RelativeTime(progStart, t.LastCommitTime),
format.Number(t.Commits),
)
}
}
if numFilteredOut > 0 {
msg := fmt.Sprintf("...%s more...", format.Number(numFilteredOut))
fmt.Printf("│%-*s│\n", colwidth-2, msg)
}
fmt.Printf("└%s┘\n", rule)
}