Debian Codegraph @

cli/src/commands/util/snapshot.rs

raw
1 // Copyright 2026 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 use crate::cli_util::CommandHelper;
16 use crate::cli_util::print_snapshot_stats;
17 use crate::command_error::CommandError;
18 use crate::ui::Ui;
19
20 /// Snapshot the working copy if needed
21 ///
22 /// Snapshots the working copy and updates the working-copy commit if the
23 /// working copy has changed since the last snapshot. Since almost every command
24 /// snapshots the working copy, there is very little reason to run this command
25 /// as a human; it is mostly meant for scripts.
26 ///
27 /// If you want to see the ID of the current operation after this command, run
28 /// `jj operation log --limit 1`. However, since that command also snapshots the
29 /// working copy, there would be no need to run `jj util snapshot` first.
30 ///
31 /// ### Example of programmatic snapshotting
32 ///
33 /// Consider the following:
34 ///
35 /// ```bash
36 /// $ echo content > new-file.txt
37 /// # At this point, `jj` does not know about `new-file.txt`.
38 /// $ ./my-script-that-runs-jj-commands
39 /// # Since the script ran `jj` commands, the changes to `new-file.txt` have
40 /// # been snapshotted in an operation, which you can see with:
41 /// $ jj operation log --patch
42 /// ```
43 ///
44 /// If `my-script-that-runs-jj-commands` has a "sandwich" of `jj util snapshot`
45 /// at the beginning and end of the script, then the operation that captures
46 /// `new-file.txt` will be distinct from the operations created by the `jj`
47 /// commands in that script. In addition, the `jj operation log --patch` that
48 /// you ran on the command-line after the script would also have its own
49 /// operation if needed. However, without `jj util snapshot`, these operations
50 /// will be mixed together in the operation log. If this is important to you,
51 /// then `jj util snapshot` is useful here to explicitly trigger a snapshot.
52 ///
53 /// ### Checking if the operation ID changed
54 ///
55 /// If you want to compare "before and after" operation IDs, it may be better to
56 /// use `jj operation log --no-graph --limit 1 -T id` to query for operation
57 /// information, which you can store in a variable in a script. You can then
58 /// compare these values as needed to figure out if anything changed during a
59 /// `jj` command, such as:
60 ///
61 /// ```bash
62 /// start_op_id="$(jj op log -G -n 1 -T id)"
63 /// jj git fetch || exit
64 /// fetch_op_id="$(jj op log -G -n 1 -T id)"
65 /// if [[ "${start_op_id}" == "${fetch_op_id}" ]]; then
66 /// # Nothing was fetched.
67 /// exit 0
68 /// fi
69 /// jj rebase -r 'mutable()' -o 'trunk()'
70 /// rebase_op_id="$(jj op log -G -n 1 -T id)"
71 /// if [[ "${fetch_op_id}" == "${rebase_op_id}" ]]; then
72 /// # Nothing was rebased.
73 /// exit 0
74 /// fi
75 /// echo "Fetched and rebased!"
76 /// ```
77 ///
78 /// Note that `jj util snapshot` does not output an operation ID, so it is not
79 /// suitable for checks such as these.
80 #[derive(clap::Args, Clone, Debug)]
81 #[command(verbatim_doc_comment)]
82 pub struct UtilSnapshotArgs {}
83
84 pub async fn cmd_util_snapshot(
85 ui: &mut Ui,
86 command: &CommandHelper,
87 _args: &UtilSnapshotArgs,
88 ) -> Result<(), CommandError> {
89 let (workspace_command, stats, was_snapshot_taken) =
90 command.workspace_helper_with_stats(ui).await?;
91 print_snapshot_stats(ui, &stats, workspace_command.env().path_converter())?;
92 if was_snapshot_taken {
93 writeln!(ui.status(), "Snapshot complete.")?;
94 } else {
95 writeln!(ui.status(), "No snapshot needed.")?;
96 }
97
98 Ok(())
99 }