Skip to content

Commit af72ab5

Browse files
author
johnhuang316
committed
fix: resolve OR search (regex pipe operator) not working in search_code_advanced
- Fix ag search strategy to properly convert glob patterns to regex - Pattern "*.py" now correctly converts to "\.py$" for ag's -G parameter - Pattern "test_*.js" now correctly converts to "^test_.*\.js$" - Ensures consistent file filtering behavior across all search tools - Resolves issue where ag would incorrectly match files due to glob/regex mismatch
1 parent d75b516 commit af72ab5

File tree

1 file changed

+24
-2
lines changed
  • src/code_index_mcp/search

1 file changed

+24
-2
lines changed

src/code_index_mcp/search/ag.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,30 @@ def search(
6868
cmd.extend(['--after', str(context_lines)])
6969

7070
if file_pattern:
71-
# Use -G to filter files by regex pattern
72-
cmd.extend(['-G', file_pattern])
71+
# Convert glob pattern to regex pattern for ag's -G parameter
72+
# ag's -G expects regex, not glob patterns
73+
regex_pattern = file_pattern
74+
if '*' in file_pattern and not file_pattern.startswith('^') and not file_pattern.endswith('$'):
75+
# Convert common glob patterns to regex
76+
if file_pattern.startswith('*.'):
77+
# Pattern like "*.py" -> "\.py$"
78+
extension = file_pattern[2:] # Remove "*."
79+
regex_pattern = f'\\.{extension}$'
80+
elif file_pattern.endswith('*'):
81+
# Pattern like "test_*" -> "^test_.*"
82+
prefix = file_pattern[:-1] # Remove "*"
83+
regex_pattern = f'^{prefix}.*'
84+
elif '*' in file_pattern:
85+
# Pattern like "test_*.py" -> "^test_.*\.py$"
86+
# First escape dots, then replace * with .*
87+
regex_pattern = file_pattern.replace('.', '\\.')
88+
regex_pattern = regex_pattern.replace('*', '.*')
89+
if not regex_pattern.startswith('^'):
90+
regex_pattern = '^' + regex_pattern
91+
if not regex_pattern.endswith('$'):
92+
regex_pattern = regex_pattern + '$'
93+
94+
cmd.extend(['-G', regex_pattern])
7395

7496
# Add -- to treat pattern as a literal argument, preventing injection
7597
cmd.append('--')

0 commit comments

Comments
 (0)