Skip to content

Commit 3ddf88e

Browse files
committed
Start adding a mechanism for grouping references configurably
Add types `RefGroup` and `RefGrouper` that will allow reference to be grouped into multiple configurable groups, and to tally up the count of references by group.
1 parent 2e0da54 commit 3ddf88e

3 files changed

Lines changed: 64 additions & 8 deletions

File tree

git-sizer.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,11 @@ func mainImplementation(args []string) error {
478478
filter = showRefFilter{filter}
479479
}
480480

481-
historySize, err := sizes.ScanRepositoryUsingGraph(repo, filter, nameStyle, progress)
481+
rg := refGrouper{
482+
filter: filter,
483+
}
484+
485+
historySize, err := sizes.ScanRepositoryUsingGraph(repo, &rg, nameStyle, progress)
482486
if err != nil {
483487
return fmt.Errorf("error scanning repository: %s", err)
484488
}
@@ -505,6 +509,18 @@ func mainImplementation(args []string) error {
505509
return nil
506510
}
507511

512+
type refGrouper struct {
513+
filter git.ReferenceFilter
514+
}
515+
516+
func (rg *refGrouper) Categorize(refname string) (bool, []sizes.RefGroupSymbol) {
517+
return rg.filter.Filter(refname), nil
518+
}
519+
520+
func (rg *refGrouper) Groups() []sizes.RefGroup {
521+
return nil
522+
}
523+
508524
type showRefFilter struct {
509525
f git.ReferenceFilter
510526
}

git_sizer_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,16 @@ func pow(x uint64, n int) uint64 {
285285
return p
286286
}
287287

288+
type refGrouper struct{}
289+
290+
func (rg refGrouper) Categorize(refname string) (bool, []sizes.RefGroupSymbol) {
291+
return true, nil
292+
}
293+
294+
func (rg refGrouper) Groups() []sizes.RefGroup {
295+
return nil
296+
}
297+
288298
func TestBomb(t *testing.T) {
289299
t.Parallel()
290300

@@ -295,7 +305,7 @@ func TestBomb(t *testing.T) {
295305

296306
h, err := sizes.ScanRepositoryUsingGraph(
297307
repo.Repository(t),
298-
git.AllReferencesFilter, sizes.NameStyleFull, false,
308+
refGrouper{}, sizes.NameStyleFull, false,
299309
)
300310
require.NoError(t, err)
301311

@@ -368,7 +378,7 @@ func TestTaggedTags(t *testing.T) {
368378

369379
h, err := sizes.ScanRepositoryUsingGraph(
370380
repo.Repository(t),
371-
git.AllReferencesFilter, sizes.NameStyleNone, false,
381+
refGrouper{}, sizes.NameStyleNone, false,
372382
)
373383
require.NoError(t, err, "scanning repository")
374384
assert.Equal(t, counts.Count32(3), h.MaxTagDepth, "tag depth")
@@ -390,7 +400,7 @@ func TestFromSubdir(t *testing.T) {
390400

391401
h, err := sizes.ScanRepositoryUsingGraph(
392402
repo.Repository(t),
393-
git.AllReferencesFilter, sizes.NameStyleNone, false,
403+
refGrouper{}, sizes.NameStyleNone, false,
394404
)
395405
require.NoError(t, err, "scanning repository")
396406
assert.Equal(t, counts.Count32(2), h.MaxPathDepth, "max path depth")
@@ -443,7 +453,7 @@ func TestSubmodule(t *testing.T) {
443453
// Analyze the main repo:
444454
h, err := sizes.ScanRepositoryUsingGraph(
445455
mainRepo.Repository(t),
446-
git.AllReferencesFilter, sizes.NameStyleNone, false,
456+
refGrouper{}, sizes.NameStyleNone, false,
447457
)
448458
require.NoError(t, err, "scanning repository")
449459
assert.Equal(t, counts.Count32(2), h.UniqueBlobCount, "unique blob count")
@@ -456,7 +466,7 @@ func TestSubmodule(t *testing.T) {
456466
}
457467
h, err = sizes.ScanRepositoryUsingGraph(
458468
submRepo2.Repository(t),
459-
git.AllReferencesFilter, sizes.NameStyleNone, false,
469+
refGrouper{}, sizes.NameStyleNone, false,
460470
)
461471
require.NoError(t, err, "scanning repository")
462472
assert.Equal(t, counts.Count32(2), h.UniqueBlobCount, "unique blob count")

sizes/graph.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,36 @@ import (
1313
"github.com/github/git-sizer/meter"
1414
)
1515

16+
type RefGroupSymbol string
17+
18+
// RefGroup is a group of references, for example "branches" or
19+
// "tags". Reference groups might overlap.
20+
type RefGroup struct {
21+
// Symbol is the unique string by which this `RefGroup` is
22+
// identified and configured. It consists of dot-separated
23+
// components, which implicitly makes a nested tree-like
24+
// structure.
25+
Symbol RefGroupSymbol
26+
27+
// Name is the name for this `ReferenceGroup` to be presented
28+
// in user-readable output.
29+
Name string
30+
}
31+
32+
type RefGrouper interface {
33+
// Categorize tells whether `refname` should be walked at all,
34+
// and if so, the symbols of the reference groups to which it
35+
// belongs.
36+
Categorize(refname string) (bool, []RefGroupSymbol)
37+
38+
// Groups returns the list of `ReferenceGroup`s, in the order
39+
// that they should be presented. The return value might
40+
// depend on which references have been seen so far.
41+
Groups() []RefGroup
42+
}
43+
1644
func ScanRepositoryUsingGraph(
17-
repo *git.Repository, filter git.ReferenceFilter, nameStyle NameStyle, progress bool,
45+
repo *git.Repository, rg RefGrouper, nameStyle NameStyle, progress bool,
1846
) (HistorySize, error) {
1947
graph := NewGraph(nameStyle)
2048
var progressMeter meter.Progress
@@ -62,7 +90,9 @@ func ScanRepositoryUsingGraph(
6290
if !ok {
6391
break
6492
}
65-
if !filter.Filter(ref.Refname) {
93+
94+
walk, _ := rg.Categorize(ref.Refname)
95+
if !walk {
6696
continue
6797
}
6898
refs = append(refs, ref)

0 commit comments

Comments
 (0)