Skip to content

Commit

Permalink
fix: duplicate processing in ordered formatters (#24)
Browse files Browse the repository at this point in the history
Fixes a bug with formatters processing paths out of order.

Signed-off-by: Brian McGee <brian@bmcgee.ie>

Reviewed-on: https://git.numtide.com/numtide/treefmt/pulls/24
Co-authored-by: Brian McGee <brian@bmcgee.ie>
Co-committed-by: Brian McGee <brian@bmcgee.ie>
  • Loading branch information
brianmcgee authored and Brian McGee committed Jan 12, 2024
1 parent c7d0138 commit 15db7f4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/cli/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestDependencyCycle(t *testing.T) {
})

_, err := cmd(t, "--config-file", configPath, "--tree-root", tempDir)
as.ErrorContains(err, "formatter cycle detected a -> b -> c")
as.ErrorContains(err, "formatter cycle detected")
}

func TestSpecifyingFormatters(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions internal/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ func (f *Formatter) SetChild(formatter *Formatter) {
// Wants is used to test if a Formatter wants path based on it's configured Includes and Excludes patterns.
// Returns true if the Formatter should be applied to path, false otherwise.
func (f *Formatter) Wants(path string) bool {
if f.parent != nil {
// we don't accept this path directly, our parent will forward it
return false
}
match := !PathMatches(path, f.excludes) && PathMatches(path, f.includes)
if match {
f.log.Debugf("match: %v", path)
Expand Down
6 changes: 2 additions & 4 deletions internal/walk/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package walk
import (
"context"
"fmt"
"github.com/go-git/go-git/v5"
"os"
"path/filepath"

"github.com/go-git/go-git/v5"
)

type gitWalker struct {
Expand All @@ -18,14 +19,12 @@ func (g *gitWalker) Root() string {
}

func (g *gitWalker) Walk(ctx context.Context, fn filepath.WalkFunc) error {

idx, err := g.repo.Storer.Index()
if err != nil {
return fmt.Errorf("%w: failed to open index", err)
}

for _, entry := range idx.Entries {

select {
case <-ctx.Done():
return ctx.Err()
Expand All @@ -38,7 +37,6 @@ func (g *gitWalker) Walk(ctx context.Context, fn filepath.WalkFunc) error {
return err
}
}

}

return nil
Expand Down
2 changes: 1 addition & 1 deletion nix/packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

nativeBuildInputs =
# we need some formatters available for the tests
(import ./formatters.nix pkgs);
import ./formatters.nix pkgs;

preCheck = ''
XDG_CACHE_HOME=$(mktemp -d)
Expand Down

0 comments on commit 15db7f4

Please sign in to comment.