just: Introduce commit-msg hook for typos

Add a `commit-msg` hook to continuously run `typos` against the COMMIT_MSG.
This commit is contained in:
Adrian Duke 2026-04-16 14:30:20 +01:00 committed by Fintan Halpenny
parent f6bf13422d
commit 179a080867
3 changed files with 33 additions and 6 deletions

View File

@ -1,4 +1,4 @@
hooks := "pre-commit pre-push post-checkout" hooks := "pre-commit pre-push post-checkout commit-msg"
hook-script := "scripts/git-hook-template.sh" hook-script := "scripts/git-hook-template.sh"
WARN := "⚠️ " + YELLOW + BOLD WARN := "⚠️ " + YELLOW + BOLD
@ -14,6 +14,12 @@ default: check-hooks
[group('hooks')] [group('hooks')]
post-checkout: post-checkout:
# Run commit-msg checks
[group('hooks')]
commit-msg file: (verify-tool "typos" "typos-cli")
@echo "{{CHECK}}Checking commit message for typos...{{NORMAL}}"
@NORMAL="{{NORMAL}}" WARN="{{WARN}}" scripts/just/commit-msg.sh "{{file}}"
# Run pre-commit checks # Run pre-commit checks
[group('hooks')] [group('hooks')]
pre-commit: format-rust check-rust check-docs check-typos check-spelling check-scripts check-keywords format-nix pre-commit: format-rust check-rust check-docs check-typos check-spelling check-scripts check-keywords format-nix
@ -66,8 +72,8 @@ check-spelling: (verify-tool "codespell")
@echo "{{CHECK}}Checking for code typos...{{NORMAL}}" @echo "{{CHECK}}Checking for code typos...{{NORMAL}}"
@git ls-files -z | xargs -0 codespell --write-changes --check-filenames @git ls-files -z | xargs -0 codespell --write-changes --check-filenames
# just runs with `/bin/sh` which has no doublestar glob # just runs with `/bin/sh` which has no doublestar glob
# expansion, furthermore, `time ls **/*.sh` takes ~5s # expansion, furthermore, `time ls **/*.sh` takes ~5s
# locally. The `find` solution below is fastest ~900ms. # locally. The `find` solution below is fastest ~900ms.
# #
# Run shellcheck on all shell scripts # Run shellcheck on all shell scripts

View File

@ -3,7 +3,7 @@ set -euo pipefail
readonly HOOK="${HOOK:-$(basename "$0")}" readonly HOOK="${HOOK:-$(basename "$0")}"
if ! [[ "$HOOK" =~ ^(pre-(commit|push)|post-checkout)$ ]] if ! [[ "$HOOK" =~ ^(pre-(commit|push)|post-checkout|commit-msg)$ ]]
then then
echo "Unknown hook '${HOOK}'." echo "Unknown hook '${HOOK}'."
exit 1 exit 1
@ -41,5 +41,10 @@ then
esac esac
fi fi
just "$HOOK" # Execute the appropriate just recipe based on the hook name.
if [ "$HOOK" = "commit-msg" ]
then
just "$HOOK" "$1"
else
just "$HOOK"
fi

16
scripts/just/commit-msg.sh Executable file
View File

@ -0,0 +1,16 @@
#! /usr/bin/env bash
set -euo pipefail
FILE=$1
while ! typos "$FILE"; do \
exec < /dev/tty; \
echo ""; \
printf "%sTypos found.%s (e)dit, (c)ontinue, or (a)bort? [E/c/a] " "${WARN}" "${NORMAL}"; \
read -r response; \
case "$response" in \
[cC]*) exit 0 ;; \
[aA]*) exit 1 ;; \
*) ${EDITOR:-nano} "$FILE" ;; \
esac; \
done