pre-commit.git-hook 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 committing your changeset if
  7. # it fails our code formatting, changelog entry formatting, module include
  8. # rules, etc...
  9. # Run only if this environment variable is set.
  10. if [ -z "$TOR_EXTRA_PRE_COMMIT_CHECKS" ]; then
  11. exit 0
  12. fi
  13. workdir=$(git rev-parse --show-toplevel)
  14. cd "$workdir" || exit 1
  15. set -e
  16. if [ $# -eq 0 ]; then
  17. # When called in pre-commit, check the files modified in this commit
  18. CHECK_FILTER="git diff --cached --name-only --diff-filter=ACMR"
  19. # Use the appropriate owned tor source list to filter the changed files
  20. # This is the layout in 0.3.5 and later.
  21. # Keep these lists consistent:
  22. # - OWNED_TOR_C_FILES in Makefile.am
  23. # - CHECK_FILES in pre-commit.git-hook and pre-push.git-hook
  24. # - try_parse in check_cocci_parse.sh
  25. CHECK_FILES="$($CHECK_FILTER \
  26. src/lib/*/*.[ch] \
  27. src/core/*/*.[ch] \
  28. src/feature/*/*.[ch] \
  29. src/app/*/*.[ch] \
  30. src/test/*.[ch] \
  31. src/test/*/*.[ch] \
  32. src/tools/*.[ch] \
  33. )"
  34. else
  35. # When called in pre-push, concatenate the argument array
  36. # Fails on special characters in file names
  37. CHECK_FILES="$*"
  38. fi
  39. ## General File Checks
  40. if [ -n "$(ls ./changes/)" ]; then
  41. python scripts/maint/lintChanges.py ./changes/*
  42. fi
  43. if [ -e scripts/maint/checkShellScripts.sh ]; then
  44. scripts/maint/checkShellScripts.sh
  45. fi
  46. if [ -e scripts/maint/checkSpaceTest.sh ]; then
  47. scripts/maint/checkSpaceTest.sh
  48. fi
  49. if [ ! "$CHECK_FILES" ]; then
  50. echo "No modified tor-owned source files, skipping further checks"
  51. exit 0
  52. fi
  53. ## Owned Source File Checks
  54. printf "Modified tor-owned source files:\\n%s\\n" "$CHECK_FILES"
  55. # We want word splitting here, because file names are space separated
  56. # shellcheck disable=SC2086
  57. perl scripts/maint/checkSpace.pl -C \
  58. $CHECK_FILES
  59. # This makes sure that we are only including things we're allowed to include.
  60. if test -e scripts/maint/practracker/includes.py; then
  61. python scripts/maint/practracker/includes.py
  62. fi
  63. if [ -e scripts/coccinelle/check_cocci_parse.sh ]; then
  64. # Run a verbose cocci parse check on the changed files
  65. # (spatch is slow, so we don't want to check all the files.)
  66. #
  67. # We want word splitting here, because file names are space separated
  68. # shellcheck disable=SC2086
  69. VERBOSE=1 scripts/coccinelle/check_cocci_parse.sh \
  70. $CHECK_FILES
  71. fi