EasyRepository SystemsPython 3

Repository Ignore Filter

Filter repository paths with simple ordered ignore and re-include rules.

30m3 sample tests6 hidden tests

Implement filter_repo_paths(paths, rules), a small .cursorignore-style path filter.

Requirements

  • Preserve the original order of kept paths.
  • Raise ValueError for invalid paths with empty segments, . segments, or .. traversal.
  • Strip leading and trailing whitespace from each rule before parsing. After strip, blank rules and rules starting with # are ignored.
  • A rule ending in / is a directory prefix match: after stripping the trailing /, path p matches when p.startswith(dir + "/"). The bare directory path equal to dir (for example path dist under rule dist/) does not match; only paths strictly under that prefix do.
  • Otherwise (after optional !), the pattern is an exact full-path match (path == pattern), even if the pattern contains / (e.g. dist/manifest.json).
  • A rule starting with ! re-includes paths matched by earlier rules (exact file or directory prefix, same match rules as non-negated forms).
  • Later matching rules win.
  • Each non-blank, non-comment rule body (after stripping a leading ! and a trailing /) must also be a valid path; raise ValueError when the rule pattern is invalid (empty segments, ., or ..).

Example

python
1paths = ["src/app.py", "node_modules/lib.js", "dist/app.js"] 2rules = ["node_modules/", "dist/"] 3assert filter_repo_paths(paths, rules) == ["src/app.py"] 4 5# Exact full-path rules may contain `/`; later rules win (including reincludes). 6paths = ["dist/app.js", "dist/manifest.json"] 7rules = ["dist/", "!dist/manifest.json"] 8assert filter_repo_paths(paths, rules) == ["dist/manifest.json"]

Constraints

  • No glob engine is required.
  • Keep matching deterministic and easy to explain.

Editor