Skip to content

Commit 37ddc22

Browse files
authored
Merge pull request #88 from github/remove-lint
Do a bunch of linter-inspired cleanups
2 parents e0ec350 + e6f8d99 commit 37ddc22

18 files changed

+366
-181
lines changed

counts/counts.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ import (
44
"math"
55
)
66

7-
// A count of something, capped at math.MaxUint32.
7+
// Count32 is a count of something, capped at math.MaxUint32.
88
type Count32 uint32
99

10+
// NewCount32 initializes a Count32 from a uint64, capped at
11+
// math.MaxUint32.
1012
func NewCount32(n uint64) Count32 {
1113
if n > math.MaxUint32 {
1214
return Count32(math.MaxUint32)
1315
}
1416
return Count32(n)
1517
}
1618

19+
// ToUint64 returns the value of `n` as a `uint64`. If the value has
20+
// overflowed, it returns `(math.MaxUint32, true)`.
1721
func (n Count32) ToUint64() (uint64, bool) {
1822
return uint64(n), n == math.MaxUint32
1923
}
2024

21-
// Return the sum of two Count32s, capped at math.MaxUint32.
25+
// Plus returns the sum of two Count32s, capped at math.MaxUint32.
2226
func (n1 Count32) Plus(n2 Count32) Count32 {
2327
n := n1 + n2
2428
if n < n1 {
@@ -28,45 +32,48 @@ func (n1 Count32) Plus(n2 Count32) Count32 {
2832
return n
2933
}
3034

31-
// Increment `*n1` by `n2`, capped at math.MaxUint32.
35+
// Increment increases `*n1` by `n2`, capped at math.MaxUint32.
3236
func (n1 *Count32) Increment(n2 Count32) {
3337
*n1 = n1.Plus(n2)
3438
}
3539

3640
// AdjustMaxIfNecessary adjusts `*n1` to be `max(*n1, n2)`. Return
3741
// true iff `n2` was greater than `*n1`.
3842
func (n1 *Count32) AdjustMaxIfNecessary(n2 Count32) bool {
39-
if n2 > *n1 {
40-
*n1 = n2
41-
return true
42-
} else {
43+
if n2 <= *n1 {
4344
return false
4445
}
46+
47+
*n1 = n2
48+
return true
4549
}
4650

4751
// AdjustMaxIfPossible adjusts `*n1` to be `max(*n1, n2)`. Return true
4852
// iff `n2` was greater than or equal to `*n1`.
4953
func (n1 *Count32) AdjustMaxIfPossible(n2 Count32) bool {
50-
if n2 >= *n1 {
51-
*n1 = n2
52-
return true
53-
} else {
54+
if n2 < *n1 {
5455
return false
5556
}
57+
58+
*n1 = n2
59+
return true
5660
}
5761

58-
// A count of something, capped at math.MaxUint64.
62+
// Count64 is a count of something, capped at math.MaxUint64.
5963
type Count64 uint64
6064

65+
// NewCount64 initializes a Count64 from a uint64.
6166
func NewCount64(n uint64) Count64 {
6267
return Count64(n)
6368
}
6469

70+
// ToUint64 returns the value of `n` as a `uint64`. If the value has
71+
// overflowed, it returns `(math.MaxUint64, true)`.
6572
func (n Count64) ToUint64() (uint64, bool) {
6673
return uint64(n), n == math.MaxUint64
6774
}
6875

69-
// Return the sum of two Count64s, capped at math.MaxUint64.
76+
// Plus returns the sum of two Count64s, capped at math.MaxUint64.
7077
func (n1 Count64) Plus(n2 Count64) Count64 {
7178
n := n1 + n2
7279
if n < n1 {
@@ -76,29 +83,29 @@ func (n1 Count64) Plus(n2 Count64) Count64 {
7683
return n
7784
}
7885

79-
// Increment `*n1` by `n2`, capped at math.MaxUint64.
86+
// Increment increases `*n1` by `n2`, capped at math.MaxUint64.
8087
func (n1 *Count64) Increment(n2 Count64) {
8188
*n1 = n1.Plus(n2)
8289
}
8390

8491
// AdjustMaxIfNecessary adjusts `*n1` to be `max(*n1, n2)`. Return
8592
// true iff `n2` was greater than `*n1`.
8693
func (n1 *Count64) AdjustMaxIfNecessary(n2 Count64) bool {
87-
if n2 > *n1 {
88-
*n1 = n2
89-
return true
90-
} else {
94+
if n2 <= *n1 {
9195
return false
9296
}
97+
98+
*n1 = n2
99+
return true
93100
}
94101

95102
// AdjustMaxIfPossible adjusts `*n1` to be `max(*n1, n2)`. Return true
96103
// iff `n2` was greater than or equal to `*n1`.
97104
func (n1 *Count64) AdjustMaxIfPossible(n2 Count64) bool {
98-
if n2 > *n1 {
99-
*n1 = n2
100-
return true
101-
} else {
105+
if n2 <= *n1 {
102106
return false
103107
}
108+
109+
*n1 = n2
110+
return true
104111
}

counts/human.go

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@ import (
44
"fmt"
55
)
66

7-
// A quantity that can be made human-readable using Human().
7+
// Humanable is a quantity that can be made human-readable using
8+
// `Humaner.Format()`.
89
type Humanable interface {
9-
// Return the value as a uint64, and a boolean telling whether it
10-
// overflowed.
10+
// ToUint64 returns the value as a uint64, and a boolean telling
11+
// whether it overflowed.
1112
ToUint64() (uint64, bool)
1213
}
1314

14-
// An object that can format a Humanable in human-readable format.
15+
// Humaner is an object that can format a Humanable in human-readable
16+
// format.
1517
type Humaner struct {
1618
name string
1719
prefixes []Prefix
1820
}
1921

22+
// Prefix is a metric-like prefix that implies a scaling factor.
2023
type Prefix struct {
2124
Name string
2225
Multiplier uint64
2326
}
2427

28+
// Metric is a Humaner representing metric prefixes.
2529
var Metric = Humaner{
2630
name: "metric",
2731
prefixes: []Prefix{
@@ -34,6 +38,8 @@ var Metric = Humaner{
3438
},
3539
}
3640

41+
// Binary is a Humaner representing power-of-1024 based prefixes,
42+
// typically used for bytes.
3743
var Binary = Humaner{
3844
name: "binary",
3945
prefixes: []Prefix{
@@ -46,13 +52,15 @@ var Binary = Humaner{
4652
},
4753
}
4854

55+
// Name returns the name of `h` ("metric" or "binary").
4956
func (h *Humaner) Name() string {
5057
return h.name
5158
}
5259

53-
// Format n, aligned, in `len(unit) + 10` or fewer characters (except
54-
// for extremely large numbers).
55-
func (h *Humaner) FormatNumber(n uint64, unit string) (string, string) {
60+
// FormatNumber formats n, aligned, in `len(unit) + 10` or fewer
61+
// characters (except for extremely large numbers). It returns strings
62+
// representing the numeral and the unit string.
63+
func (h *Humaner) FormatNumber(n uint64, unit string) (numeral string, unitString string) {
5664
prefix := h.prefixes[0]
5765

5866
wholePart := n
@@ -66,25 +74,28 @@ func (h *Humaner) FormatNumber(n uint64, unit string) (string, string) {
6674

6775
if prefix.Multiplier == 1 {
6876
return fmt.Sprintf("%d", n), unit
69-
} else {
70-
mantissa := float64(n) / float64(prefix.Multiplier)
71-
var format string
77+
}
7278

73-
if wholePart >= 100 {
74-
// `mantissa` can actually be up to 1023.999.
75-
format = "%.0f"
76-
} else if wholePart >= 10 {
77-
format = "%.1f"
78-
} else {
79-
format = "%.2f"
80-
}
81-
return fmt.Sprintf(format, mantissa), prefix.Name + unit
79+
mantissa := float64(n) / float64(prefix.Multiplier)
80+
var format string
81+
82+
switch {
83+
case wholePart >= 100:
84+
// `mantissa` can actually be up to 1023.999.
85+
format = "%.0f"
86+
case wholePart >= 10:
87+
format = "%.1f"
88+
default:
89+
format = "%.2f"
8290
}
91+
92+
return fmt.Sprintf(format, mantissa), prefix.Name + unit
8393
}
8494

85-
// Format values, aligned, in `len(unit) + 10` or fewer characters
86-
// (except for extremely large numbers).
87-
func (h *Humaner) Format(value Humanable, unit string) (string, string) {
95+
// Format formats values, aligned, in `len(unit) + 10` or fewer
96+
// characters (except for extremely large numbers). It returns strings
97+
// representing the numeral and the unit string.
98+
func (h *Humaner) Format(value Humanable, unit string) (numeral string, unitString string) {
8899
n, overflow := value.ToUint64()
89100
if overflow {
90101
return "∞", unit

git-sizer.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/github/git-sizer/sizes"
1818
)
1919

20-
const Usage = `usage: git-sizer [OPTS]
20+
const usage = `usage: git-sizer [OPTS]
2121
2222
--threshold THRESHOLD minimum level of concern (i.e., number of stars)
2323
that should be reported. Default:
@@ -110,13 +110,10 @@ func mainImplementation(args []string) error {
110110
// Try to open the repository, but it's not an error yet if this
111111
// fails, because the user might only be asking for `--help`.
112112
repo, repoErr := git.NewRepository(".")
113-
if repoErr == nil {
114-
defer repo.Close()
115-
}
116113

117114
flags := pflag.NewFlagSet("git-sizer", pflag.ContinueOnError)
118115
flags.Usage = func() {
119-
fmt.Print(Usage)
116+
fmt.Print(usage)
120117
}
121118

122119
flags.VarP(
@@ -164,7 +161,9 @@ func mainImplementation(args []string) error {
164161
flags.Lookup("no-progress").NoOptDefVal = "true"
165162

166163
flags.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
167-
flags.MarkHidden("cpuprofile")
164+
if err := flags.MarkHidden("cpuprofile"); err != nil {
165+
return fmt.Errorf("marking option hidden: %w", err)
166+
}
168167

169168
var configger refopts.Configger
170169
if repo != nil {
@@ -191,9 +190,11 @@ func mainImplementation(args []string) error {
191190
if cpuprofile != "" {
192191
f, err := os.Create(cpuprofile)
193192
if err != nil {
194-
return fmt.Errorf("couldn't set up cpuprofile file: %s", err)
193+
return fmt.Errorf("couldn't set up cpuprofile file: %w", err)
194+
}
195+
if err := pprof.StartCPUProfile(f); err != nil {
196+
return fmt.Errorf("starting CPU profiling: %w", err)
195197
}
196-
pprof.StartCPUProfile(f)
197198
defer pprof.StopCPUProfile()
198199
}
199200

@@ -211,7 +212,7 @@ func mainImplementation(args []string) error {
211212
}
212213

213214
if repoErr != nil {
214-
return fmt.Errorf("couldn't open Git repository: %s", repoErr)
215+
return fmt.Errorf("couldn't open Git repository: %w", repoErr)
215216
}
216217

217218
if jsonOutput {
@@ -270,7 +271,7 @@ func mainImplementation(args []string) error {
270271

271272
historySize, err := sizes.ScanRepositoryUsingGraph(repo, rg, nameStyle, progress)
272273
if err != nil {
273-
return fmt.Errorf("error scanning repository: %s", err)
274+
return fmt.Errorf("error scanning repository: %w", err)
274275
}
275276

276277
if jsonOutput {
@@ -285,14 +286,16 @@ func mainImplementation(args []string) error {
285286
return fmt.Errorf("JSON version must be 1 or 2")
286287
}
287288
if err != nil {
288-
return fmt.Errorf("could not convert %v to json: %s", historySize, err)
289+
return fmt.Errorf("could not convert %v to json: %w", historySize, err)
289290
}
290291
fmt.Printf("%s\n", j)
291292
} else {
292-
io.WriteString(
293+
if _, err := io.WriteString(
293294
os.Stdout,
294295
historySize.TableString(rg.Groups(), threshold, nameStyle),
295-
)
296+
); err != nil {
297+
return fmt.Errorf("writing output: %w", err)
298+
}
296299
}
297300

298301
return nil

0 commit comments

Comments
 (0)