From 3e7ad4d23cadd292f0ad4e564a51bfbc68760012 Mon Sep 17 00:00:00 2001 From: Matt Kulukundis Date: Tue, 18 Jun 2024 23:17:03 -0400 Subject: [PATCH] Add `jj debug snapshot` command and use it in trigger The command only takes a snapshot and avoids other overhead, so it can be used as a target for the watchman trigger that gets installed. --- cli/src/commands/debug/mod.rs | 4 ++++ cli/src/commands/debug/snapshot.rs | 33 +++++++++++++++++++++++++++ cli/tests/test_workspaces.rs | 36 ++++++++++++++++++++++++++++++ lib/src/fsmonitor.rs | 5 ++--- 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 cli/src/commands/debug/snapshot.rs diff --git a/cli/src/commands/debug/mod.rs b/cli/src/commands/debug/mod.rs index 5285fe2218..d10de1c25f 100644 --- a/cli/src/commands/debug/mod.rs +++ b/cli/src/commands/debug/mod.rs @@ -17,6 +17,7 @@ pub mod index; pub mod operation; pub mod reindex; pub mod revset; +pub mod snapshot; pub mod template; pub mod tree; pub mod watchman; @@ -33,6 +34,7 @@ use self::index::{cmd_debug_index, IndexArgs}; use self::operation::{cmd_debug_operation, OperationArgs}; use self::reindex::{cmd_debug_reindex, ReindexArgs}; use self::revset::{cmd_debug_revset, RevsetArgs}; +use self::snapshot::{cmd_debug_snapshot, SnapshotArgs}; use self::template::{cmd_debug_template, TemplateArgs}; use self::tree::{cmd_debug_tree, TreeArgs}; use self::watchman::{cmd_debug_watchman, WatchmanCommand}; @@ -57,6 +59,7 @@ pub enum DebugCommand { Tree(TreeArgs), #[command(subcommand)] Watchman(WatchmanCommand), + Snapshot(SnapshotArgs), } pub fn cmd_debug( @@ -74,6 +77,7 @@ pub fn cmd_debug( DebugCommand::Operation(args) => cmd_debug_operation(ui, command, args), DebugCommand::Tree(args) => cmd_debug_tree(ui, command, args), DebugCommand::Watchman(args) => cmd_debug_watchman(ui, command, args), + DebugCommand::Snapshot(args) => cmd_debug_snapshot(ui, command, args), } } diff --git a/cli/src/commands/debug/snapshot.rs b/cli/src/commands/debug/snapshot.rs new file mode 100644 index 0000000000..31cfb758a9 --- /dev/null +++ b/cli/src/commands/debug/snapshot.rs @@ -0,0 +1,33 @@ +// Copyright 2024 The Jujutsu Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::fmt::Debug; + +use crate::cli_util::CommandHelper; +use crate::command_error::CommandError; +use crate::ui::Ui; + +/// Trigger a snapshot in the op log +#[derive(clap::Args, Clone, Debug)] +pub struct SnapshotArgs {} + +pub fn cmd_debug_snapshot( + ui: &mut Ui, + command: &CommandHelper, + _args: &SnapshotArgs, +) -> Result<(), CommandError> { + // workspace helper will snapshot as needed + command.workspace_helper(ui)?; + Ok(()) +} diff --git a/cli/tests/test_workspaces.rs b/cli/tests/test_workspaces.rs index e84983de53..560af84875 100644 --- a/cli/tests/test_workspaces.rs +++ b/cli/tests/test_workspaces.rs @@ -782,6 +782,42 @@ fn test_workspaces_root() { "###); } +#[test] +fn test_debug_snapshot() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); + let repo_path = test_env.env_root().join("repo"); + + std::fs::write(repo_path.join("file"), "contents").unwrap(); + test_env.jj_cmd_ok(&repo_path, &["debug", "snapshot"]); + let stdout = test_env.jj_cmd_success(&repo_path, &["op", "log"]); + insta::assert_snapshot!(stdout, @r###" + @ 2d01fc903f0f test-username@host.example.com 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 + │ snapshot working copy + │ args: jj debug snapshot + ◉ b51416386f26 test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 + │ add workspace 'default' + ◉ 9a7d829846af test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 + │ initialize repo + ◉ 000000000000 root() + "###); + test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "initial"]); + let stdout = test_env.jj_cmd_success(&repo_path, &["op", "log"]); + insta::assert_snapshot!(stdout, @r###" + @ 53a64275e379 test-username@host.example.com 2001-02-03 04:05:10.000 +07:00 - 2001-02-03 04:05:10.000 +07:00 + │ describe commit 123ed18e4c4c0d77428df41112bc02ffc83fb935 + │ args: jj describe -m initial + ◉ 2d01fc903f0f test-username@host.example.com 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 + │ snapshot working copy + │ args: jj debug snapshot + ◉ b51416386f26 test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 + │ add workspace 'default' + ◉ 9a7d829846af test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 + │ initialize repo + ◉ 000000000000 root() + "###); +} + fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String { let template = r#" separate(" ", diff --git a/lib/src/fsmonitor.rs b/lib/src/fsmonitor.rs index 388f0c745c..0fff7a9c95 100644 --- a/lib/src/fsmonitor.rs +++ b/lib/src/fsmonitor.rs @@ -283,9 +283,8 @@ pub mod watchman { name: "jj-background-monitor".to_string(), command: vec![ "jj".to_string(), - "files".to_string(), - "-r".to_string(), - "root()".to_string(), + "debug".to_string(), + "snapshot".to_string(), ], expression: Some(self.build_exclude_expr()), ..Default::default()