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