| 1 |
use core::str; |
| 2 |
|
| 3 |
use clap::Arg; |
| 4 |
use format_bytes::format_bytes; |
| 5 |
use hg::dirstate::status::DirstateStatus; |
| 6 |
use hg::dirstate::status::StatusError; |
| 7 |
use hg::dirstate::status::StatusOptions; |
| 8 |
use hg::errors::IoErrorContext; |
| 9 |
use hg::errors::IoResultExt; |
| 10 |
use hg::matchers::get_ignore_files; |
| 11 |
use hg::matchers::AlwaysMatcher; |
| 12 |
use hg::matchers::IntersectionMatcher; |
| 13 |
use hg::narrow; |
| 14 |
use hg::sparse; |
| 15 |
use hg::utils::hg_path::hg_path_to_path_buf; |
| 16 |
use hg::vfs::Vfs; |
| 17 |
use hg::warnings::HgWarningContext; |
| 18 |
use hg::{self}; |
| 19 |
|
| 20 |
use crate::error::CommandError; |
| 21 |
use crate::ui::print_warnings; |
| 22 |
use crate::ui::Ui; |
| 23 |
|
| 24 |
pub const HELP_TEXT: &str = " |
| 25 |
removes files not tracked by Mercurial |
| 26 |
|
| 27 |
Delete files not known to Mercurial. This is useful to test local and |
| 28 |
uncommitted changes in an otherwise-clean source tree. |
| 29 |
|
| 30 |
This means that purge will delete the following by default: |
| 31 |
|
| 32 |
- Unknown files: files marked with \"?\" by 'hg status' |
| 33 |
- Empty directories: in fact Mercurial ignores directories unless they contain |
| 34 |
files under source control management |
| 35 |
|
| 36 |
But it will leave untouched: |
| 37 |
|
| 38 |
- Modified and unmodified tracked files |
| 39 |
- Ignored files (unless -i or --all is specified) |
| 40 |
- New files added to the repository (with 'hg add') |
| 41 |
|
| 42 |
The --files and --dirs options can be used to direct purge to delete only |
| 43 |
files, only directories, or both. If neither option is given, both will be |
| 44 |
deleted. |
| 45 |
|
| 46 |
If directories are given on the command line, only files in these directories |
| 47 |
are considered. |
| 48 |
|
| 49 |
Be careful with purge, as you could irreversibly delete some files you forgot |
| 50 |
to add to the repository. If you only want to print the list of files that |
| 51 |
this program would delete, use the --print option. |
| 52 |
"; |
| 53 |
|
| 54 |
pub fn args() -> clap::Command { |
| 55 |
clap::command!("purge") |
| 56 |
.alias("clean") |
| 57 |
.arg( |
| 58 |
Arg::new("abort_on_error") |
| 59 |
.help("abort if an error occurs") |
| 60 |
.short('a') |
| 61 |
.long("abort-on-error") |
| 62 |
.action(clap::ArgAction::SetTrue), |
| 63 |
) |
| 64 |
.arg( |
| 65 |
Arg::new("all") |
| 66 |
.help("purge ignored files too") |
| 67 |
.long("all") |
| 68 |
.action(clap::ArgAction::SetTrue), |
| 69 |
) |
| 70 |
.arg( |
| 71 |
Arg::new("ignored") |
| 72 |
.help("purge only ignored files") |
| 73 |
.short('i') |
| 74 |
.long("ignored") |
| 75 |
.action(clap::ArgAction::SetTrue), |
| 76 |
) |
| 77 |
.arg( |
| 78 |
Arg::new("dirs") |
| 79 |
.help("purge empty directories") |
| 80 |
.long("dirs") |
| 81 |
.action(clap::ArgAction::SetTrue), |
| 82 |
) |
| 83 |
.arg( |
| 84 |
Arg::new("files") |
| 85 |
.help("purge files") |
| 86 |
.long("files") |
| 87 |
.action(clap::ArgAction::SetTrue), |
| 88 |
) |
| 89 |
.arg( |
| 90 |
Arg::new("print") |
| 91 |
.help("print filenames instead of deleting them") |
| 92 |
.short('p') |
| 93 |
.long("print") |
| 94 |
.action(clap::ArgAction::SetTrue), |
| 95 |
) |
| 96 |
.arg( |
| 97 |
Arg::new("print0") |
| 98 |
.help("end filenames with NUL, for use with xargs (implies -p/--print)") |
| 99 |
.short('0') |
| 100 |
.long("print0") |
| 101 |
.action(clap::ArgAction::SetTrue), |
| 102 |
) |
| 103 |
.arg( |
| 104 |
Arg::new("confirm") |
| 105 |
.help("ask before permanently deleting files") |
| 106 |
.long("confirm") |
| 107 |
.action(clap::ArgAction::SetTrue), |
| 108 |
) |
| 109 |
.arg( |
| 110 |
Arg::new("no-confirm") |
| 111 |
.help("don't ask before permanently deleting files") |
| 112 |
.long("no-confirm") |
| 113 |
.action(clap::ArgAction::SetTrue), |
| 114 |
) |
| 115 |
.arg( |
| 116 |
Arg::new("verbose") |
| 117 |
.help("enable additional output") |
| 118 |
.short('v') |
| 119 |
.action(clap::ArgAction::SetTrue) |
| 120 |
.long("verbose"), |
| 121 |
) |
| 122 |
.about(HELP_TEXT) |
| 123 |
} |
| 124 |
|
| 125 |
fn handle_removal_result( |
| 126 |
result: Result<(), impl ToString>, |
| 127 |
abort_on_error: bool, |
| 128 |
ui: &Ui, |
| 129 |
) -> Result<(), CommandError> { |
| 130 |
match result { |
| 131 |
Ok(()) => Ok(()), |
| 132 |
Err(e) => { |
| 133 |
let message = e.to_string(); |
| 134 |
if abort_on_error { |
| 135 |
Err(CommandError::abort_bytes(message)) |
| 136 |
} else { |
| 137 |
let message = |
| 138 |
message.strip_prefix("abort: ").unwrap_or(&message); |
| 139 |
ui.write_stderr(&format_bytes!( |
| 140 |
b"warning: {}\n", |
| 141 |
message.as_bytes() |
| 142 |
))?; |
| 143 |
Ok(()) |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> { |
| 150 |
let ui = invocation.ui; |
| 151 |
let args = invocation.subcommand_args; |
| 152 |
|
| 153 |
let repo = invocation.repo?; |
| 154 |
|
| 155 |
let abort_on_error = args.get_flag("abort_on_error"); |
| 156 |
let all = args.get_flag("all"); |
| 157 |
let ignored = args.get_flag("ignored"); |
| 158 |
let mut remove_dirs = args.get_flag("dirs"); |
| 159 |
let mut remove_files = args.get_flag("files"); |
| 160 |
let print = args.get_flag("print"); |
| 161 |
let print0 = args.get_flag("print0"); |
| 162 |
let verbose = args.get_flag("verbose"); |
| 163 |
|
| 164 |
let confirm = if args.get_flag("confirm") { |
| 165 |
true |
| 166 |
} else if args.get_flag("no-confirm") { |
| 167 |
false |
| 168 |
} else { |
| 169 |
invocation.config.get(b"extensions", b"purge").is_none() |
| 170 |
}; |
| 171 |
|
| 172 |
if confirm { |
| 173 |
return Err(CommandError::unsupported( |
| 174 |
"rhg purge does not support --confirm", |
| 175 |
)); |
| 176 |
} |
| 177 |
|
| 178 |
let act = !(print || print0); |
| 179 |
|
| 180 |
let unknown = !ignored || all; |
| 181 |
let ignored = ignored || all; |
| 182 |
|
| 183 |
if !(remove_dirs || remove_files) { |
| 184 |
remove_dirs = true; |
| 185 |
remove_files = true; |
| 186 |
} |
| 187 |
|
| 188 |
let mut dmap = repo.dirstate_map_mut()?; |
| 189 |
|
| 190 |
let check_exec = hg::checkexec::check_exec(repo.working_directory_path()); |
| 191 |
|
| 192 |
let options = StatusOptions { |
| 193 |
check_exec, |
| 194 |
list_clean: false, |
| 195 |
list_unknown: unknown, |
| 196 |
list_ignored: ignored, |
| 197 |
list_copies: false, |
| 198 |
collect_traversed_dirs: true, |
| 199 |
empty_dirs_keep_files: !remove_files, |
| 200 |
}; |
| 201 |
|
| 202 |
type StatusResult<'a> = Result<DirstateStatus<'a>, StatusError>; |
| 203 |
let after_status = |res: StatusResult, |
| 204 |
warnings: HgWarningContext| |
| 205 |
-> Result<_, CommandError> { |
| 206 |
print_warnings(ui, warnings, repo.working_directory_path()); |
| 207 |
|
| 208 |
let ds_status = res?; |
| 209 |
|
| 210 |
let mut files_to_remove = ds_status |
| 211 |
.ignored |
| 212 |
.into_iter() |
| 213 |
.chain(ds_status.unknown) |
| 214 |
.collect::<Vec<_>>(); |
| 215 |
|
| 216 |
let mut dirs_to_remove = ds_status.empty_dirs; |
| 217 |
|
| 218 |
files_to_remove.sort(); |
| 219 |
|
| 220 |
dirs_to_remove.sort(); |
| 221 |
dirs_to_remove.reverse(); |
| 222 |
|
| 223 |
if remove_files { |
| 224 |
// TODO: This loop can be parallelized. |
| 225 |
for file in files_to_remove { |
| 226 |
if act { |
| 227 |
let path = hg_path_to_path_buf(&*file.path)?; |
| 228 |
|
| 229 |
if verbose { |
| 230 |
ui.write_stdout(&format_bytes!( |
| 231 |
b"removing file {}\n", |
| 232 |
file.path.as_bytes() |
| 233 |
))?; |
| 234 |
} |
| 235 |
|
| 236 |
handle_removal_result( |
| 237 |
repo.working_directory_vfs().unlink(&path), |
| 238 |
abort_on_error, |
| 239 |
ui, |
| 240 |
)?; |
| 241 |
} else { |
| 242 |
ui.write_stdout(&format_bytes!( |
| 243 |
b"{}\n", |
| 244 |
file.path.as_bytes() |
| 245 |
))?; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
if remove_dirs { |
| 251 |
for dir in dirs_to_remove { |
| 252 |
if act { |
| 253 |
let path = hg_path_to_path_buf(&*dir)?; |
| 254 |
|
| 255 |
if verbose { |
| 256 |
ui.write_stdout(&format_bytes!( |
| 257 |
b"removing directory {}\n", |
| 258 |
*dir.as_bytes() |
| 259 |
))?; |
| 260 |
} |
| 261 |
|
| 262 |
let full_path = repo.working_directory_path().join(path); |
| 263 |
handle_removal_result( |
| 264 |
std::fs::remove_dir(&full_path).with_context(|| { |
| 265 |
IoErrorContext::RemovingFile(full_path.clone()) |
| 266 |
}), |
| 267 |
abort_on_error, |
| 268 |
ui, |
| 269 |
)?; |
| 270 |
} else { |
| 271 |
ui.write_stdout(&format_bytes!(b"{}\n", *dir.as_bytes()))?; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
Ok(()) |
| 277 |
}; |
| 278 |
|
| 279 |
let warning_context = HgWarningContext::new(); |
| 280 |
let warnings_sender = warning_context.sender(); |
| 281 |
|
| 282 |
let narrow_matcher = narrow::matcher(repo, warnings_sender)?; |
| 283 |
let sparse_matcher = sparse::matcher(repo, None, warnings_sender)?; |
| 284 |
// Sparse is only applicable for the working copy, not history. |
| 285 |
|
| 286 |
let matcher = match (repo.has_narrow(), repo.has_sparse()) { |
| 287 |
(true, true) => { |
| 288 |
Box::new(IntersectionMatcher::new(narrow_matcher, sparse_matcher)) |
| 289 |
} |
| 290 |
(true, false) => narrow_matcher, |
| 291 |
(false, true) => sparse_matcher, |
| 292 |
(false, false) => Box::new(AlwaysMatcher), |
| 293 |
}; |
| 294 |
|
| 295 |
dmap.with_status( |
| 296 |
&matcher, |
| 297 |
repo.working_directory_path().to_owned(), |
| 298 |
get_ignore_files(repo), |
| 299 |
options, |
| 300 |
after_status, |
| 301 |
)?; |
| 302 |
|
| 303 |
Ok(()) |
| 304 |
} |