husky 0.1.7 copy "husky: ^0.1.7" to clipboard
husky: ^0.1.7 copied to clipboard

Husky make it easy to manage git hooks for your Dart and Flutter projects.

husky #

Pub Version popularity likes CI

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.

Features #

  • Powered by modern new Git feature (core.hooksPath)
  • User-friendly messages
  • Support
    • macOS, Linux and Windows
    • Git GUIs
    • Custom directories
    • Monorepos

Usage #

  1. Add husky to your dev_dependencies in pubspec.yaml
dart pub add --dev husky
  1. 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.

  1. 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.

Uninstall #

dart run husky uninstall
dart pub remove --dev husky
git config --unset core.hooksPath

Recipes #

Monorepo #

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.

Custom directory #

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

Bypass hooks #

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!

Test hooks #

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

Git-flow #

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

FAQ #

Does it work on Windows? #

Yes. When you install Git on Windows, it comes with the necessary software to run shell scripts.

Troubleshoot #

Hooks not running #

  1. Ensure that you don't have a typo in your filename. For example, precommit or pre-commit.sh are invalid names. See Git hooks documentation for valid names.
  2. Check that git config core.hooksPath returns .husky (or your custom hooks directory).
  3. Verify that hook files are executable. This is automatically set when using husky add command but you can run chmod +x .husky/<hookname> to fix that.
  4. Check that your version of Git is greater than 2.9.\

Cannot work with Gerrit as Change-Id is missing #

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'

.git/hooks/ not working after uninstall #

If after uninstalling husky, hooks in .git/hooks/ aren't working. Run git config --unset core.hooksPath.

Articles #

30
likes
160
pub points
89%
popularity

Publisher

unverified uploader

Husky make it easy to manage git hooks for your Dart and Flutter projects.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

args, path

More

Packages that depend on husky