Debian Codegraph @

cli/src/commands/debug/watchman.rs

raw
1 // Copyright 2023 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #[cfg(feature = "watchman")]
16 use std::io::Write as _;
17
18 use clap::Subcommand;
19 #[cfg(feature = "watchman")]
20 use jj_lib::fsmonitor::FsmonitorSettings;
21 #[cfg(feature = "watchman")]
22 use jj_lib::fsmonitor::WatchmanConfig;
23 #[cfg(feature = "watchman")]
24 use jj_lib::local_working_copy::LocalWorkingCopy;
25 #[cfg(feature = "watchman")]
26 use jj_lib::working_copy::WorkingCopy;
27
28 use crate::cli_util::CommandHelper;
29 use crate::command_error::CommandError;
30 use crate::command_error::user_error;
31 use crate::ui::Ui;
32
33 #[derive(Subcommand, Clone, Debug)]
34 pub enum DebugWatchmanCommand {
35 /// Check whether `watchman` is enabled and whether it's correctly installed
36 Status,
37 QueryClock,
38 QueryChangedFiles,
39 ResetClock,
40 }
41
42 #[cfg(feature = "watchman")]
43 pub async fn cmd_debug_watchman(
44 ui: &mut Ui,
45 command: &CommandHelper,
46 subcommand: &DebugWatchmanCommand,
47 ) -> Result<(), CommandError> {
48 use jj_lib::local_working_copy::LockedLocalWorkingCopy;
49
50 let mut workspace_command = command.workspace_helper(ui).await?;
51 let repo = workspace_command.repo().clone();
52 let watchman_config = WatchmanConfig {
53 // The value is likely irrelevant here. TODO(ilyagr): confirm
54 register_trigger: false,
55 };
56 match subcommand {
57 DebugWatchmanCommand::Status => {
58 // TODO(ilyagr): It would be nice to add colors here
59 let config = match FsmonitorSettings::from_settings(workspace_command.settings())? {
60 FsmonitorSettings::Watchman(config) => {
61 writeln!(ui.stdout(), "Watchman is enabled via `fsmonitor.backend`.")?;
62 writeln!(
63 ui.stdout(),
64 "Background snapshotting is {}. Use \
65 `fsmonitor.watchman.register-snapshot-trigger` to control it.",
66 if config.register_trigger {
67 "enabled"
68 } else {
69 "disabled"
70 }
71 )?;
72 config
73 }
74 FsmonitorSettings::None => {
75 writeln!(
76 ui.stdout(),
77 r#"Watchman is disabled. Set `fsmonitor.backend="watchman"` to enable."#
78 )?;
79 writeln!(
80 ui.stdout(),
81 "Attempting to contact the `watchman` CLI regardless..."
82 )?;
83 watchman_config
84 }
85 other_fsmonitor => {
86 return Err(user_error(format!(
87 r"This command does not support the currently enabled filesystem monitor: {other_fsmonitor:?}."
88 )));
89 }
90 };
91 let wc = check_local_disk_wc(workspace_command.working_copy())?;
92 wc.query_watchman(&config).await?;
93 writeln!(
94 ui.stdout(),
95 "The watchman server seems to be installed and working correctly."
96 )?;
97 writeln!(
98 ui.stdout(),
99 "Background snapshotting is currently {}.",
100 if wc.is_watchman_trigger_registered(&config).await? {
101 "active"
102 } else {
103 "inactive"
104 }
105 )?;
106 }
107 DebugWatchmanCommand::QueryClock => {
108 let wc = check_local_disk_wc(workspace_command.working_copy())?;
109 let (clock, _changed_files) = wc.query_watchman(&watchman_config).await?;
110 writeln!(ui.stdout(), "Clock: {clock:?}")?;
111 }
112 DebugWatchmanCommand::QueryChangedFiles => {
113 let wc = check_local_disk_wc(workspace_command.working_copy())?;
114 let (_clock, changed_files) = wc.query_watchman(&watchman_config).await?;
115 writeln!(ui.stdout(), "Changed files: {changed_files:?}")?;
116 }
117 DebugWatchmanCommand::ResetClock => {
118 let (mut locked_ws, _commit) = workspace_command.start_working_copy_mutation().await?;
119 let Some(locked_local_wc): Option<&mut LockedLocalWorkingCopy> =
120 locked_ws.locked_wc().downcast_mut()
121 else {
122 return Err(user_error(
123 "This command requires a standard local-disk working copy",
124 ));
125 };
126 locked_local_wc.reset_watchman()?;
127 locked_ws.finish(repo.op_id().clone()).await?;
128 writeln!(ui.status(), "Reset Watchman clock.")?;
129 }
130 }
131 Ok(())
132 }
133
134 #[cfg(not(feature = "watchman"))]
135 pub async fn cmd_debug_watchman(
136 _ui: &mut Ui,
137 _command: &CommandHelper,
138 _subcommand: &DebugWatchmanCommand,
139 ) -> Result<(), CommandError> {
140 Err(user_error(
141 "Cannot query Watchman because jj was not compiled with the `watchman` feature",
142 ))
143 }
144
145 #[cfg(feature = "watchman")]
146 fn check_local_disk_wc(x: &dyn WorkingCopy) -> Result<&LocalWorkingCopy, CommandError> {
147 x.downcast_ref()
148 .ok_or_else(|| user_error("This command requires a standard local-disk working copy"))
149 }