Repository navigation
Conversation
Adds `GitIgnoredFiles` to lib-extra, which answers whether git ignores a file using the same rules as `git status`: the file matches a pattern from any .gitignore, .git/info/exclude or core.excludesFile AND is not tracked. Tracked files are never skipped. Ignored paths are computed once via a JGit IndexDiff restricted to the project subtree; fully ignored directories (node_modules, build, ...) are not descended into. - Maven: `<respectGitignore>true</respectGitignore>` globally and per format (format-level wins); composes with ratchetFrom. - Gradle: `respectGitignore()` / `respectGitignore(boolean)` globally and per format. Filters the task's input FileCollection so up-to-date checks stay correct, and is configuration-cache safe: the git state is transient, so .gitignore edits are honored by the next build. - Fails fast with a clear message outside a git repository. Default is off, so existing builds are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MTr23vs1UZzH1yBNJUacfL
The check violation message lists paths with native separators, so asserting on "src/generated/Ignored.md" failed on Windows where the output contains "src\generated\Ignored.md". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MTr23yqwK9BPLYGVJwwsVB
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2443
Why
Spotless picks files with globs (
target '**/*.md',<include>src/**/*.java</include>). Broad globs easily pick up files that are not part of the project's source: generated code,node_modules, local scratch files, IDE output. These are already listed once, in.gitignore. Today users have to copy those rules intotargetExclude/<excludes>and keep both lists in sync. Even then they can't express negations (!keep.log) or nested.gitignorefiles. The workaround in the issue thread shells out to an npm tool to turn.gitignoreinto globs, which shows how much this is wanted (6 👍).What
One opt-in flag in both build plugins, off by default:
Gradle
Maven
Same rules as
git statusA file is skipped only if both are true:
.gitignore(root or nested),.git/info/exclude, orcore.excludesFileTracked files are always formatted, even if they match an ignore pattern. That is how git behaves, and it avoids silently dropping committed sources from formatting.
Behaviour details
ratchetFrom).ratchetFrom(a Maven test covers this).Respecting .gitignore requires a git repository, but none was found in ….FileCollection, so up-to-date checks see the filtered set. It is configuration-cache safe: the computed git state istransient, so it is recomputed every build and.gitignoreedits take effect on the next run. Tested with the configuration cache on and off.How
lib-extra: newGitIgnoredFiles.IndexDiff(index vs. working tree, no HEAD needed), limited to the project subtree, and readsgetIgnoredNotInIndex().node_modules,build) cost almost nothing.Serializablewith transient state.GitWorkaroundsto find the repository, so git worktrees are handled the same way as forratchetFrom.plugin-maven: arespectGitignoreparameter on the mojo and onFormatterFactory. Filtering happens incollectFiles, after include/exclude (and ratchet) resolution.plugin-gradle:respectGitignore()/respectGitignore(boolean)/setRespectGitignore/isRespectGitignoreonSpotlessExtensionandFormatExtension.setupTaskwraps the target inFileCollection.filter(...)with a small serializableSpec.Tests
Written test-first: each suite was run red against an API-only stub or missing DSL, then made green.
GitIgnoredFilesTest!keep.log; tracked-but-matching is not ignored; nested.gitignorescoping;.git/info/exclude;core.excludesFile; subproject root inherits repo rules; file outside root; not a git repo → clear error; serialization round-tripRespectGitignoreTestfalseoverrides globaltrue; combined withratchetFromRespectGitignoreTest.gitignoreedited between builds and the not-a-repo error; parameterized with configuration cache on and offAlso run:
spotlessCheckand SpotBugs forlib-extra,plugin-maven,plugin-gradle✅lib-extra:GitRachetMergeBaseTest,GitAttributesTest,GitWorkaroundsTest, all ✅ (34/34 including the new tests)plugin-gradle:GitRatchetGradleTest,ConfigurationCacheTest,TargetExclude*,MultipleTargetsTest,UpToDateTest,MultiProjectTest,GradleIncrementalResolutionTest, all ✅ (47/47;ConfigurationCacheTestpassed on rerun after Maven Central rate-limited a download)plugin-maven:GitRatchetMavenTest,IncludesExcludesTest,MultipleFormatsTest,SpotlessCheckMojoTest✅ (23/27)SpecificFilesTest×3,MultiModuleProjectTest.testConfigurationDependency) are pre-existing. They fail the same way on unmodifiedmain(7cc923d), withgoogle-java-format(java.lang.reflect.InvocationTargetException)in my environment (JDK 21), and are unrelated to this PR.Out of scope / possible follow-ups
IndexDifffor the project subtree. This is cheap because ignored directories are skipped, but it could be shared per project later if anyone measures a hot spot.Checklist (from the PR template)
[Unreleased]entries inCHANGES.md,plugin-gradle/CHANGES.md,plugin-maven/CHANGES.md(linking Support .gitignore #2443)🤖 Generated with Claude Code
https://claude.ai/code/session_01MTr23vs1UZzH1yBNJUacfL