Skip to content

Support .gitignore #2443

Open
Open

Description

@ivandimitrov8080

I'd like to be able to optionally enable .gitignore support where spotless honors the rules set there and automatically ignores files I don't track.

There are workarounds for this but I'd like to have a simple interface like

<enableGitignore />

Activity

  1. tommy-mitchell commented on Sep 29, 2026

    @tommy-mitchell

    There are workarounds for this

    To document one: I was able to parse the patterns from .gitignore using gradle-node-plugin and gitignore-to-glob-cli and apply them to the targetExclude option:

    plugins {
      id "com.github.node-gradle.node"
    }
    
    def inputFile = rootProject.file(".gitignore")
    def outputFile = layout.buildDirectory.file("gitignore.txt")
    
    task gitignore(type: NpxTask) {
      command = "gitignore-to-glob-cli"
      args = ["--input", inputFile.absolutePath]
    
      inputs.file(inputFile)
      outputs.file(outputFile)
    
      execOverrides {
        standardOutput = new FileOutputStream(outputFile.get().asFile)
      }
    }
    
    def excludePatterns = providers.provider {
      def patterns = outputFile.get().asFile.readLines()
      rootProject.fileTree(rootProject.projectDir) {
        include(patterns)
      }
    }
    
    spotless {
      format "foo", {
        target "foo.bar"
        targetExclude excludePatterns
      }
    }
    
    tasks.named("spotlessFoo").configure {
      dependsOn gitignore
    }
  2. Pankraz76 commented on Nov 8, 2026

    @Pankraz76

    Thanks @ivandimitrov8080 for the suggestion, and @tommy-mitchell for sharing the workaround. It made the use case very concrete!

    I've opened a draft PR that adds this: #3176

    It's opt-in and works in both plugins, globally or per format:

    • Gradle: spotless { respectGitignore() }
    • Maven: <respectGitignore>true</respectGitignore>

    A file is skipped using the same rules as git status: it matches any .gitignore (including nested ones and negations), .git/info/exclude or core.excludesFile, and it isn't tracked. Tracked files are still formatted even if they match a pattern, so nothing committed silently drops out of formatting. On Gradle it also works with the configuration cache.

    Feedback is very welcome, especially on:

    • the name (respectGitignore vs. something like excludeGitIgnored)
    • whether tracked-but-ignored files should keep being formatted (current behaviour, same as git)
    • whether it should eventually become the default

    If you get a chance to try it on a real project, I'd love to hear how it goes. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions