package daemon import ( "fmt" "log" "os" "path/filepath" "strings" ) // setupWatchers initializes the filesystem watcher for the repository func (d *Daemon) setupWatchers() error { // Get the repository root repoRoot := filepath.Dir(d.repo.GetOnyxPath()) // Add the root directory to the watcher if err := d.addWatchRecursive(repoRoot); err != nil { return fmt.Errorf("failed to add watches: %w", err) } log.Printf("Watching repository at: %s", repoRoot) return nil } // addWatchRecursive adds watches for a directory and all its subdirectories func (d *Daemon) addWatchRecursive(path string) error { // Walk the directory tree return filepath.Walk(path, func(walkPath string, info os.FileInfo, err error) error { if err != nil { // Skip directories we can't access log.Printf("Warning: cannot access %s: %v", walkPath, err) return nil } // Skip files, only watch directories if !info.IsDir() { return nil } // Skip .git and .onx directories if shouldIgnorePath(walkPath) { return filepath.SkipDir } // Add watch for this directory if err := d.watcher.Add(walkPath); err != nil { log.Printf("Warning: cannot watch %s: %v", walkPath, err) return nil } return nil }) } // shouldIgnorePath determines if a path should be ignored by the watcher func shouldIgnorePath(path string) bool { // Get the base name and check against ignored patterns base := filepath.Base(path) // Ignore .git and .onx directories if base == ".git" || base == ".onx" { return true } // Ignore hidden directories starting with . if strings.HasPrefix(base, ".") && base != "." { return true } // Ignore common build/dependency directories ignoredDirs := []string{ "node_modules", "vendor", "target", "build", "dist", ".vscode", ".idea", "__pycache__", ".pytest_cache", ".mypy_cache", } for _, ignored := range ignoredDirs { if base == ignored { return true } } // Ignore temporary and backup files if strings.HasSuffix(path, "~") || strings.HasSuffix(path, ".swp") || strings.HasSuffix(path, ".tmp") { return true } return false } // AddWatch adds a new directory to the watch list (useful for newly created directories) func (d *Daemon) AddWatch(path string) error { if shouldIgnorePath(path) { return nil } return d.watcher.Add(path) } // RemoveWatch removes a directory from the watch list func (d *Daemon) RemoveWatch(path string) error { return d.watcher.Remove(path) }