diff --git a/hooks/pre-commit b/hooks/pre-commit new file mode 100755 index 0000000..7c1296a --- /dev/null +++ b/hooks/pre-commit @@ -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||${file}|"); + RESULT=$(( RESULT || $? )); + fi; +done; + +exit ${RESULT};