grep.go

  1package tools
  2
  3import (
  4	"bufio"
  5	"context"
  6	"encoding/json"
  7	"fmt"
  8	"io"
  9	"os"
 10	"os/exec"
 11	"path/filepath"
 12	"regexp"
 13	"sort"
 14	"strconv"
 15	"strings"
 16	"sync"
 17	"time"
 18
 19	"github.com/charmbracelet/crush/internal/fsext"
 20)
 21
 22// regexCache provides thread-safe caching of compiled regex patterns
 23type regexCache struct {
 24	cache map[string]*regexp.Regexp
 25	mu    sync.RWMutex
 26}
 27
 28// newRegexCache creates a new regex cache
 29func newRegexCache() *regexCache {
 30	return &regexCache{
 31		cache: make(map[string]*regexp.Regexp),
 32	}
 33}
 34
 35// get retrieves a compiled regex from cache or compiles and caches it
 36func (rc *regexCache) get(pattern string) (*regexp.Regexp, error) {
 37	// Try to get from cache first (read lock)
 38	rc.mu.RLock()
 39	if regex, exists := rc.cache[pattern]; exists {
 40		rc.mu.RUnlock()
 41		return regex, nil
 42	}
 43	rc.mu.RUnlock()
 44
 45	// Compile the regex (write lock)
 46	rc.mu.Lock()
 47	defer rc.mu.Unlock()
 48
 49	// Double-check in case another goroutine compiled it while we waited
 50	if regex, exists := rc.cache[pattern]; exists {
 51		return regex, nil
 52	}
 53
 54	// Compile and cache the regex
 55	regex, err := regexp.Compile(pattern)
 56	if err != nil {
 57		return nil, err
 58	}
 59
 60	rc.cache[pattern] = regex
 61	return regex, nil
 62}
 63
 64// Global regex cache instances
 65var (
 66	searchRegexCache = newRegexCache()
 67	globRegexCache   = newRegexCache()
 68	// Pre-compiled regex for glob conversion (used frequently)
 69	globBraceRegex = regexp.MustCompile(`\{([^}]+)\}`)
 70)
 71
 72type GrepParams struct {
 73	Pattern     string `json:"pattern"`
 74	Path        string `json:"path"`
 75	Include     string `json:"include"`
 76	LiteralText bool   `json:"literal_text"`
 77}
 78
 79type grepMatch struct {
 80	path     string
 81	modTime  time.Time
 82	lineNum  int
 83	lineText string
 84}
 85
 86type GrepResponseMetadata struct {
 87	NumberOfMatches int  `json:"number_of_matches"`
 88	Truncated       bool `json:"truncated"`
 89}
 90
 91type grepTool struct {
 92	workingDir string
 93}
 94
 95const (
 96	GrepToolName    = "grep"
 97	grepDescription = `Fast content search tool that finds files containing specific text or patterns, returning matching file paths sorted by modification time (newest first).
 98
 99WHEN TO USE THIS TOOL:
100- Use when you need to find files containing specific text or patterns
101- Great for searching code bases for function names, variable declarations, or error messages
102- Useful for finding all files that use a particular API or pattern
103
104HOW TO USE:
105- Provide a regex pattern to search for within file contents
106- Set literal_text=true if you want to search for the exact text with special characters (recommended for non-regex users)
107- Optionally specify a starting directory (defaults to current working directory)
108- Optionally provide an include pattern to filter which files to search
109- Results are sorted with most recently modified files first
110
111REGEX PATTERN SYNTAX (when literal_text=false):
112- Supports standard regular expression syntax
113- 'function' searches for the literal text "function"
114- 'log\..*Error' finds text starting with "log." and ending with "Error"
115- 'import\s+.*\s+from' finds import statements in JavaScript/TypeScript
116
117COMMON INCLUDE PATTERN EXAMPLES:
118- '*.js' - Only search JavaScript files
119- '*.{ts,tsx}' - Only search TypeScript files
120- '*.go' - Only search Go files
121
122LIMITATIONS:
123- Results are limited to 100 files (newest first)
124- Performance depends on the number of files being searched
125- Very large binary files may be skipped
126- Hidden files (starting with '.') are skipped
127
128IGNORE FILE SUPPORT:
129- Respects .gitignore patterns to skip ignored files and directories
130- Respects .crushignore patterns for additional ignore rules
131- Both ignore files are automatically detected in the search root directory
132
133CROSS-PLATFORM NOTES:
134- Uses ripgrep (rg) command if available for better performance
135- Falls back to built-in Go implementation if ripgrep is not available
136- File paths are normalized automatically for cross-platform compatibility
137
138TIPS:
139- For faster, more targeted searches, first use Glob to find relevant files, then use Grep
140- When doing iterative exploration that may require multiple rounds of searching, consider using the Agent tool instead
141- Always check if results are truncated and refine your search pattern if needed
142- Use literal_text=true when searching for exact text containing special characters like dots, parentheses, etc.`
143)
144
145func NewGrepTool(workingDir string) BaseTool {
146	return &grepTool{
147		workingDir: workingDir,
148	}
149}
150
151func (g *grepTool) Name() string {
152	return GrepToolName
153}
154
155func (g *grepTool) Info() ToolInfo {
156	return ToolInfo{
157		Name:        GrepToolName,
158		Description: grepDescription,
159		Parameters: map[string]any{
160			"pattern": map[string]any{
161				"type":        "string",
162				"description": "The regex pattern to search for in file contents",
163			},
164			"path": map[string]any{
165				"type":        "string",
166				"description": "The directory to search in. Defaults to the current working directory.",
167			},
168			"include": map[string]any{
169				"type":        "string",
170				"description": "File pattern to include in the search (e.g. \"*.js\", \"*.{ts,tsx}\")",
171			},
172			"literal_text": map[string]any{
173				"type":        "boolean",
174				"description": "If true, the pattern will be treated as literal text with special regex characters escaped. Default is false.",
175			},
176		},
177		Required: []string{"pattern"},
178	}
179}
180
181// escapeRegexPattern escapes special regex characters so they're treated as literal characters
182func escapeRegexPattern(pattern string) string {
183	specialChars := []string{"\\", ".", "+", "*", "?", "(", ")", "[", "]", "{", "}", "^", "$", "|"}
184	escaped := pattern
185
186	for _, char := range specialChars {
187		escaped = strings.ReplaceAll(escaped, char, "\\"+char)
188	}
189
190	return escaped
191}
192
193func (g *grepTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
194	var params GrepParams
195	if err := json.Unmarshal([]byte(call.Input), &params); err != nil {
196		return NewTextErrorResponse(fmt.Sprintf("error parsing parameters: %s", err)), nil
197	}
198
199	if params.Pattern == "" {
200		return NewTextErrorResponse("pattern is required"), nil
201	}
202
203	// If literal_text is true, escape the pattern
204	searchPattern := params.Pattern
205	if params.LiteralText {
206		searchPattern = escapeRegexPattern(params.Pattern)
207	}
208
209	searchPath := params.Path
210	if searchPath == "" {
211		searchPath = g.workingDir
212	}
213
214	matches, truncated, err := searchFiles(ctx, searchPattern, searchPath, params.Include, 100)
215	if err != nil {
216		return ToolResponse{}, fmt.Errorf("error searching files: %w", err)
217	}
218
219	var output strings.Builder
220	if len(matches) == 0 {
221		output.WriteString("No files found")
222	} else {
223		fmt.Fprintf(&output, "Found %d matches\n", len(matches))
224
225		currentFile := ""
226		for _, match := range matches {
227			if currentFile != match.path {
228				if currentFile != "" {
229					output.WriteString("\n")
230				}
231				currentFile = match.path
232				fmt.Fprintf(&output, "%s:\n", match.path)
233			}
234			if match.lineNum > 0 {
235				fmt.Fprintf(&output, "  Line %d: %s\n", match.lineNum, match.lineText)
236			} else {
237				fmt.Fprintf(&output, "  %s\n", match.path)
238			}
239		}
240
241		if truncated {
242			output.WriteString("\n(Results are truncated. Consider using a more specific path or pattern.)")
243		}
244	}
245
246	return WithResponseMetadata(
247		NewTextResponse(output.String()),
248		GrepResponseMetadata{
249			NumberOfMatches: len(matches),
250			Truncated:       truncated,
251		},
252	), nil
253}
254
255func searchFiles(ctx context.Context, pattern, rootPath, include string, limit int) ([]grepMatch, bool, error) {
256	matches, err := searchWithRipgrep(ctx, pattern, rootPath, include)
257	if err != nil {
258		matches, err = searchFilesWithRegex(pattern, rootPath, include)
259		if err != nil {
260			return nil, false, err
261		}
262	}
263
264	sort.Slice(matches, func(i, j int) bool {
265		return matches[i].modTime.After(matches[j].modTime)
266	})
267
268	truncated := len(matches) > limit
269	if truncated {
270		matches = matches[:limit]
271	}
272
273	return matches, truncated, nil
274}
275
276func searchWithRipgrep(ctx context.Context, pattern, path, include string) ([]grepMatch, error) {
277	cmd := getRgSearchCmd(ctx, pattern, path, include)
278	if cmd == nil {
279		return nil, fmt.Errorf("ripgrep not found in $PATH")
280	}
281
282	cmd.Args = append(
283		cmd.Args,
284		"--ignore-file", filepath.Join(path, ".gitignore"),
285		"--ignore-file", filepath.Join(path, ".crushignore"),
286	)
287
288	output, err := cmd.Output()
289	if err != nil {
290		if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 {
291			return []grepMatch{}, nil
292		}
293		return nil, err
294	}
295
296	lines := strings.Split(strings.TrimSpace(string(output)), "\n")
297	matches := make([]grepMatch, 0, len(lines))
298
299	for _, line := range lines {
300		if line == "" {
301			continue
302		}
303
304		// Parse ripgrep output format: file:line:content
305		parts := strings.SplitN(line, ":", 3)
306		if len(parts) < 3 {
307			continue
308		}
309
310		filePath := parts[0]
311		lineNum, err := strconv.Atoi(parts[1])
312		if err != nil {
313			continue
314		}
315		lineText := parts[2]
316
317		fileInfo, err := os.Stat(filePath)
318		if err != nil {
319			continue // Skip files we can't access
320		}
321
322		matches = append(matches, grepMatch{
323			path:     filePath,
324			modTime:  fileInfo.ModTime(),
325			lineNum:  lineNum,
326			lineText: lineText,
327		})
328	}
329
330	return matches, nil
331}
332
333func searchFilesWithRegex(pattern, rootPath, include string) ([]grepMatch, error) {
334	matches := []grepMatch{}
335
336	// Use cached regex compilation
337	regex, err := searchRegexCache.get(pattern)
338	if err != nil {
339		return nil, fmt.Errorf("invalid regex pattern: %w", err)
340	}
341
342	var includePattern *regexp.Regexp
343	if include != "" {
344		regexPattern := globToRegex(include)
345		includePattern, err = globRegexCache.get(regexPattern)
346		if err != nil {
347			return nil, fmt.Errorf("invalid include pattern: %w", err)
348		}
349	}
350
351	// Create walker with gitignore and crushignore support
352	walker := fsext.NewFastGlobWalker(rootPath)
353
354	err = filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
355		if err != nil {
356			return nil // Skip errors
357		}
358
359		if info.IsDir() {
360			return nil // Skip directories
361		}
362
363		// Use walker's shouldSkip method instead of just SkipHidden
364		if walker.ShouldSkip(path) {
365			return nil
366		}
367
368		// Skip hidden files (starting with a dot) to match ripgrep's default behavior
369		base := filepath.Base(path)
370		if base != "." && strings.HasPrefix(base, ".") {
371			return nil
372		}
373
374		if includePattern != nil && !includePattern.MatchString(path) {
375			return nil
376		}
377
378		match, lineNum, lineText, err := fileContainsPattern(path, regex)
379		if err != nil {
380			return nil // Skip files we can't read
381		}
382
383		if match {
384			matches = append(matches, grepMatch{
385				path:     path,
386				modTime:  info.ModTime(),
387				lineNum:  lineNum,
388				lineText: lineText,
389			})
390
391			if len(matches) >= 200 {
392				return filepath.SkipAll
393			}
394		}
395
396		return nil
397	})
398	if err != nil {
399		return nil, err
400	}
401
402	return matches, nil
403}
404
405func fileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, string, error) {
406	// Quick binary file detection
407	if isBinaryFile(filePath) {
408		return false, 0, "", nil
409	}
410
411	file, err := os.Open(filePath)
412	if err != nil {
413		return false, 0, "", err
414	}
415	defer file.Close()
416
417	scanner := bufio.NewScanner(file)
418	lineNum := 0
419	for scanner.Scan() {
420		lineNum++
421		line := scanner.Text()
422		if pattern.MatchString(line) {
423			return true, lineNum, line, nil
424		}
425	}
426
427	return false, 0, "", scanner.Err()
428}
429
430var binaryExts = map[string]struct{}{
431	".exe": {}, ".dll": {}, ".so": {}, ".dylib": {},
432	".bin": {}, ".obj": {}, ".o": {}, ".a": {},
433	".zip": {}, ".tar": {}, ".gz": {}, ".bz2": {},
434	".jpg": {}, ".jpeg": {}, ".png": {}, ".gif": {},
435	".pdf": {}, ".doc": {}, ".docx": {}, ".xls": {},
436	".mp3": {}, ".mp4": {}, ".avi": {}, ".mov": {},
437}
438
439// isBinaryFile performs a quick check to determine if a file is binary
440func isBinaryFile(filePath string) bool {
441	// Check file extension first (fastest)
442	ext := strings.ToLower(filepath.Ext(filePath))
443	if _, isBinary := binaryExts[ext]; isBinary {
444		return true
445	}
446
447	// Quick content check for files without clear extensions
448	file, err := os.Open(filePath)
449	if err != nil {
450		return false // If we can't open it, let the caller handle the error
451	}
452	defer file.Close()
453
454	// Read first 512 bytes to check for null bytes
455	buffer := make([]byte, 512)
456	n, err := file.Read(buffer)
457	if err != nil && err != io.EOF {
458		return false
459	}
460
461	// Check for null bytes (common in binary files)
462	for i := range n {
463		if buffer[i] == 0 {
464			return true
465		}
466	}
467
468	return false
469}
470
471func globToRegex(glob string) string {
472	regexPattern := strings.ReplaceAll(glob, ".", "\\.")
473	regexPattern = strings.ReplaceAll(regexPattern, "*", ".*")
474	regexPattern = strings.ReplaceAll(regexPattern, "?", ".")
475
476	// Use pre-compiled regex instead of compiling each time
477	regexPattern = globBraceRegex.ReplaceAllStringFunc(regexPattern, func(match string) string {
478		inner := match[1 : len(match)-1]
479		return "(" + strings.ReplaceAll(inner, ",", "|") + ")"
480	})
481
482	return regexPattern
483}