diff options
author | Tharre <tharre3@gmail.com> | 2018-04-23 02:46:40 +0200 |
---|---|---|
committer | Tharre <tharre3@gmail.com> | 2018-04-27 00:24:09 +0200 |
commit | 96a4b8c279940c22b5db62b88d7987ec6cadef51 (patch) | |
tree | 65f631400b5b00fd7efecc00e733f6ad816312fb /.hooks | |
parent | 875319524d220e10564d6c3e19783d298f09d5c4 (diff) | |
download | sepm-groupproject-96a4b8c279940c22b5db62b88d7987ec6cadef51.tar.gz sepm-groupproject-96a4b8c279940c22b5db62b88d7987ec6cadef51.tar.xz sepm-groupproject-96a4b8c279940c22b5db62b88d7987ec6cadef51.zip |
Add pre-commit hook that checks code formatting
Diffstat (limited to '.hooks')
-rwxr-xr-x | .hooks/pre-commit | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/.hooks/pre-commit b/.hooks/pre-commit new file mode 100755 index 0000000..0f4c49a --- /dev/null +++ b/.hooks/pre-commit @@ -0,0 +1,34 @@ +#!/bin/sh + +trap 'rm -rf "$temp_dir"' EXIT +temp_dir=".tmp" + +IFS=' +' +for staged_file in $(git diff --cached --name-only); do + [ -f "${staged_file}" ] || continue + [ "${staged_file##*.}" = "java" ] || continue + + check_files=y + mkdir -p "$temp_dir/$(dirname "$staged_file")" + git show :"$staged_file" > "$temp_dir/$staged_file" +done +unset IFS + +[ -z "$check_files" ] && exit 0 + +printf "%s\n" "-a -n --set-exit-if-changed" > "$temp_dir/gf_config" + +find "$temp_dir" -name '*.java' >> "$temp_dir/gf_config" + +output=$(./mvnw -q exec:java \ + -Dexec.mainClass="com.google.googlejavaformat.java.Main" \ + -Dexec.args="@$temp_dir/gf_config") + +if [ $? != 0 ]; then + echo 'Run "./mvnw -q com.coveo:fmt-maven-plugin:format" first.' >&2 + echo "Following files were rejected by the formatter:" >&2 + escaped=$(printf '%s\n' "$temp_dir/" | sed 's/[[\.*^$/]/\\&/g') + printf "%s\n" "$output" | sed "s/$escaped/ /" >&2 + exit 1 +fi |