Skip to content

Git hooks

gspot init installs these hooks and turns them on, and in a new clone, gspot install turns them on. The hooks run code from your repository, as Trust describes.

Hook Checks
pre-commit The commit checks, on the files as you staged them.
pre-push The push checks, on the files that the push changes. At level all, commits/commitlint-pushed also checks each pushed commit message.
commit-msg The message checks, on the new commit message: commits/references, and at level all also commits/commitlint and prose/commit-message.
What the hooks checkgit commit runs the commit checks in the pre-commit hook, then the message checks in the commit-msg hook, and a failed check stops the commit. git push runs the push checks in the pre-push hook, and a failed check stops the push.

passed

passed

failed

failed

passed

failed

git commit

pre-commit hook: commit checks on the staged files

commit-msg hook: message checks

commit

no commit

git push

pre-push hook: push checks on the pushed files

push

no push

What the hooks checkgit commit runs the commit checks in the pre-commit hook, then the message checks in the commit-msg hook, and a failed check stops the commit. git push runs the push checks in the pre-push hook, and a failed check stops the push.

passed

passed

failed

failed

passed

failed

git commit

pre-commit hook: commit checks on the staged files

commit-msg hook: message checks

commit

no commit

git push

pre-push hook: push checks on the pushed files

push

no push

A commit checks what you stage, and what your staged change breaks in other files, so a commit that touches an old file fails on that file’s old findings. A change to gspot.toml does not run the commit checks on other files, so the setup commit fails only on what gspot/conflicting-configs reports and on the files that it stages. Clean up the first findings shows what to do.

The pre-push hook checks only the newest commit of each pushed branch, so a newer commit can fix a mistake in an older one. It leaves out the commit checks, because the pre-commit hook ran them on each commit, and CI runs both. Checks that read the whole project, such as the type checker, report findings in files you did not change, so the first push fails until you fix or ignore them.

Hooks in npm, pnpm, and Bun repositories start gspot from node_modules/.bin, so they need node on the PATH that the Git client gives hooks, and Yarn and mise repositories need yarn or mise there instead. That PATH can differ from the one in your terminal, so a hook can find an older Node.js and stop on the Node.js minimum. When a hook cannot start gspot, the commit stops, and Troubleshooting shows how to fix it.

If a finding does not apply to your code, add an ignore with a reason, as Ignore findings describes. Stage gspot.toml and the generated files that the command wrote with your change.

To turn the hooks on, run gspot set hooks.enabled true, then gspot install. To turn them off, run gspot set hooks.enabled false, and gspot deletes .gspot/hooks and unsets the Git setting core.hooksPath. gspot/weakenings then fails in CI until you give a reason.

If Husky, Lefthook, pre-commit, simple-git-hooks, or a hooks folder of your own already runs your Git hooks, gspot leaves it in charge, and gspot install prints what to add. A .githooks/ or .git-hooks/ folder counts only when core.hooksPath points into it. A hook without those lines runs no gspot checks. For a hook folder or simple-git-hooks, add each line to the hook of the same name:

# pre-commit
.gspot/hooks/pre-commit "$@"
# pre-push
.gspot/hooks/pre-push "$@"
# commit-msg
.gspot/hooks/commit-msg "$@"

Lefthook passes no push input to a command without use_stdin, so add this to lefthook.yml:

pre-commit:
commands:
gspot:
run: .gspot/hooks/pre-commit
pre-push:
commands:
gspot:
run: .gspot/hooks/pre-push {0}
use_stdin: true
commit-msg:
commands:
gspot:
run: .gspot/hooks/commit-msg {1}

pre-commit passes no push input to any hook, so the push checks run from .git/hooks/pre-push. Add this to the repos of .pre-commit-config.yaml:

- repo: local
hooks:
- id: gspot-pre-commit
name: gspot pre-commit
entry: .gspot/hooks/pre-commit
language: system
always_run: true
pass_filenames: false
stages: [pre-commit]
- id: gspot-commit-msg
name: gspot commit-msg
entry: .gspot/hooks/commit-msg
language: system
always_run: true
stages: [commit-msg]

Then run these commands in each clone, because .git/hooks is not tracked:

pre-commit install --hook-type pre-commit --hook-type commit-msg
printf '#!/bin/sh\n.gspot/hooks/pre-push "$@"\n' > .git/hooks/pre-push
chmod +x .git/hooks/pre-push

These commands run the checks of the pre-commit and commit-msg hooks without a commit:

gspot check --staged --only commit
gspot check --hook commit-msg -- .git/COMMIT_EDITMSG

The pre-push hook reads the remote and the list of pushed commits from Git, so test it with a real git push. After a failed push, the rerun: line of the report is gspot check --only <check> --base <hash>. <hash> is the commit that the remote had. For a new branch, it is the newest commit that the branch shares with its upstream, or else with the default branch of origin. Without such a commit, it is the first commit of the branch. To see the result without pushing, run gspot check --base origin/HEAD.

When a tracked file has the Git attribute filter=lfs, gspot writes the pre-push, post-checkout, post-commit, and post-merge hooks that Git Large File Storage (LFS) needs, so LFS needs no other hook manager. A filter=lfs pattern that matches no tracked file adds none of them. The pre-push hook runs the gspot checks before git lfs pre-push, so a push that the checks stop uploads no LFS file. If git-lfs is not on PATH, the hook exits with code 2 before any check runs, and asks you to install it.