Skip to content

Commit bbb575d

Browse files
authored
Merge pull request #246 from johbo/fix-grep-tool
Allow to search for "--pattern" in the grep tool with rg
2 parents b9fdbb0 + 8f1cb91 commit bbb575d

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

aider/tools/grep.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ def execute(
166166
if tool_name == "grep":
167167
cmd_args.append("--exclude-dir=.git")
168168

169+
# Pattern may start with a dash, this will make rg interpret it as
170+
# an option, "-e" works around this.
171+
if tool_name == "rg":
172+
cmd_args.extend(["-e"])
173+
169174
# Add pattern and directory path
170175
cmd_args.extend(["--", pattern, str(search_dir_path)])
171176

tests/tools/test_grep.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import shutil
2+
from types import SimpleNamespace
3+
from unittest.mock import Mock
4+
5+
import pytest
6+
7+
from aider.tools import grep
8+
9+
10+
@pytest.mark.skipif(shutil.which("rg") is None, reason="rg is required")
11+
@pytest.mark.parametrize("search_term", [
12+
"--pattern",
13+
"--pat tern",
14+
"-pattern",
15+
"--",
16+
"-- -test",
17+
])
18+
def test_dash_prefixed_pattern_is_searched_literally(search_term, tmp_path, monkeypatch):
19+
sample = tmp_path / "example.txt"
20+
sample.write_text(f"flag {search_term} should be found\n")
21+
22+
coder = SimpleNamespace(
23+
repo=SimpleNamespace(root=str(tmp_path)),
24+
io=SimpleNamespace(
25+
tool_error=Mock(),
26+
tool_output=Mock(),
27+
tool_warning=Mock(),
28+
),
29+
verbose=False,
30+
root=str(tmp_path),
31+
)
32+
33+
monkeypatch.setattr(
34+
grep.Tool, "_find_search_tool", lambda: ("rg", shutil.which("rg"))
35+
)
36+
37+
result = grep.Tool.execute(
38+
coder,
39+
pattern=search_term,
40+
file_pattern="*.txt",
41+
directory=".",
42+
use_regex=False,
43+
case_insensitive=False,
44+
context_before=0,
45+
context_after=0,
46+
)
47+
48+
assert "Found matches" in result
49+
assert search_term in result
50+
coder.io.tool_error.assert_not_called()

0 commit comments

Comments
 (0)