For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/git-hooks.md.
close
  • English
  • Git hooks

    Use rs setup 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 setup, then run rs staged from the pre-commit hook to lint and format the files staged for the commit.

    Set up hooks

    Add rs setup to the prepare script of the project that owns the repository hooks:

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

    Run the script once to install the hooks:

    npm
    yarn
    pnpm
    bun
    npm run prepare

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

    git config --local --get core.hooksPath
    # .rstack/hooks/_
    Tip
    • The _ directory is generated dynamically and ignored by Git by default.
    • If rs setup detects another hooks path or existing Git hooks, it skips installation. Migrate any hooks you want to keep, remove the existing configuration, and then try again. See the rs setup guide for details.

    Pre-commit checks

    A pre-commit hook can lint and format the files staged for the current commit.

    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 setup. 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 fixes, then rs fmt formats the result. Remove --fix if lint errors should block the commit without changing files.

    After every task passes, the commit continues and includes the fixed and formatted results. If any task fails, the commit stops; fix the issue and then try again.