dart_husky

dart_husky

Git hook manager for Dart & Flutter — no binaries, no fuss.

pub version pub points license dart

Pure Dart. Zero external dependencies. Works with Flutter, FVM, or bare Dart SDK.
Inspired by husky and lefthook.


Why dart_husky?

Most git hook tools require installing a separate binary (Go, Node.js, etc.). dart_husky is pure Dart — if your team has Dart, they have everything they need.

✦ Pure Dart — no Go, no Node, no extra installs
✦ YAML config — familiar, readable, version-controlled
✦ Built-in conventional commits validation
✦ Sequential or parallel command execution
✦ Staged-only mode — run commands on staged files only
✦ Glob filtering — skip commands when no matching files are staged
✦ Works with dart, flutter, and fvm
✦ Melos workspace support — run tests at package level via preset: melos
✦ Lowercase enforcement — optionally force lowercase commit messages

Installation

Add to your pubspec.yaml:

dev_dependencies:
  dart_husky: ^1.5.0

Install dependencies and set up hooks:

dart pub get
dart run dart_husky install

That's it. Your hooks are live.


Configuration

Create dart_husky.yaml in your project root:

dart_husky:
  verbose: true       # print detailed output (default: true)
  staged_only: false  # run on staged files only (default: false)

pre-commit:
  commands:
    format:
      run: dart format --set-exit-if-changed .
    analyze:
      run: dart analyze

commit-msg:
  commands:
    conventional:
      preset: conventional

Supported Hooks

Hook Triggered when...
pre-commit Before a commit is created
commit-msg After you write a commit message
pre-push Before pushing to remote
post-checkout After switching branches
pre-merge-commit Before a merge commit

Commands

# Install hooks defined in dart_husky.yaml
dart run dart_husky install

# Remove all installed hooks
dart run dart_husky uninstall

# Manually trigger a hook
dart run dart_husky run pre-commit

# List configured hooks and install status
dart run dart_husky list

# Check that the installed hooks match dart_husky.yaml (for scripts and CI)
dart run dart_husky check

Checking hooks in scripts

check compares every hook in your hooks folder with what install would write today, prints one line per hook, and reports the result through its exit code:

Exit code Meaning
0 Every configured hook is installed and up to date, and no leftover dart_husky hooks remain
1 At least one hook is missing, foreign (not written by dart_husky), outdated, stale (installed but no longer in dart_husky.yaml), or not executable (git skips it). dart run dart_husky install fixes it, replacing any foreign hook
2 The check couldn't run: no dart_husky.yaml, an invalid one, or not run from the root of a git repository
$ dart run dart_husky check
pre-commit: in sync
commit-msg: outdated (differs from what install would write) - run "dart run dart_husky install" to fix

Use it in a setup script to install hooks only when they need it:

#!/bin/sh
dart run dart_husky check
case $? in
  0) ;;                             # hooks are up to date
  1) dart run dart_husky install ;; # missing or out of date
  *) exit 1 ;;                      # couldn't check, reason printed above
esac

Conventional Commits

Enable built-in conventional commits validation with one line:

commit-msg:
  commands:
    conventional:
      preset: conventional

Valid format:

<type>(<optional scope>): <subject>
Type When to use
feat A new feature
fix A bug fix
chore Maintenance, deps, tooling
docs Documentation only
style Formatting, whitespace
refactor Code change, no feature or fix
test Adding or fixing tests
build Build system changes
ci CI configuration
perf Performance improvement
revert Revert a previous commit

Examples:

git commit -m "feat(auth): add login screen"         ✅
git commit -m "fix: resolve null pointer exception"  ✅
git commit -m "feat!: breaking api change"           ✅
git commit -m "updated stuff"                        ❌

Custom Commit Types

Append custom types on top of the built-in list:

commit-msg:
  commands:
    conventional:
      preset: conventional
      types:
        append: [wip, release]

Or completely override the built-in types:

commit-msg:
  commands:
    conventional:
      preset: conventional
      types:
        override: [feat, fix, hotfix, release]

Lowercase Enforcement

Force all commit messages to be lowercase (default: true):

commit-msg:
  commands:
    conventional:
      preset: conventional
      only_small_case: true  # default

To allow mixed case:

commit-msg:
  commands:
    conventional:
      preset: conventional
      only_small_case: false

Applies to any preset, not just conventional commits.

Staged-Only Mode

Run commands only on staged files instead of the entire project. Faster hooks for large codebases.

Enable globally for all commands:

dart_husky:
  staged_only: true

pre-commit:
  commands:
    format:
      run: dart format --set-exit-if-changed .
    analyze:
      run: dart analyze
    test:
      run: dart test
      staged_only: false  # override — always run full test suite

Command-level staged_only always overrides the global setting. If no staged files are found, the command is skipped automatically.


Melos Preset

If your project uses melos for monorepo management, you can replace a run command with the melos preset to execute tests at the package level:

pre-commit:
  commands:
    test:
      preset: melos              # no `run` field — preset handles everything
      staged_only: true          # optional — only packages with staged files

When preset: melos is set, the run field must be omitted (the parser rejects both).

How it works

  1. Package discovery — Runs melos list --json --relative to find all workspace packages. Supports both melos.yaml and melos: config key in pubspec.yaml.

  2. Test command auto-detection — Picks the first available: fvm flutter test > flutter test > dart test.

  3. Execution — Uses melos exec to run tests. The staged_only flag controls scope:

    staged_only Behavior
    true Runs tests only in packages containing staged files (melos exec --scope=pkg1 --scope=pkg2 -- ...)
    false Runs tests in all packages (melos exec -- ...)

Command-level staged_only overrides the global dart_husky.staged_only setting, just like regular run commands.


Glob Filtering

Skip commands entirely when no staged files match a pattern:

pre-commit:
  commands:
    format:
      run: dart format --set-exit-if-changed .
      glob: '**/*.dart'   # skip if no .dart files are staged
    analyze:
      run: dart analyze
      glob: '**/*.dart'

If you only staged README.md, both format and analyze are skipped — no unnecessary work.


Parallel Execution

Speed up slow hooks by running commands simultaneously:

pre-commit:
  parallel: true
  commands:
    format:
      run: dart format --set-exit-if-changed .
    analyze:
      run: dart analyze
    test:
      run: dart test

How It Works

you run: git commit
         └── git checks .git/hooks/pre-commit
                  └── dart run dart_husky run pre-commit
                           └── reads dart_husky.yaml
                                    └── runs each command
                                             ├── all pass → commit created ✅
                                             └── any fail → commit blocked ❌

dart_husky install writes a small shell script for each configured hook into the folder git reads hooks from: .git/hooks/ by default, or core.hooksPath if you set it. The script detects whether to use dart or fvm dart automatically.

Run dart_husky from the root of your repository. In a linked worktree (git worktree add), run it from the worktree's root. By default, worktrees share the main repository's hooks, so one install covers all of them.


Contributing

Contributions are welcome! Please make sure your commits follow the conventional commits format — dart_husky will enforce it. 😄


Made with 🎯 by @moulibheemaneti
MIT License

Libraries

dart_husky
dart_husky — Git hook manager for Dart & Flutter projects