From 1f638176847bc7a572aafa3705b8881915da5ab2 Mon Sep 17 00:00:00 2001 From: derped Date: Wed, 20 Nov 2024 20:30:51 +0100 Subject: [PATCH] Add pre-commit hook script to run nixfmt check. --- hooks/pre-commit | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100755 hooks/pre-commit 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};