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