Skip to content

Commit 088b96b

Browse files
perf(mcp): prefilter simple suffix globs on Windows
Signed-off-by: Ertan <ertan.kucukoglu@gmail.com>
1 parent 076c8ae commit 088b96b

3 files changed

Lines changed: 104 additions & 14 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8751,25 +8751,57 @@ static int search_result_cmp(const void *a, const void *b) {
87518751
return rb->score - ra->score; /* descending */
87528752
}
87538753

8754+
/* Moving an arbitrary file_pattern ahead of Select-String is not generally results-preserving:
8755+
* the current Windows path applies PowerShell -like to the full MatchInfo.Path, while POSIX
8756+
* delegates glob semantics to grep --include. Restrict the Windows optimization to plain suffix
8757+
* globs whose meaning cannot depend on path separator normalization or directory components. The
8758+
* original post-scan filter remains in place as a second guard. */
8759+
bool cbm_search_code_file_pattern_can_prefilter(const char *file_pattern) {
8760+
if (!file_pattern || file_pattern[0] != '*' || file_pattern[1] != '.' ||
8761+
file_pattern[2] == '\0') {
8762+
return false;
8763+
}
8764+
for (const unsigned char *p = (const unsigned char *)file_pattern + 2; *p; p++) {
8765+
if (!((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') ||
8766+
*p == '.' || *p == '_' || *p == '-')) {
8767+
return false;
8768+
}
8769+
}
8770+
return true;
8771+
}
8772+
87548773
/* Build the grep/search command string based on scoped vs recursive mode.
87558774
* On Windows, uses PowerShell Select-String with tab-delimited output.
87568775
* On POSIX, uses grep with colon-delimited output. */
8757-
static void build_grep_cmd(char *cmd, size_t cmd_sz, bool use_regex, bool scoped,
8758-
const char *file_pattern, const char *tmpfile, const char *filelist,
8759-
const char *root_path) {
8776+
void cbm_search_code_build_grep_cmd(char *cmd, size_t cmd_sz, bool use_regex, bool scoped,
8777+
const char *file_pattern, const char *tmpfile,
8778+
const char *filelist, const char *root_path) {
87608779
#ifdef _WIN32
87618780
const char *sm = use_regex ? "" : " -SimpleMatch";
87628781
if (scoped) {
87638782
if (file_pattern) {
8764-
snprintf(
8765-
cmd, cmd_sz,
8766-
"powershell -Command \"$pat = Get-Content -Encoding UTF8 -LiteralPath '%s'; "
8767-
"Get-Content -Encoding UTF8 -LiteralPath '%s' | ForEach-Object { Select-String "
8768-
"-LiteralPath $_ -Pattern $pat%s "
8769-
"-ErrorAction SilentlyContinue }"
8770-
" | Where-Object { $_.Path -like '*%s' }"
8771-
" | ForEach-Object { $_.Path + [char]9 + $_.LineNumber + [char]9 + $_.Line }\"",
8772-
tmpfile, filelist, sm, file_pattern);
8783+
if (cbm_search_code_file_pattern_can_prefilter(file_pattern)) {
8784+
snprintf(
8785+
cmd, cmd_sz,
8786+
"powershell -Command \"$pat = Get-Content -Encoding UTF8 -LiteralPath '%s'; "
8787+
"Get-Content -Encoding UTF8 -LiteralPath '%s'"
8788+
" | Where-Object { $_ -like '%s' }"
8789+
" | ForEach-Object { Select-String -LiteralPath $_ -Pattern $pat%s "
8790+
"-ErrorAction SilentlyContinue }"
8791+
" | Where-Object { $_.Path -like '*%s' }"
8792+
" | ForEach-Object { $_.Path + [char]9 + $_.LineNumber + [char]9 + $_.Line }\"",
8793+
tmpfile, filelist, file_pattern, sm, file_pattern);
8794+
} else {
8795+
snprintf(
8796+
cmd, cmd_sz,
8797+
"powershell -Command \"$pat = Get-Content -Encoding UTF8 -LiteralPath '%s'; "
8798+
"Get-Content -Encoding UTF8 -LiteralPath '%s' | ForEach-Object { Select-String "
8799+
"-LiteralPath $_ -Pattern $pat%s "
8800+
"-ErrorAction SilentlyContinue }"
8801+
" | Where-Object { $_.Path -like '*%s' }"
8802+
" | ForEach-Object { $_.Path + [char]9 + $_.LineNumber + [char]9 + $_.Line }\"",
8803+
tmpfile, filelist, sm, file_pattern);
8804+
}
87738805
} else {
87748806
snprintf(
87758807
cmd, cmd_sz,
@@ -9807,8 +9839,8 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
98079839
search_scratch_close(&scratch);
98089840
} else {
98099841
char cmd[CBM_SZ_4K];
9810-
build_grep_cmd(cmd, sizeof(cmd), use_regex, scoped, file_pattern, tmpfile, filelist,
9811-
root_path);
9842+
cbm_search_code_build_grep_cmd(cmd, sizeof(cmd), use_regex, scoped, file_pattern, tmpfile,
9843+
filelist, root_path);
98129844

98139845
FILE *fp = cbm_popen(cmd, "r");
98149846
if (!fp) {

‎src/mcp/mcp_internal.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,18 @@ bool cbm_mcp_auto_index_within_file_limit(const char *root_path, int file_limit,
4949
bool cbm_detect_node_in_hunks(const cbm_node_t *node, const cbm_changed_hunk_t *hunks,
5050
int hunk_count, const char *file);
5151

52+
/* search_code Windows pre-scan optimization: only simple suffix globs can be
53+
* moved ahead of
54+
* Select-String without changing the existing full-path
55+
* PowerShell -like contract. Exposed for
56+
* direct boundary tests only. */
57+
bool cbm_search_code_file_pattern_can_prefilter(const char *file_pattern);
58+
59+
/* Internal command builder exposed so tests can pin the PowerShell pipeline
60+
* ordering without
61+
* starting an external shell. */
62+
void cbm_search_code_build_grep_cmd(char *cmd, size_t cmd_sz, bool use_regex, bool scoped,
63+
const char *file_pattern, const char *tmpfile,
64+
const char *filelist, const char *root_path);
65+
5266
#endif

‎tests/test_mcp.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4501,6 +4501,48 @@ TEST(search_code_path_filter_matches_nothing) {
45014501
PASS();
45024502
}
45034503

4504+
TEST(search_code_file_pattern_prefilter_boundaries) {
4505+
ASSERT_TRUE(cbm_search_code_file_pattern_can_prefilter("*.pas"));
4506+
ASSERT_TRUE(cbm_search_code_file_pattern_can_prefilter("*.PAS"));
4507+
ASSERT_TRUE(cbm_search_code_file_pattern_can_prefilter("*.d.ts"));
4508+
ASSERT_TRUE(cbm_search_code_file_pattern_can_prefilter("*.foo-bar_1"));
4509+
4510+
ASSERT_FALSE(cbm_search_code_file_pattern_can_prefilter(NULL));
4511+
ASSERT_FALSE(cbm_search_code_file_pattern_can_prefilter(""));
4512+
ASSERT_FALSE(cbm_search_code_file_pattern_can_prefilter(".pas"));
4513+
ASSERT_FALSE(cbm_search_code_file_pattern_can_prefilter("*.*"));
4514+
ASSERT_FALSE(cbm_search_code_file_pattern_can_prefilter("src/*.pas"));
4515+
ASSERT_FALSE(cbm_search_code_file_pattern_can_prefilter("src\\*.pas"));
4516+
ASSERT_FALSE(cbm_search_code_file_pattern_can_prefilter("*.c++"));
4517+
ASSERT_FALSE(cbm_search_code_file_pattern_can_prefilter("*R&D*.go"));
4518+
PASS();
4519+
}
4520+
4521+
TEST(search_code_windows_prefilter_precedes_content_scan) {
4522+
#ifdef _WIN32
4523+
char command[CBM_SZ_4K];
4524+
cbm_search_code_build_grep_cmd(command, sizeof(command), false, true, "*.go", "C:/tmp/pattern",
4525+
"C:/tmp/filelist", "C:/tmp/root");
4526+
4527+
const char *prefilter = strstr(command, "Where-Object { $_ -like '*.go' }");
4528+
const char *content_scan = strstr(command, "ForEach-Object { Select-String");
4529+
const char *postfilter = strstr(command, "Where-Object { $_.Path -like '**.go' }");
4530+
ASSERT_NOT_NULL(prefilter);
4531+
ASSERT_NOT_NULL(content_scan);
4532+
ASSERT_NOT_NULL(postfilter);
4533+
ASSERT_TRUE(prefilter < content_scan);
4534+
ASSERT_TRUE(content_scan < postfilter);
4535+
4536+
cbm_search_code_build_grep_cmd(command, sizeof(command), false, true, "*handler*.go",
4537+
"C:/tmp/pattern", "C:/tmp/filelist", "C:/tmp/root");
4538+
ASSERT_NULL(strstr(command, "Where-Object { $_ -like '*handler*.go' }"));
4539+
ASSERT_NOT_NULL(strstr(command, "Where-Object { $_.Path -like '**handler*.go' }"));
4540+
PASS();
4541+
#else
4542+
SKIP_PLATFORM("PowerShell prefilter runs on Windows");
4543+
#endif
4544+
}
4545+
45044546
/* issue #283: search_code with regex=true and a syntactically invalid pattern
45054547
* must return an explicit error, not an empty result indistinguishable from a
45064548
* legitimate no-match. */
@@ -10722,6 +10764,8 @@ SUITE(mcp) {
1072210764
#endif
1072310765
RUN_TEST(search_code_path_filter_prefilter_keeps_matches);
1072410766
RUN_TEST(search_code_path_filter_matches_nothing);
10767+
RUN_TEST(search_code_file_pattern_prefilter_boundaries);
10768+
RUN_TEST(search_code_windows_prefilter_precedes_content_scan);
1072510769
RUN_TEST(search_code_invalid_regex_errors_issue283);
1072610770
RUN_TEST(search_code_literal_pipe_warns_issue282);
1072710771
RUN_TEST(search_code_ampersand_accepted_issue272);

0 commit comments

Comments
 (0)