pre-commit 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env bash
  2. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  3. # file Copyright.txt or https://cmake.org/licensing for details.
  4. die() {
  5. echo 'pre-commit hook failure' 1>&2
  6. echo '-----------------------' 1>&2
  7. echo '' 1>&2
  8. echo "$@" 1>&2
  9. exit 1
  10. }
  11. #-------------------------------------------------------------------------------
  12. line_too_long=80
  13. bad=$(regex=".{$line_too_long}" &&
  14. git diff-index --cached HEAD --name-only --diff-filter=AM \
  15. --pickaxe-regex -S"$regex" -- 'Source/*.h' 'Source/*.cxx' |
  16. while read file; do
  17. lines_too_long=$(git diff-index -p --cached HEAD \
  18. --pickaxe-regex -S"$regex" -- "$file")
  19. if echo "$lines_too_long" | egrep -q '^\+'"$regex"; then
  20. echo "$lines_too_long"
  21. fi
  22. done)
  23. test -z "$bad" ||
  24. die 'The following changes add lines too long for our C++ style:
  25. '"$bad"'
  26. Use lines strictly less than '"$line_too_long"' characters in C++ code.'
  27. #-----------------------------------------------------------------------------
  28. # Check that development setup is up-to-date.
  29. lastSetupForDevelopment=$(git config --get hooks.SetupForDevelopment || echo 0)
  30. eval $(grep '^SetupForDevelopment_VERSION=' "${BASH_SOURCE%/*}/../SetupForDevelopment.sh")
  31. test -n "$SetupForDevelopment_VERSION" || SetupForDevelopment_VERSION=0
  32. if test $lastSetupForDevelopment -lt $SetupForDevelopment_VERSION; then
  33. die 'Developer setup in this work tree is out of date. Please re-run
  34. Utilities/SetupForDevelopment.sh
  35. '
  36. fi
  37. #-------------------------------------------------------------------------------
  38. if test -z "$HOOKS_ALLOW_KWSYS"; then
  39. # Disallow changes to KWSys
  40. files=$(git diff-index --name-only --cached HEAD -- Source/kwsys) &&
  41. if test -n "$files"; then
  42. die 'Changes to KWSys files
  43. '"$(echo "$files" | sed 's/^/ /')"'
  44. should not be made directly in CMake. KWSys is kept in its own Git
  45. repository and shared by several projects. Please visit
  46. https://gitlab.kitware.com/utils/kwsys
  47. to contribute changes directly to KWSys. Run
  48. git reset HEAD -- Source/kwsys
  49. to unstage these changes. Alternatively, set environment variable
  50. HOOKS_ALLOW_KWSYS=1
  51. to disable this check and commit the changes locally.'
  52. fi
  53. fi