For AI agents: the complete documentation index is available at https://rstack.rs/llms.txt, the full documentation bundle is available at https://rstack.rs/llms-full.txt, and this page is available as Markdown at https://rstack.rs/guide/git-hooks.md.
close
  • English
  • 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:

    package.json
    {
      "scripts": {
        "prepare": "rs hooks"
      }
    }

    Run the script once to install the hooks:

    npm
    yarn
    pnpm
    bun
    npm run prepare

    rs hooks sets the repository's core.hooksPath to .rstack/hooks/_. Verify the installation with:

    git config --get core.hooksPath
    # .rstack/hooks/_
    Tip
    • The _ directory is generated dynamically and ignored by Git by default.
    • If rs hooks detects another hooks path or existing Git hooks, it skips installation. Run rs hooks --force to install Rstack hooks anyway. See the rs hooks guide 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:

    rstack.config.ts
    import { define } from 'rstack';
    
    define.staged({
      '*.{js,jsx,ts,tsx,mjs,cjs,mts,cts}': ['rs lint --fix', 'rs fmt'],
      '*.{json,md,mdx,css,scss,less,html,yml,yaml}': 'rs fmt',
    });

    Add the hook

    Create .rstack/hooks/pre-commit and run rs staged from it:

    .rstack/hooks/pre-commit
    rs staged

    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.