Skip to content

Commit 41cd01a

Browse files
committed
wip: Don't print referecence count when no reference is traversed
1 parent 721e353 commit 41cd01a

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

Diff for: git-sizer.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,18 @@ func mainImplementation(ctx context.Context, stdout, stderr io.Writer, args []st
343343
return fmt.Errorf("error scanning repository: %w", err)
344344
}
345345

346+
formatOptions := sizes.FormatOptions{
347+
WithoutReferenceCount: git.IsNoReferencesFilter(rgb.GetTopLevelGroup().GetFilter()),
348+
}
349+
346350
if jsonOutput {
347351
var j []byte
348352
var err error
349353
switch jsonVersion {
350354
case 1:
351355
j, err = json.MarshalIndent(historySize, "", " ")
352356
case 2:
353-
j, err = historySize.JSON(rg.Groups(), threshold, nameStyle)
357+
j, err = historySize.JSON(rg.Groups(), threshold, nameStyle, formatOptions)
354358
default:
355359
return fmt.Errorf("JSON version must be 1 or 2")
356360
}
@@ -360,7 +364,7 @@ func mainImplementation(ctx context.Context, stdout, stderr io.Writer, args []st
360364
fmt.Fprintf(stdout, "%s\n", j)
361365
} else {
362366
if _, err := io.WriteString(
363-
stdout, historySize.TableString(rg.Groups(), threshold, nameStyle),
367+
stdout, historySize.TableString(rg.Groups(), threshold, nameStyle, formatOptions),
364368
); err != nil {
365369
return fmt.Errorf("writing output: %w", err)
366370
}

Diff for: sizes/output.go

+36-27
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,22 @@ type section struct {
8686
contents []tableContents
8787
}
8888

89-
func newSection(name string, contents ...tableContents) *section {
90-
return &section{
89+
func newSection(name string, contents ...tableContents) section {
90+
return section{
9191
name: name,
9292
contents: contents,
9393
}
9494
}
9595

96-
func (s *section) Emit(t *table) {
96+
func (s section) Emit(t *table) {
9797
for _, c := range s.contents {
9898
subTable := t.subTable(s.name)
9999
c.Emit(subTable)
100100
t.addSection(subTable)
101101
}
102102
}
103103

104-
func (s *section) CollectItems(items map[string]*item) {
104+
func (s section) CollectItems(items map[string]*item) {
105105
for _, c := range s.contents {
106106
c.CollectItems(items)
107107
}
@@ -141,7 +141,7 @@ func newItem(
141141
}
142142
}
143143

144-
func (i *item) Emit(t *table) {
144+
func (i item) Emit(t *table) {
145145
levelOfConcern, interesting := i.levelOfConcern(t.threshold)
146146
if !interesting {
147147
return
@@ -154,7 +154,7 @@ func (i *item) Emit(t *table) {
154154
)
155155
}
156156

157-
func (i *item) Footnote(nameStyle NameStyle) string {
157+
func (i item) Footnote(nameStyle NameStyle) string {
158158
if i.path == nil || i.path.OID == git.NullOID {
159159
return ""
160160
}
@@ -173,7 +173,7 @@ func (i *item) Footnote(nameStyle NameStyle) string {
173173
// If this item's alert level is at least as high as the threshold,
174174
// return the string that should be used as its "level of concern" and
175175
// `true`; otherwise, return `"", false`.
176-
func (i *item) levelOfConcern(threshold Threshold) (string, bool) {
176+
func (i item) levelOfConcern(threshold Threshold) (string, bool) {
177177
value, overflow := i.value.ToUint64()
178178
if overflow {
179179
return "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", true
@@ -188,11 +188,11 @@ func (i *item) levelOfConcern(threshold Threshold) (string, bool) {
188188
return stars[:int(alert)], true
189189
}
190190

191-
func (i *item) CollectItems(items map[string]*item) {
192-
items[i.symbol] = i
191+
func (i item) CollectItems(items map[string]*item) {
192+
items[i.symbol] = &i
193193
}
194194

195-
func (i *item) MarshalJSON() ([]byte, error) {
195+
func (i item) MarshalJSON() ([]byte, error) {
196196
// How we want to emit an item as JSON.
197197
value, _ := i.value.ToUint64()
198198

@@ -224,7 +224,7 @@ func (i *item) MarshalJSON() ([]byte, error) {
224224

225225
// Indented returns an `item` that is just like `i`, but indented by
226226
// `depth` more levels.
227-
func (i *item) Indented(depth int) tableContents {
227+
func (i item) Indented(depth int) tableContents {
228228
return &indentedItem{
229229
tableContents: i,
230230
depth: depth,
@@ -236,7 +236,7 @@ type indentedItem struct {
236236
depth int
237237
}
238238

239-
func (i *indentedItem) Emit(t *table) {
239+
func (i indentedItem) Emit(t *table) {
240240
subTable := t.indented("", i.depth)
241241
i.tableContents.Emit(subTable)
242242
t.addSection(subTable)
@@ -373,8 +373,9 @@ type table struct {
373373

374374
func (s *HistorySize) TableString(
375375
refGroups []RefGroup, threshold Threshold, nameStyle NameStyle,
376+
opts FormatOptions,
376377
) string {
377-
contents := s.contents(refGroups)
378+
contents := s.contents(refGroups, opts)
378379
t := table{
379380
threshold: threshold,
380381
nameStyle: nameStyle,
@@ -454,17 +455,20 @@ func (t *table) formatRow(
454455
)
455456
}
456457

458+
type FormatOptions struct {
459+
WithoutReferenceCount bool
460+
}
461+
457462
func (s *HistorySize) JSON(
458-
refGroups []RefGroup, threshold Threshold, nameStyle NameStyle,
459-
) ([]byte, error) {
460-
contents := s.contents(refGroups)
463+
refGroups []RefGroup, threshold Threshold, nameStyle NameStyle, opts FormatOptions) ([]byte, error) {
464+
contents := s.contents(refGroups, opts)
461465
items := make(map[string]*item)
462466
contents.CollectItems(items)
463467
j, err := json.MarshalIndent(items, "", " ")
464468
return j, err
465469
}
466470

467-
func (s *HistorySize) contents(refGroups []RefGroup) tableContents {
471+
func (s *HistorySize) contents(refGroups []RefGroup, opts FormatOptions) tableContents {
468472
S := newSection
469473
I := newItem
470474
metric := counts.Metric
@@ -489,6 +493,20 @@ func (s *HistorySize) contents(refGroups []RefGroup) tableContents {
489493
rgis = append(rgis, rgi.Indented(indent))
490494
}
491495

496+
var refCountSection section
497+
if !opts.WithoutReferenceCount {
498+
refCountSection = S(
499+
"References",
500+
I("referenceCount", "Count",
501+
"The total number of references",
502+
nil, s.ReferenceCount, metric, "", 25e3),
503+
S(
504+
"",
505+
rgis...,
506+
),
507+
)
508+
}
509+
492510
return S(
493511
"",
494512
S(
@@ -533,16 +551,7 @@ func (s *HistorySize) contents(refGroups []RefGroup) tableContents {
533551
nil, s.UniqueTagCount, metric, "", 25e3),
534552
),
535553

536-
S(
537-
"References",
538-
I("referenceCount", "Count",
539-
"The total number of references",
540-
nil, s.ReferenceCount, metric, "", 25e3),
541-
S(
542-
"",
543-
rgis...,
544-
),
545-
),
554+
refCountSection,
546555
),
547556

548557
S("Biggest objects",

0 commit comments

Comments
 (0)