@@ -5,6 +5,7 @@ import (
6 "encoding/json"
6 "encoding/json"
10 "path/filepath"
11 "path/filepath"
@@ -377,6 +378,11 @@ func searchFilesWithRegex(pattern, rootPath, include string) ([]grepMatch, error
379func fileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, string, error) {
380func fileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, string, error) {
381+ // Quick binary file detection
382+ if isBinaryFile(filePath) {
383+ return false, 0, "", nil
380 file, err := os.Open(filePath)
386 file, err := os.Open(filePath)
381 if err != nil {
387 if err != nil {
382 return false, 0, "", err
388 return false, 0, "", err
@@ -396,6 +402,47 @@ func fileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, st
396 return false, 0, "", scanner.Err()
402 return false, 0, "", scanner.Err()
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": {},
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 {
422+ // Quick content check for files without clear extensions
423+ file, err := os.Open(filePath)
425+ return false // If we can't open it, let the caller handle the error
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 {
436+ // Check for null bytes (common in binary files)
399func globToRegex(glob string) string {
446func globToRegex(glob string) string {
400 regexPattern := strings.ReplaceAll(glob, ".", "\\.")
447 regexPattern := strings.ReplaceAll(glob, ".", "\\.")
401 regexPattern = strings.ReplaceAll(regexPattern, "*", ".*")
448 regexPattern = strings.ReplaceAll(regexPattern, "*", ".*")