Skip to main content

Git hooks

Git hooks are a mechanism for running scripts at pre-defined phases in git's lifecycle, most commonly pre-commit, pre-push, or pre-merge.

While moon provides no functionality for managing hooks (yet), you can run moon itself from hooks! This is useful for ensuring that commit messages are formatted correctly, or that source code quality thresholds are met.

Pre-commit

A perfect use case for the pre-commit hook is to check linting and formatting of the files being committed. If either of these tasks fail, the commit will abort until they are fixed. Be sure to use the --affected option so that we only run on changed projects!

.git/hooks/pre-commit
#!/bin/sh
./node_modules/@moonrepo/cli/moon run :lint :format --affected --status=staged

By default this will run on the entire project (all files). If you want to filter it to only the changed files, enable the affectedFiles task option.