Skip to content

Commit

Permalink
Still run git for-each-ref when explicit roots and reference filter…
Browse files Browse the repository at this point in the history
…s are specified

When explicit roots and reference filters are both provided, we want to make
sure that the optimisation don't kick in. As we still want to iterate
over all references in order to apply the filters.

Here is an example with git-sizer being run with explicit roots and reference filters
```
git-sizer main~20^{tree} main~10^{tree} --tags --branches
```
  • Loading branch information
elhmn committed Sep 29, 2023
1 parent 9d71973 commit 721e353
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
19 changes: 13 additions & 6 deletions git-sizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,26 +307,33 @@ func mainImplementation(ctx context.Context, stdout, stderr io.Writer, args []st
}

var roots []sizes.Root
var explicitRoots []sizes.Root
// If arguments are provided, use them as explicit roots.
if len(flags.Args()) > 0 {
roots = make([]sizes.Root, 0, len(flags.Args()))
explicitRoots = make([]sizes.Root, 0, len(flags.Args()))
for _, arg := range flags.Args() {
oid, err := repo.ResolveObject(arg)
if err != nil {
return fmt.Errorf("resolving command-line argument %q: %w", arg, err)
}
roots = append(roots, sizes.NewExplicitRoot(arg, oid))
explicitRoots = append(explicitRoots, sizes.NewExplicitRoot(arg, oid))
}
}

// If no reference filters and no explicit roots were provided
if git.IsNoReferencesFilter(rgb.GetTopLevelGroup().GetFilter()) {
roots = explicitRoots
} else {
refs, err := sizes.CollectReferences(ctx, repo, rg)
refRoots, err := sizes.CollectReferences(ctx, repo, rg)
if err != nil {
return fmt.Errorf("determining which reference to scan: %w", err)
}

roots = make([]sizes.Root, 0, len(refs))
for _, ref := range refs {
roots = append(roots, ref)
roots = make([]sizes.Root, 0, len(refRoots)+len(explicitRoots))
for _, refRoot := range refRoots {
roots = append(roots, refRoot)
}
roots = append(roots, explicitRoots...)
}

historySize, err := sizes.ScanRepositoryUsingGraph(
Expand Down
5 changes: 5 additions & 0 deletions git/ref_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,8 @@ type regexpFilter struct {
func (f regexpFilter) Filter(refname string) bool {
return f.re.MatchString(refname)
}

func IsNoReferencesFilter(val interface{}) bool {
_, ok := val.(noReferencesFilter)
return ok
}
4 changes: 4 additions & 0 deletions internal/refopts/ref_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type refGroup struct {
otherRefGroup *sizes.RefGroup
}

func (rg *refGroup) GetFilter() git.ReferenceFilter {
return rg.filter
}

func (rg *refGroup) collectSymbols(refname string) (bool, []sizes.RefGroupSymbol) {
walk := false
var symbols []sizes.RefGroupSymbol
Expand Down
4 changes: 4 additions & 0 deletions internal/refopts/ref_group_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type RefGroupBuilder struct {
groups map[sizes.RefGroupSymbol]*refGroup
}

func (rgb *RefGroupBuilder) GetTopLevelGroup() *refGroup {
return rgb.topLevelGroup
}

// NewRefGroupBuilder creates and returns a `RefGroupBuilder`
// instance.
func NewRefGroupBuilder(configger Configger) (*RefGroupBuilder, error) {
Expand Down

0 comments on commit 721e353

Please sign in to comment.