From 538858c0f53597923cd90500cf774d8083ac6907 Mon Sep 17 00:00:00 2001 From: nlaverdure Date: Thu, 4 Jun 2026 17:13:03 -0400 Subject: [PATCH 1/3] Add pre-commit hook for automatic Spotless formatting Moves the hook into a tracked .githooks/ directory and adds an installHooks Gradle task (wired to compileJava) that runs `git config core.hooksPath .githooks` on first build, so new contributors get the hook automatically without any manual setup. Co-Authored-By: Claude Sonnet 4.6 --- .githooks/pre-commit | 7 +++++++ README.md | 6 ++++-- build.gradle | 6 ++++++ doc/CONTRIBUTING.md | 13 +++---------- 4 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100644 index 0000000..112ab8d --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,7 @@ +#!/bin/sh +./gradlew spotlessApply --quiet +if [ $? -ne 0 ]; then + echo "spotlessApply failed — commit aborted" + exit 1 +fi +git diff --name-only | xargs -r git add \ No newline at end of file diff --git a/README.md b/README.md index f225f91..8e919a6 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,9 @@ Use the Gradle wrapper — no separate Gradle installation needed. ## Code Formatting -This project uses [Spotless](https://github.com/diffplug/spotless) with Google Java Format. Formatting is applied automatically on every build. +This project uses [Spotless](https://github.com/diffplug/spotless) with Google Java Format. Formatting is applied automatically on every build and on every commit (via `.githooks/pre-commit`). + +The hook is installed automatically the first time you run `./gradlew build`. **Apply formatting manually:** ```bash @@ -142,7 +144,7 @@ This project is licensed under the [BSD 3-Clause License](LICENSE). See individu ### Triple Helix Robotics (FRC Team 2363) -The robot-specific subsystems and supporting utilities are original work by Triple Helix Robotics, copyright 2025. +The robot-specific subsystems and supporting utilities are original work by Triple Helix Robotics, copyright 2025-2026. ### AdvantageKit — Littleton Robotics (FRC 6328) diff --git a/build.gradle b/build.gradle index bf72faa..e4778ae 100644 --- a/build.gradle +++ b/build.gradle @@ -203,6 +203,12 @@ task(eventDeploy) { } createVersionFile.dependsOn(eventDeploy) +// Install git hooks on first build +task installHooks(type: Exec) { + commandLine "git", "config", "core.hooksPath", ".githooks" +} +project.compileJava.dependsOn(installHooks) + // Spotless formatting project.compileJava.dependsOn(spotlessApply) spotless { diff --git a/doc/CONTRIBUTING.md b/doc/CONTRIBUTING.md index 22268a2..ed81b3c 100644 --- a/doc/CONTRIBUTING.md +++ b/doc/CONTRIBUTING.md @@ -166,20 +166,13 @@ If `main` has changes that overlap with your branch, Git will flag conflicts. Do ### Final check before opening a PR -Run the full build and formatting check one more time before pushing. Use `./gradlew` on Mac/Linux or Git Bash; use `gradlew` in Windows Command Prompt: +Run the full build one more time before pushing. Use `./gradlew` on Mac/Linux or Git Bash; use `gradlew` in Windows Command Prompt: ```bash ./gradlew build -./gradlew spotlessCheck ``` -If `spotlessCheck` fails, it means there are formatting issues. Apply the formatter automatically: - -```bash -./gradlew spotlessApply -``` - -Review the diff, then stage and commit the formatting changes before pushing. CI will run `spotlessCheck` automatically on your PR and will fail if formatting is not clean. +Formatting is applied automatically on every commit by the pre-commit hook (installed on first build), so it should already be clean. CI will run `spotlessCheck` on your PR and will fail if formatting is not clean — if that happens, run `./gradlew spotlessApply`, commit, and push. ### Open a pull request @@ -207,7 +200,7 @@ Once approved and CI is green, a mentor or senior programmer will merge the PR. ### Formatting -This project uses [Spotless](https://github.com/diffplug/spotless) with Google Java Format to keep code style consistent across the whole codebase. Formatting is applied automatically every time you run `./gradlew build`, so in most cases you do not need to think about it. If you ever want to apply it manually without building: +This project uses [Spotless](https://github.com/diffplug/spotless) with Google Java Format to keep code style consistent across the whole codebase. Formatting is applied automatically on every build and on every commit via a pre-commit hook in `.githooks/`. The hook is installed automatically the first time you run `./gradlew build`, so in most cases you do not need to think about it. If you ever want to apply it manually: ```bash ./gradlew spotlessApply From 3f74e5855e66d672ea3de9383b2bb5871f4a3564 Mon Sep 17 00:00:00 2001 From: nlaverdure Date: Thu, 4 Jun 2026 17:16:28 -0400 Subject: [PATCH 2/3] Skip installHooks in CI where .git directory is absent Co-Authored-By: Claude Sonnet 4.6 --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index e4778ae..161ef2f 100644 --- a/build.gradle +++ b/build.gradle @@ -205,6 +205,7 @@ createVersionFile.dependsOn(eventDeploy) // Install git hooks on first build task installHooks(type: Exec) { + onlyIf { file(".git").exists() } commandLine "git", "config", "core.hooksPath", ".githooks" } project.compileJava.dependsOn(installHooks) From 2d7b74fefac6cdfe98085e77c3bbc28f92b509c3 Mon Sep 17 00:00:00 2001 From: nlaverdure Date: Thu, 4 Jun 2026 17:19:23 -0400 Subject: [PATCH 3/3] Skip installHooks when CI env var is set Co-Authored-By: Claude Sonnet 4.6 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 161ef2f..bc3248d 100644 --- a/build.gradle +++ b/build.gradle @@ -205,7 +205,7 @@ createVersionFile.dependsOn(eventDeploy) // Install git hooks on first build task installHooks(type: Exec) { - onlyIf { file(".git").exists() } + onlyIf { !System.getenv("CI") } commandLine "git", "config", "core.hooksPath", ".githooks" } project.compileJava.dependsOn(installHooks)