Skip to content

Commit d8b2507

Browse files
committed
Allow arbitrary references to be included/excluded by prefix
1 parent 4f6bfb5 commit d8b2507

File tree

2 files changed

+91
-28
lines changed

2 files changed

+91
-28
lines changed

git-sizer.go

Lines changed: 91 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ import (
1717
)
1818

1919
const Usage = `usage: git-sizer [OPTS]
20-
--branches process all branches
21-
--tags process all tags
22-
--remotes process all remotes
20+
2321
-v, --verbose report all statistics, whether concerning or not
2422
--threshold threshold minimum level of concern (i.e., number of stars)
2523
that should be reported. Default:
@@ -34,6 +32,22 @@ const Usage = `usage: git-sizer [OPTS]
3432
Default: --json-version=1.
3533
--[no-]progress report (don't report) progress to stderr.
3634
--version only report the git-sizer version number
35+
36+
Reference selection:
37+
38+
By default, git-sizer processes all Git objects that are reachable from any
39+
reference. The following options can be used to limit which references to
40+
include. The last rule matching a reference determines whether that reference
41+
is processed:
42+
43+
--branches process branches
44+
--tags process tags
45+
--remotes process remote refs
46+
--include prefix process references with the specified prefix
47+
(e.g., '--include=refs/remotes/origin')
48+
--exclude prefix don't process references with the specified
49+
prefix (e.g., '--exclude=refs/notes')
50+
3751
`
3852

3953
var ReleaseVersion string
@@ -65,6 +79,59 @@ func (v *NegatedBoolValue) Type() string {
6579
return "bool"
6680
}
6781

82+
type filterValue struct {
83+
filter *git.IncludeExcludeFilter
84+
polarity git.Polarity
85+
prefix string
86+
}
87+
88+
func (v *filterValue) Set(s string) error {
89+
var prefix string
90+
var polarity git.Polarity
91+
92+
if v.prefix == "" {
93+
prefix = s
94+
polarity = v.polarity
95+
} else {
96+
prefix = v.prefix
97+
// Allow a boolean value to alter the polarity:
98+
b, err := strconv.ParseBool(s)
99+
if err != nil {
100+
return err
101+
}
102+
if b {
103+
polarity = git.Include
104+
} else {
105+
polarity = git.Exclude
106+
}
107+
}
108+
109+
switch polarity {
110+
case git.Include:
111+
v.filter.Include(git.PrefixFilter(prefix))
112+
case git.Exclude:
113+
v.filter.Exclude(git.PrefixFilter(prefix))
114+
}
115+
116+
return nil
117+
}
118+
119+
func (v *filterValue) Get() interface{} {
120+
return nil
121+
}
122+
123+
func (v *filterValue) String() string {
124+
return ""
125+
}
126+
127+
func (v *filterValue) Type() string {
128+
if v.prefix == "" {
129+
return "prefix"
130+
} else {
131+
return ""
132+
}
133+
}
134+
68135
func main() {
69136
err := mainImplementation()
70137
if err != nil {
@@ -74,25 +141,40 @@ func main() {
74141
}
75142

76143
func mainImplementation() error {
77-
var processBranches bool
78-
var processTags bool
79-
var processRemotes bool
80144
var nameStyle sizes.NameStyle = sizes.NameStyleFull
81145
var cpuprofile string
82146
var jsonOutput bool
83147
var jsonVersion uint
84148
var threshold sizes.Threshold = 1
85149
var progress bool
86150
var version bool
151+
var filter git.IncludeExcludeFilter
87152

88153
flags := pflag.NewFlagSet("git-sizer", pflag.ContinueOnError)
89154
flags.Usage = func() {
90155
fmt.Print(Usage)
91156
}
92157

93-
flags.BoolVar(&processBranches, "branches", false, "process all branches")
94-
flags.BoolVar(&processTags, "tags", false, "process all tags")
95-
flags.BoolVar(&processRemotes, "remotes", false, "process all remote-tracking branches")
158+
flags.Var(&filterValue{&filter, git.Include, ""}, "include", "include specified references")
159+
flags.Var(&filterValue{&filter, git.Exclude, ""}, "exclude", "exclude specified references")
160+
161+
flag := flags.VarPF(
162+
&filterValue{&filter, git.Include, "refs/heads/"}, "branches", "",
163+
"process all branches",
164+
)
165+
flag.NoOptDefVal = "true"
166+
167+
flag = flags.VarPF(
168+
&filterValue{&filter, git.Include, "refs/tags/"}, "tags", "",
169+
"process all tags",
170+
)
171+
flag.NoOptDefVal = "true"
172+
173+
flag = flags.VarPF(
174+
&filterValue{&filter, git.Include, "refs/remotes/"}, "remotes", "",
175+
"process all remotes",
176+
)
177+
flag.NoOptDefVal = "true"
96178

97179
flags.VarP(
98180
sizes.NewThresholdFlagValue(&threshold, 0),
@@ -181,19 +263,6 @@ func mainImplementation() error {
181263

182264
var historySize sizes.HistorySize
183265

184-
var filter git.IncludeExcludeFilter
185-
if processBranches || processTags || processRemotes {
186-
if processBranches {
187-
filter.Include(git.BranchesFilter)
188-
}
189-
if processTags {
190-
filter.Include(git.TagsFilter)
191-
}
192-
if processRemotes {
193-
filter.Include(git.RemotesFilter)
194-
}
195-
}
196-
197266
historySize, err = sizes.ScanRepositoryUsingGraph(repo, filter.Filter, nameStyle, progress)
198267
if err != nil {
199268
return fmt.Errorf("error scanning repository: %s", err)

git/ref_filter.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,3 @@ func PrefixFilter(prefix string) ReferenceFilter {
7575
(len(r.Refname) == len(prefix) || r.Refname[len(prefix)] == '/')
7676
}
7777
}
78-
79-
var (
80-
BranchesFilter = PrefixFilter("refs/heads/")
81-
TagsFilter = PrefixFilter("refs/tags/")
82-
RemotesFilter = PrefixFilter("refs/remotes/")
83-
)

0 commit comments

Comments
 (0)