Skip to content

Commit 24edaad

Browse files
authored
pre-commit: don't pass filenames (#277)
By default, hooks will pass filenames to yamlfmt. On purpose, yamlfmt will handle this by accepting each file argument and passing it through the formatter with no guards. This is not a good default experience. This PR changes the hook to not pass filenames, and the default `entry` to be `yamlfmt .`. This should match the experience I thought the hook would be giving all along.
1 parent a48496e commit 24edaad

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

.pre-commit-hooks.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
- id: yamlfmt
1616
name: yamlfmt
1717
description: This hook uses github.com/google/yamlfmt to format yaml files. Requires Go >1.18 to be installed.
18-
entry: yamlfmt
18+
entry: yamlfmt .
19+
pass_filenames: false
1920
language: golang

docs/pre-commit.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,55 @@ Starting in v0.7.1, `yamlfmt` can be used as a hook for the popular [pre-commit]
44

55
```yaml
66
- repo: https://github.com/google/yamlfmt
7-
rev: v0.18.0
7+
rev: v0.18.1
88
hooks:
99
- id: yamlfmt
1010
```
1111
12-
When running yamlfmt with the `pre-commit` hook, the only way to configure it is through a `.yamlfmt` configuration file in the root of the repo or a system wide config directory (see [Configuration File](./config-file.md) docs).
12+
## Configuration
13+
14+
The default `entry` for the hook is `yamlfmt .`. This is a reasonable default experience if you are not providing `yamlfmt` with a configuration. You can provide configuration either [through a file](./config-file.md) or [through the command line](./command-usage.md). This may require you to override the `entry`. For example, if you have a configuration file with the exact formatting experience you want (all the right files passed in, all the right formatter settings) then you may want to modify the entry to simply run the command with no arguments:
15+
16+
```yaml
17+
- repo: https://github.com/google/yamlfmt
18+
rev: v0.18.1
19+
hooks:
20+
- id: yamlfmt
21+
entry: yamlfmt
22+
```
23+
24+
You may also wish to provide all your configuration directly through the configuration flags like so:
25+
26+
```yaml
27+
- repo: https://github.com/google/yamlfmt
28+
rev: v0.18.1
29+
hooks:
30+
- id: yamlfmt
31+
entry: yamlfmt -doublestar true **/*.{yaml,yml}
32+
```
1333

1434
## Use `yamlfmt` installed on the system instead of pre-commit building with Go
1535

1636
If you would prefer to manage your `yamlfmt` installation yourself, you can have the hook use your installed `yamlfmt` binary instead. As long as `yamlfmt` is in your PATH, you can override the `language` setting to `system`.
1737

1838
```yaml
1939
- repo: https://github.com/google/yamlfmt
20-
rev: v0.18.0
40+
rev: v0.18.1
2141
hooks:
2242
- id: yamlfmt
2343
language: system
2444
```
2545

46+
## Restore old behaviour
47+
48+
In `v0.18.0` and `v0.18.1`, the experience was changed to what is documented here. What is documented here now is the intended experience. However, originally the hook was configured to only run on the `yaml` filetype, and all discovered files would be passed as a list of arguments to the command. This behaviour can be restored like so:
49+
50+
```yaml
51+
- repo: https://github.com/google/yamlfmt
52+
rev: v0.18.1
53+
hooks:
54+
- id: yamlfmt
55+
entry: yamlfmt
56+
files: [yaml]
57+
pass_filenames: true
58+
```

0 commit comments

Comments
 (0)