Skip to content

Fix directory exclusion patterns to properly exclude contents#52

Merged
simone-viozzi merged 4 commits intomainfrom
copilot/fix-inclusion-exclusion-pattern
Nov 23, 2025
Merged

Fix directory exclusion patterns to properly exclude contents#52
simone-viozzi merged 4 commits intomainfrom
copilot/fix-inclusion-exclusion-pattern

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 19, 2025

Directory exclusion patterns like --exclude-dir app or -e "app/" were not excluding files within those directories. The patterns matched only the directory itself, not its contents.

Changes

Core Pattern Matching

  • filter.py::matches(): Removed early return for files when pattern ends with /. Patterns like node_modules/ now match both the directory and all descendants. Wildcard patterns ending with / (e.g., tmp/**/) now correctly match only directories.
  • filter.py::_include_can_match_descendant(): Replaced naive ** check with path prefix analysis. Prevents unnecessary traversal when patterns like build/reports/** cannot match under node_modules/.

Compressed View

  • gpt_copy.py::collect_file_info(): Added filter engine check when collecting children for compressed directory display. Previously only checked gitignore rules, now respects CLI patterns.

Documentation

  • Enhanced README with pattern matching rules and real-world examples
  • Updated CLI help text to clarify trailing / behavior and last-match-wins semantics

Examples

# These now correctly exclude directories AND their contents
gpt-copy . --exclude-dir app --exclude-dir tests
gpt-copy . -e "node_modules/" -e "build/**" -i "build/reports/**"

# Pattern matching follows gitignore semantics
gpt-copy . -e "tmp/*" -i "tmp/**/"  # Excludes direct children, traverses subdirs

Testing

Added 5 tests covering the reported scenarios. Updated 1 existing test to reflect correct behavior. All 58 tests pass.

Original prompt

This section details on the original issue you should resolve

<issue_title>inclusion / exclusion pattern is not working / not documented correctly</issue_title>
<issue_description>i tried this variations

> gpt-copy . -e "uv.lock" -e .pre-commit-config.yaml -e .gitignore -e "tests/" -e "notebooks/" -e "frontend/" -e "app/" -i deployment > deploy.md
> gpt-copy . -e "uv.lock" -e .pre-commit-config.yaml -e .gitignore --exclude-dir "tests" --exclude-dir "notebooks" --exclude-dir "frontend" --exclude-dir "app" -i deployment > deploy.md
> gpt-copy . -e "uv.lock" -e .pre-commit-config.yaml -e .gitignore --exclude-dir "tests/" --exclude-dir "notebooks/" --exclude-dir "frontend/" --exclude-dir "app/" -i deployment > deploy.md
> gpt-copy . -e "uv.lock" -e .pre-commit-config.yaml -e .gitignore --exclude-dir "./tests/" --exclude-dir "./notebooks/" --exclude-dir "./frontend/" --exclude-dir "./app/" -i deployment > deploy.md
> gpt-copy . -e "uv.lock" -e .pre-commit-config.yaml -e .gitignore --exclude-dir "./tests" --exclude-dir "./notebooks" --exclude-dir "./frontend" --exclude-dir "./app" -i deployment > deploy.md
> gpt-copy . -e "uv.lock" -e .pre-commit-config.yaml -e .gitignore -e "./tests" -e "./notebooks" -e "./frontend" -e "./app" -i deployment > deploy.md

All of those failed to exclude files in the notebook folder, and the other excluded folders

❯ ls -l
total 3264
drwxrwxr-x 7 ubuntu ubuntu    4096 Nov 18 16:32 app
drwxrwxr-x 3 ubuntu ubuntu    4096 Oct 28 13:48 deployment
-rw-rw-r-- 1 ubuntu ubuntu     766 Nov 18 10:23 Dockerfile
drwxrwxr-x 6 ubuntu ubuntu    4096 Nov 18 16:31 frontend
-rw-rw-r-- 1 ubuntu ubuntu    4780 Nov 18 10:23 Makefile
drwxrwxr-x 2 ubuntu ubuntu    4096 Oct 28 13:48 notebooks
-rw-rw-r-- 1 ubuntu ubuntu    2662 Nov 18 16:31 pyproject.toml
-rw-rw-r-- 1 ubuntu ubuntu    8764 Nov 18 16:31 README.md
drwxrwxr-x 4 ubuntu ubuntu    4096 Nov 18 10:23 tests
-rw-rw-r-- 1 ubuntu ubuntu    1480 Nov 18 16:33 TODO.md
-rw-rw-r-- 1 ubuntu ubuntu  849451 Nov 18 10:23 uv.lock

why exclusion is not working? what is the correct command to exclude all the folders except for deployment, and inlcude all files in the first level?

exclusion was reviewed in #47

</issue_description>

<agent_instructions>if the logic is working as it should, then add documentation to explain how to use it, even in the help message. otherwise fix the logic</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits November 19, 2025 11:14
- Fixed matches() method to properly handle directory patterns with trailing /
- Directory patterns like "dir/" now correctly exclude the directory AND all its contents
- Patterns with wildcards and trailing / (like "tmp/**/") now only match directories, not files
- Improved may_have_late_include_descendant to avoid unnecessary traversal
- Updated compressed view to only show directories without excluded children
- All tests passing

Co-authored-by: simone-viozzi <19674168+simone-viozzi@users.noreply.github.com>
- Updated README with comprehensive filtering documentation
- Enhanced CLI help text with clear examples
- Added 5 new tests specifically for user's issue scenarios
- All 58 tests passing
- Manual verification complete - all user scenarios work correctly

Co-authored-by: simone-viozzi <19674168+simone-viozzi@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix inclusion/exclusion pattern issues in gpt-copy command Fix directory exclusion patterns to properly exclude contents Nov 19, 2025
Copilot AI requested a review from simone-viozzi November 19, 2025 11:24
@simone-viozzi simone-viozzi marked this pull request as ready for review November 23, 2025 16:39
@simone-viozzi simone-viozzi merged commit 3ec351d into main Nov 23, 2025
3 checks passed
@simone-viozzi simone-viozzi deleted the copilot/fix-inclusion-exclusion-pattern branch November 23, 2025 16:40
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.

inclusion / exclusion pattern is not working / not documented correctly

2 participants