2021-07-08 00:31:12 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2022-11-20 22:10:39 +00:00
|
|
|
if git rev-parse --verify HEAD >/dev/null 2>&1; then
|
2021-07-08 00:31:12 +00:00
|
|
|
against=HEAD
|
|
|
|
else
|
|
|
|
# Initial commit: diff against an empty tree object
|
2022-11-20 22:10:39 +00:00
|
|
|
against=$(${git}/bin/git hash-object -t tree /dev/null)
|
2021-07-08 00:31:12 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
diff="git diff-index --name-only --cached $against --diff-filter d"
|
|
|
|
|
2022-11-20 22:10:39 +00:00
|
|
|
all_files=($($diff))
|
2021-07-08 00:31:12 +00:00
|
|
|
|
2022-11-20 22:10:39 +00:00
|
|
|
# Format staged files.
|
|
|
|
if ((${#all_files[@]} != 0)); then
|
|
|
|
treefmt "${all_files[@]}" &&
|
|
|
|
git add "${all_files[@]}"
|
2021-07-08 00:31:12 +00:00
|
|
|
fi
|
|
|
|
|
2022-11-20 22:10:39 +00:00
|
|
|
# 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
|
2021-07-08 00:31:12 +00:00
|
|
|
fi
|