Created
January 25, 2019 01:25
-
-
Save bskinner/7ec7483701ad9a00701659e16ff7fbb8 to your computer and use it in GitHub Desktop.
checkstyle pre-commit hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euf -o pipefail | |
| function cleanup { | |
| # Reset the trap handler so it's not called twice | |
| trap - EXIT | |
| } | |
| trap cleanup EXIT | |
| #### | |
| ## Main Body | |
| CHECKSTYLE_URL="https://github.com/checkstyle/checkstyle/releases/download/checkstyle-8.11/checkstyle-8.11-all.jar" | |
| CHECKSTYLE_JAR=".checkstyle.jar" | |
| CHECKSTYLE_CONFIG="pepper-apis/checkstyle.xml" | |
| function get_checkstyle_jar() { | |
| local curl_path=$(which curl 2>/dev/null) | |
| local wget_path=$(which wget 2>/dev/null) | |
| if [[ -n "${curl_path}" ]]; then | |
| "${curl_path}" \ | |
| -sL \ | |
| -o "${CHECKSTYLE_JAR}" \ | |
| "${CHECKSTYLE_URL}" | |
| elif [[ -n "${wget_path}" ]]; then | |
| "${wget_path}" \ | |
| -o "${CHECKSTYLE_JAR}" \ | |
| "${CHECKSTYLE_URL}" | |
| else | |
| echo "error: unable to find wget or curl" | |
| return -1 | |
| fi | |
| } | |
| if [[ ! -f "${CHECKSTYLE_JAR}" ]]; then | |
| echo "checkstyle not found- fetching" | |
| get_checkstyle_jar | |
| fi | |
| if git rev-parse --verify HEAD >/dev/null 2>&1; then | |
| against=HEAD | |
| else | |
| # Initial commit: diff against an empty tree object | |
| against=$(git hash-object -t tree /dev/null) | |
| fi | |
| IFS=$'\n' | |
| changed_files=($(git diff --cached --name-only --diff-filter=ACM ${against})) | |
| unset IFS | |
| java_files=() | |
| for file in ${changed_files[@]}; do | |
| case "${file}" in | |
| *.java ) | |
| java_files+=("${file}") | |
| ;; | |
| *) | |
| ;; | |
| esac | |
| done | |
| java \ | |
| -jar "${CHECKSTYLE_JAR}" \ | |
| -c "${CHECKSTYLE_CONFIG}" \ | |
| $( IFS=' '; echo "${java_files[*]}" ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment