git hooks
Opinionated set of git hooks.
At the moment supports JavaScript, CSS, shell scripts and markdown files.
Installation
via npm dependency (recommended)
npm install --save-dev @saji/git-hooks
- Add
install-git-hooks
to yourpostinstall
script
from global npm install
npm install -g @saji/git-hooks
- Run
install-git-hooks PATH_TO_YOUR_PROJECT
from cloned repository
- Clone this repository anywhere
- Run
YOUR_ANYWHERE/install.sh PATH_TO_YOUR_PROJECT
by changing hooksPath
- Clone this repository anywhere
- Run
git config core.hooksPath YOUR_ANYWHERE
(usegit config --global
if you want to do it globally)
via whatever
You may also use git submodules. Or download files, drop them in your
.git/hooks
, I don’t care.
Configuration
Disabling a hook
git config --type=int hooks.$HOOK_NAME.enabled false
# e.g. hooks.lint.enabled
You can also disable a hook for single run, e.g.
git -c hooks.npm-test.enabled=false push
As a shorthand you can omit .enabled
:
git -c hooks.npm-test=false push
Disabling all hooks
Apart from passing --no-verify
you can also use hooks.enabled
config option.
Changing verbosity
git config --type=int hooks.verbosity 2
- -1: Only error messages
- 0: Also show celebratory success message (default)
- 1: Also print hook names as they run
- 2: Also print hook execution time after they finish.
- 7: Also print main commands for some hooks
- 9: Also run everything with
set -x
(TODO) If not specified in config, verbosity is controlled by number
of -v
parameter(s) passed to git-commit.
Temporary worktree
git config --type=bool hooks.tmpWorkspace true
Some hooks might run for a longer time (e.g. npm-test pre-push). With
this option enabled, they might choose to run in a temporary worktree
(see git worktree --help
for details) so that your main worktree is
not blocked — you can switch branches, commit etc. while your tests
run.
Hooks
pre-push: npm-test
Detects if jest is being used for testing and if so, only runs tests that are related to modified files, but that heuristic is not perfect. This option enables you to additionally always run selected tests, e.g.
git config hooks.pre-push.npm-test.forcedFiles tests/storybook.test.js
pre-push: branch-name
Allows to set up a extended grep regular expression to match branches when pushing.
git config hooks.pre-push.branch-name.allow-regexp '^(feature|fix|doc|chore)/'
You don’t have to list your main branch.