Merge pull request #18500 from eileenmcnaughton/loc
[civicrm-core.git] / tools / scripts / git / pre-commit
1 #!/bin/bash
2
3 # Note: This shell script should be designed and tested for cross-platform use
4
5 # URL: https://github.com/totten/git-php-syntax-checker/
6 # Author: Remigijus Jarmalavičius <remigijus@jarmalavicius.lt>
7 # Author: Vytautas Povilaitis <php-checker@vytux.lt>
8 # License: GPLv3
9
10 ROOT_DIR="$(pwd)/"
11 LIST=$(git diff-index --cached --name-only --diff-filter=ACMR HEAD)
12 ERRORS_BUFFER=""
13 for file in $LIST
14 do
15 EXTENSION=$(echo "$file" | grep -E ".php$|.module$|.inc$|.install$")
16 if [ "$EXTENSION" != "" ]; then
17 ERRORS=$(php -l $ROOT_DIR$file 2>&1 | grep "Parse error")
18 if [ "$ERRORS" != "" ]; then
19 if [ "$ERRORS_BUFFER" != "" ]; then
20 ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
21 else
22 ERRORS_BUFFER="$ERRORS"
23 fi
24 echo "Syntax errors found in file: $file "
25 fi
26 fi
27 done
28 if [ "$ERRORS_BUFFER" != "" ]; then
29 echo
30 echo "These errors were found in try-to-commit files: "
31 echo -e $ERRORS_BUFFER
32 echo
33 echo "Can't commit, fix errors first."
34 exit 1
35 else
36 echo "Committed successfully."
37 fi