Skip to content

Commit

Permalink
Add pre-commit hook to run spotless for formatting/linting
Browse files Browse the repository at this point in the history
  • Loading branch information
miguno committed May 7, 2024
1 parent 7380d20 commit 7f42517
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

# Run spotless checks on staged files only.
# This is faster than running spotless on all files.
./mvnw spotless:check -DspotlessFiles="$(git diff --staged --name-only | grep ".java$" | sed 's/^/.*/' | paste -sd ',' -)"

declare errcode=$?
if [ $errcode -ne 0 ]; then
echo
echo "Run \`./mvnw spotless:apply\` to automatically fix these format violations."
exit $errcode
fi
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
<maven-javadoc-plugin.version>3.6.3</maven-javadoc-plugin.version>
<maven-surefire-plugin.version>3.2.5</maven-surefire-plugin.version>
<git-build-hook-maven-plugin.version>3.5.0</git-build-hook-maven-plugin.version>
<jacoco.version>0.8.12</jacoco.version>
<spotbugs.version>4.8.5</spotbugs.version>
<spotbugs-maven-plugin.version>4.8.5.0</spotbugs-maven-plugin.version>
Expand Down Expand Up @@ -175,6 +176,13 @@
<version>${spotless.version}</version>
</plugin>

<!-- To run git hooks locally as part of maven's lifecycle. -->
<plugin>
<groupId>com.rudikershaw.gitbuildhook</groupId>
<artifactId>git-build-hook-maven-plugin</artifactId>
<version>${git-build-hook-maven-plugin.version}</version>
</plugin>

<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
Expand Down Expand Up @@ -434,6 +442,26 @@
</java>
</configuration>
</plugin>

<plugin>
<groupId>com.rudikershaw.gitbuildhook</groupId>
<artifactId>git-build-hook-maven-plugin</artifactId>
<version>${git-build-hook-maven-plugin.version}</version>
<configuration>
<gitConfig>
<!-- Directory that stores our git hooks. -->
<core.hooksPath>hooks/</core.hooksPath>
</gitConfig>
</configuration>
<executions>
<execution>
<goals>
<!-- Sets git config specified under configuration > gitConfig. -->
<goal>configure</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down

0 comments on commit 7f42517

Please sign in to comment.