pre-commit
https://pre-commit.com/logo.svg
Git hook scripts are useful for identifying simple issues before submission to code review. We run our hooks on every commit to automatically point out issues in code such as missing semicolons, trailing whitespace, and debug statements. By pointing these issues out before code review, this allows a code reviewer to focus on the architecture of a change while not wasting time with trivial style nitpicks. As we created more libraries and projects we recognized that sharing our pre-commit hooks across projects is painful. We copied and pasted unwieldy bash scripts from project to project and had to manually change the hooks to work for different project structures.
We believe that you should always use the best industry standard linters. Some of the best linters are written in languages that you do not use in your project or have installed on your machine. For example scss-lint is a linter for SCSS written in Ruby. If you’re writing a project in node you should be able to use scss-lint as a pre-commit hook without adding a Gemfile to your project or understanding how to get scss-lint installed. We built pre-commit to solve our hook issues. It is a multi-language package manager for pre-commit hooks. You specify a list of hooks you want and pre-commit manages the installation and execution of any hook written in any language before every commit. pre-commit is specifically designed to not require root access. If one of your developers doesn’t have node installed but modifies a JavaScript file, pre-commit automatically handles downloading and building node to run eslint without root.
インストール
git init
code: (bash)
❯ pre-commit --version
pre-commit 2.20.0
❯ pre-commit -h
usage: pre-commit -h -V {autoupdate,clean,gc,init-templatedir,install,install-hooks,migrate-config,run,sample-config,try-repo,uninstall,validate-config,validate-manifest,help,hook-impl} ... positional arguments:
{autoupdate,clean,gc,init-templatedir,install,install-hooks,migrate-config,run,sample-config,try-repo,uninstall,validate-config,validate-manifest,help,hook-impl}
autoupdate Auto-update pre-commit config to the latest repos' versions.
clean Clean out pre-commit files.
gc Clean unused cached repos.
init-templatedir Install hook script in a directory intended for use with git config init.templateDir.
install Install the pre-commit script.
install-hooks Install hook environments for all environments in the config file. You may find pre-commit install --install-hooks more useful.
migrate-config Migrate list configuration to new map configuration.
run Run hooks.
sample-config Produce a sample .pre-commit-config.yaml file
try-repo Try the hooks in a repository, useful for developing new hooks.
uninstall Uninstall the pre-commit script.
validate-config Validate .pre-commit-config.yaml files
validate-manifest Validate .pre-commit-hooks.yaml files
help Show help for a specific command.
options:
-h, --help show this help message and exit
-V, --version show program's version number and exit
code: (bash)
❯ pre-commit sample-config | tee .pre-commit-config.yaml
repos:
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
code: (bash)
❯ pre-commit install # core.hooksPath が設定されているとインストールできない damm!!!
ERROR Cowardly refusing to install hooks with core.hooksPath set. hint: git config --unset-all core.hooksPath
❯ pre-commit install
pre-commit installed at .git/hooks/pre-commit
サポートされている Hook
Reference