pre-commit 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. if test -z "$HOOKS_ALLOW_KWSYS"; then
  29. # Disallow changes to KWSys
  30. files=$(git diff-index --name-only --cached HEAD -- Source/kwsys) &&
  31. if test -n "$files"; then
  32. die 'Changes to KWSys files
  33. '"$(echo "$files" | sed 's/^/ /')"'
  34. should not be made directly in CMake. KWSys is kept in its own Git
  35. repository and shared by several projects. Please visit
  36. https://gitlab.kitware.com/utils/kwsys
  37. to contribute changes directly to KWSys. Run
  38. git reset HEAD -- Source/kwsys
  39. to unstage these changes. Alternatively, set environment variable
  40. HOOKS_ALLOW_KWSYS=1
  41. to disable this check and commit the changes locally.'
  42. fi
  43. fi