MCPcopy Create free account
hub / github.com/CodeGraphContext/CodeGraphContext / CGCIgnoreMatcher

Class CGCIgnoreMatcher

src/codegraphcontext/core/cgcignore.py:89–212  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

87 return user_patterns + list(default_patterns)
88
89class CGCIgnoreMatcher:
90 def __init__(self, patterns: list[str], root_dir: Path):
91 self.root_dir = root_dir
92 self.rules = self._compile_patterns(patterns)
93
94 def _translate_segment(self, seg: str) -> str:
95 """Translates a single gitwildmatch segment into regex, completely avoiding fnmatch."""
96 i, n = 0, len(seg)
97 res = ""
98 while i < n:
99 c = seg[i]
100 i += 1
101 if c == '*':
102 res += '[^/]*'
103 elif c == '?':
104 res += '[^/]'
105 elif c == '\\':
106 # Handle gitignore escape sequence (e.g. \! matches a literal !)
107 if i < n:
108 res += re.escape(seg[i])
109 i += 1
110 else:
111 res += re.escape('\\')
112 elif c == '[':
113 j = i
114 if j < n and seg[j] == '!':
115 j += 1
116 if j < n and seg[j] == ']':
117 j += 1
118 while j < n and seg[j] != ']':
119 j += 1
120 if j >= n:
121 res += '\\['
122 else:
123 stuff = seg[i:j].replace('\\', '\\\\')
124 i = j + 1
125 if stuff[0] == '!':
126 stuff = '^' + stuff[1:]
127 elif stuff[0] == '^':
128 stuff = '\\' + stuff
129 res += '[' + stuff + ']'
130 else:
131 res += re.escape(c)
132 return res
133
134 def _compile_patterns(self, patterns: list[str]) -> list[Tuple[bool, re.Pattern]]:
135 rules = []
136 for raw_pat in patterns:
137 pat = raw_pat.strip()
138 if not pat or pat.startswith('#'):
139 continue
140
141 is_negation = pat.startswith('!')
142 if is_negation:
143 pat = pat[1:]
144
145 is_dir_only = pat.endswith('/')
146 if is_dir_only:

Calls

no outgoing calls