aboutsummaryrefslogtreecommitdiffstats
path: root/.hooks/pre-commit
blob: 0f4c49a3504ba841f04a810e3825128b94868374 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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