A better pre-commit hook for git.
Install the gem
$ gem install pre-commit
Use the pre-commit command to generate a stub pre-commit hook
# In your git repo
$ pre-commit install
This creates a .git/hooks/pre-commit script which will check your git config and run checks that are enabled.
If you want to use Bundler to specify a version of RuboCop, add the following to Gemfile
:
group :development do
gem "pre-commit", require: false
gem "rubocop", require: false
end
And run the following to run pre-commit
via Bundler:
$ git config pre-commit.ruby "bundle exec ruby"
If you are using rvm you need to install pre-commit into the default
gemset, because it does not use the current
environment
$ rvm default do gem install pre-commit
Alternatively you can configure pre-commit to use the current
rvm gemset
$ git config pre-commit.ruby "rvm `rvm current` do ruby" # OR:
$ git config pre-commit.ruby `rvm wrapper current show ruby` # available in RVM 1.26.12
These are the available checks:
config/pre-commit.rb
with list of changed files)pre_commit:ci
rake task and pass or fail accordingly)before(:all)
)Use pre-commit list
to see the list of default and enabled checks and warnings.
git config pre-commit.checks "[whitespace, jshint, debugger]"
To disable, simply leave one off the list
git config pre-commit.checks "[whitespace, jshint]"
pre-commit <enable|disable> <git|yaml> <checks|warnings> check1 [check2...]
The git
provider can be used for local machine configuration, the yaml
can be used for shared
project configuration.
Example move jshint
from checks
to warnings
in yaml
provider and save configuration to git:
pre-commit disable yaml checks jshint
pre-commit enable yaml warnings jshint
git add config/pre_commit.yml
git commit -m "pre-commit: move jshint from checks to warnings"
Example config/pre_commit.yml
:
---
:warnings_remove: []
:warnings_add:
- :jshint
- :tabs
This functionality was added in version 0.17.0
pre-commit run # run on the files added to index not yet commited
pre-commit run all # run on all files in current directory
pre-commit run git # run on all git-tracked files, respect gitignore (added in 0.19.0)
pre-commit run <file-list> # run on the list of files, patterns not supported
pre-commit
comes with 4 configuration providers:
default
- basic settings, read onlygit
- reads configuration from git config pre-commit.*
, allow local updateyaml
- reads configuration from /etc/pre_commit.yml
, $HOME/.pre_commit.yml
and config/pre_commit.yml
, allows config/pre_commit.yml
updatesenv
- reads configuration from environment variablespre-commit
uses git
to get list of files to check, you can ignore
the list of git files to check with:
.gitignore
- git supported file shared beteen all checkouts.git/info/exclude
- git supported file only for this checkout.pre_commit.ignore
- pre-commit
specific list can be shared,
Allowed filters