-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpre-commit
executable file
·64 lines (57 loc) · 2.48 KB
/
pre-commit
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
set -e
RELEASE=1.23.0
JAR_NAME="google-java-format-${RELEASE}-all-deps.jar"
RELEASES_URL=https://repo1.maven.org/maven2/com/google/googlejavaformat/google-java-format
JAR_URL="${RELEASES_URL}/${RELEASE}/${JAR_NAME}"
CACHE_DIR="$HOME/.cache/google-java-format-git-pre-commit-hook"
JAR_FILE="$CACHE_DIR/$JAR_NAME"
JAR_DOWNLOAD_FILE="${JAR_FILE}.tmp"
JAR_PGP_ASC_FILE="${JAR_FILE}.asc"
PGP_SIGN_KEY_ID=EE0CA873074092F806F59B65D364ABAA39A47320
if [[ ! -f "$JAR_FILE" ]]
then
mkdir -p "$CACHE_DIR"
curl -L "$JAR_URL" -o "$JAR_DOWNLOAD_FILE"
# Downloaded from: "${JAR_URL}.asc"
cat >"$JAR_PGP_ASC_FILE" <<EOF
-----BEGIN PGP SIGNATURE-----
iQIzBAABCgAdFiEE7gyocwdAkvgG9Ztl02SrqjmkcyAFAmapT68ACgkQ02Srqjmk
cyCN/Q//XICI70a9pH07FcWHsH416mk12nF7+3OugY2FH0j9TlwXL5FQB9Nq/pWt
n5btRA+uNcaangFrKVfMYOVyLbaa3udahFOEQ2ewxqPTO8sZ4q+sWABdpKhzKjOg
KE0hESZvs2QMvfXRbUYEGEDKTXaUP8U3I/Xuj0mnVPaucEZXGMuu1/2BhBinD4J1
T7mAUCmX43HboFU98MvinZ70hW+ON8RD4IarHE8dCEHn/lSeFZFAW9jgUTQGcwcb
VLt6CI1xx9CG3SmnMMPB1LUFPpyI/XryWJDI4WZKIx8OOiCoKvimXMcqZXcUpViL
7qYuObb6xrg719nsHnGLkQkI67mRA+Ckhqzr9Ml2U4aZOJK9/0+wykd4TLDFuJjo
TcxrmHdpBaOXnaiQhf2tNHqZGXY2uclbGkmvTKlnkcZ4s1yX0hS4Lv/Bf7wrKUsY
iO+3vOlkxu5UoJUWzWZzb0R5BPKIvIIoZUAYsw3tMNffPVj2tV/qYSY/GlopjNsB
qkC92rg6/Ou6c7+Q6mzoiWVFIWD63iFLfrT83L2SNZ3IivpkBGXqJ2JEsrs15804
bOPsNgv2aj/amU5YG8f8T9EqtclfrOBmtLDSULUtBYi+/l7wVk+fFs6IAN8hqnDs
d9mk7DgjBHX+wJVrOCQ0u70AaXji2gAFUpxHnA+oD0hUSiWAnUE=
=ZwP8
-----END PGP SIGNATURE-----
EOF
if ! gpg --list-keys "$PGP_SIGN_KEY_ID"
then
gpg --keyserver hkps://keyserver.ubuntu.com --recv-key "$PGP_SIGN_KEY_ID"
fi
gpg --verify "$JAR_PGP_ASC_FILE" "$JAR_DOWNLOAD_FILE"
mv "$JAR_DOWNLOAD_FILE" "$JAR_FILE"
fi
changed_java_files=$(git diff --cached --name-only --diff-filter=ACMR | grep ".*java$" || true)
if [[ -n "$changed_java_files" ]]
then
echo "Reformatting Java files: $changed_java_files"
if ! java --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
-jar "$JAR_FILE" --replace --set-exit-if-changed $changed_java_files
then
echo "An error occurred, aborting commit!" >&2
exit 1
fi
else
echo "No Java files changes found."
fi