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
|
package refopts
import (
"fmt"
"github.com/github/git-sizer/git"
"github.com/github/git-sizer/sizes"
)
// refGroup represents one reference group and also its relationship
// to its parent group and any subgroups.. Note that reference groups
// don't intrinsically have anything to do with the layout of the
// reference namespace, but they will often be used that way.
type refGroup struct {
sizes.RefGroup
// filter is the filter for just this reference group. Filters
// for any parent groups must also be applied.
filter git.ReferenceFilter
parent *refGroup
// subgroups are the `refGroup` instances representing any
// direct subgroups.
subgroups []*refGroup
// otherRefGroup, if set, is the refGroup for tallying
// references that match `filter` but don't match any of the
// subgroups.
otherRefGroup *sizes.RefGroup
}
func (rg *refGroup) collectSymbols(refname string) (bool, []sizes.RefGroupSymbol) {
walk := false
var symbols []sizes.RefGroupSymbol
if rg.filter == nil {
// The tree doesn't have its own filter. Consider it matched
// iff at least one subtree matches it.
for _, sg := range rg.subgroups {
w, ss := sg.collectSymbols(refname)
if w {
walk = true
}
if len(ss) > 0 && len(symbols) == 0 {
symbols = append(symbols, rg.Symbol)
}
symbols = append(symbols, ss...)
}
} else {
// The tree has its own filter. If it doesn't match the
// reference, then the subtrees don't even get a chance to
// try.
if !rg.filter.Filter(refname) {
return false, nil
}
walk = true
symbols = append(symbols, rg.Symbol)
for _, sg := range rg.subgroups {
_, ss := sg.collectSymbols(refname)
symbols = append(symbols, ss...)
}
// References that match the tree filter but no subtree
// filters are counted as "other":
if rg.otherRefGroup != nil && len(symbols) == 1 {
symbols = append(symbols, rg.otherRefGroup.Symbol)
}
}
return walk, symbols
}
// augmentFromConfig augments `rg` based on configuration in the
// gitconfig and returns the result. It is not considered an error if
// there are no usable config entries for the filter.
func (rg *refGroup) augmentFromConfig(configger Configger) error {
config, err := configger.GetConfig(fmt.Sprintf("refgroup.%s", rg.Symbol))
if err != nil {
return err
}
for _, entry := range config.Entries {
switch entry.Key {
case "name":
rg.Name = entry.Value
case "include":
rg.filter = git.Include.Combine(
rg.filter, git.PrefixFilter(entry.Value),
)
case "includeregexp":
f, err := git.RegexpFilter(entry.Value)
if err != nil {
return fmt.Errorf(
"invalid regular expression for '%s': %w",
config.FullKey(entry.Key), err,
)
}
rg.filter = git.Include.Combine(rg.filter, f)
case "exclude":
rg.filter = git.Exclude.Combine(
rg.filter, git.PrefixFilter(entry.Value),
)
case "excluderegexp":
f, err := git.RegexpFilter(entry.Value)
if err != nil {
return fmt.Errorf(
"invalid regular expression for '%s': %w",
config.FullKey(entry.Key), err,
)
}
rg.filter = git.Exclude.Combine(rg.filter, f)
default:
// Ignore unrecognized keys.
}
}
return nil
}
|