Skip to content

Commit 9a4dc3b

Browse files
authored
Merge pull request #81 from github/more-ref-options
Add some more reference selection options
2 parents 9a9ad2b + 45cf9e2 commit 9a4dc3b

File tree

3 files changed

+213
-91
lines changed

3 files changed

+213
-91
lines changed

git-sizer.go

Lines changed: 91 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,18 @@ const Usage = `usage: git-sizer [OPTS]
4949
include. The last rule matching a reference determines whether that reference
5050
is processed:
5151
52-
--branches process branches
53-
--tags process tags
54-
--remotes process remote refs
52+
--[no-]branches process [don't process] branches
53+
--[no-]tags process [don't process] tags
54+
--[no-]remotes process [don't process] remote-tracking references
55+
--[no-]notes process [don't process] git-notes references
56+
--[no-]stash process [don't process] refs/stash
5557
--include PREFIX process references with the specified PREFIX
5658
(e.g., '--include=refs/remotes/origin')
5759
--include-regexp REGEXP process references matching the specified
5860
regular expression (e.g.,
5961
'--include-regexp=refs/tags/release-.*')
6062
--exclude PREFIX don't process references with the specified
61-
PREFIX (e.g., '--exclude=refs/notes')
63+
PREFIX (e.g., '--exclude=refs/changes')
6264
--exclude-regexp REGEXP don't process references matching the specified
6365
regular expression
6466
--show-refs show which refs are being included/excluded
@@ -99,38 +101,53 @@ func (v *NegatedBoolValue) Type() string {
99101
}
100102

101103
type filterValue struct {
102-
filter *git.IncludeExcludeFilter
104+
// The filter to which values will be appended:
105+
filter *git.IncludeExcludeFilter
106+
107+
// The polarity of this option (i.e., does it cause the things
108+
// that it references to be included or excluded?):
103109
polarity git.Polarity
104-
pattern string
105-
regexp bool
110+
111+
// If this is set, then it is used as the pattern. If not, then
112+
// the user should supply the pattern.
113+
pattern string
114+
115+
// Should `pattern` be interpreted as a regexp (as opposed to a
116+
// prefix)?
117+
regexp bool
106118
}
107119

108120
func (v *filterValue) Set(s string) error {
109-
var polarity git.Polarity
110121
var filter git.ReferenceFilter
122+
polarity := v.polarity
123+
124+
var pattern string
125+
if v.pattern != "" {
126+
// The pattern is fixed for this option:
127+
pattern = v.pattern
128+
129+
// It's not really expected, but if the user supplied a
130+
// `false` boolean value, invert the polarity:
131+
b, err := strconv.ParseBool(s)
132+
if err != nil {
133+
return err
134+
}
135+
if !b {
136+
polarity = polarity.Inverted()
137+
}
138+
} else {
139+
// The user must supply the pattern.
140+
pattern = s
141+
}
111142

112143
if v.regexp {
113-
polarity = v.polarity
114144
var err error
115-
filter, err = git.RegexpFilter(s)
145+
filter, err = git.RegexpFilter(pattern)
116146
if err != nil {
117147
return fmt.Errorf("invalid regexp: %q", s)
118148
}
119-
} else if v.pattern == "" {
120-
polarity = v.polarity
121-
filter = git.PrefixFilter(s)
122149
} else {
123-
// Allow a boolean value to alter the polarity:
124-
b, err := strconv.ParseBool(s)
125-
if err != nil {
126-
return err
127-
}
128-
if b {
129-
polarity = git.Include
130-
} else {
131-
polarity = git.Exclude
132-
}
133-
filter = git.PrefixFilter(v.pattern)
150+
filter = git.PrefixFilter(pattern)
134151
}
135152

136153
switch polarity {
@@ -152,12 +169,12 @@ func (v *filterValue) String() string {
152169
}
153170

154171
func (v *filterValue) Type() string {
155-
if v.regexp {
172+
if v.pattern != "" {
173+
return "bool"
174+
} else if v.regexp {
156175
return "regexp"
157-
} else if v.pattern == "" {
158-
return "prefix"
159176
} else {
160-
return ""
177+
return "prefix"
161178
}
162179
}
163180

@@ -203,20 +220,62 @@ func mainImplementation(args []string) error {
203220
)
204221

205222
flag := flags.VarPF(
206-
&filterValue{&filter, git.Include, "refs/heads/", false}, "branches", "",
223+
&filterValue{&filter, git.Include, "refs/heads", false}, "branches", "",
207224
"process all branches",
208225
)
209226
flag.NoOptDefVal = "true"
210227

211228
flag = flags.VarPF(
212-
&filterValue{&filter, git.Include, "refs/tags/", false}, "tags", "",
229+
&filterValue{&filter, git.Exclude, "refs/heads", false}, "no-branches", "",
230+
"exclude all branches",
231+
)
232+
flag.NoOptDefVal = "true"
233+
234+
flag = flags.VarPF(
235+
&filterValue{&filter, git.Include, "refs/tags", false}, "tags", "",
213236
"process all tags",
214237
)
215238
flag.NoOptDefVal = "true"
216239

217240
flag = flags.VarPF(
218-
&filterValue{&filter, git.Include, "refs/remotes/", false}, "remotes", "",
219-
"process all remotes",
241+
&filterValue{&filter, git.Exclude, "refs/tags", false}, "no-tags", "",
242+
"exclude all tags",
243+
)
244+
flag.NoOptDefVal = "true"
245+
246+
flag = flags.VarPF(
247+
&filterValue{&filter, git.Include, "refs/remotes", false}, "remotes", "",
248+
"process all remote-tracking references",
249+
)
250+
flag.NoOptDefVal = "true"
251+
252+
flag = flags.VarPF(
253+
&filterValue{&filter, git.Exclude, "refs/remotes", false}, "no-remotes", "",
254+
"exclude all remote-tracking references",
255+
)
256+
flag.NoOptDefVal = "true"
257+
258+
flag = flags.VarPF(
259+
&filterValue{&filter, git.Include, "refs/notes", false}, "notes", "",
260+
"process all git-notes references",
261+
)
262+
flag.NoOptDefVal = "true"
263+
264+
flag = flags.VarPF(
265+
&filterValue{&filter, git.Exclude, "refs/notes", false}, "no-notes", "",
266+
"exclude all git-notes references",
267+
)
268+
flag.NoOptDefVal = "true"
269+
270+
flag = flags.VarPF(
271+
&filterValue{&filter, git.Include, "refs/stash", true}, "stash", "",
272+
"process refs/stash",
273+
)
274+
flag.NoOptDefVal = "true"
275+
276+
flag = flags.VarPF(
277+
&filterValue{&filter, git.Exclude, "refs/stash", true}, "no-stash", "",
278+
"exclude refs/stash",
220279
)
221280
flag.NoOptDefVal = "true"
222281

git/ref_filter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ const (
1818
Exclude
1919
)
2020

21+
func (p Polarity) Inverted() Polarity {
22+
switch p {
23+
case Include:
24+
return Exclude
25+
case Exclude:
26+
return Include
27+
default:
28+
// This shouldn't happen:
29+
return Exclude
30+
}
31+
}
32+
2133
// polarizedFilter is a filter that might match, in which case it
2234
// includes or excludes the reference (according to its polarity). If
2335
// it doesn't match, then it doesn't say anything about the reference.

0 commit comments

Comments
 (0)