Debian Codegraph @

cli/src/commands/file/untrack.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 std::io::Write as _;
16
17 use clap_complete::ArgValueCompleter;
18 use itertools::Itertools as _;
19 use jj_lib::merge::Merge;
20 use jj_lib::merged_tree_builder::MergedTreeBuilder;
21 use tracing::instrument;
22
23 use crate::cli_util::CommandHelper;
24 use crate::cli_util::export_working_copy_changes_to_git;
25 use crate::cli_util::print_snapshot_stats;
26 use crate::cli_util::print_unmatched_explicit_paths;
27 use crate::command_error::CommandError;
28 use crate::command_error::user_error;
29 use crate::complete;
30 use crate::ui::Ui;
31
32 /// Stop tracking specified paths in the working copy
33 #[derive(clap::Args, Clone, Debug)]
34 pub(crate) struct FileUntrackArgs {
35 /// Paths to untrack. They must already be ignored.
36 ///
37 /// The paths could be ignored via a .gitignore or .git/info/exclude (in
38 /// colocated workspaces).
39 #[arg(required = true, value_name = "FILESETS", value_hint = clap::ValueHint::AnyPath)]
40 #[arg(add = ArgValueCompleter::new(complete::all_revision_files))]
41 paths: Vec<String>,
42 }
43
44 #[instrument(skip_all)]
45 pub(crate) async fn cmd_file_untrack(
46 ui: &mut Ui,
47 command: &CommandHelper,
48 args: &FileUntrackArgs,
49 ) -> Result<(), CommandError> {
50 let mut workspace_command = command.workspace_helper(ui).await?;
51 let fileset_expression = workspace_command.parse_file_patterns(ui, &args.paths)?;
52 let matcher = fileset_expression.to_matcher();
53 let auto_tracking_matcher = workspace_command.auto_tracking_matcher(ui)?;
54 let options =
55 workspace_command.snapshot_options_with_start_tracking_matcher(&auto_tracking_matcher)?;
56
57 let working_copy_shared_with_git = workspace_command.working_copy_shared_with_git();
58 let workspace_root = workspace_command.workspace_root().to_owned();
59
60 let mut tx = workspace_command.start_transaction().into_inner();
61 let (mut locked_ws, wc_commit) = workspace_command.start_working_copy_mutation().await?;
62 // Create a new tree without the unwanted files
63 let mut tree_builder = MergedTreeBuilder::new(wc_commit.tree());
64 let wc_tree = wc_commit.tree();
65 for (path, _value) in wc_tree.entries_matching(matcher.as_ref()) {
66 tree_builder.set_or_remove(path, Merge::absent());
67 }
68 let new_tree = tree_builder.write_tree().await?;
69 let new_commit = tx
70 .repo_mut()
71 .rewrite_commit(&wc_commit)
72 .set_tree(new_tree)
73 .write()
74 .await?;
75 // Reset the working copy to the new commit
76 locked_ws.locked_wc().reset(&new_commit).await?;
77 // Commit the working copy again so we can inform the user if paths couldn't be
78 // untracked because they're not ignored.
79 let (new_wc_tree, stats) = locked_ws.locked_wc().snapshot(&options).await?;
80 if new_wc_tree.tree_ids() != new_commit.tree_ids() {
81 let added_back = new_wc_tree.entries_matching(matcher.as_ref()).collect_vec();
82 if !added_back.is_empty() {
83 drop(locked_ws);
84 let path = &added_back[0].0;
85 let ui_path = workspace_command.format_file_path(path);
86 let message = if added_back.len() > 1 {
87 format!(
88 "'{}' and {} other files are not ignored.",
89 ui_path,
90 added_back.len() - 1
91 )
92 } else {
93 format!("'{ui_path}' is not ignored.")
94 };
95 return Err(user_error(message).hinted(
96 "Files that are not ignored will be added back by the next command.
97 Make sure they're ignored, then try again.",
98 ));
99 } else {
100 // This means there were some concurrent changes made in the working copy. We
101 // don't want to mix those in, so reset the working copy again.
102 locked_ws.locked_wc().reset(&new_commit).await?;
103 }
104 }
105 let num_rebased = tx.repo_mut().rebase_descendants().await?;
106 if num_rebased > 0 {
107 writeln!(ui.status(), "Rebased {num_rebased} descendant commits.")?;
108 }
109 if working_copy_shared_with_git {
110 export_working_copy_changes_to_git(
111 ui,
112 tx.repo_mut(),
113 &workspace_root,
114 &wc_tree,
115 &new_commit.tree(),
116 )
117 .await?;
118 }
119 let repo = tx.commit("untrack paths").await?;
120 locked_ws.finish(repo.op_id().clone()).await?;
121 print_unmatched_explicit_paths(ui, &workspace_command, &fileset_expression, [&wc_tree]).await?;
122 print_snapshot_stats(ui, &stats, workspace_command.env().path_converter())?;
123 Ok(())
124 }