black_format.sh 762 B

12345678910111213141516171819202122232425
  1. #!/usr/bin/env bash
  2. # This script runs black on all Python files in the repo.
  3. set -uo pipefail
  4. # Apply black.
  5. echo -e "Formatting Python files..."
  6. PY_FILES=$(git ls-files -- '*SConstruct' '*SCsub' '*.py' ':!:.git/*' ':!:thirdparty/*')
  7. black -l 120 $PY_FILES
  8. diff=$(git diff --color)
  9. # If no patch has been generated all is OK, clean up, and exit.
  10. if [ -z "$diff" ] ; then
  11. printf "Files in this commit comply with the black style rules.\n"
  12. exit 0
  13. fi
  14. # A patch has been created, notify the user, clean up, and exit.
  15. printf "\n*** The following differences were found between the code "
  16. printf "and the formatting rules:\n\n"
  17. echo "$diff"
  18. printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
  19. exit 1