Added git hooks scripts for java formatting
This commit is contained in:
parent
ad8c408645
commit
cbac8fc8db
3 changed files with 51 additions and 0 deletions
14
git-hooks/format.sh
Executable file
14
git-hooks/format.sh
Executable file
|
@ -0,0 +1,14 @@
|
||||||
|
#!/bin/sh -e
|
||||||
|
jar_version=1.6
|
||||||
|
jar_dir="$HOME/.local/share/java"
|
||||||
|
jar_file="$jar_dir/google-java-format-$jar_version-all-deps.jar"
|
||||||
|
java_cmd="java"
|
||||||
|
|
||||||
|
# download jar file if missing
|
||||||
|
if [ ! -f "$jar_file" ]; then
|
||||||
|
mkdir -p "$jar_dir"
|
||||||
|
wget -O "$jar_file" https://github.com/google/google-java-format/releases/download/google-java-format-$jar_version/google-java-format-$jar_version-all-deps.jar
|
||||||
|
fi
|
||||||
|
|
||||||
|
# execute formatter
|
||||||
|
$java_cmd -jar "$jar_file" $@
|
18
git-hooks/pre-commit.sh
Executable file
18
git-hooks/pre-commit.sh
Executable file
|
@ -0,0 +1,18 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "Java formatter running..."
|
||||||
|
|
||||||
|
format_cmd="$(dirname $(realpath "$0"))/format.sh"
|
||||||
|
|
||||||
|
# skip if NO_VERIFY env var set
|
||||||
|
if [ "$NO_VERIFY" ]; then
|
||||||
|
echo 'google-java-format skipped' 1>&2
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# list all added/copied/modified/renamed java files
|
||||||
|
git diff --staged --name-only --diff-filter=ACMR | egrep -a '.java$' | tr "\n" "\0" |
|
||||||
|
# run google-java-format on each file and re-stage any new changes
|
||||||
|
xargs -0 -I % echo "$format_cmd --aosp -i '%'; git add -f '%'" | sh
|
19
git-hooks/setup.sh
Executable file
19
git-hooks/setup.sh
Executable file
|
@ -0,0 +1,19 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
if ! git remote get-url origin | grep "lab.si.usi.ch" >/dev/null 2>/dev/null; then
|
||||||
|
echo "Not in the project!"
|
||||||
|
echo "Call this script while in the root directory of the backend project";
|
||||||
|
exit 1;
|
||||||
|
elif ! [ -d "./git-hooks" ]; then
|
||||||
|
echo "Not in the right directory!"
|
||||||
|
echo "Call this script while in the root directory of the backend project";
|
||||||
|
exit 1;
|
||||||
|
fi;
|
||||||
|
|
||||||
|
git config --unset core.hooksPath
|
||||||
|
|
||||||
|
this_dir="$(dirname $(realpath "$0"))"
|
||||||
|
hook_script="$this_dir/pre-commit.sh"
|
||||||
|
ln -svf "$hook_script" "$this_dir/../.git/hooks/pre-commit"
|
||||||
|
|
||||||
|
echo "Commit hook installed"
|
Loading…
Reference in a new issue