diff --git a/build.gradle.kts b/build.gradle.kts index 445b590c..fa805a87 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -80,8 +80,18 @@ subprojects { } } + tasks.register("updateGitHooks") { + description = "Copies the pre-commit Git Hook to the .git/hooks folder." + group = "verification" + from("${project.rootDir}/scripts/pre-commit") + into("${project.rootDir}/.git/hooks") + } + tasks.withType { - dependsOn(tasks.withType()) + dependsOn( + tasks.withType(), + tasks.named("updateGitHooks") + ) } tasks.withType { useJUnitPlatform() } diff --git a/scripts/pre-commit b/scripts/pre-commit new file mode 100755 index 00000000..7c2770c3 --- /dev/null +++ b/scripts/pre-commit @@ -0,0 +1,33 @@ +#!/bin/bash +echo "*********************************************************" +echo "Running git pre-commit hook. Running Spotless Apply... " +echo "*********************************************************" + +# Gather the staged files - to make sure changes are saved only for these files. +stagedFiles=$(git diff --staged --name-only) + +# run spotless apply +./gradlew spotlessApply + +status=$? + +if [ "$status" = 0 ] ; then + echo "Static analysis found no problems." + # Add staged file changes to git + for file in $stagedFiles; do + if test -f "$file"; then + git add $file + fi + done + #Exit + exit 0 +else + echo "*********************************************************" + echo " ******************************************** " + echo 1>&2 "Spotless Apply found violations it could not fix." + echo "Run spotless apply in your terminal and fix the issues before trying to commit again." + echo " ******************************************** " + echo "*********************************************************" + #Exit + exit 1 +fi