@@ -8,12 +8,14 @@ import (
88 "os"
99 "runtime/pprof"
1010 "strconv"
11+ "time"
1112
1213 "github.com/spf13/pflag"
1314
1415 "github.com/github/git-sizer/git"
1516 "github.com/github/git-sizer/internal/refopts"
1617 "github.com/github/git-sizer/isatty"
18+ "github.com/github/git-sizer/meter"
1719 "github.com/github/git-sizer/sizes"
1820)
1921
@@ -91,29 +93,30 @@ var ReleaseVersion string
9193var BuildVersion string
9294
9395func main () {
94- err := mainImplementation (os .Args [1 :])
96+ err := mainImplementation (os .Stdout , os . Stderr , os . Args [1 :])
9597 if err != nil {
9698 fmt .Fprintf (os .Stderr , "error: %s\n " , err )
9799 os .Exit (1 )
98100 }
99101}
100102
101- func mainImplementation (args []string ) error {
103+ func mainImplementation (stdout , stderr io. Writer , args []string ) error {
102104 var nameStyle sizes.NameStyle = sizes .NameStyleFull
103105 var cpuprofile string
104106 var jsonOutput bool
105107 var jsonVersion int
106108 var threshold sizes.Threshold = 1
107109 var progress bool
108110 var version bool
111+ var showRefs bool
109112
110113 // Try to open the repository, but it's not an error yet if this
111114 // fails, because the user might only be asking for `--help`.
112115 repo , repoErr := git .NewRepository ("." )
113116
114117 flags := pflag .NewFlagSet ("git-sizer" , pflag .ContinueOnError )
115118 flags .Usage = func () {
116- fmt .Print ( usage )
119+ fmt .Fprint ( stdout , usage )
117120 }
118121
119122 flags .VarP (
@@ -151,11 +154,15 @@ func mainImplementation(args []string) error {
151154 flags .BoolVarP (& jsonOutput , "json" , "j" , false , "output results in JSON format" )
152155 flags .IntVar (& jsonVersion , "json-version" , 1 , "JSON format version to output (1 or 2)" )
153156
154- atty , err := isatty .Isatty (os .Stderr .Fd ())
155- if err != nil {
156- atty = false
157+ defaultProgress := false
158+ if f , ok := stderr .(* os.File ); ok {
159+ atty , err := isatty .Isatty (f .Fd ())
160+ if err == nil && atty {
161+ defaultProgress = true
162+ }
157163 }
158- flags .BoolVar (& progress , "progress" , atty , "report progress to stderr" )
164+
165+ flags .BoolVar (& progress , "progress" , defaultProgress , "report progress to stderr" )
159166 flags .BoolVar (& version , "version" , false , "report the git-sizer version number" )
160167 flags .Var (& NegatedBoolValue {& progress }, "no-progress" , "suppress progress output" )
161168 flags .Lookup ("no-progress" ).NoOptDefVal = "true"
@@ -177,6 +184,8 @@ func mainImplementation(args []string) error {
177184
178185 rgb .AddRefopts (flags )
179186
187+ flags .BoolVar (& showRefs , "show-refs" , false , "list the references being processed" )
188+
180189 flags .SortFlags = false
181190
182191 err = flags .Parse (args )
@@ -200,9 +209,9 @@ func mainImplementation(args []string) error {
200209
201210 if version {
202211 if ReleaseVersion != "" {
203- fmt .Printf ( "git-sizer release %s\n " , ReleaseVersion )
212+ fmt .Fprintf ( stdout , "git-sizer release %s\n " , ReleaseVersion )
204213 } else {
205- fmt .Printf ( "git-sizer build %s\n " , BuildVersion )
214+ fmt .Fprintf ( stdout , "git-sizer build %s\n " , BuildVersion )
206215 }
207216 return nil
208217 }
@@ -269,7 +278,17 @@ func mainImplementation(args []string) error {
269278 return err
270279 }
271280
272- historySize , err := sizes .ScanRepositoryUsingGraph (repo , rg , nameStyle , progress )
281+ if showRefs {
282+ fmt .Fprintf (stderr , "References (included references marked with '+'):\n " )
283+ rg = refopts .NewShowRefGrouper (rg , stderr )
284+ }
285+
286+ var progressMeter meter.Progress = meter .NoProgressMeter
287+ if progress {
288+ progressMeter = meter .NewProgressMeter (stderr , 100 * time .Millisecond )
289+ }
290+
291+ historySize , err := sizes .ScanRepositoryUsingGraph (repo , rg , nameStyle , progressMeter )
273292 if err != nil {
274293 return fmt .Errorf ("error scanning repository: %w" , err )
275294 }
@@ -288,11 +307,10 @@ func mainImplementation(args []string) error {
288307 if err != nil {
289308 return fmt .Errorf ("could not convert %v to json: %w" , historySize , err )
290309 }
291- fmt .Printf ( "%s\n " , j )
310+ fmt .Fprintf ( stdout , "%s\n " , j )
292311 } else {
293312 if _ , err := io .WriteString (
294- os .Stdout ,
295- historySize .TableString (rg .Groups (), threshold , nameStyle ),
313+ stdout , historySize .TableString (rg .Groups (), threshold , nameStyle ),
296314 ); err != nil {
297315 return fmt .Errorf ("writing output: %w" , err )
298316 }
0 commit comments