@@ -63,6 +63,11 @@ const Usage = `usage: git-sizer [OPTS]
6363 PREFIX (e.g., '--exclude=refs/changes')
6464 --exclude-regexp REGEXP don't process references matching the specified
6565 regular expression
66+ --refgroup=NAME process reference in group defined by gitconfig:
67+ 'refgroup.NAME.include',
68+ 'refgroup.NAME.includeRegexp',
69+ 'refgroup.NAME.exclude', and
70+ 'refgroup.NAME.excludeRegexp' as above.
6671 --show-refs show which refs are being included/excluded
6772
6873 Prefixes must match at a boundary; for example 'refs/foo' matches
@@ -178,6 +183,70 @@ func (v *filterValue) Type() string {
178183 }
179184}
180185
186+ type filterGroupValue struct {
187+ filter * git.IncludeExcludeFilter
188+ repo * git.Repository
189+ }
190+
191+ func (v * filterGroupValue ) Set (name string ) error {
192+ // At this point, it is not yet certain that the command was run
193+ // inside a Git repository. If not, ignore this option (the
194+ // command will error out anyway).
195+ if v .repo == nil {
196+ fmt .Fprintf (
197+ os .Stderr ,
198+ "warning: not in Git repository; ignoring '--refgroup' option.\n " ,
199+ )
200+ return nil
201+ }
202+
203+ config , err := v .repo .Config (fmt .Sprintf ("refgroup.%s" , name ))
204+ if err != nil {
205+ return err
206+ }
207+ for _ , entry := range config .Entries {
208+ switch entry .Key {
209+ case "include" :
210+ v .filter .Include (git .PrefixFilter (entry .Value ))
211+ case "includeregexp" :
212+ filter , err := git .RegexpFilter (entry .Value )
213+ if err != nil {
214+ return fmt .Errorf (
215+ "invalid regular expression for 'refgroup.%s.%s': %w" ,
216+ name , entry .Key , err ,
217+ )
218+ }
219+ v .filter .Include (filter )
220+ case "exclude" :
221+ v .filter .Exclude (git .PrefixFilter (entry .Value ))
222+ case "excluderegexp" :
223+ filter , err := git .RegexpFilter (entry .Value )
224+ if err != nil {
225+ return fmt .Errorf (
226+ "invalid regular expression for 'refgroup.%s.%s': %w" ,
227+ name , entry .Key , err ,
228+ )
229+ }
230+ v .filter .Exclude (filter )
231+ default :
232+ // Ignore unrecognized keys.
233+ }
234+ }
235+ return nil
236+ }
237+
238+ func (v * filterGroupValue ) Get () interface {} {
239+ return nil
240+ }
241+
242+ func (v * filterGroupValue ) String () string {
243+ return ""
244+ }
245+
246+ func (v * filterGroupValue ) Type () string {
247+ return "name"
248+ }
249+
181250func main () {
182251 err := mainImplementation (os .Args [1 :])
183252 if err != nil {
@@ -197,6 +266,13 @@ func mainImplementation(args []string) error {
197266 var filter git.IncludeExcludeFilter
198267 var showRefs bool
199268
269+ // Try to open the repository, but it's not an error yet if this
270+ // fails, because the user might only be asking for `--help`.
271+ repo , repoErr := git .NewRepository ("." )
272+ if repoErr == nil {
273+ defer repo .Close ()
274+ }
275+
200276 flags := pflag .NewFlagSet ("git-sizer" , pflag .ContinueOnError )
201277 flags .Usage = func () {
202278 fmt .Print (Usage )
@@ -279,6 +355,11 @@ func mainImplementation(args []string) error {
279355 )
280356 flag .NoOptDefVal = "true"
281357
358+ flag = flags .VarPF (
359+ & filterGroupValue {& filter , repo }, "refgroup" , "" ,
360+ "process references in refgroup defined by gitconfig" ,
361+ )
362+
282363 flags .VarP (
283364 sizes .NewThresholdFlagValue (& threshold , 0 ),
284365 "verbose" , "v" , "report all statistics, whether concerning or not" ,
@@ -359,11 +440,9 @@ func mainImplementation(args []string) error {
359440 return errors .New ("excess arguments" )
360441 }
361442
362- repo , err := git .NewRepository ("." )
363- if err != nil {
364- return fmt .Errorf ("couldn't open Git repository: %s" , err )
443+ if repoErr != nil {
444+ return fmt .Errorf ("couldn't open Git repository: %s" , repoErr )
365445 }
366- defer repo .Close ()
367446
368447 if jsonOutput {
369448 if ! flags .Changed ("json-version" ) {
0 commit comments