Skip to content

Commit 9a9ad2b

Browse files
authored
Merge pull request #83 from github/gitconfig
Allow several options to be set via gitconfig
2 parents 615b309 + f25ea53 commit 9a9ad2b

File tree

2 files changed

+145
-15
lines changed

2 files changed

+145
-15
lines changed

git-sizer.go

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,28 @@ import (
1818

1919
const Usage = `usage: git-sizer [OPTS]
2020
21-
-v, --verbose report all statistics, whether concerning or not
2221
--threshold THRESHOLD minimum level of concern (i.e., number of stars)
2322
that should be reported. Default:
24-
'--threshold=1'.
25-
--critical only report critical statistics
23+
'--threshold=1'. Can be set via gitconfig:
24+
'sizer.threshold'.
25+
-v, --verbose report all statistics, whether concerning or
26+
not; equivalent to '--threshold=0
27+
--no-verbose equivalent to '--threshold=1'
28+
--critical only report critical statistics; equivalent
29+
to '--threshold=30'
2630
--names=[none|hash|full] display names of large objects in the specified
27-
style: 'none' (omit footnotes entirely), 'hash'
28-
(show only the SHA-1s of objects), or 'full'
29-
(show full names). Default is '--names=full'.
31+
style. Values:
32+
* 'none' - omit footnotes entirely
33+
* 'hash' - show only the SHA-1s of objects
34+
* 'full' - show full names
35+
Default is '--names=full'. Can be set via
36+
gitconfig: 'sizer.names'.
3037
-j, --json output results in JSON format
3138
--json-version=[1|2] choose which JSON format version to output.
32-
Default: --json-version=1.
33-
--[no-]progress report (don't report) progress to stderr.
39+
Default: --json-version=1. Can be set via
40+
gitconfig: 'sizer.jsonVersion'.
41+
--[no-]progress report (don't report) progress to stderr. Can
42+
be set via gitconfig: 'sizer.progress'.
3443
--version only report the git-sizer version number
3544
3645
Reference selection:
@@ -164,8 +173,8 @@ func mainImplementation(args []string) error {
164173
var nameStyle sizes.NameStyle = sizes.NameStyleFull
165174
var cpuprofile string
166175
var jsonOutput bool
167-
var jsonVersion uint
168-
var threshold sizes.Threshold = 1
176+
var jsonVersion int
177+
var threshold sizes.Threshold
169178
var progress bool
170179
var version bool
171180
var filter git.IncludeExcludeFilter
@@ -217,6 +226,12 @@ func mainImplementation(args []string) error {
217226
)
218227
flags.Lookup("verbose").NoOptDefVal = "true"
219228

229+
flags.Var(
230+
sizes.NewThresholdFlagValue(&threshold, 1),
231+
"no-verbose", "report statistics that are at all concerning",
232+
)
233+
flags.Lookup("no-verbose").NoOptDefVal = "true"
234+
220235
flags.Var(
221236
&threshold, "threshold",
222237
"minimum level of concern (i.e., number of stars) that should be\n"+
@@ -238,7 +253,7 @@ func mainImplementation(args []string) error {
238253
)
239254

240255
flags.BoolVarP(&jsonOutput, "json", "j", false, "output results in JSON format")
241-
flags.UintVar(&jsonVersion, "json-version", 1, "JSON format version to output (1 or 2)")
256+
flags.IntVar(&jsonVersion, "json-version", 1, "JSON format version to output (1 or 2)")
242257

243258
atty, err := isatty.Isatty(os.Stderr.Fd())
244259
if err != nil {
@@ -263,10 +278,6 @@ func mainImplementation(args []string) error {
263278
return err
264279
}
265280

266-
if jsonOutput && !(jsonVersion == 1 || jsonVersion == 2) {
267-
return fmt.Errorf("JSON version must be 1 or 2")
268-
}
269-
270281
if cpuprofile != "" {
271282
f, err := os.Create(cpuprofile)
272283
if err != nil {
@@ -295,6 +306,55 @@ func mainImplementation(args []string) error {
295306
}
296307
defer repo.Close()
297308

309+
if jsonOutput {
310+
if !flags.Changed("json-version") {
311+
v, err := repo.ConfigIntDefault("sizer.jsonVersion", jsonVersion)
312+
if err != nil {
313+
return err
314+
}
315+
jsonVersion = v
316+
if !(jsonVersion == 1 || jsonVersion == 2) {
317+
return fmt.Errorf("JSON version (read from gitconfig) must be 1 or 2")
318+
}
319+
} else if !(jsonVersion == 1 || jsonVersion == 2) {
320+
return fmt.Errorf("JSON version must be 1 or 2")
321+
}
322+
}
323+
324+
if !flags.Changed("threshold") &&
325+
!flags.Changed("verbose") &&
326+
!flags.Changed("no-verbose") &&
327+
!flags.Changed("critical") {
328+
s, err := repo.ConfigStringDefault("sizer.threshold", fmt.Sprintf("%g", threshold))
329+
if err != nil {
330+
return err
331+
}
332+
v, err := strconv.ParseFloat(s, 64)
333+
if err != nil {
334+
return fmt.Errorf("parsing gitconfig value for 'sizer.threshold': %w", err)
335+
}
336+
threshold = sizes.Threshold(v)
337+
}
338+
339+
if !flags.Changed("names") {
340+
s, err := repo.ConfigStringDefault("sizer.names", "full")
341+
if err != nil {
342+
return err
343+
}
344+
err = nameStyle.Set(s)
345+
if err != nil {
346+
return fmt.Errorf("parsing gitconfig value for 'sizer.names': %w", err)
347+
}
348+
}
349+
350+
if !flags.Changed("progress") && !flags.Changed("no-progress") {
351+
v, err := repo.ConfigBoolDefault("sizer.progress", progress)
352+
if err != nil {
353+
return fmt.Errorf("parsing gitconfig value for 'sizer.progress': %w", err)
354+
}
355+
progress = v
356+
}
357+
298358
var historySize sizes.HistorySize
299359

300360
var refFilter git.ReferenceFilter = filter.Filter

git/gitconfig.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package git
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"strconv"
7+
)
8+
9+
func (repo *Repository) ConfigStringDefault(key string, defaultValue string) (string, error) {
10+
cmd := repo.gitCommand(
11+
"config",
12+
"--default", defaultValue,
13+
key,
14+
)
15+
16+
out, err := cmd.Output()
17+
if err != nil {
18+
return defaultValue, fmt.Errorf("running 'git config': %w", err)
19+
}
20+
21+
if len(out) > 0 && out[len(out)-1] == '\n' {
22+
out = out[:len(out)-1]
23+
}
24+
25+
return string(out), nil
26+
}
27+
28+
func (repo *Repository) ConfigBoolDefault(key string, defaultValue bool) (bool, error) {
29+
cmd := repo.gitCommand(
30+
"config",
31+
"--type", "bool",
32+
"--default", strconv.FormatBool(defaultValue),
33+
key,
34+
)
35+
36+
out, err := cmd.Output()
37+
if err != nil {
38+
return defaultValue, fmt.Errorf("running 'git config': %w", err)
39+
}
40+
41+
s := string(bytes.TrimSpace(out))
42+
value, err := strconv.ParseBool(s)
43+
if err != nil {
44+
return defaultValue, fmt.Errorf("unexpected bool value from 'git config': %q", s)
45+
}
46+
47+
return value, nil
48+
}
49+
50+
func (repo *Repository) ConfigIntDefault(key string, defaultValue int) (int, error) {
51+
cmd := repo.gitCommand(
52+
"config",
53+
"--type", "int",
54+
"--default", strconv.Itoa(defaultValue),
55+
key,
56+
)
57+
58+
out, err := cmd.Output()
59+
if err != nil {
60+
return defaultValue, fmt.Errorf("running 'git config': %w", err)
61+
}
62+
63+
s := string(bytes.TrimSpace(out))
64+
value, err := strconv.Atoi(s)
65+
if err != nil {
66+
return defaultValue, fmt.Errorf("unexpected int value from 'git config': %q", s)
67+
}
68+
69+
return value, nil
70+
}

0 commit comments

Comments
 (0)