Skip to content

Commit

Permalink
Merge pull request #41 from OSGP/feature/FDP-2318-spotless-pre-commit…
Browse files Browse the repository at this point in the history
…-hook

FDP-2318: Spotless pre-commit hook
  • Loading branch information
loesimmens authored Sep 12, 2024
2 parents 494ec81 + a5d2990 commit 7fd80ee
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
12 changes: 11 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,18 @@ subprojects {
}
}

tasks.register<Copy>("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<KotlinCompile> {
dependsOn(tasks.withType<GenerateAvroJavaTask>())
dependsOn(
tasks.withType<GenerateAvroJavaTask>(),
tasks.named("updateGitHooks")
)
}

tasks.withType<Test> { useJUnitPlatform() }
Expand Down
33 changes: 33 additions & 0 deletions scripts/pre-commit
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7fd80ee

Please sign in to comment.