Repository navigation
Expand file tree
/
Copy pathgit-sizer.go
More file actions
166 lines (137 loc) · 3.89 KB
/
Copy pathgit-sizer.go
File metadata and controls
166 lines (137 loc) · 3.89 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
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"
)
type NegatedBoolValue struct {
value *bool
}
func (b *NegatedBoolValue) Set(s string) error {
v, err := strconv.ParseBool(s)
*b.value = !v
return err
}
func (b *NegatedBoolValue) Get() interface{} {
return !*b.value
}
func (b *NegatedBoolValue) String() string {
if b == nil || b.value == nil {
return "true"
} else {
return strconv.FormatBool(!*b.value)
}
}
func (v *NegatedBoolValue) Type() string {
return "bool"
}
func main() {
err := mainImplementation()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}
func mainImplementation() error {
var processBranches bool
var processTags bool
var processRemotes bool
var nameStyle sizes.NameStyle = sizes.NameStyleFull
var cpuprofile string
var jsonOutput bool
var threshold sizes.Threshold = 1
var progress bool
pflag.BoolVar(&processBranches, "branches", false, "process all branches")
pflag.BoolVar(&processTags, "tags", false, "process all tags")
pflag.BoolVar(&processRemotes, "remotes", false, "process all remote-tracking branches")
pflag.VarP(
sizes.NewThresholdFlagValue(&threshold, 0),
"verbose", "v", "report all statistics, whether concerning or not",
)
pflag.Lookup("verbose").NoOptDefVal = "true"
pflag.Var(
&threshold, "threshold",
"minimum level of concern (i.e., number of stars) that should be\n"+
" reported",
)
pflag.Var(
sizes.NewThresholdFlagValue(&threshold, 30),
"critical", "only report critical statistics",
)
pflag.Lookup("critical").NoOptDefVal = "true"
pflag.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",
)
pflag.BoolVarP(&jsonOutput, "json", "j", false, "output results in JSON format")
atty, err := isatty.Isatty(os.Stderr.Fd())
if err != nil {
atty = false
}
pflag.BoolVar(&progress, "progress", atty, "report progress to stderr")
pflag.Var(&NegatedBoolValue{&progress}, "no-progress", "suppress progress output")
pflag.Lookup("no-progress").NoOptDefVal = "true"
pflag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
pflag.CommandLine.MarkHidden("cpuprofile")
pflag.CommandLine.SortFlags = false
pflag.Parse()
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()
}
args := pflag.Args()
if len(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 filter git.ReferenceFilter
if processBranches || processTags || processRemotes {
var filters []git.ReferenceFilter
if processBranches {
filters = append(filters, git.BranchesFilter)
}
if processTags {
filters = append(filters, git.TagsFilter)
}
if processRemotes {
filters = append(filters, git.RemotesFilter)
}
filter = git.OrFilter(filters...)
} else {
filter = git.AllReferencesFilter
}
historySize, err = sizes.ScanRepositoryUsingGraph(repo, filter, nameStyle, progress)
if err != nil {
return fmt.Errorf("error scanning repository: %s", err)
}
if jsonOutput {
s, err := json.MarshalIndent(historySize, "", " ")
if err != nil {
return fmt.Errorf("could not convert %v to json: %s", historySize, err)
}
fmt.Printf("%s\n", s)
} else {
io.WriteString(os.Stdout, historySize.TableString(threshold, nameStyle))
}
return nil
}