fix: make mage fmt skip gitignored files

Use git ls-files instead of filepath.Walk to collect Go files,
so that gitignored files are no longer formatted.
This commit is contained in:
kolaente 2026-03-19 10:06:07 +01:00
parent 3bc0093686
commit e74265d921
No known key found for this signature in database
GPG Key ID: F40E70337AB24C9B
1 changed files with 6 additions and 12 deletions

View File

@ -27,7 +27,6 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"math"
"net"
"net/http"
@ -385,18 +384,13 @@ func waitForHTTP(ctx context.Context, url string, timeout time.Duration) error {
// Fmt formats the code using go fmt
func Fmt(ctx context.Context) error {
mg.Deps(initVars)
var goFiles []string
err := filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && filepath.Ext(path) == ".go" {
goFiles = append(goFiles, path)
}
return nil
})
out, err := exec.CommandContext(ctx, "git", "ls-files", "--cached", "--others", "--exclude-standard", "*.go").Output()
if err != nil {
return err
return fmt.Errorf("failed to list go files from git: %w", err)
}
goFiles := strings.Split(strings.TrimSpace(string(out)), "\n")
if len(goFiles) == 0 || (len(goFiles) == 1 && goFiles[0] == "") {
return nil
}
args := append([]string{"-s", "-w"}, goFiles...)
return runAndStreamOutput(ctx, "gofmt", args...)