Commit Graph

67 Commits

Author SHA1 Message Date
Brian McGee
147dcdbcd6
fix: path handling and checking git index
- validates that all path arguments exist and are contained within the tree root
- fixes a bug with git index checking, where we were ignoring directories

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-09-24 10:55:35 +01:00
Jeremy Fleischman
074be54b13
nit: "only" -> "exactly"
The previous wording ("only one") makes it sound like we have *more*
than 1, but this is also handling the scenario where we have 0.
2024-09-14 12:53:20 -07:00
Jeremy Fleischman
d7c106480e
Avoid hanging: ensure we always close f.filesCh
The contract seems to be that the `walkFilesystem` goroutine is
responsible for closing `f.filesCh`, but before this change, there were
codepaths that could result in the gorouting exiting without closing
`f.filesCh`. That shouldn't be possible anymore, so long as we keep this
statement at the top of the function =)

This fixes https://github.com/numtide/treefmt/issues/406
2024-09-14 12:45:43 -07:00
Brian McGee
b834fa33da
feat: best-effort application of files to formatters
If a formatter errors out, continue with subsequent formatters regardless.

Do not cache the result to ensure later invocations re-try the same files.

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-09-03 15:35:26 +01:00
zimbatm
5306a5e092 fix: resolve commands relative to the root
When configuring treefmt with relative paths to commands, for example if
you want to create your own repo-specific wrappers, treefmt would fail
if executed in a sub-directory.

This change makes sure that the relative path is resolved based on the
treefmt root instead of the current directory.
2024-08-29 16:17:00 +00:00
zimbatm
f1c8c76f1c chore(test): add sub-folder test
Describe what happens when running treefmt in a sub-folder of the
project.
2024-08-29 16:17:00 +00:00
zimbatm
f89b4102da chore(test): remove unneeded argument
The -C is not relevant for this unit test.
2024-08-29 16:17:00 +00:00
Brian McGee
1734bcf3e1
fix: remove assertFormatted, use assertStats instead
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-08-21 11:16:49 +01:00
Brian McGee
c53a1f1b77
feat: log changed files at error level when --fail-on-change is enabled
By default, we log the changed file at debug level. If the `--fail-on-change` flag has been provided, we log the changed file at error level.

This is to surface issues better in CI pipelines for example.

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-08-13 20:37:16 +01:00
Brian McGee
f9bc8b0c9b
fix(git): gracefully handle a file in the index but not in the filesystem
This can happen if a user removes a file from the filesystem and that change hasn't been staged yet.

We log a warning when this happens and continue.

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-08-08 09:52:29 +01:00
Brian McGee
d4aaba9dc2
feat: ci mode
Adds a ci flag which does the following:

- ensures INFO level logging at a minimum
- --no-cache
- --fail-on-change

This also adds a delay on startup which is intended to ensure we do not start doing anything until we have moved into the second after the one in which the process started. This helps to ensure accurate comparisons of `modtime` using truncated second-level precision for change detection.

This is only an issue in scenarios in which treefmt is being executed quickly in succession or directly after an automatic checkout, such as in CI.

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-08-01 17:09:10 +01:00
zimbatm
bc1ae33535 feat: search for .treefmt.toml
This restores treefmt v1 compatibility by also searching for the dot
prefixed version of the config file.

Fixes #355
2024-07-21 12:14:06 +02:00
zimbatm
43c2071975 fix(cli): restore PRJ_ROOT env var
PRJ_ROOT is coming from the https://github.com/numtide/prj-spec and was
used in treefmt 1.x

Fixes #353
2024-07-20 08:55:49 +02:00
Brian McGee
0a8ffe0c63
feat: move filtering of directories and symlinks into walker implementations
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-07-09 19:21:09 +01:00
Brian McGee
85ce0a2f74
fix: use second precision when comparing file mod times
We were using whatever precision the underlying file system was giving us, and in some cases that can be very detailed. Some formatters mess with the mod time, but not to the same precision (e.g. dos2unix).

POSIX also specifies that mod time should be EPOCH (second) precision.

This change brings us back in line with how 1.x worked, and should resolve issues with false fail on change errors.
2024-07-06 17:38:43 +01:00
Brian McGee
23e563b239
Merge pull request #344 from numtide/fix/no-cache
Some checks failed
gh-pages / build (push) Has been cancelled
golangci-lint / lint (push) Has been cancelled
gh-pages / deploy (push) Has been cancelled
fix: --no-cache
2024-07-05 22:16:05 +01:00
Brian McGee
42decbfafb
fix: --no-cache
Separates out the logic for detecting changes from the logic for updating the cache, fixing the case where both `--fail-on-change` and `--no-cache` are enabled.

Signed-off-by: Brian McGee <brian@bmcgee.ie>
Co-authored-by: Jonas Chevalier <zimbatm@zimbatm.com>
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-07-05 21:52:34 +01:00
Brian McGee
6776b9f095
Update cli/format.go
Co-authored-by: Jonas Chevalier <zimbatm@zimbatm.com>
2024-07-05 21:23:13 +01:00
Brian McGee
b2000dc1ec
Update cli/format.go
Co-authored-by: Jonas Chevalier <zimbatm@zimbatm.com>
2024-07-05 18:26:13 +01:00
Brian McGee
da7b015190
feat: improve summary stats
Close #340

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-07-05 16:17:26 +01:00
Brian McGee
33a7bba5b1
fix: --no-cache
Separates out the logic
for detecting changes from the logic for updating the cache, fixing the case where both `--fail-on-change` and `--no-cache`
are enabled.

