hooks: apply upstream pre-commit changes

This commit is contained in:
teutat3s 2022-08-02 15:39:03 +02:00
parent 7749667888
commit 0fc1a7fe20
Signed by: teutat3s
GPG key ID: 4FA1D3FA524F22C1

View file

@ -13,25 +13,29 @@ diff="git diff-index --name-only --cached $against --diff-filter d"
mapfile -t nix_files < <($diff -- '*.nix') mapfile -t nix_files < <($diff -- '*.nix')
mapfile -t all_files < <($diff) mapfile -t all_files < <($diff)
# Format staged nix files. # Format staged nix files
if [[ -n "${nix_files[@]}" ]]; then if (( ${#nix_files[@]} != 0 )); then
# stash only unstaged changes, keeping staged changes # Stash only unstaged changes, keeping staged changes
old_stash=$(git rev-parse --quiet --verify refs/stash) old_stash=$(git rev-parse --quiet --verify refs/stash)
git stash push --quiet --keep-index -m 'Unstaged changes before pre-commit hook' git stash push --quiet --keep-index -m 'Unstaged changes before pre-commit hook'
new_stash=$(git rev-parse --quiet --verify refs/stash) new_stash=$(git rev-parse --quiet --verify refs/stash)
# format staged changes # Format staged changes
nixpkgs-fmt "${nix_files[@]}" \ nixpkgs-fmt "${nix_files[@]}" \
&& git add "${nix_files[@]}" && git add "${nix_files[@]}"
# if unstaged changes were stashed reapply to working tree # If unstaged changes were stashed re-apply to working tree
if [ "$old_stash" != "$new_stash" ]; then if [ "$old_stash" != "$new_stash" ]; then
git stash pop --quiet git stash pop --quiet
fi fi
fi fi
# check editorconfig # Check editorconfig
if ! editorconfig-checker -- "${all_files[@]}"; then if (( ${#all_files[@]} != 0 )); then
editorconfig-checker -- "${all_files[@]}"
fi
if [[ $? != '0' ]]; then
printf "%b\n" \ printf "%b\n" \
"\nCode is not aligned with .editorconfig" \ "\nCode is not aligned with .editorconfig" \
"Review the output and commit your fixes" >&2 "Review the output and commit your fixes" >&2