Skip to content

fix(customgitignore) parsing to respect repository boundaries #1859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

x-stp
Copy link

@x-stp x-stp commented May 2, 2025

Fixes #209

This PR improves .gitignore parsing by ensuring patterns are only applied within the boundaries of the current Git repository.

Previously, .gitignore files from outside a Git repo - or from a parent repo - could be incorrectly applied.
This led to unexpected exclusions in nested repository setups, such as submodules or vendored repos.

The updated logic includes proper detection of repository boundaries and support for symlink resolution to handle more complex project layouts.
Symlink resolution is fallback-safe and automatically skipped on unsupported platforms like Windows.

Changes included:

  • Updated readIgnoreFilesFromParents to stop traversal at repo boundaries
  • Added isSubRepository to detect nested repositories
  • Resolved symlinks using filepath.EvalSymlinks for more accurate path handling
  • Updated parentPath to resolve symlinked parent directories
  • Symlink-related code has fallback logic to avoid hard failures
  • Symlink tests are skipped where unsupported

Tests added:

  • Parsing behavior inside subrepositories (no parent .gitignore leakage)
  • Parsing behavior outside any Git repository (no .gitignore files applied)
  • Symlink handling under POSIX; auto-skipped on Windows

The docstring for ParseGitIgnores was also updated to reflect this behavior, without large structural changes.

If you’d prefer the symlink logic to go in a follow-up PR, happy to split it. Thanks in advance for reviewing!

~ Pepijn

x-stp added 3 commits May 2, 2025 15:08
…y boundaries | fixes google#209

- Added detection of repository boundaries when parsing gitignore files
- Ensures gitignore files outside of repositories are not parsed
- Prevents parent repository gitignores from being applied to subrepositories
- Added tests to verify the fixes work correctly
…oundaries

- Updated the ParseGitIgnores comment to reflect recent logic changes
- Explicitly notes that .gitignore files from parent repositories are ignored
- Keeps existing structure intact to minimize comment churn
This change enhances gitignore parsing to properly handle symlinks:
- Added symlink resolution to parentPath function
- Enhanced isSubRepository to correctly detect repository boundaries with symlinks
- Added tests for symlink handling
- Updated documentation to reflect symlink handling capabilities
Copy link
Collaborator

@another-rex another-rex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution, but it's not very clear to me what the change accomplishes.

I commented out the isSubRepository() function and all the newly added tests still passes just fine?

@x-stp x-stp marked this pull request as draft May 9, 2025 20:22
@x-stp
Copy link
Author

x-stp commented May 9, 2025

Thanks for the contribution, but it's not very clear to me what the change accomplishes.

I commented out the isSubRepository() function and all the newly added tests still passes just fine?

Thank you for pointing this out; think that my tests need a more comprehensive case covering nested repo's.

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.

Gitignore parsing does not respect repository boundaries
2 participants