Closes #343

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-07-05 16:11:22 +01:00
Brian McGee
f266fc863b
feat: if we can't open the cache, fallback to no cache
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-07-03 09:40:46 +01:00
Florian Klink
601af09772 fix: only try opening the cache if cache is enabled
Otherwise `--no-cache` still fails to open the cache.
2024-07-01 17:40:12 +03:00
Brian McGee
30d30b54f0
Merge pull request #321 from numtide/softer-cancel
Softer cancel
2024-06-15 11:28:48 +01:00
zimbatm
e29107d3ee fix: only cancel formatters on background errors
If any of the formatting action returns an error, keep going to surface
as many errors as possible.

If the user hits Ctrl-C then terminate everything.
2024-06-15 11:57:57 +02:00
zimbatm
6d6a71a3ca chore: do a golangci-lint pass
golangci-lint doesn't follow the treefmt spec so it can't be integrated
with treefmt unfortunately.
2024-06-14 12:32:57 +02:00
Brian McGee
56d8561125
feat: improve unmatched logic
Separates global excludes processing from `Formatter.Wants`. This removes redundant processing of global excludes in each `Formatter.Wants` call.

If a file has been globally excluded, we do not emit an `on-unmatched` log message. This should help reduce as reported in #317.

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-06-14 10:31:47 +01:00
Paul Meyer
c07305e6b0 cli: remove global state, init function usage
Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
2024-06-06 20:28:37 +02:00
Brian McGee
089eb171a0
fix: --stdin flag
This was incorrectly ported from Rust to Go.

When `--stdin` is provided, `treefmt` copy the `stdin` into a temporary file, using the first path argument as the filename. This allows the user to control which formatters will match this temp file based on their `treefmt` config.

After the formatters have been applied, the contents of this temporary file are then printed to stdout and the temp file is removed.

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-06-05 15:06:10 +01:00
Brian McGee
4f086c204e
Revert "fix: --stdin flag"
This reverts commit 9934a5764d.
2024-06-04 17:05:56 +01:00
Brian McGee
9934a5764d
fix: --stdin flag
This was incorrectly ported from Rust to Go.

When provided, `treefmt` will take the contents of stdin and place them into the file provided with the `--stdin` flag, then format it according to the configured formatters.

If the file doesn't exist it is created. If it exists, it is first truncated and then populated with stdin.

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-06-03 17:07:34 +01:00
zimbatm
7f4697fc49 simplify logic 2024-05-30 19:56:26 +02:00
zimbatm
bc89625cb9 fix TreeRootFile type
kong resolves path types to absolute paths
2024-05-30 19:44:17 +02:00
zimbatm
bc928bdf2b fixup! Update cli/cli.go 2024-05-30 19:31:31 +02:00
zimbatm
0511e9acf1 fixes https://github.com/numtide/treefmt/pull/309#discussion_r1620949820 2024-05-30 19:17:57 +02:00
Jonas Chevalier
088479eb27
Update cli/cli.go
Co-authored-by: Brian McGee <brian@bmcgee.ie>
2024-05-30 19:10:07 +02:00
zimbatm
bd32d36a33 cli: search for the tree root by default
Restore the treefmt 1.x behaviour where it would search for the tree
root by recursively searching for the treefmt.toml file up the
filesystem, starting from the current directory.

The `--tree-root-file` option will be useful to remove this bash wrapper: 2fba33a182/module-options.nix (L116-L135)

Fixes #308
2024-05-30 15:46:00 +02:00
Brian McGee
bbe50fbe75
doc: refine --on-unmatched usage
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-29 10:32:02 +01:00
Brian McGee
1b517c6502
feat: add --on-unmatched
By default, if a path does not match any formatter a log message at WARN level will be emitted. A user can change this by providing the `--on-unmatched` or `-u` flag and specifying a log level `debug,info,warn,error,fatal`.

If fatal, the process will exit with an error on the first unmatched path encountered.

Closes #302

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-29 10:32:02 +01:00
Brian McGee
6cf9524aa7
fix: cleanup cwd change in deterministic pipeline test 2024-05-29 10:32:02 +01:00
Brian McGee
6c7fd57b5c
fix: remove config2 alias in format_test.go
It's unnecessary.
2024-05-29 10:32:02 +01:00
Brian McGee
ce14ee828f
feat: simplify pipeline model
For each path we determine the list of formatters that are interested in formatting it. From there, we sort
the list of formatters first by priority (lower value, higher priority) and then by name (lexicographically).

With this information we create a batch key which is based on the unique sequence of formatters. When enough paths with the same sequence is ready we apply them in order to each formatter.

By default, with no special configuration, this model guarantees that a given path will only be processed by one formatter at a time.

If a user wishes to influence the order in which formatters are applied they can use the priority field.

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-26 16:52:04 +01:00
Brian McGee
06e41453ec
feat: error out when a path matches multiple formatters
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-17 11:55:31 +01:00
mergify[bot]
3b3828251f
Merge branch 'main' into feat/improve-logging 2024-05-17 10:16:51 +00:00
Brian McGee
c002eca8b7
feat: log out when a path is not matched to any formatter
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-17 10:48:53 +01:00
Brian McGee
4d07521d97
fix: -f short flag for specifying formatters
Preserves backwards compatibility with v1

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-14 20:29:42 +01:00
Brian McGee
92321c8737
feat: improve specifying formatters test
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-10 11:43:28 +01:00
Brian McGee
53ea16a42c
feat: improve stats assertion feedback
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-10 11:26:10 +01:00
Brian McGee
e5da10e873
doc: refine docs and usage
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-10 10:46:39 +01:00
Brian McGee
8b92dca9c6
feat: streaming processing of paths from stdin and paths argument
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-07 17:20:04 +01:00