diff --git a/lib/src/fsmonitor.rs b/lib/src/fsmonitor.rs index e55f11e14d..3cb9ef5d3d 100644 --- a/lib/src/fsmonitor.rs +++ b/lib/src/fsmonitor.rs @@ -187,7 +187,9 @@ pub mod watchman { }; if kind == super::FsmonitorKind::WatchmanTrigger { - monitor.register_trigger().await?; + if !monitor.is_trigger_registered().await? { + monitor.register_trigger().await?; + } } Ok(monitor) } @@ -245,6 +247,20 @@ pub mod watchman { } } + /// Return whether or not a trigger has been registered already. + #[instrument(skip(self))] + async fn is_trigger_registered(&self) -> Result { + info!("Checking for an existing Watchman trigger..."); + Ok(self + .client + .list_triggers(&self.resolved_root) + .await + .map_err(Error::WatchmanTriggerError)? + .triggers + .iter() + .any(|t| t.name == "jj-background-monitor")) + } + /// Register trigger for changed files. #[instrument(skip(self))] async fn register_trigger(&self) -> Result<(), Error> { @@ -255,7 +271,7 @@ pub mod watchman { .register_trigger( &self.resolved_root, TriggerRequest { - name: format!("jj-trigger-{:?}", self.resolved_root.path()), + name: "jj-background-monitor".to_string(), command: vec!["jj".to_string(), "st".to_string()], expression: Some(self.build_exclude_expr()), ..Default::default()