| #!/bin/sh | |
| # This file will be used as .git/hooks/pre-commit . | |
| # However, it should be edited as .git.pre-commit . | |
| # You can install it by running: ant prep | |
| # Fail if any command fails | |
| set -e | |
| # "ant -e check-style" would check every file; on commit we only need to | |
| # check files that changed. | |
| # Need to keep checked files in sync with formatted.java.files in | |
| # build.xml. Otherwise `ant reformat` might not reformat a file that this | |
| # hook complains about. | |
| CHANGED_JAVA_FILES=`git diff --staged --name-only --diff-filter=ACM | grep '\.java$' | grep -v '/jdk/' | grep -v 'stubparser/' | grep -v '/eclipse/' ` || true | |
| # echo CHANGED_JAVA_FILES "'"${CHANGED_JAVA_FILES}"'" | |
| if [ ! -z "$CHANGED_JAVA_FILES" ]; then | |
| (((cd .run-google-java-format && git pull -q) || git clone -q https://github.com/plume-lib/run-google-java-format.git .run-google-java-format) || true) | |
| ## For debugging: | |
| # echo "CHANGED_JAVA_FILES: ${CHANGED_JAVA_FILES}" | |
| python .run-google-java-format/check-google-java-format.py --aosp ${CHANGED_JAVA_FILES} | |
| fi | |
| # This is to handle non-.java files, since the above already handled .java files. | |
| # May need to remove files that are allowed to have trailing whitespace. | |
| CHANGED_STYLE_FILES=`git diff --staged --name-only --diff-filter=ACM | grep -v '.png$' | grep -v '.xcf$'` || true | |
| if [ ! -z "$CHANGED_STYLE_FILES" ]; then | |
| # echo "CHANGED_STYLE_FILES: ${CHANGED_STYLE_FILES}" | |
| grep -q '[[:blank:]]$' ${CHANGED_STYLE_FILES} 2>&1 && (echo "Some files have trailing whitespace:" && grep -l '[[:blank:]]$' ${CHANGED_STYLE_FILES} && exit 1) | |
| fi |