feat: Respect .gitignore when discovering files#6
Open
laplaque wants to merge 3 commits intoMikeRecognex:mainfrom
Open
feat: Respect .gitignore when discovering files#6laplaque wants to merge 3 commits intoMikeRecognex:mainfrom
laplaque wants to merge 3 commits intoMikeRecognex:mainfrom
Conversation
In git repositories, filter discovered files through `git check-ignore --stdin` so that paths matched by .gitignore, .git/info/exclude, and the global gitignore are excluded from the index. Falls back gracefully to the existing exclude_patterns logic when the project is not a git repo or git is unavailable.
When PROJECT_ROOT spans multiple git repositories (or is not itself a git repo), group discovered files by their containing repo and run git check-ignore once per repo. This ensures each repo's .gitignore is respected even when the indexed directory is a parent of several independent repos. Add _find_git_root() helper and a multi-repo test case with three independent repos under a shared parent directory.
The incremental update path in _maybe_incremental_update() only checked the hardcoded exclude_patterns, not .gitignore. Collect candidate paths from the changeset, batch-check them via _get_git_ignored_paths(), and skip any that are git-ignored before reindexing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
git check-ignore --stdinso that paths matched by.gitignore,.git/info/exclude, and the global gitignore are excluded from the indexexclude_patternslogic when the project is not a git repo or git is unavailablesubprocess(already used bygit_tracker.py)Motivation
The hardcoded
exclude_patternslist covers common directories like__pycache__andnode_modules, but misses project-specific ignores (e.g..mypy_cache,build/,dist/, IDE directories). Most projects already define these in.gitignore. By respecting it, the indexer automatically excludes the right files without requiring users to patch the source.Changes
src/mcp_codebase_index/project_indexer.py_get_git_ignored_paths()method; call it from_discover_files()after glob matchingtests/test_project_indexer.pyTestGitIgnoreclass with 4 tests (git-ignored detection, discover exclusion, non-git fallback, nested patterns)How it works
After the existing glob +
exclude_patternsfiltering,_discover_files()passes all candidate paths togit check-ignore --stdin. Any paths git reports as ignored are removed from the result set. This runs once per full index build (not per query).Test plan
TestGitIgnore::test_git_ignored_paths_detected— verifies_get_git_ignored_pathsreturns git-ignored filesTestGitIgnore::test_discover_files_excludes_git_ignored— verifies end-to-end: indexed files exclude git-ignored pathsTestGitIgnore::test_non_git_project_no_filtering— verifies graceful no-op in non-git directoriesTestGitIgnore::test_gitignore_with_nested_patterns— verifies dynamically added.gitignorepatterns are respectedmain)