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
|
I'm leaning against merging this, although it's a great implementation. Here is my reasoning:
Thumbs up on this comment if you agree and think gitignore is an unnecessary feature, thumbs down on this comment if you disagree and want this PR merged. |
…iffplug#2443) Merges main and narrows the feature to what was called reasonable in review: - Gradle: `targetExcludeUsingGitignore()` on a format, no global switch. - Maven: `<excludeUsingGitignore>true</excludeUsingGitignore>` on a format, no global switch. - `GitIgnoredFiles` walks the index and the working tree with a `TreeWalk` instead of an `IndexDiff`, so no file content is read or stat-compared. - Gradle shares one walk per project and build through `SpotlessTaskService` instead of one per format. - Docs present it as the slowest option, after a tight `target` and `targetExclude`. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MTr23xR5a5pjEhA69fPdaE
- `GitIgnoredFiles` javadoc: it lists directories, it does not read file content. - Maven `excludeUsingGitignore`: plain `@Parameter`, like the other format fields. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MTr23xR5a5pjEhA69fPdaE
|
Thanks for the honest take. I reworked the PR into the narrowest form of the idea, so the decision is about the feature itself and not about this implementation:
What this does not change: it is still strictly slower than a tight target, because the files are found by I'm fine either way. If you feel the docs can't keep people on plan A, feel free to close this. It would help to hear from the people who upvoted #2443 whether a tight |
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