diff --git a/grep.py b/grep.py new file mode 100644 index 0000000..3ffcabc --- /dev/null +++ b/grep.py @@ -0,0 +1,32 @@ +import os +import re + +def codebase_grep(search_directory, pattern, file_extension=None, case_sensitive=False): + results = [] + exclude_dirs = {'.git', 'node_modules', 'venv', '.venv', '__pycache__'} + flags = 0 if case_sensitive else re.IGNORECASE + compiled_pattern = re.compile(pattern, flags) + + for root, dirs, files in os.walk(search_directory): + # Remove excluded directories + dirs[:] = [d for d in dirs if os.path.join(root, d) not in exclude_dirs] + + for file in files: + file_path = os.path.join(root, file) + if file_extension and not file.endswith(file_extension): + continue + + try: + with open(file_path, 'r', encoding='utf-8') as f: + for line_num, line in enumerate(f): + if compiled_pattern.search(line): + results.append({ + 'file': file_path, + 'line': line_num + 1, + 'content': line.strip() + }) + if len(results) >= 100: + return results + except UnicodeDecodeError: + continue + return results \ No newline at end of file