Skip to content

gitignore: dirOnly re-include patterns must not cascade to descendants - #2310

Closed
thesahibnanda-max wants to merge 1 commit into
go-git:mainfrom
thesahibnanda-max:fix/gitignore-dir-reinclude-cascade
Closed

thesahibnanda-max wants to merge 1 commit into
go-git:mainfrom
thesahibnanda-max:fix/gitignore-dir-reinclude-cascade

Conversation

@thesahibnanda-max

Copy link
Copy Markdown

Fixes #2112

Bug

A .gitignore re-include pattern like !dir/ is meant to restore only the directory entry itself, not everything nested inside it. simpleNameMatch let a dirOnly inclusion pattern that matched an ancestor path component stand in as a match for the full descendant path, so:

*
!my-dir/

wrongly reported files under my-dir/ (e.g. my-dir/sub/file.txt) as untracked in Worktree.Status() instead of ignored, even though real git keeps them ignored.

Fix

In plumbing/format/gitignore/pattern.go, simpleNameMatch now skips (rather than matches) when a dirOnly inclusion pattern hits a non-final path component — it only applies at the exact matched entry, not its descendants. Exclusion patterns are unaffected.

Testing

  • New unit tests in pattern_test.go covering the pattern in isolation.
  • New case in conformance_test.go, cross-checked against the real git binary via the existing oracle harness.
  • New end-to-end regression test in worktree_status_test.go reproducing the original report via Worktree.Status().
  • go test ./plumbing/format/gitignore/... and go test . pass (aside from one pre-existing, unrelated oracle mismatch confirmed present on unmodified main too).

…descendants. Fixes go-git#2112

A pattern like !dir/ only restores the directory entry itself; it does
not implicitly re-include everything nested inside it. simpleNameMatch
let a dirOnly inclusion pattern matching an ancestor path component stand
in for a match on the full descendant path, so a .gitignore like:

    *
    !my-dir/

wrongly reported files under my-dir/ as untracked instead of ignored.

Signed-off-by: Sahib Nanda <[email protected]>
@thesahibnanda-max

Copy link
Copy Markdown
Author

Closing in favor of #2311 — same change, rebased onto a fresh branch with a corrected commit message (package-prefixed per CONTRIBUTING.md) and DCO sign-off, which this PR's CI checks were failing on.

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.

go-git ignores files inside un-ignored directory with wildcard pattern

1 participant