1
0
forked from clan/clan-core
clan-core/scripts/pre-commit
Jörg Thalheim f46fd3ace6 make git-hooks opt-in
pre-commit hook break git commits and are disruptive.
Therefore people that want to enable this feature, should enable it locally instead.
I.e. treefmt will also check untracked files that are not meant for the current commit.
2024-05-27 11:08:17 +02:00

29 lines
567 B
Bash
Executable File

#!/usr/bin/env bash
# To install:
# ln -sf ../../scripts/pre-commit .git/hooks/pre-commit
set -o errexit
set -o nounset
set -o pipefail
readarray staged < <(git diff --name-only --cached)
[[ ${#staged[@]} = 0 ]] && exit
unstash() {
local ret=$?
set +e
git stash pop -q
exit "$ret"
}
git stash push --quiet --keep-index --message "pre-commit"
trap unstash EXIT
nix fmt
{
changed=$(git diff --name-only --exit-code);
status=$?;
} || true
if [[ $status -ne 0 ]]; then
exec 1>&2
echo Files changed by pre-commit hook:
echo "$changed"
exit $status
fi