ratatui_toolkit/services/git_watcher/methods/
watch.rs

1//! Watch a git repository for changes.
2
3use notify::{RecursiveMode, Watcher};
4use std::path::Path;
5
6use crate::services::git_watcher::GitWatcher;
7
8impl GitWatcher {
9    /// Start watching a git repository for changes.
10    ///
11    /// Watches the `.git` directory recursively to detect any changes
12    /// to the repository state (commits, staging, branches, etc.).
13    ///
14    /// # Arguments
15    ///
16    /// * `repo_path` - Path to the repository root (where `.git` is located).
17    ///
18    /// # Errors
19    ///
20    /// Returns a `notify::Error` if the path cannot be watched or
21    /// if the `.git` directory doesn't exist.
22    ///
23    /// # Example
24    ///
25    /// ```no_run
26    /// use ratatui_toolkit::services::git_watcher::GitWatcher;
27    /// use std::path::Path;
28    ///
29    /// let mut watcher = GitWatcher::new().unwrap();
30    /// watcher.watch(Path::new("/path/to/repo")).unwrap();
31    /// ```
32    pub fn watch(&mut self, repo_path: &Path) -> Result<(), notify::Error> {
33        let git_dir = repo_path.join(".git");
34
35        // Watch the .git directory recursively
36        self.watcher.watch(&git_dir, RecursiveMode::Recursive)?;
37
38        self.repo_path = Some(repo_path.to_path_buf());
39        // Mark that we have pending changes on initial watch so stats are computed
40        self.has_pending_changes = true;
41
42        Ok(())
43    }
44}