@@ -5,6 +5,7 @@ import (
55 "context"
66 "encoding/json"
77 "fmt"
8+ "io"
89 "os"
910 "os/exec"
1011 "path/filepath"
@@ -377,6 +378,11 @@ func searchFilesWithRegex(pattern, rootPath, include string) ([]grepMatch, error
377378}
378379
379380func fileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, string, error) {
381+ // Quick binary file detection
382+ if isBinaryFile(filePath) {
383+ return false, 0, "", nil
384+ }
385+
380386 file, err := os.Open(filePath)
381387 if err != nil {
382388 return false, 0, "", err
@@ -396,6 +402,47 @@ func fileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, st
396402 return false, 0, "", scanner.Err()
397403}
398404
405+var binaryExts = map[string]struct{}{
406+ ".exe": {}, ".dll": {}, ".so": {}, ".dylib": {},
407+ ".bin": {}, ".obj": {}, ".o": {}, ".a": {},
408+ ".zip": {}, ".tar": {}, ".gz": {}, ".bz2": {},
409+ ".jpg": {}, ".jpeg": {}, ".png": {}, ".gif": {},
410+ ".pdf": {}, ".doc": {}, ".docx": {}, ".xls": {},
411+ ".mp3": {}, ".mp4": {}, ".avi": {}, ".mov": {},
412+}
413+
414+// isBinaryFile performs a quick check to determine if a file is binary
415+func isBinaryFile(filePath string) bool {
416+ // Check file extension first (fastest)
417+ ext := strings.ToLower(filepath.Ext(filePath))
418+ if _, isBinary := binaryExts[ext]; isBinary {
419+ return true
420+ }
421+
422+ // Quick content check for files without clear extensions
423+ file, err := os.Open(filePath)
424+ if err != nil {
425+ return false // If we can't open it, let the caller handle the error
426+ }
427+ defer file.Close()
428+
429+ // Read first 512 bytes to check for null bytes
430+ buffer := make([]byte, 512)
431+ n, err := file.Read(buffer)
432+ if err != nil && err != io.EOF {
433+ return false
434+ }
435+
436+ // Check for null bytes (common in binary files)
437+ for i := range n {
438+ if buffer[i] == 0 {
439+ return true
440+ }
441+ }
442+
443+ return false
444+}
445+
399446func globToRegex(glob string) string {
400447 regexPattern := strings.ReplaceAll(glob, ".", "\\.")
401448 regexPattern = strings.ReplaceAll(regexPattern, "*", ".*")