diff --git a/cli/src/commands/debug/watchman.rs b/cli/src/commands/debug/watchman.rs index dfb1faf8f..908b613fa 100644 --- a/cli/src/commands/debug/watchman.rs +++ b/cli/src/commands/debug/watchman.rs @@ -46,28 +46,52 @@ pub fn cmd_debug_watchman( match subcommand { WatchmanCommand::Status => { // TODO(ilyagr): It would be nice to add colors here - match command.settings().fsmonitor_settings()? { - FsmonitorSettings::Watchman { .. } => { - writeln!(ui.stdout(), "Watchman is enabled via `core.fsmonitor`.")? + let config = match command.settings().fsmonitor_settings()? { + FsmonitorSettings::Watchman(config) => { + writeln!(ui.stdout(), "Watchman is enabled via `core.fsmonitor`.")?; + writeln!( + ui.stdout(), + r"Background snapshotting is {}. Use `core.watchman.register_snapshot_trigger` to control it.", + if config.register_trigger { + "enabled" + } else { + "disabled" + } + )?; + config + } + FsmonitorSettings::None => { + writeln!( + ui.stdout(), + r#"Watchman is disabled. Set `core.fsmonitor="watchman"` to enable."# + )?; + writeln!( + ui.stdout(), + "Attempting to contact the `watchman` CLI regardless..." + )?; + WatchmanConfig::default() } - FsmonitorSettings::None => writeln!( - ui.stdout(), - "Watchman is disabled. Set `core.fsmonitor=\"watchman\"` to \ - enable.\nAttempting to contact the `watchman` CLI regardless..." - )?, other_fsmonitor => { return Err(user_error(format!( - "This command does not support the currently enabled filesystem monitor: \ - {other_fsmonitor:?}." + r"This command does not support the currently enabled filesystem monitor: {other_fsmonitor:?}." ))) } }; let wc = check_local_disk_wc(workspace_command.working_copy().as_any())?; - let _ = wc.query_watchman(&WatchmanConfig::default())?; + let _ = wc.query_watchman(&config)?; writeln!( ui.stdout(), "The watchman server seems to be installed and working correctly." )?; + writeln!( + ui.stdout(), + "Background snapshotting is currently {}.", + if wc.is_watchman_trigger_registered(&config)? { + "active" + } else { + "inactive" + } + )?; } WatchmanCommand::QueryClock => { let wc = check_local_disk_wc(workspace_command.working_copy().as_any())?; diff --git a/lib/src/fsmonitor.rs b/lib/src/fsmonitor.rs index 0fff7a9c9..c4152159d 100644 --- a/lib/src/fsmonitor.rs +++ b/lib/src/fsmonitor.rs @@ -30,7 +30,7 @@ use crate::settings::ConfigResultExt; #[derive(Default, Eq, PartialEq, Clone, Debug)] pub struct WatchmanConfig { /// Whether to use triggers to monitor for changes in the background. - register_trigger: bool, + pub register_trigger: bool, } /// The recognized kinds of filesystem monitors. @@ -260,7 +260,7 @@ pub mod watchman { /// Return whether or not a trigger has been registered already. #[instrument(skip(self))] - async fn is_trigger_registered(&self) -> Result { + pub async fn is_trigger_registered(&self) -> Result { info!("Checking for an existing Watchman trigger..."); Ok(self .client diff --git a/lib/src/local_working_copy.rs b/lib/src/local_working_copy.rs index e4c417e6c..55a7ed677 100644 --- a/lib/src/local_working_copy.rs +++ b/lib/src/local_working_copy.rs @@ -739,6 +739,22 @@ impl TreeState { Ok(changed_files) } + #[cfg(feature = "watchman")] + #[tokio::main(flavor = "current_thread")] + #[instrument(skip(self))] + pub async fn is_watchman_trigger_registered( + &self, + config: &WatchmanConfig, + ) -> Result { + let fsmonitor = watchman::Fsmonitor::init(&self.working_copy_path, config) + .await + .map_err(|err| TreeStateError::Fsmonitor(Box::new(err)))?; + fsmonitor + .is_trigger_registered() + .await + .map_err(|err| TreeStateError::Fsmonitor(Box::new(err))) + } + /// Look for changes to the working copy. If there are any changes, create /// a new tree from it and return it, and also update the dirstate on disk. #[instrument(skip_all)] @@ -1696,6 +1712,19 @@ impl LocalWorkingCopy { err: err.into(), }) } + + #[cfg(feature = "watchman")] + pub fn is_watchman_trigger_registered( + &self, + config: &WatchmanConfig, + ) -> Result { + self.tree_state()? + .is_watchman_trigger_registered(config) + .map_err(|err| WorkingCopyStateError { + message: "Failed to query watchman".to_string(), + err: err.into(), + }) + } } pub struct LocalWorkingCopyFactory {}