nixos/hooks/pre-commit

23 lines
496 B
Text
Raw Permalink Normal View History

#!/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};