Skip to content
6 changes: 5 additions & 1 deletion git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,17 @@
// batch mode to efficiently retrieve their sizes.
func (repo *Repository) GetUnreachableStats() (UnreachableStats, error) {
// Run git fsck. Using CombinedOutput captures both stdout and stderr.
cmd := exec.Command(repo.gitBin, "-C", repo.gitDir, "fsck", "--unreachable", "--no-reflogs", "--full")
gitDir, err := repo.GitDir()
if err != nil {
return UnreachableStats{Count: 0, Size: 0}, fmt.Errorf("failed to retrieve Git directory: %w", err)
}
cmd := exec.Command(repo.gitBin, "-C", gitDir, "fsck", "--unreachable", "--no-reflogs", "--full")

Check failure on line 196 in git/git.go

View workflow job for this annotation

GitHub Actions / lint

G204: Subprocess launched with a potential tainted input or cmd arguments (gosec)
cmd.Env = os.Environ()
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "An error occurred trying to process unreachable objects.")
os.Stderr.Write(output)

Check failure on line 202 in git/git.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.Stderr.Write` is not checked (errcheck)
fmt.Fprintln(os.Stderr)
return UnreachableStats{Count: 0, Size: 0}, err
}
Expand Down Expand Up @@ -225,7 +229,7 @@
// the provided OIDs. It writes each OID to stdin and reads back lines in the
// format: "<oid> <type> <size>".
func (repo *Repository) getTotalSizeFromOids(oids []string) (int64, error) {
cmd := exec.Command(repo.gitBin, "-C", repo.gitDir, "cat-file", "--batch-check")

Check failure on line 232 in git/git.go

View workflow job for this annotation

GitHub Actions / lint

G204: Subprocess launched with a potential tainted input or cmd arguments (gosec)
stdinPipe, err := cmd.StdinPipe()
if err != nil {
return 0, fmt.Errorf("failed to get stdin pipe: %w", err)
Expand All @@ -241,9 +245,9 @@

// Write all OIDs to the batch process.
go func() {
defer stdinPipe.Close()

Check failure on line 248 in git/git.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `stdinPipe.Close` is not checked (errcheck)
for _, oid := range oids {
io.WriteString(stdinPipe, oid+"\n")

Check failure on line 250 in git/git.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `io.WriteString` is not checked (errcheck)
}
}()

Expand All @@ -254,7 +258,7 @@
parts := strings.Fields(scanner.Text())
if len(parts) == 3 {
var size int64
fmt.Sscanf(parts[2], "%d", &size)

Check failure on line 261 in git/git.go

View workflow job for this annotation

GitHub Actions / lint

G104: Errors unhandled. (gosec)
totalSize += size
} else {
return 0, fmt.Errorf("unexpected output format: %s", scanner.Text())
Expand Down
Loading