Skip to content

Commit da82b80

Browse files
committed
feat: support --no-cache
Signed-off-by: Brian McGee <[email protected]> diff --git a/cli/cli.go b/cli/cli.go index 8b23262..b370ee7 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -11,6 +11,7 @@ var Cli = Format{} type Format struct { AllowMissingFormatter bool `default:"false" help:"Do not exit with error if a configured formatter is missing"` WorkingDirectory kong.ChangeDirFlag `default:"." short:"C" help:"Run as if treefmt was started in the specified working directory instead of the current working directory"` + NoCache bool `help:"Ignore the evaluation cache entirely. Useful for CI"` ClearCache bool `short:"c" help:"Reset the evaluation cache. Use in case the cache is not precise enough"` ConfigFile string `type:"existingfile" default:"./treefmt.toml"` FailOnChange bool `help:"Exit with error if any changes were made. Useful for CI."` diff --git a/cli/format.go b/cli/format.go index 6c46096..14ac16c 100644 --- a/cli/format.go +++ b/cli/format.go @@ -5,6 +5,7 @@ import ( "context" "errors" "fmt" + "io/fs" "os" "os/signal" "strings" @@ -168,6 +169,20 @@ func (f *Format) Run() error { var changes int + processBatch := func() error { + if Cli.NoCache { + changes += len(batch) + } else { + count, err := cache.Update(batch) + if err != nil { + return err + } + changes += count + } + batch = batch[:0] + return nil + } + LOOP: for { select { @@ -179,22 +194,17 @@ func (f *Format) Run() error { } batch = append(batch, path) if len(batch) == batchSize { - count, err := cache.Update(batch) - if err != nil { + if err = processBatch(); err != nil { return err } - changes += count - batch = batch[:0] } } } // final flush - count, err := cache.Update(batch) - if err != nil { + if err = processBatch(); err != nil { return err } - changes += count if Cli.FailOnChange && changes != 0 { return ErrFailOnChange @@ -251,6 +261,22 @@ func (f *Format) Run() error { } defer close(pathsCh) + + if Cli.NoCache { + return walker.Walk(ctx, func(path string, info fs.FileInfo, err error) error { + select { + case <-ctx.Done(): + return ctx.Err() + default: + // ignore symlinks and directories + if !(info.IsDir() || info.Mode()&os.ModeSymlink == os.ModeSymlink) { + pathsCh <- path + } + return nil + } + }) + } + return cache.ChangeSet(ctx, walker, pathsCh) }) diff --git a/cli/format_test.go b/cli/format_test.go index fb389fe..2349767 100644 --- a/cli/format_test.go +++ b/cli/format_test.go @@ -216,6 +216,15 @@ func TestCache(t *testing.T) { as.NoError(err) as.Contains(string(out), "0 files changed") + // clear cache + out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir, "-c") + as.NoError(err) + as.Contains(string(out), fmt.Sprintf("%d files changed", 29)) + + out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir) + as.NoError(err) + as.Contains(string(out), "0 files changed") + // no cache out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir, "--no-cache") as.NoError(err) diff --git a/nix/packages.nix b/nix/packages.nix index 127eb08..e0f8604 100644 --- a/nix/packages.nix +++ b/nix/packages.nix @@ -13,7 +13,7 @@ packages = rec { treefmt = inputs'.gomod2nix.legacyPackages.buildGoApplication rec { pname = "treefmt"; - version = "0.0.1+dev"; + version = "2.0.0+dev"; # ensure we are using the same version of go to build with inherit (pkgs) go; diff --git a/cli/cli.go b/cli/cli.go index 8b23262..b370ee7 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -11,6 +11,7 @@ var Cli = Format{} type Format struct { AllowMissingFormatter bool `default:"false" help:"Do not exit with error if a configured formatter is missing"` WorkingDirectory kong.ChangeDirFlag `default:"." short:"C" help:"Run as if treefmt was started in the specified working directory instead of the current working directory"` + NoCache bool `help:"Ignore the evaluation cache entirely. Useful for CI"` ClearCache bool `short:"c" help:"Reset the evaluation cache. Use in case the cache is not precise enough"` ConfigFile string `type:"existingfile" default:"./treefmt.toml"` FailOnChange bool `help:"Exit with error if any changes were made. Useful for CI."` diff --git a/cli/format.go b/cli/format.go index 6c46096..14ac16c 100644 --- a/cli/format.go +++ b/cli/format.go @@ -5,6 +5,7 @@ import ( "context" "errors" "fmt" + "io/fs" "os" "os/signal" "strings" @@ -168,6 +169,20 @@ func (f *Format) Run() error { var changes int + processBatch := func() error { + if Cli.NoCache { + changes += len(batch) + } else { + count, err := cache.Update(batch) + if err != nil { + return err + } + changes += count + } + batch = batch[:0] + return nil + } + LOOP: for { select { @@ -179,22 +194,17 @@ func (f *Format) Run() error { } batch = append(batch, path) if len(batch) == batchSize { - count, err := cache.Update(batch) - if err != nil { + if err = processBatch(); err != nil { return err } - changes += count - batch = batch[:0] } } } // final flush - count, err := cache.Update(batch) - if err != nil { + if err = processBatch(); err != nil { return err } - changes += count if Cli.FailOnChange && changes != 0 { return ErrFailOnChange @@ -251,6 +261,22 @@ func (f *Format) Run() error { } defer close(pathsCh) + + if Cli.NoCache { + return walker.Walk(ctx, func(path string, info fs.FileInfo, err error) error { + select { + case <-ctx.Done(): + return ctx.Err() + default: + // ignore symlinks and directories + if !(info.IsDir() || info.Mode()&os.ModeSymlink == os.ModeSymlink) { + pathsCh <- path + } + return nil + } + }) + } + return cache.ChangeSet(ctx, walker, pathsCh) }) diff --git a/nix/packages.nix b/nix/packages.nix index 127eb08..e0f8604 100644 --- a/nix/packages.nix +++ b/nix/packages.nix @@ -13,7 +13,7 @@ packages = rec { treefmt = inputs'.gomod2nix.legacyPackages.buildGoApplication rec { pname = "treefmt"; - version = "0.0.1+dev"; + version = "2.0.0+dev"; # ensure we are using the same version of go to build with inherit (pkgs) go;
1 parent d53f98e commit da82b80

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

cli/cli.go

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var Cli = Format{}
1111
type Format struct {
1212
AllowMissingFormatter bool `default:"false" help:"Do not exit with error if a configured formatter is missing"`
1313
WorkingDirectory kong.ChangeDirFlag `default:"." short:"C" help:"Run as if treefmt was started in the specified working directory instead of the current working directory"`
14+
NoCache bool `help:"Ignore the evaluation cache entirely. Useful for CI"`
1415
ClearCache bool `short:"c" help:"Reset the evaluation cache. Use in case the cache is not precise enough"`
1516
ConfigFile string `type:"existingfile" default:"./treefmt.toml"`
1617
FailOnChange bool `help:"Exit with error if any changes were made. Useful for CI."`

cli/format.go

+33-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"errors"
77
"fmt"
8+
"io/fs"
89
"os"
910
"os/signal"
1011
"strings"
@@ -168,6 +169,20 @@ func (f *Format) Run() error {
168169

169170
var changes int
170171

172+
processBatch := func() error {
173+
if Cli.NoCache {
174+
changes += len(batch)
175+
} else {
176+
count, err := cache.Update(batch)
177+
if err != nil {
178+
return err
179+
}
180+
changes += count
181+
}
182+
batch = batch[:0]
183+
return nil
184+
}
185+
171186
LOOP:
172187
for {
173188
select {
@@ -179,22 +194,17 @@ func (f *Format) Run() error {
179194
}
180195
batch = append(batch, path)
181196
if len(batch) == batchSize {
182-
count, err := cache.Update(batch)
183-
if err != nil {
197+
if err = processBatch(); err != nil {
184198
return err
185199
}
186-
changes += count
187-
batch = batch[:0]
188200
}
189201
}
190202
}
191203

192204
// final flush
193-
count, err := cache.Update(batch)
194-
if err != nil {
205+
if err = processBatch(); err != nil {
195206
return err
196207
}
197-
changes += count
198208

199209
if Cli.FailOnChange && changes != 0 {
200210
return ErrFailOnChange
@@ -251,6 +261,22 @@ func (f *Format) Run() error {
251261
}
252262

253263
defer close(pathsCh)
264+
265+
if Cli.NoCache {
266+
return walker.Walk(ctx, func(path string, info fs.FileInfo, err error) error {
267+
select {
268+
case <-ctx.Done():
269+
return ctx.Err()
270+
default:
271+
// ignore symlinks and directories
272+
if !(info.IsDir() || info.Mode()&os.ModeSymlink == os.ModeSymlink) {
273+
pathsCh <- path
274+
}
275+
return nil
276+
}
277+
})
278+
}
279+
254280
return cache.ChangeSet(ctx, walker, pathsCh)
255281
})
256282

nix/packages.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
packages = rec {
1414
treefmt = inputs'.gomod2nix.legacyPackages.buildGoApplication rec {
1515
pname = "treefmt";
16-
version = "0.0.1+dev";
16+
version = "2.0.0+dev";
1717

1818
# ensure we are using the same version of go to build with
1919
inherit (pkgs) go;

0 commit comments

Comments
 (0)