Skip to content

Commit 87e2f1a

Browse files
committed
Change --include and --exclude to take more flexible arguments
1 parent 136b901 commit 87e2f1a

2 files changed

Lines changed: 48 additions & 9 deletions

File tree

git_sizer_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,41 +191,41 @@ func TestRefSelections(t *testing.T) {
191191
},
192192
{ // 12
193193
name: "foo",
194-
args: []string{"--include-regexp", ".*foo.*"},
194+
args: []string{"--include", "/.*foo.*/"},
195195
},
196196
{ // 13
197197
name: "refs/foo as prefix",
198198
args: []string{"--include", "refs/foo"},
199199
},
200200
{ // 14
201201
name: "refs/foo as regexp",
202-
args: []string{"--include-regexp", "refs/foo"},
202+
args: []string{"--include", "/refs/foo/"},
203203
},
204204
{ // 15
205205
name: "release tags",
206-
args: []string{"--include-regexp", "refs/tags/release-.*"},
206+
args: []string{"--include", "/refs/tags/release-.*/"},
207207
},
208208
{ // 16
209209
name: "combination",
210210
args: []string{
211211
"--include=refs/heads",
212212
"--tags",
213213
"--exclude", "refs/heads/foo",
214-
"--include-regexp", ".*foo.*",
214+
"--include", "/.*foo.*/",
215215
"--exclude", "refs/foo",
216-
"--exclude-regexp", "refs/tags/release-.*",
216+
"--exclude", "/refs/tags/release-.*/",
217217
},
218218
},
219219
{ // 17
220220
name: "branches-refgroup",
221-
args: []string{"--refgroup=mygroup"},
221+
args: []string{"--include=@mygroup"},
222222
config: []git.ConfigEntry{
223223
{"refgroup.mygroup.include", "refs/heads"},
224224
},
225225
},
226226
{ // 18
227227
name: "combination-refgroup",
228-
args: []string{"--refgroup=mygroup"},
228+
args: []string{"--include=@mygroup"},
229229
config: []git.ConfigEntry{
230230
{"refgroup.mygroup.include", "refs/heads"},
231231
{"refgroup.mygroup.include", "refs/tags"},

internal/refopts/filter_value.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package refopts
22

33
import (
4+
"errors"
45
"fmt"
56
"strconv"
7+
"strings"
68

79
"github.com/github/git-sizer/git"
10+
"github.com/github/git-sizer/sizes"
811
)
912

1013
type filterValue struct {
@@ -23,7 +26,7 @@ type filterValue struct {
2326
pattern string
2427

2528
// regexp specifies whether `pattern` should be interpreted as
26-
// a regexp (as opposed to a prefix).
29+
// a regexp (as opposed to handling it flexibly).
2730
regexp bool
2831
}
2932

@@ -57,14 +60,50 @@ func (v *filterValue) Set(s string) error {
5760
return fmt.Errorf("invalid regexp: %q", s)
5861
}
5962
} else {
60-
filter = git.PrefixFilter(pattern)
63+
var err error
64+
filter, err = v.interpretFlexibly(pattern)
65+
if err != nil {
66+
return err
67+
}
6168
}
6269

6370
v.rgb.topLevelGroup.filter = combiner.Combine(v.rgb.topLevelGroup.filter, filter)
6471

6572
return nil
6673
}
6774

75+
// Interpret an option argument flexibly:
76+
//
77+
// * If it is bracketed with `/` characters, treat it as a regexp.
78+
//
79+
// * If it starts with `@`, then consider it a refgroup name. That
80+
// refgroup must already be defined. Use its filter. This construct
81+
// is only allowed at the top level.
82+
//
83+
// * Otherwise treat it as a prefix.
84+
func (v *filterValue) interpretFlexibly(s string) (git.ReferenceFilter, error) {
85+
if len(s) >= 2 && strings.HasPrefix(s, "/") && strings.HasSuffix(s, "/") {
86+
pattern := s[1 : len(s)-1]
87+
return git.RegexpFilter(pattern)
88+
}
89+
90+
if len(s) >= 1 && s[0] == '@' {
91+
name := sizes.RefGroupSymbol(s[1:])
92+
if name == "" {
93+
return nil, errors.New("missing refgroup name")
94+
}
95+
96+
refGroup := v.rgb.groups[name]
97+
if refGroup == nil {
98+
return nil, fmt.Errorf("undefined refgroup '%s'", name)
99+
}
100+
101+
return refGroupFilter{refGroup}, nil
102+
}
103+
104+
return git.PrefixFilter(s), nil
105+
}
106+
68107
func (v *filterValue) Get() interface{} {
69108
return nil
70109
}

0 commit comments

Comments
 (0)