Git hooks made easy for your Dart and Flutter projects. Inspired by JavaScript husky package.
APACHE-2.0 License
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.
core.hooksPath
)dev_dependencies
in pubspec.yamldart pub add --dev husky
dart run husky install
This will generate .husky
directory under your project's root path.
Note: make sure to commit .husky
to git repository.
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:
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.
precommit
or pre-commit.sh
are invalid names. See Git hooks documentation for valid names.git config core.hooksPath
returns .husky
(or your custom hooks directory).husky add
command but you can run chmod +x .husky/<hookname>
to fix that.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
.