@@ -189,19 +189,12 @@ type UnreachableStats struct {
189189// batch mode to efficiently retrieve their sizes.
190190func (repo * Repository ) GetUnreachableStats () (UnreachableStats , error ) {
191191 // Run git fsck. Using CombinedOutput captures both stdout and stderr.
192- gitDir , err := repo .GitDir ()
192+ cmd := repo .GitCommand ("fsck" , "--unreachable" , "--no-reflogs" , "--full" )
193+ output , err := cmd .Output ()
193194 if err != nil {
194- return UnreachableStats {Count : 0 , Size : 0 }, fmt .Errorf ("failed to retrieve Git directory: %w" , err )
195- }
196- cmd := exec .Command (repo .gitBin , "-C" , gitDir , "fsck" , "--unreachable" , "--no-reflogs" , "--full" )
197- cmd .Env = os .Environ ()
198- output , err := cmd .CombinedOutput ()
199- if err != nil {
200- fmt .Fprintln (os .Stderr )
201- fmt .Fprintln (os .Stderr , "An error occurred trying to process unreachable objects." )
202- os .Stderr .Write (output )
203- fmt .Fprintln (os .Stderr )
204- return UnreachableStats {Count : 0 , Size : 0 }, err
195+ return UnreachableStats {Count : 0 , Size : 0 }, fmt .Errorf (
196+ "running 'git fsck --unreachable --no-reflogs --full': %w" , err ,
197+ )
205198 }
206199
207200 var oids []string
@@ -229,7 +222,7 @@ func (repo *Repository) GetUnreachableStats() (UnreachableStats, error) {
229222// the provided OIDs. It writes each OID to stdin and reads back lines in the
230223// format: "<oid> <type> <size>".
231224func (repo * Repository ) getTotalSizeFromOids (oids []string ) (int64 , error ) {
232- cmd := exec . Command ( repo .gitBin , "-C" , repo . gitDir , "cat-file" , "--batch-check" )
225+ cmd := repo .GitCommand ( "cat-file" , "--batch-check" )
233226 stdinPipe , err := cmd .StdinPipe ()
234227 if err != nil {
235228 return 0 , fmt .Errorf ("failed to get stdin pipe: %w" , err )
@@ -245,9 +238,16 @@ func (repo *Repository) getTotalSizeFromOids(oids []string) (int64, error) {
245238
246239 // Write all OIDs to the batch process.
247240 go func () {
248- defer stdinPipe .Close ()
241+ defer func () {
242+ if err := stdinPipe .Close (); err != nil {
243+ fmt .Fprintf (os .Stderr , "failed to close stdin pipe: %v\n " , err )
244+ }
245+ }()
249246 for _ , oid := range oids {
250- io .WriteString (stdinPipe , oid + "\n " )
247+ if _ , err := io .WriteString (stdinPipe , oid + "\n " ); err != nil {
248+ fmt .Fprintf (os .Stderr , "failed to write to stdin pipe: %v\n " , err )
249+ return
250+ }
251251 }
252252 }()
253253
@@ -258,7 +258,9 @@ func (repo *Repository) getTotalSizeFromOids(oids []string) (int64, error) {
258258 parts := strings .Fields (scanner .Text ())
259259 if len (parts ) == 3 {
260260 var size int64
261- fmt .Sscanf (parts [2 ], "%d" , & size )
261+ if _ , err := fmt .Sscanf (parts [2 ], "%d" , & size ); err != nil {
262+ return 0 , fmt .Errorf ("failed to parse size from output: %w" , err )
263+ }
262264 totalSize += size
263265 } else {
264266 return 0 , fmt .Errorf ("unexpected output format: %s" , scanner .Text ())
0 commit comments