os/shell/hooks/pre-commit.sh

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
1.4 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
if git rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(${git}/bin/git hash-object -t tree /dev/null)
fi
# Stash only unstaged changes, keeping staged changes
# We have to stash two times, because:
# --keep-index also stashes the staged changes.
# The staged changes end up in both the stage AND the stash.
# https://overflow.hostux.net/questions/7650797/how-to-stash-only-unstaged-changes-in-git#60875082
old_stash=$(git rev-parse --quiet --verify refs/stash)
git stash push --quiet --staged --message "Staged changes before pre-commit hook"
git stash push --quiet --keep-index --message "Unstaged changes before pre-commit hook"
git stash pop --quiet --index "stash@{1}"
new_stash=$(git rev-parse --quiet --verify refs/stash)
diff="git diff-index --name-only --cached $against --diff-filter d"
mapfile -t all_files < <($diff)
# Format staged files
if ((${#all_files[@]} != 0)); then
treefmt "${all_files[@]}" &&
git add "${all_files[@]}"
fi
# If unstaged changes were stashed re-apply to working tree
if [ "$old_stash" != "$new_stash" ]; then
git stash pop --quiet
fi
# Check editorconfig
if ((${#all_files[@]} != 0)); then
if ! editorconfig-checker -- "${all_files[@]}"; then
printf "%b\n" \
"\nCode is not aligned with .editorconfig" \
"Review the output and commit your fixes" >&2
exit 1
fi
fi