Skip to content

files: match directory-only gitignore rules in ShouldExclude - #1228

Open
amalsaidov wants to merge 1 commit into
ipfs:mainfrom
amalsaidov:fix/filter-directory-gitignore-rules
Open

amalsaidov wants to merge 1 commit into
ipfs:mainfrom
amalsaidov:fix/filter-directory-gitignore-rules

Conversation

@amalsaidov

Copy link
Copy Markdown

Problem

files.Filter.ShouldExclude does not apply .gitignore rules that end in a
slash. Those patterns ("bar/", "node_modules/", ...) match directories
only, and gitignore semantics are the
documented behavior: NewFilter lists bar/ as a supported rule.

The underlying matcher (crackcomm/go-gitignore) tells a directory apart from a
file by a trailing slash on the path it is handed. ShouldExclude always passed
the bare fileInfo.Name(), so MatchesPath("bar") never matched a bar/ rule
and the directory was added anyway. This is the first point of #648.

Quick confirmation against the dependency as pinned in go.mod:

rule="bar/"  path="bar"   => false   // what ShouldExclude asks today
rule="bar/"  path="bar/"  => true    // what it should ask for a directory

Fix

When the entry is a directory, append / to the path before matching. A
pattern without a trailing slash already matches the slashed form
(rule="bar", rule="*.app" → both match "bar/" / "foo.app/"), so files
and non-directory rules are unchanged; only directory-only patterns flip from
"matches nothing" to "matches the directory". No exported API changes.

This is scoped to the directory-pattern case. The second part of #648, matching
rules that contain a path (for example a/b) against the entry's path relative
to the filter root, needs ShouldExclude to receive that relative path and is
left out of this change.

Testing

  • Added TestFileFilterDirectoryRules in files/filter_test.go, covering
    dir-only rules on directories vs files, plain rules, and glob rules. It fails
    before this change (ShouldExclude("bar") = false, want true) and passes
    after.
  • go test ./files/..., go vet ./files/..., gofmt -l all clean.

Companion kubo PR: this touches non-test code in files, so it is not exempt
from the companion-kubo requirement. The change keeps the ShouldExclude
signature and all exported APIs identical, so kubo builds against it unchanged.
I have not opened the kubo PR or run the full kubo build here; happy to open the
pin-and-CI companion if you'd like, or to defer to a maintainer. Flagging it
honestly rather than claiming an exemption.


AI-assisted; directed, reviewed and tested by the author (amalsaidov).

@amalsaidov
amalsaidov requested a review from a team as a code owner November 13, 2026 07:26
A .gitignore pattern that ends in a slash, like "bar/", only matches
directories. The go-gitignore matcher tells directories apart by a
trailing slash on the path it is given, but ShouldExclude always passed
the bare file name, so these patterns matched nothing and the directory
was included anyway. NewFilter documents "bar/" as a supported rule.

Append a trailing slash to the path when the entry is a directory.
Patterns without a trailing slash already match the slashed form, so
files and non-directory rules behave exactly as before.

Fixes the directory-pattern case of ipfs#648.
@amalsaidov
amalsaidov force-pushed the fix/filter-directory-gitignore-rules branch from 8128523 to 5c00e5d Compare November 13, 2026 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant