forked from maltzj/google-style-precommit-hook
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat-code.sh
More file actions
executable file
·36 lines (32 loc) · 1.09 KB
/
Copy pathformat-code.sh
File metadata and controls
executable file
·36 lines (32 loc) · 1.09 KB
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
#!/usr/bin/env sh
RELEASE=1.8
JAR_NAME="google-java-format-${RELEASE}-all-deps.jar"
RELEASES_URL=https://github.com/google/google-java-format/releases/download
JAR_URL="${RELEASES_URL}/google-java-format-${RELEASE}/${JAR_NAME}"
CACHE_DIR="$HOME/.cache/google-java-format-git-pre-commit-hook"
JAR_FILE="$CACHE_DIR/$JAR_NAME"
echo "=================================="
echo "Running pre-commit code formatter"
echo "=================================="
if [[ ! -f "$JAR_FILE" ]]
then
echo "Setting up google-java-format"
mkdir -p "$CACHE_DIR"
curl -L "$JAR_URL" -o "$JAR_FILE"
fi
changed_java_files=$(git diff --cached --name-only --diff-filter=ACMR | grep ".*java$" || true)
if [[ -n "$changed_java_files" ]]
then
echo "The following files will checked and reformatted:"
echo "$changed_java_files"
if ! java -jar "$JAR_FILE" --replace --set-exit-if-changed $changed_java_files
then
echo "\nBad dod no biscuit. Some files were reformatted!"
else
echo "All good!"
fi
git add $changed_java_files
else
echo "All good!"
fi
echo "=================================="