@@ -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 ) {
0 commit comments