pre-commit.git-hook 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env bash
  2. #
  3. # To install this script, copy it to .git/hooks/pre-commit in local copy of
  4. # tor git repo and make sure it has permission to execute.
  5. #
  6. # This is pre-commit git hook script that prevents commiting your changeset if
  7. # it fails our code formatting, changelog entry formatting, module include
  8. # rules, or best practices tracker.
  9. workdir=$(git rev-parse --show-toplevel)
  10. cd "$workdir" || exit 1
  11. set -e
  12. if [ $# -eq 0 ]; then
  13. # When called in pre-commit, check the files modified in this commit
  14. CHECK_FILTER="git diff --cached --name-only --diff-filter=ACMR"
  15. # Use the appropriate owned tor source list to filter the changed files
  16. # This is the layout in 0.3.5 and later.
  17. # Keep these lists consistent:
  18. # - OWNED_TOR_C_FILES in Makefile.am
  19. # - CHECK_FILES in pre-commit.git-hook and pre-push.git-hook
  20. # - try_parse in check_cocci_parse.sh
  21. CHECK_FILES="$($CHECK_FILTER \
  22. src/lib/*/*.[ch] \
  23. src/core/*/*.[ch] \
  24. src/feature/*/*.[ch] \
  25. src/app/*/*.[ch] \
  26. src/test/*.[ch] \
  27. src/test/*/*.[ch] \
  28. src/tools/*.[ch] \
  29. )"
  30. else
  31. # When called in pre-push, concatenate the argument array
  32. # Fails on special characters in file names
  33. CHECK_FILES="$*"
  34. fi
  35. ## General File Checks
  36. if [ -n "$(ls ./changes/)" ]; then
  37. python scripts/maint/lintChanges.py ./changes/*
  38. fi
  39. if [ -e scripts/maint/checkShellScripts.sh ]; then
  40. scripts/maint/checkShellScripts.sh
  41. fi
  42. # Always run the practracker unit tests
  43. PT_DIR=scripts/maint/practracker
  44. if [ -e "${PT_DIR}/test_practracker.sh" ]; then
  45. "${PT_DIR}/test_practracker.sh"
  46. fi
  47. if [ -e scripts/maint/checkSpaceTest.sh ]; then
  48. scripts/maint/checkSpaceTest.sh
  49. fi
  50. if [ ! "$CHECK_FILES" ]; then
  51. echo "No modified tor-owned source files, skipping further checks"
  52. exit 0
  53. fi
  54. ## Owned Source File Checks
  55. printf "Modified tor-owned source files:\\n%s\\n" "$CHECK_FILES"
  56. # We want word splitting here, because file names are space separated
  57. # shellcheck disable=SC2086
  58. perl scripts/maint/checkSpace.pl -C \
  59. $CHECK_FILES
  60. if test -e scripts/maint/practracker/includes.py; then
  61. python scripts/maint/practracker/includes.py
  62. fi
  63. # Only call practracker if ${PT_DIR}/.enable_practracker_in_hooks exists
  64. # We do this check so that we can enable practracker in hooks in master, and
  65. # disable it on maint branches
  66. if [ -e "${PT_DIR}/practracker.py" ]; then
  67. if [ -e "${PT_DIR}/.enable_practracker_in_hooks" ]; then
  68. python3 "${PT_DIR}/practracker.py" "$workdir"
  69. fi
  70. fi
  71. if [ -e scripts/coccinelle/check_cocci_parse.sh ]; then
  72. # Run a verbose cocci parse check on the changed files
  73. # (spatch is slow, so we don't want to check all the files.)
  74. #
  75. # We want word splitting here, because file names are space separated
  76. # shellcheck disable=SC2086
  77. VERBOSE=1 scripts/coccinelle/check_cocci_parse.sh \
  78. $CHECK_FILES
  79. fi