Use .gitignore for directory exclusion during traversal#83
Conversation
Replace hardcoded EXCLUDED_DIRECTORIES list with .gitignore-based filtering when a .gitignore file exists. Falls back to the hardcoded list when no .gitignore is found. Hidden directories and ai-rules are always excluded regardless of strategy.
|
Hi @deanbaker, Thank you so much for your contribution. Using .gitignore instead of a hardcoded list is a great idea! I noticed two issues with the current implementation:
What do you think? Would love to hear your thoughts on how to handle these. Thank you! |
Replace the binary should_traverse() with a three-state TraversalDecision enum (Enter, SkipCallbackButRecurse, Skip) so that ignored directories with negated children are still recursed into. - Use a Vec<Gitignore> chain instead of a single matcher so each nested .gitignore is built with its own directory as root (works around the ignore crate's limitation with cross-directory negation patterns) - Track parent_ignored state during traversal so children of ignored directories inherit the ignore unless explicitly whitelisted - Read each .gitignore file once via build_gitignore_for_dir(), producing both the compiled matcher and negation patterns in a single pass - Only clone the filter chain when a child .gitignore actually exists - Add 6 new tests covering negation, nested .gitignore, combined negation+nesting, full skip, depth boundaries, and decision assertions
Thanks for the feedback @lifeizhou-ap, I have made another commit that has addressed it |
lifeizhou-ap
left a comment
There was a problem hiding this comment.
Thank you @deanbaker for your contribution. The PR looks good! I've approved the PR. I've added a comment about the code separation. WDYT? If it makes sense, would you please extract these functions to the a separate file/mod before merging to main? Thank you!
Moves TraversalDecision, DirectoryFilter, build_gitignore_for_dir, has_negated_children, should_traverse_directory, and EXCLUDED_DIRECTORIES into src/utils/dir_filter.rs per PR review feedback. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
EXCLUDED_DIRECTORIESlist with.gitignore-based filtering via theignorecrate when a.gitignorefile exists.gitignoreis foundai-rulesare always excluded regardless of strategyTest plan
.gitignore, and hardcoded fallbackcargo clippycleancargo run -- statusin a repo with.gitignoreconfirmstarget/still excluded.gitignoreconfirms hardcoded fallback works