Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: glob and regexp related improvements #583

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pre-commit:
commands:
backend-linter:
glob: "*.rb" # glob filter
exclude: "application.rb|routes.rb" # regexp filter
exclude: '(^|/)(application|routes)\.rb$' # regexp filter
run: bundle exec rubocop --force-exclusion {all_files}
```

Expand Down
16 changes: 11 additions & 5 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ pre-commit:
rubocop:
tags: backend style
glob: "*.rb"
exclude: "application.rb|routes.rb"
exclude: '(^|/)(application|routes)\.rb$'
run: bundle exec rubocop --force-exclusion {all_files}
```

Expand Down Expand Up @@ -871,7 +871,7 @@ If you've specified `glob` but don't have a files template in [`run`](#run) opti
pre-commit:
commands:
lint:
glob: ".js"
glob: "*.js"
run: npm run lint # skipped if no .js files staged
```

Expand Down Expand Up @@ -997,6 +997,10 @@ pre-commit:

You can provide a regular expression to exclude some files from being passed to [`run`](#run) command.

The regular expression is matched against full paths to files in the repo,
relative to the repo root, using `/` as the directory separator on all platforms.
File paths do not begin with the separator or any other prefix.

**Example**

Run Rubocop on staged files with `.rb` extension except for `application.rb`, `routes.rb`, and `rails_helper.rb` (wherever they are).
Expand All @@ -1007,13 +1011,15 @@ Run Rubocop on staged files with `.rb` extension except for `application.rb`, `r
pre-commit:
commands:
lint:
glob: ".rb"
exclude: "application.rb|routes.rb|rails_helper.rb"
glob: "*.rb"
exclude: '(^|/)(application|routes|rails_helper)\.rb$'
run: bundle exec rubocop --force-exclusion {staged_files}
```

**Notes**

Be careful with the config file format's string quoting and escaping rules when writing regexps in it. For YAML, single quotes are often the simplest choice.

If you've specified `exclude` but don't have a files template in [`run`](#run) option, lefthook will check `{staged_files}` for `pre-commit` hook and `{push_files}` for `pre-push` hook and apply filtering. If no files left, the command will be skipped.

```yml
Expand All @@ -1022,7 +1028,7 @@ If you've specified `exclude` but don't have a files template in [`run`](#run) o
pre-commit:
commands:
lint:
exclude: "application.rb"
exclude: '(^|/)application\.rb$'
run: bundle exec rubocop # skipped if only application.rb was staged
```

Expand Down
2 changes: 1 addition & 1 deletion examples/complete/lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pre-commit:
rubocop:
tags: backend style
glob: "*.rb"
exclude: "application.rb|routes.rb"
exclude: '(^|/)(application|routes)\.rb$'
run: bundle exec rubocop --force-exclusion {all_files}
scripts:
"good_job.js":
Expand Down
8 changes: 4 additions & 4 deletions internal/lefthook/run/filter/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@ func TestByExclude(t *testing.T) {
},
{
source: []string{"folder/subfolder/0.rb", "1.txt", "2.RB", "3.rb"},
exclude: "^[^/]*\\.rb",
exclude: "^[^/]*\\.rb$",
result: []string{"folder/subfolder/0.rb", "1.txt", "2.RB"},
},
{
source: []string{"folder/subfolder/0.rb", "1.rb"},
exclude: "^.+/.+.*\\.rb",
exclude: "^.+/.+.*\\.rb$",
result: []string{"1.rb"},
},
{
source: []string{"folder/0.rb", "1.rBs", "2.rbv"},
exclude: ".*\\.rb.?",
exclude: ".*\\.rb.?$",
result: []string{"1.rBs"},
},
{
source: []string{"f.a", "f.b", "f.c", "f.cn"},
exclude: ".*\\.(a|b|cn)",
exclude: ".*\\.(a|b|cn)$",
result: []string{"f.c"},
},
} {
Expand Down
2 changes: 1 addition & 1 deletion internal/templates/config.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# rubocop:
# tags: backend style
# glob: "*.rb"
# exclude: "application.rb|routes.rb"
# exclude: '(^|/)(application|routes)\.rb$'
# run: bundle exec rubocop --force-exclusion {all_files}
# govet:
# tags: backend style
Expand Down
Loading