|
| 1 | +--- |
| 2 | +description: 'Set up repository Git hooks with Rstack CLI and automatically lint and format staged files before each commit.' |
| 3 | +--- |
| 4 | + |
| 5 | +# Git hooks |
| 6 | + |
| 7 | +import { PackageManagerTabs } from '@rspress/core/theme'; |
| 8 | + |
| 9 | +Use [`rs setup`](./cli/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. |
| 10 | + |
| 11 | +This page uses `pre-commit` as an example: first install Git hooks with `rs setup`, then run [`rs staged`](./cli/staged) from the `pre-commit` hook to lint and format the files staged for the commit. |
| 12 | + |
| 13 | +## Set up hooks |
| 14 | + |
| 15 | +Add `rs setup` to the `prepare` script of the project that owns the repository hooks: |
| 16 | + |
| 17 | +```json title="package.json" |
| 18 | +{ |
| 19 | + "scripts": { |
| 20 | + "prepare": "rs setup" |
| 21 | + } |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +Run the script once to install the hooks: |
| 26 | + |
| 27 | +<PackageManagerTabs |
| 28 | + command={{ |
| 29 | + npm: 'npm run prepare', |
| 30 | + yarn: 'yarn run prepare', |
| 31 | + pnpm: 'pnpm run prepare', |
| 32 | + bun: 'bun run prepare', |
| 33 | + }} |
| 34 | +/> |
| 35 | + |
| 36 | +`rs setup` sets the repository's `core.hooksPath` to `.rstack/hooks/_`. Verify the installation with: |
| 37 | + |
| 38 | +```bash |
| 39 | +git config --local --get core.hooksPath |
| 40 | +# .rstack/hooks/_ |
| 41 | +``` |
| 42 | + |
| 43 | +:::tip |
| 44 | + |
| 45 | +- The `_` directory is generated dynamically and ignored by Git by default. |
| 46 | +- 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](./cli/setup#hook-files) for details. |
| 47 | + |
| 48 | +::: |
| 49 | + |
| 50 | +## Pre-commit checks |
| 51 | + |
| 52 | +A `pre-commit` hook can lint and format the files staged for the current commit. |
| 53 | + |
| 54 | +### Configure tasks |
| 55 | + |
| 56 | +Add staged-file tasks to the Rstack config file. Adjust the glob patterns for the languages used by your project: |
| 57 | + |
| 58 | +```ts title="rstack.config.ts" |
| 59 | +import { define } from 'rstack'; |
| 60 | + |
| 61 | +define.staged({ |
| 62 | + '*.{js,jsx,ts,tsx,mjs,cjs,mts,cts}': ['rs lint --fix', 'rs fmt'], |
| 63 | + '*.{json,md,mdx,css,scss,less,html,yml,yaml}': 'rs fmt', |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +### Add the hook |
| 68 | + |
| 69 | +Create `.rstack/hooks/pre-commit` and run `rs staged` from it: |
| 70 | + |
| 71 | +```sh title=".rstack/hooks/pre-commit" |
| 72 | +rs staged |
| 73 | +``` |
| 74 | + |
| 75 | +### How it works |
| 76 | + |
| 77 | +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. |
| 78 | + |
| 79 | +`rs staged` passes matching staged files to each command. Commands in an array run in order: [`rs lint --fix`](./cli/lint) first applies available fixes, then [`rs fmt`](./cli/fmt) formats the result. Remove `--fix` if lint errors should block the commit without changing files. |
| 80 | + |
| 81 | +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. |
0 commit comments