pre-commit 881 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/bin/sh
  2. # Copyright 2013,2017 Darren Bane. All rights reserved.
  3. # Use of this source code is governed by the
  4. # license that can be found in the NOTICE file.
  5. # git indent pre-commit hook
  6. #
  7. # To use, symlink to .git/hooks/pre-commit inside your repository and make sure
  8. # it has execute permissions.
  9. #
  10. # This script does not handle file names that contain spaces.
  11. cppfiles=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|h)$')
  12. [ -z "$cppfiles" ] && exit 0
  13. unformatted=""
  14. for fn in $cppfiles; do
  15. astyle < $fn > /tmp/indent.$$
  16. if ! diff $fn /tmp/indent.$$ > /dev/null; then
  17. unformatted="$unformatted $fn";
  18. fi
  19. done
  20. rm /tmp/indent.$$
  21. [ -z "$unformatted" ] && exit 0
  22. # Some files are not indented. Print message and fail.
  23. echo >&2 "C++ files must be formatted with astyle. Please run:"
  24. for fn in $unformatted; do
  25. echo >&2 " astyle $PWD/$fn"
  26. done
  27. exit 1