NewDaemon creates a new watch daemon for the given root
(root string, verbose bool)
| 27 | |
| 28 | // NewDaemon creates a new watch daemon for the given root |
| 29 | func NewDaemon(root string, verbose bool) (*Daemon, error) { |
| 30 | absRoot, err := filepath.Abs(root) |
| 31 | if err != nil { |
| 32 | return nil, fmt.Errorf("invalid root path: %w", err) |
| 33 | } |
| 34 | |
| 35 | watcher, err := fsnotify.NewWatcher() |
| 36 | if err != nil { |
| 37 | return nil, fmt.Errorf("failed to create watcher: %w", err) |
| 38 | } |
| 39 | |
| 40 | gitCache := scanner.NewGitIgnoreCache(root) |
| 41 | |
| 42 | // Check if git repo (fast, one-time) |
| 43 | isGitRepo := false |
| 44 | if _, err := os.Stat(filepath.Join(absRoot, ".git")); err == nil { |
| 45 | isGitRepo = true |
| 46 | } |
| 47 | |
| 48 | d := &Daemon{ |
| 49 | root: absRoot, |
| 50 | watcher: watcher, |
| 51 | gitCache: gitCache, |
| 52 | verbose: verbose, |
| 53 | done: make(chan struct{}), |
| 54 | eventLog: filepath.Join(absRoot, ".codemap", "events.log"), |
| 55 | graph: &Graph{ |
| 56 | Root: absRoot, |
| 57 | Files: make(map[string]*scanner.FileInfo), |
| 58 | DepCtx: make(map[string]*DepContext), |
| 59 | State: make(map[string]*FileState), |
| 60 | Events: make([]Event, 0), |
| 61 | WorkingSet: NewWorkingSet(), |
| 62 | IsGitRepo: isGitRepo, |
| 63 | }, |
| 64 | } |
| 65 | |
| 66 | return d, nil |
| 67 | } |
| 68 | |
| 69 | // Start begins watching and returns immediately |
| 70 | func (d *Daemon) Start() error { |