Skip to content

Commit 53c855e

Browse files
coopernetesclaude
andcommitted
chore: add pre-commit hook to auto-apply spotless formatting
Adds .githooks/pre-commit which runs `./gradlew spotlessApply` and re-stages any reformatted files before each commit. Also adds an `installGitHooks` Gradle task (run once after cloning) that sets `core.hooksPath = .githooks` so git picks up the hook automatically. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 9749c6e commit 53c855e

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

.githooks/pre-commit

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
# Auto-apply Spotless formatting before each commit.
3+
# Install once with: ./gradlew installGitHooks
4+
5+
set -euo pipefail
6+
7+
echo "[pre-commit] Running spotlessApply..."
8+
./gradlew spotlessApply --quiet
9+
10+
# Re-stage any files that Spotless just reformatted so the formatted
11+
# versions are included in the commit (not the originals).
12+
git diff --name-only | xargs -r git add
13+
14+
echo "[pre-commit] Spotless: OK"

build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ allprojects {
2626
}
2727

2828

29+
tasks.register('installGitHooks') {
30+
group = 'build setup'
31+
description = 'Configures git to use the .githooks/ directory (run once after cloning).'
32+
doLast {
33+
['git', 'config', 'core.hooksPath', '.githooks'].execute(null, rootDir).waitFor()
34+
println 'Git hooks installed — .githooks/pre-commit will run spotlessApply before each commit.'
35+
}
36+
}
37+
2938
subprojects {
3039
apply plugin: 'com.diffplug.spotless'
3140

0 commit comments

Comments
 (0)