Git hooks
Use rs hooks to manage repository-level Git hooks that run project commands. By default, hook scripts live in .rstack/hooks. You can use them to validate commit messages, check code before pushing, or format files before committing.
This page uses pre-commit as an example: first install Git hooks with rs hooks, then run rs staged from the pre-commit hook to lint and format the files staged for the commit.
Set up hooks
Add rs hooks to the prepare script of the project that owns the repository hooks:
Run the script once to install the hooks:
rs hooks sets the repository's core.hooksPath to .rstack/hooks/_. Verify the installation with:
- The
_directory is generated dynamically and ignored by Git by default. - If
rs hooksdetects another hooks path or existing Git hooks, it skips installation. Runrs hooks --forceto install Rstack hooks anyway. See thers hooksguide for details.
Pre-commit checks
Configure tasks
Add staged-file tasks to the Rstack config file. Adjust the glob patterns for the languages used by your project:
Add the hook
Create .rstack/hooks/pre-commit and run rs staged from it:
How it works
When you run git commit, Git invokes the hook installed by rs hooks. The hook executes .rstack/hooks/pre-commit, and rs staged then runs the configured tasks on the staged files.
rs staged passes matching staged files to each command. Commands in an array run in order: rs lint --fix first applies available lint fixes, then rs fmt formats the files. Remove --fix if you do not want lint issues fixed automatically.
If all tasks pass, the commit includes the fixed and formatted results. If any task fails, the commit stops.