2020-07-13 15:34:34 +08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# This script runs black on all Python files in the repo.
|
|
|
|
|
|
|
|
set -uo pipefail
|
|
|
|
|
|
|
|
# Apply black.
|
|
|
|
echo -e "Formatting Python files..."
|
2022-02-01 16:55:07 +08:00
|
|
|
PY_FILES=$(git ls-files -- '*SConstruct' '*SCsub' '*.py' ':!:.git/*' ':!:thirdparty/*')
|
2020-07-13 15:34:34 +08:00
|
|
|
black -l 120 $PY_FILES
|
|
|
|
|
2022-01-28 00:34:33 +08:00
|
|
|
diff=$(git diff --color)
|
2020-07-13 15:34:34 +08:00
|
|
|
|
2022-04-23 06:13:08 +08:00
|
|
|
# If no diff has been generated all is OK, clean up, and exit.
|
2022-01-28 00:34:33 +08:00
|
|
|
if [ -z "$diff" ] ; then
|
2020-07-13 15:34:34 +08:00
|
|
|
printf "Files in this commit comply with the black style rules.\n"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2022-04-23 06:13:08 +08:00
|
|
|
# A diff has been created, notify the user, clean up, and exit.
|
2020-07-13 15:34:34 +08:00
|
|
|
printf "\n*** The following differences were found between the code "
|
|
|
|
printf "and the formatting rules:\n\n"
|
2022-01-28 00:34:33 +08:00
|
|
|
echo "$diff"
|
2020-07-13 15:34:34 +08:00
|
|
|
printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
|
|
|
|
exit 1
|