fileutil.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package fsext
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "sort"
  9. "strings"
  10. "time"
  11. "github.com/bmatcuk/doublestar/v4"
  12. "github.com/charlievieth/fastwalk"
  13. "github.com/charmbracelet/crush/internal/log"
  14. ignore "github.com/sabhiram/go-gitignore"
  15. )
  16. var rgPath string
  17. func init() {
  18. var err error
  19. rgPath, err = exec.LookPath("rg")
  20. if err != nil {
  21. if log.Initialized() {
  22. slog.Warn("Ripgrep (rg) not found in $PATH. Some grep features might be limited or slower.")
  23. }
  24. }
  25. }
  26. func GetRgCmd(globPattern string) *exec.Cmd {
  27. if rgPath == "" {
  28. return nil
  29. }
  30. rgArgs := []string{
  31. "--files",
  32. "-L",
  33. "--null",
  34. }
  35. if globPattern != "" {
  36. if !filepath.IsAbs(globPattern) && !strings.HasPrefix(globPattern, "/") {
  37. globPattern = "/" + globPattern
  38. }
  39. rgArgs = append(rgArgs, "--glob", globPattern)
  40. }
  41. return exec.Command(rgPath, rgArgs...)
  42. }
  43. func GetRgSearchCmd(pattern, path, include string) *exec.Cmd {
  44. if rgPath == "" {
  45. return nil
  46. }
  47. // Use -n to show line numbers and include the matched line
  48. args := []string{"-H", "-n", pattern}
  49. if include != "" {
  50. args = append(args, "--glob", include)
  51. }
  52. args = append(args, path)
  53. return exec.Command(rgPath, args...)
  54. }
  55. type FileInfo struct {
  56. Path string
  57. ModTime time.Time
  58. }
  59. func SkipHidden(path string) bool {
  60. // Check for hidden files (starting with a dot)
  61. base := filepath.Base(path)
  62. if base != "." && strings.HasPrefix(base, ".") {
  63. return true
  64. }
  65. commonIgnoredDirs := map[string]bool{
  66. ".crush": true,
  67. "node_modules": true,
  68. "vendor": true,
  69. "dist": true,
  70. "build": true,
  71. "target": true,
  72. ".git": true,
  73. ".idea": true,
  74. ".vscode": true,
  75. "__pycache__": true,
  76. "bin": true,
  77. "obj": true,
  78. "out": true,
  79. "coverage": true,
  80. "tmp": true,
  81. "temp": true,
  82. "logs": true,
  83. "generated": true,
  84. "bower_components": true,
  85. "jspm_packages": true,
  86. }
  87. parts := strings.SplitSeq(path, string(os.PathSeparator))
  88. for part := range parts {
  89. if commonIgnoredDirs[part] {
  90. return true
  91. }
  92. }
  93. return false
  94. }
  95. // FastGlobWalker provides gitignore-aware file walking with fastwalk
  96. type FastGlobWalker struct {
  97. gitignore *ignore.GitIgnore
  98. rootPath string
  99. }
  100. func NewFastGlobWalker(searchPath string) *FastGlobWalker {
  101. walker := &FastGlobWalker{
  102. rootPath: searchPath,
  103. }
  104. // Load gitignore if it exists
  105. gitignorePath := filepath.Join(searchPath, ".gitignore")
  106. if _, err := os.Stat(gitignorePath); err == nil {
  107. if gi, err := ignore.CompileIgnoreFile(gitignorePath); err == nil {
  108. walker.gitignore = gi
  109. }
  110. }
  111. return walker
  112. }
  113. func (w *FastGlobWalker) shouldSkip(path string) bool {
  114. if SkipHidden(path) {
  115. return true
  116. }
  117. if w.gitignore != nil {
  118. relPath, err := filepath.Rel(w.rootPath, path)
  119. if err == nil && w.gitignore.MatchesPath(relPath) {
  120. return true
  121. }
  122. }
  123. return false
  124. }
  125. func GlobWithDoubleStar(pattern, searchPath string, limit int) ([]string, bool, error) {
  126. walker := NewFastGlobWalker(searchPath)
  127. var matches []FileInfo
  128. conf := fastwalk.Config{
  129. Follow: true,
  130. // Use forward slashes when running a Windows binary under WSL or MSYS
  131. ToSlash: fastwalk.DefaultToSlash(),
  132. Sort: fastwalk.SortFilesFirst,
  133. }
  134. err := fastwalk.Walk(&conf, searchPath, func(path string, d os.DirEntry, err error) error {
  135. if err != nil {
  136. return nil // Skip files we can't access
  137. }
  138. if d.IsDir() {
  139. if walker.shouldSkip(path) {
  140. return filepath.SkipDir
  141. }
  142. return nil
  143. }
  144. if walker.shouldSkip(path) {
  145. return nil
  146. }
  147. // Check if path matches the pattern
  148. relPath, err := filepath.Rel(searchPath, path)
  149. if err != nil {
  150. relPath = path
  151. }
  152. matched, err := doublestar.Match(pattern, relPath)
  153. if err != nil || !matched {
  154. return nil
  155. }
  156. info, err := d.Info()
  157. if err != nil {
  158. return nil
  159. }
  160. matches = append(matches, FileInfo{Path: path, ModTime: info.ModTime()})
  161. if limit > 0 && len(matches) >= limit*2 {
  162. return filepath.SkipAll
  163. }
  164. return nil
  165. })
  166. if err != nil {
  167. return nil, false, fmt.Errorf("fastwalk error: %w", err)
  168. }
  169. sort.Slice(matches, func(i, j int) bool {
  170. return matches[i].ModTime.After(matches[j].ModTime)
  171. })
  172. truncated := false
  173. if limit > 0 && len(matches) > limit {
  174. matches = matches[:limit]
  175. truncated = true
  176. }
  177. results := make([]string, len(matches))
  178. for i, m := range matches {
  179. results[i] = m.Path
  180. }
  181. return results, truncated, nil
  182. }
  183. func PrettyPath(path string) string {
  184. // replace home directory with ~
  185. homeDir, err := os.UserHomeDir()
  186. if err == nil {
  187. path = strings.ReplaceAll(path, homeDir, "~")
  188. }
  189. return path
  190. }
  191. func DirTrim(pwd string, lim int) string {
  192. var (
  193. out string
  194. sep = string(filepath.Separator)
  195. )
  196. dirs := strings.Split(pwd, sep)
  197. if lim > len(dirs)-1 || lim <= 0 {
  198. return pwd
  199. }
  200. for i := len(dirs) - 1; i > 0; i-- {
  201. out = sep + out
  202. if i == len(dirs)-1 {
  203. out = dirs[i]
  204. } else if i >= len(dirs)-lim {
  205. out = string(dirs[i][0]) + out
  206. } else {
  207. out = "..." + out
  208. break
  209. }
  210. }
  211. out = filepath.Join("~", out)
  212. return out
  213. }