Debian Codegraph @

cli/src/commands/workspace/forget.rs

raw
1 // Copyright 2020 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 clap_complete::ArgValueCandidates;
16 use itertools::Itertools as _;
17 use jj_lib::ref_name::WorkspaceNameBuf;
18 use jj_lib::workspace_store::SimpleWorkspaceStore;
19 use jj_lib::workspace_store::WorkspaceStore as _;
20 use tracing::instrument;
21
22 use crate::cli_util::CommandHelper;
23 use crate::command_error::CommandError;
24 use crate::complete;
25 use crate::ui::Ui;
26
27 /// Stop tracking a workspace's working-copy commit in the repo
28 ///
29 /// The workspace will not be touched on disk. It can be deleted from disk
30 /// before or after running this command.
31 #[derive(clap::Args, Clone, Debug)]
32 pub struct WorkspaceForgetArgs {
33 /// Names of the workspaces to forget. By default, forgets only the current
34 /// workspace.
35 #[arg(add = ArgValueCandidates::new(complete::workspaces))]
36 workspaces: Vec<WorkspaceNameBuf>,
37 }
38
39 #[instrument(skip_all)]
40 pub async fn cmd_workspace_forget(
41 ui: &mut Ui,
42 command: &CommandHelper,
43 args: &WorkspaceForgetArgs,
44 ) -> Result<(), CommandError> {
45 let mut workspace_command = command.workspace_helper(ui).await?;
46
47 let wss = if args.workspaces.is_empty() {
48 vec![workspace_command.workspace_name().to_owned()]
49 } else {
50 args.workspaces.clone()
51 };
52
53 let mut forget_ws = Vec::new();
54 for ws in &wss {
55 if workspace_command
56 .repo()
57 .view()
58 .get_wc_commit_id(ws)
59 .is_none()
60 {
61 writeln!(
62 ui.warning_default(),
63 "No such workspace: {}",
64 ws.as_symbol(),
65 )?;
66 } else {
67 forget_ws.push(ws);
68 }
69 }
70 if forget_ws.is_empty() {
71 writeln!(ui.status(), "Nothing changed.")?;
72 return Ok(());
73 }
74
75 let workspace_store = SimpleWorkspaceStore::load(workspace_command.repo_path())?;
76
77 // bundle every workspace forget into a single transaction, so that e.g.
78 // undo correctly restores all of them at once.
79 let mut tx = workspace_command.start_transaction();
80
81 for ws in &forget_ws {
82 tx.repo_mut().remove_workspace(ws).await?;
83 }
84
85 workspace_store.forget(&forget_ws.iter().map(|x| x.as_ref()).collect::<Vec<_>>())?;
86
87 let description = if let [ws] = forget_ws.as_slice() {
88 format!("forget workspace {}", ws.as_symbol())
89 } else {
90 format!(
91 "forget workspaces {}",
92 forget_ws.iter().map(|ws| ws.as_symbol()).join(", ")
93 )
94 };
95
96 tx.finish(ui, description).await?;
97 Ok(())
98 }