forked from clan/clan-core
Jörg Thalheim
f46fd3ace6
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.
29 lines
567 B
Bash
Executable File
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
|