diff --git a/README.md b/README.md index 5f7733ce..14369deb 100644 --- a/README.md +++ b/README.md @@ -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} ``` diff --git a/docs/configuration.md b/docs/configuration.md index c1135a44..e2273d73 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -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} ``` @@ -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 ``` @@ -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). @@ -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 @@ -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 ``` diff --git a/examples/complete/lefthook.yml b/examples/complete/lefthook.yml index 4235a4e6..25a5804e 100644 --- a/examples/complete/lefthook.yml +++ b/examples/complete/lefthook.yml @@ -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": diff --git a/internal/lefthook/run/filter/filters_test.go b/internal/lefthook/run/filter/filters_test.go index 996525ed..17ce2c8c 100644 --- a/internal/lefthook/run/filter/filters_test.go +++ b/internal/lefthook/run/filter/filters_test.go @@ -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"}, }, } { diff --git a/internal/templates/config.tmpl b/internal/templates/config.tmpl index 0f013f56..f6e5dfe5 100644 --- a/internal/templates/config.tmpl +++ b/internal/templates/config.tmpl @@ -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