Dart version husky (Inspired by JavaScript version husky)
Husky make it easy to manage your Dart and Flutter project's git hooks.
You can use it to lint your commit messages, run tests, lint code, etc... when you commit or push. Husky supports all Git hooks.
- Powered by modern new Git feature (
core.hooksPath
) - User-friendly messages
- Support
- macOS, Linux and Windows
- Git GUIs
- Custom directories
- Monorepos
- Add husky to your
dev_dependencies
in pubspec.yaml
dart pub add --dev husky
- Enable Git hooks.
dart run husky install
This will generate .husky
directory under your project's root path.
Note: make sure to commit .husky
to git repository.
- Create a hook:
dart run husky add .husky/pre-commit "dart test"
git add .husky/pre-commit
Try to make a commit:
git commit -m "Keep calm and commit"
# `dart test` will run
If dart test
command fails, your commit will be automatically aborted.
dart run husky uninstall
dart pub remove --dev husky
git config --unset core.hooksPath
It's recommended to add husky in root pubspec.yaml
. You can use tools like melos and filters to only run scripts in packages that have been changed.
If you want to install husky in another directory, for example .config
, you can pass it to install
command. For example:
dart run husky install .config/husky
Another case you may be in is if your pubspec.yaml
file and .git
directory are not at the same level. For example, project/.git
and project/sub/pubspec.yaml
.
By design, husky install
must be run in the same directory as .git
, but you can change directory when running dart run husky install
and pass a subdirectory:
dart run husky install sub/.husky
In your hooks, you'll also need to change directory:
# .husky/pre-commit
# ...
cd sub
dart test
You can bypass pre-commit
and commit-msg
hooks using Git -n/--no-verify
option:
git commit -m "yolo!" --no-verify
For Git commands that don't have a --no-verify
option, you can use HUSKY
environment variable:
HUSKY=0 git push # yolo!
If you want to test a hook, you can add exit 1
at the end of the script to abort git command.
# .husky/pre-commit
# ...
exit 1 # Commit will be aborted
If using git-flow you need to ensure your git-flow hooks directory is set to use Husky's (.husky
by default).
git config gitflow.path.hooks .husky
Note:
- If you are configuring git-flow after you have installed husky, the git-flow setup process will correctly suggest the .husky directory.
- If you have set a custom directory for husky you need to specify that (ex.
git config gitflow.path.hooks .config/husky
)
To revert the git-flow hooks directory back to its default you need to reset the config to point to the default Git hooks directory.
git config gitflow.path.hooks .git/hooks
Yes. When you install Git on Windows, it comes with the necessary software to run shell scripts.
- Ensure that you don't have a typo in your filename. For example,
precommit
orpre-commit.sh
are invalid names. See Git hooks documentation for valid names. - Check that
git config core.hooksPath
returns.husky
(or your custom hooks directory). - Verify that hook files are executable. This is automatically set when using
husky add
command but you can runchmod x .husky/<hookname>
to fix that. - Check that your version of Git is greater than
2.9
.\
You can create a commit-msg
hook to call .git/hooks/commit-msg
dart run husky add .husky/commit-msg 'gitdir=$(git rev-parse --git-dir); ${gitdir}/hooks/commit-msg $1'
If after uninstalling husky
, hooks in .git/hooks/
aren't working. Run git config --unset core.hooksPath
.