From cbac8fc8db561500f32a424c8f124f3b063fca55 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Mon, 24 Feb 2020 14:12:51 +0100 Subject: [PATCH 1/2] Added git hooks scripts for java formatting --- git-hooks/format.sh | 14 ++++++++++++++ git-hooks/pre-commit.sh | 18 ++++++++++++++++++ git-hooks/setup.sh | 19 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100755 git-hooks/format.sh create mode 100755 git-hooks/pre-commit.sh create mode 100755 git-hooks/setup.sh diff --git a/git-hooks/format.sh b/git-hooks/format.sh new file mode 100755 index 0000000..c972530 --- /dev/null +++ b/git-hooks/format.sh @@ -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" $@ diff --git a/git-hooks/pre-commit.sh b/git-hooks/pre-commit.sh new file mode 100755 index 0000000..68ab401 --- /dev/null +++ b/git-hooks/pre-commit.sh @@ -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 diff --git a/git-hooks/setup.sh b/git-hooks/setup.sh new file mode 100755 index 0000000..b04c8d4 --- /dev/null +++ b/git-hooks/setup.sh @@ -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" From f8c268111cdaf9b5527acddeebc14aa463e1e2fd Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Mon, 24 Feb 2020 14:15:49 +0100 Subject: [PATCH 2/2] Formatted code according to google java (aosp) format --- .../ch/usi/inf/sa4/sanmarinoes/smarthut/HelloWorld.java | 2 +- .../inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java | 7 +++---- .../sa4/sanmarinoes/smarthut/SmarthutApplicationTests.java | 6 ++---- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/HelloWorld.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/HelloWorld.java index 2abbbf2..e1fda82 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/HelloWorld.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/HelloWorld.java @@ -12,4 +12,4 @@ public class HelloWorld { String home() { return "Hello World!"; } -} \ No newline at end of file +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java index 4c956eb..ba489c5 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java @@ -6,8 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SmarthutApplication { - public static void main(String[] args) { - SpringApplication.run(SmarthutApplication.class, args); - } - + public static void main(String[] args) { + SpringApplication.run(SmarthutApplication.class, args); + } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplicationTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplicationTests.java index 44a8af9..5f1f8fd 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplicationTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplicationTests.java @@ -6,8 +6,6 @@ import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class SmarthutApplicationTests { - @Test - void contextLoads() { - } - + @Test + void contextLoads() {} }