-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathgit-sizer.go
More file actions
341 lines (290 loc) · 8.9 KB
/
git-sizer.go
File metadata and controls
341 lines (290 loc) · 8.9 KB
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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"runtime/pprof"
"strconv"
"github.com/github/git-sizer/git"
"github.com/github/git-sizer/isatty"
"github.com/github/git-sizer/sizes"
"github.com/spf13/pflag"
)
const Usage = `usage: git-sizer [OPTS]
-v, --verbose report all statistics, whether concerning or not
--threshold THRESHOLD minimum level of concern (i.e., number of stars)
that should be reported. Default:
'--threshold=1'.
--critical only report critical statistics
--names=[none|hash|full] display names of large objects in the specified
style: 'none' (omit footnotes entirely), 'hash'
(show only the SHA-1s of objects), or 'full'
(show full names). Default is '--names=full'.
-j, --json output results in JSON format
--json-version=[1|2] choose which JSON format version to output.
Default: --json-version=1.
--[no-]progress report (don't report) progress to stderr.
--version only report the git-sizer version number
Reference selection:
By default, git-sizer processes all Git objects that are reachable from any
reference. The following options can be used to limit which references to
include. The last rule matching a reference determines whether that reference
is processed:
--branches process branches
--tags process tags
--remotes process remote refs
--include PREFIX process references with the specified PREFIX
(e.g., '--include=refs/remotes/origin')
--include-regexp REGEXP process references matching the specified
regular expression (e.g.,
'--include-regexp=refs/tags/release-.*')
--exclude PREFIX don't process references with the specified
PREFIX (e.g., '--exclude=refs/notes')
--exclude-regexp REGEXP don't process references matching the specified
regular expression
--show-refs show which refs are being included/excluded
Prefixes must match at a boundary; for example 'refs/foo' matches
'refs/foo' and 'refs/foo/bar' but not 'refs/foobar'. Regular
expression patterns must match the full reference name.
`
var ReleaseVersion string
var BuildVersion string
type NegatedBoolValue struct {
value *bool
}
func (v *NegatedBoolValue) Set(s string) error {
b, err := strconv.ParseBool(s)
*v.value = !b
return err
}
func (v *NegatedBoolValue) Get() interface{} {
return !*v.value
}
func (v *NegatedBoolValue) String() string {
if v == nil || v.value == nil {
return "true"
} else {
return strconv.FormatBool(!*v.value)
}
}
func (v *NegatedBoolValue) Type() string {
return "bool"
}
type filterValue struct {
filter *git.IncludeExcludeFilter
polarity git.Polarity
pattern string
regexp bool
}
func (v *filterValue) Set(s string) error {
var polarity git.Polarity
var filter git.ReferenceFilter
if v.regexp {
polarity = v.polarity
var err error
filter, err = git.RegexpFilter(s)
if err != nil {
return fmt.Errorf("invalid regexp: %q", s)
}
} else if v.pattern == "" {
polarity = v.polarity
filter = git.PrefixFilter(s)
} else {
// Allow a boolean value to alter the polarity:
b, err := strconv.ParseBool(s)
if err != nil {
return err
}
if b {
polarity = git.Include
} else {
polarity = git.Exclude
}
filter = git.PrefixFilter(v.pattern)
}
switch polarity {
case git.Include:
v.filter.Include(filter)
case git.Exclude:
v.filter.Exclude(filter)
}
return nil
}
func (v *filterValue) Get() interface{} {
return nil
}
func (v *filterValue) String() string {
return ""
}
func (v *filterValue) Type() string {
if v.regexp {
return "regexp"
} else if v.pattern == "" {
return "prefix"
} else {
return ""
}
}
func main() {
err := mainImplementation(os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}
func mainImplementation(args []string) error {
var nameStyle sizes.NameStyle = sizes.NameStyleFull
var cpuprofile string
var jsonOutput bool
var jsonVersion uint
var threshold sizes.Threshold = 1
var progress bool
var version bool
var filter git.IncludeExcludeFilter
var showRefs bool
flags := pflag.NewFlagSet("git-sizer", pflag.ContinueOnError)
flags.Usage = func() {
fmt.Print(Usage)
}
flags.Var(
&filterValue{&filter, git.Include, "", false}, "include",
"include specified references",
)
flags.Var(
&filterValue{&filter, git.Include, "", true}, "include-regexp",
"include references matching the specified regular expression",
)
flags.Var(
&filterValue{&filter, git.Exclude, "", false}, "exclude",
"exclude specified references",
)
flags.Var(
&filterValue{&filter, git.Exclude, "", true}, "exclude-regexp",
"exclude references matching the specified regular expression",
)
flag := flags.VarPF(
&filterValue{&filter, git.Include, "refs/heads/", false}, "branches", "",
"process all branches",
)
flag.NoOptDefVal = "true"
flag = flags.VarPF(
&filterValue{&filter, git.Include, "refs/tags/", false}, "tags", "",
"process all tags",
)
flag.NoOptDefVal = "true"
flag = flags.VarPF(
&filterValue{&filter, git.Include, "refs/remotes/", false}, "remotes", "",
"process all remotes",
)
flag.NoOptDefVal = "true"
flags.VarP(
sizes.NewThresholdFlagValue(&threshold, 0),
"verbose", "v", "report all statistics, whether concerning or not",
)
flags.Lookup("verbose").NoOptDefVal = "true"
flags.Var(
&threshold, "threshold",
"minimum level of concern (i.e., number of stars) that should be\n"+
" reported",
)
flags.Var(
sizes.NewThresholdFlagValue(&threshold, 30),
"critical", "only report critical statistics",
)
flags.Lookup("critical").NoOptDefVal = "true"
flags.Var(
&nameStyle, "names",
"display names of large objects in the specified `style`:\n"+
" --names=none omit footnotes entirely\n"+
" --names=hash show only the SHA-1s of objects\n"+
" --names=full show full names",
)
flags.BoolVarP(&jsonOutput, "json", "j", false, "output results in JSON format")
flags.UintVar(&jsonVersion, "json-version", 1, "JSON format version to output (1 or 2)")
atty, err := isatty.Isatty(os.Stderr.Fd())
if err != nil {
atty = false
}
flags.BoolVar(&progress, "progress", atty, "report progress to stderr")
flags.BoolVar(&showRefs, "show-refs", false, "list the references being processed")
flags.BoolVar(&version, "version", false, "report the git-sizer version number")
flags.Var(&NegatedBoolValue{&progress}, "no-progress", "suppress progress output")
flags.Lookup("no-progress").NoOptDefVal = "true"
flags.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
flags.MarkHidden("cpuprofile")
flags.SortFlags = false
err = flags.Parse(args)
if err != nil {
if err == pflag.ErrHelp {
return nil
}
return err
}
if jsonOutput && !(jsonVersion == 1 || jsonVersion == 2) {
return fmt.Errorf("JSON version must be 1 or 2")
}
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
return fmt.Errorf("couldn't set up cpuprofile file: %s", err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if version {
if ReleaseVersion != "" {
fmt.Printf("git-sizer release %s\n", ReleaseVersion)
} else {
fmt.Printf("git-sizer build %s\n", BuildVersion)
}
return nil
}
if len(flags.Args()) != 0 {
return errors.New("excess arguments")
}
repo, err := git.NewRepository(".")
if err != nil {
return fmt.Errorf("couldn't open Git repository: %s", err)
}
defer repo.Close()
var historySize sizes.HistorySize
var refFilter git.ReferenceFilter = filter.Filter
if showRefs {
oldRefFilter := refFilter
fmt.Fprintf(os.Stderr, "References (included references marked with '+'):\n")
refFilter = func(refname string) bool {
b := oldRefFilter(refname)
if b {
fmt.Fprintf(os.Stderr, "+ %s\n", refname)
} else {
fmt.Fprintf(os.Stderr, " %s\n", refname)
}
return b
}
}
historySize, err = sizes.ScanRepositoryUsingGraph(repo, refFilter, nameStyle, progress)
if err != nil {
return fmt.Errorf("error scanning repository: %s", err)
}
if jsonOutput {
var j []byte
var err error
switch jsonVersion {
case 1:
j, err = json.MarshalIndent(historySize, "", " ")
case 2:
j, err = historySize.JSON(threshold, nameStyle)
default:
return fmt.Errorf("JSON version must be 1 or 2")
}
if err != nil {
return fmt.Errorf("could not convert %v to json: %s", historySize, err)
}
fmt.Printf("%s\n", j)
} else {
io.WriteString(os.Stdout, historySize.TableString(threshold, nameStyle))
}
return nil
}