Add pre-commit hook script to run nixfmt check.

This commit is contained in:
Kevin Baensch 2024-11-20 20:30:51 +01:00
parent b7f2e439c8
commit 1f63817684
Signed by: derped
GPG key ID: C0F1D326C7626543

22
hooks/pre-commit Executable file
View file

@ -0,0 +1,22 @@
#!/usr/bin/env bash
printf "%s\n" "Running nixfmt pre-commit hook.";
if ! which grep >/dev/null; then
printf '%s\n' "ERR: grep not found.";
exit 1;
fi;
if ! which nixfmt >/dev/null; then
printf '%s\n' "ERR: nixfmt not found.";
exit 1;
fi;
RESULT=0;
for file in $(git diff --name-only --cached); do
if grep -q "\.nix$" <<< "${file}"; then
nixfmt -c <<< "$(git cat-file blob :"${file}")" 2> >(sed "s|<stdin>|${file}|");
RESULT=$(( RESULT || $? ));
fi;
done;
exit ${RESULT};