| 1 |
// status.rs |
| 2 |
// |
| 3 |
// Copyright 2020, Georges Racinet <[email protected]> |
| 4 |
// |
| 5 |
// This software may be used and distributed according to the terms of the |
| 6 |
// GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
use std::io; |
| 9 |
use std::mem::take; |
| 10 |
|
| 11 |
use clap::Arg; |
| 12 |
use format_bytes::format_bytes; |
| 13 |
use hg::dirstate::entry::TruncatedTimestamp; |
| 14 |
use hg::dirstate::status::BadMatch; |
| 15 |
use hg::dirstate::status::DirstateStatus; |
| 16 |
use hg::dirstate::status::StatusError; |
| 17 |
use hg::dirstate::status::StatusOptions; |
| 18 |
use hg::dirstate::status::StatusPath; |
| 19 |
use hg::errors::HgError; |
| 20 |
use hg::errors::IoResultExt; |
| 21 |
use hg::file_patterns::parse_pattern_args; |
| 22 |
use hg::lock::LockError; |
| 23 |
use hg::matchers::get_ignore_files; |
| 24 |
use hg::matchers::AlwaysMatcher; |
| 25 |
use hg::matchers::IntersectionMatcher; |
| 26 |
use hg::narrow; |
| 27 |
use hg::repo::Repo; |
| 28 |
use hg::revlog::filelog::is_file_modified; |
| 29 |
use hg::revlog::filelog::FileCompOutcome; |
| 30 |
use hg::revlog::options::default_revlog_options; |
| 31 |
use hg::revlog::RevisionOrWdir; |
| 32 |
use hg::revlog::RevlogError; |
| 33 |
use hg::revlog::RevlogType; |
| 34 |
use hg::sparse; |
| 35 |
use hg::utils::debug::debug_wait_for_file; |
| 36 |
use hg::utils::files::get_bytes_from_os_str; |
| 37 |
use hg::utils::hg_path::hg_path_to_path_buf; |
| 38 |
use hg::warnings::HgWarningContext; |
| 39 |
use hg::Revision; |
| 40 |
use hg::{self}; |
| 41 |
use rayon::prelude::*; |
| 42 |
use tracing::info; |
| 43 |
|
| 44 |
use crate::error::CommandError; |
| 45 |
use crate::ui::print_warnings; |
| 46 |
use crate::ui::relative_paths; |
| 47 |
use crate::ui::RelativePaths; |
| 48 |
use crate::ui::Ui; |
| 49 |
use crate::utils::path_utils::RelativizePaths; |
| 50 |
|
| 51 |
pub const HELP_TEXT: &str = " |
| 52 |
Show changed files in the working directory |
| 53 |
|
| 54 |
This is a pure Rust version of `hg status`. |
| 55 |
|
| 56 |
Some options might be missing, check the list below. |
| 57 |
"; |
| 58 |
|
| 59 |
pub fn args() -> clap::Command { |
| 60 |
clap::command!("status") |
| 61 |
.alias("st") |
| 62 |
.args_override_self(true) |
| 63 |
.about(HELP_TEXT) |
| 64 |
.arg( |
| 65 |
Arg::new("file") |
| 66 |
.value_parser(clap::value_parser!(std::ffi::OsString)) |
| 67 |
.help("show only these files") |
| 68 |
.action(clap::ArgAction::Append), |
| 69 |
) |
| 70 |
.arg( |
| 71 |
Arg::new("all") |
| 72 |
.help("show status of all files") |
| 73 |
.short('A') |
| 74 |
.action(clap::ArgAction::SetTrue) |
| 75 |
.long("all"), |
| 76 |
) |
| 77 |
.arg( |
| 78 |
Arg::new("modified") |
| 79 |
.help("show only modified files") |
| 80 |
.short('m') |
| 81 |
.action(clap::ArgAction::SetTrue) |
| 82 |
.long("modified"), |
| 83 |
) |
| 84 |
.arg( |
| 85 |
Arg::new("added") |
| 86 |
.help("show only added files") |
| 87 |
.short('a') |
| 88 |
.action(clap::ArgAction::SetTrue) |
| 89 |
.long("added"), |
| 90 |
) |
| 91 |
.arg( |
| 92 |
Arg::new("removed") |
| 93 |
.help("show only removed files") |
| 94 |
.short('r') |
| 95 |
.action(clap::ArgAction::SetTrue) |
| 96 |
.long("removed"), |
| 97 |
) |
| 98 |
.arg( |
| 99 |
Arg::new("clean") |
| 100 |
.help("show only clean files") |
| 101 |
.short('c') |
| 102 |
.action(clap::ArgAction::SetTrue) |
| 103 |
.long("clean"), |
| 104 |
) |
| 105 |
.arg( |
| 106 |
Arg::new("deleted") |
| 107 |
.help("show only deleted files") |
| 108 |
.short('d') |
| 109 |
.action(clap::ArgAction::SetTrue) |
| 110 |
.long("deleted"), |
| 111 |
) |
| 112 |
.arg( |
| 113 |
Arg::new("unknown") |
| 114 |
.help("show only unknown (not tracked) files") |
| 115 |
.short('u') |
| 116 |
.action(clap::ArgAction::SetTrue) |
| 117 |
.long("unknown"), |
| 118 |
) |
| 119 |
.arg( |
| 120 |
Arg::new("ignored") |
| 121 |
.help("show only ignored files") |
| 122 |
.short('i') |
| 123 |
.action(clap::ArgAction::SetTrue) |
| 124 |
.long("ignored"), |
| 125 |
) |
| 126 |
.arg( |
| 127 |
Arg::new("copies") |
| 128 |
.help("show source of copied files (DEFAULT: ui.statuscopies)") |
| 129 |
.short('C') |
| 130 |
.action(clap::ArgAction::SetTrue) |
| 131 |
.long("copies"), |
| 132 |
) |
| 133 |
.arg( |
| 134 |
Arg::new("no-copies") |
| 135 |
.action(clap::ArgAction::SetTrue) |
| 136 |
.long("no-copies") |
| 137 |
.overrides_with("copies"), |
| 138 |
) |
| 139 |
.arg( |
| 140 |
Arg::new("print0") |
| 141 |
.help("end filenames with NUL, for use with xargs") |
| 142 |
.short('0') |
| 143 |
.action(clap::ArgAction::SetTrue) |
| 144 |
.long("print0"), |
| 145 |
) |
| 146 |
.arg( |
| 147 |
Arg::new("no-status") |
| 148 |
.help("hide status prefix") |
| 149 |
.short('n') |
| 150 |
.action(clap::ArgAction::SetTrue) |
| 151 |
.long("no-status"), |
| 152 |
) |
| 153 |
.arg( |
| 154 |
Arg::new("verbose") |
| 155 |
.help("enable additional output") |
| 156 |
.short('v') |
| 157 |
.action(clap::ArgAction::SetTrue) |
| 158 |
.long("verbose"), |
| 159 |
) |
| 160 |
.arg( |
| 161 |
Arg::new("rev") |
| 162 |
.help("show difference from/to revision") |
| 163 |
.long("rev") |
| 164 |
.num_args(1) |
| 165 |
.action(clap::ArgAction::Append) |
| 166 |
.value_name("REV"), |
| 167 |
) |
| 168 |
.arg( |
| 169 |
Arg::new("change") |
| 170 |
.help("list the changed files of a revision") |
| 171 |
.long("change") |
| 172 |
.value_name("REV") |
| 173 |
.conflicts_with("rev"), |
| 174 |
) |
| 175 |
} |
| 176 |
|
| 177 |
fn parse_revpair( |
| 178 |
repo: &Repo, |
| 179 |
revs: Option<Vec<String>>, |
| 180 |
) -> Result<Option<(Revision, Revision)>, CommandError> { |
| 181 |
let Some(revs) = revs else { |
| 182 |
return Ok(None); |
| 183 |
}; |
| 184 |
let resolve = |
| 185 |
|input| match hg::revset::resolve_single(input, repo)?.exclude_wdir() { |
| 186 |
Some(rev) => Ok(rev), |
| 187 |
None => Err(RevlogError::WDirUnsupported), |
| 188 |
}; |
| 189 |
match revs.as_slice() { |
| 190 |
[] => Ok(None), |
| 191 |
[rev1, rev2] => Ok(Some((resolve(rev1)?, resolve(rev2)?))), |
| 192 |
_ => Err(CommandError::unsupported("expected 0 or 2 --rev flags")), |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/// Pure data type allowing the caller to specify file states to display |
| 197 |
#[derive(Copy, Clone, Debug)] |
| 198 |
pub struct DisplayStates { |
| 199 |
pub modified: bool, |
| 200 |
pub added: bool, |
| 201 |
pub removed: bool, |
| 202 |
pub clean: bool, |
| 203 |
pub deleted: bool, |
| 204 |
pub unknown: bool, |
| 205 |
pub ignored: bool, |
| 206 |
} |
| 207 |
|
| 208 |
pub const DEFAULT_DISPLAY_STATES: DisplayStates = DisplayStates { |
| 209 |
modified: true, |
| 210 |
added: true, |
| 211 |
removed: true, |
| 212 |
clean: false, |
| 213 |
deleted: true, |
| 214 |
unknown: true, |
| 215 |
ignored: false, |
| 216 |
}; |
| 217 |
|
| 218 |
pub const ALL_DISPLAY_STATES: DisplayStates = DisplayStates { |
| 219 |
modified: true, |
| 220 |
added: true, |
| 221 |
removed: true, |
| 222 |
clean: true, |
| 223 |
deleted: true, |
| 224 |
unknown: true, |
| 225 |
ignored: true, |
| 226 |
}; |
| 227 |
|
| 228 |
impl DisplayStates { |
| 229 |
pub fn is_empty(&self) -> bool { |
| 230 |
!(self.modified |
| 231 |
|| self.added |
| 232 |
|| self.removed |
| 233 |
|| self.clean |
| 234 |
|| self.deleted |
| 235 |
|| self.unknown |
| 236 |
|| self.ignored) |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
fn has_unfinished_merge(repo: &Repo) -> Result<bool, CommandError> { |
| 241 |
Ok(repo.dirstate_parents()?.is_merge()) |
| 242 |
} |
| 243 |
|
| 244 |
fn has_unfinished_state(repo: &Repo) -> Result<bool, CommandError> { |
| 245 |
// These are all the known values for the [fname] argument of |
| 246 |
// [addunfinished] function in [state.py] |
| 247 |
let known_state_files: &[&str] = &[ |
| 248 |
"bisect.state", |
| 249 |
"graftstate", |
| 250 |
"histedit-state", |
| 251 |
"rebasestate", |
| 252 |
"shelvedstate", |
| 253 |
"transplant/journal", |
| 254 |
"updatestate", |
| 255 |
]; |
| 256 |
if has_unfinished_merge(repo)? { |
| 257 |
return Ok(true); |
| 258 |
}; |
| 259 |
for f in known_state_files { |
| 260 |
if repo.hg_vfs().join(f).exists() { |
| 261 |
return Ok(true); |
| 262 |
} |
| 263 |
} |
| 264 |
Ok(false) |
| 265 |
} |
| 266 |
|
| 267 |
#[tracing::instrument(level = "debug", skip_all, name = "rhg status")] |
| 268 |
pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> { |
| 269 |
// TODO: lift these limitations |
| 270 |
if invocation.config.get(b"commands", b"status.terse").is_some() { |
| 271 |
return Err(CommandError::unsupported( |
| 272 |
"status.terse is not yet supported with rhg status", |
| 273 |
)); |
| 274 |
} |
| 275 |
|
| 276 |
let ui = invocation.ui; |
| 277 |
let config = invocation.config; |
| 278 |
let args = invocation.subcommand_args; |
| 279 |
|
| 280 |
let revs = args.get_many::<String>("rev"); |
| 281 |
let change = args.get_one::<String>("change"); |
| 282 |
let print0 = args.get_flag("print0"); |
| 283 |
let verbose = args.get_flag("verbose") |
| 284 |
|| config.get_bool(b"ui", b"verbose")? |
| 285 |
|| config.get_bool(b"commands", b"status.verbose")?; |
| 286 |
let verbose = verbose && !print0; |
| 287 |
|
| 288 |
let all = args.get_flag("all"); |
| 289 |
let display_states = if all { |
| 290 |
// TODO when implementing `--quiet`: it excludes clean files |
| 291 |
// from `--all` |
| 292 |
ALL_DISPLAY_STATES |
| 293 |
} else { |
| 294 |
let requested = DisplayStates { |
| 295 |
modified: args.get_flag("modified"), |
| 296 |
added: args.get_flag("added"), |
| 297 |
removed: args.get_flag("removed"), |
| 298 |
clean: args.get_flag("clean"), |
| 299 |
deleted: args.get_flag("deleted"), |
| 300 |
unknown: args.get_flag("unknown"), |
| 301 |
ignored: args.get_flag("ignored"), |
| 302 |
}; |
| 303 |
if requested.is_empty() { |
| 304 |
DEFAULT_DISPLAY_STATES |
| 305 |
} else { |
| 306 |
requested |
| 307 |
} |
| 308 |
}; |
| 309 |
let no_status = args.get_flag("no-status"); |
| 310 |
let list_copies = if args.get_flag("copies") { |
| 311 |
true |
| 312 |
} else if args.get_flag("no-copies") { |
| 313 |
false |
| 314 |
} else { |
| 315 |
config.get_bool(b"ui", b"statuscopies")? |
| 316 |
}; |
| 317 |
let list_copies = (list_copies || all) && !no_status; |
| 318 |
|
| 319 |
let repo = invocation.repo?; |
| 320 |
let revpair = parse_revpair(repo, revs.map(|i| i.cloned().collect()))?; |
| 321 |
let change = |
| 322 |
change.map(|rev| hg::revset::resolve_single(rev, repo)).transpose()?; |
| 323 |
// Treat `rhg status --change wdir()` the same as `rhg status`. |
| 324 |
let change = change.and_then(RevisionOrWdir::exclude_wdir); |
| 325 |
|
| 326 |
if verbose && has_unfinished_state(repo)? { |
| 327 |
return Err(CommandError::unsupported( |
| 328 |
"verbose status output is not supported by rhg (and is needed because we're in an unfinished operation)", |
| 329 |
)); |
| 330 |
} |
| 331 |
|
| 332 |
let mut dmap = repo.dirstate_map_mut()?; |
| 333 |
|
| 334 |
let check_exec = hg::checkexec::check_exec(repo.working_directory_path()); |
| 335 |
|
| 336 |
let options = StatusOptions { |
| 337 |
check_exec, |
| 338 |
list_clean: display_states.clean, |
| 339 |
list_unknown: display_states.unknown, |
| 340 |
list_ignored: display_states.ignored, |
| 341 |
list_copies, |
| 342 |
collect_traversed_dirs: false, |
| 343 |
empty_dirs_keep_files: false, |
| 344 |
}; |
| 345 |
|
| 346 |
type StatusResult<'a> = Result<DirstateStatus<'a>, StatusError>; |
| 347 |
|
| 348 |
let relative_status = config |
| 349 |
.get_option(b"commands", b"status.relative")? |
| 350 |
.expect("commands.status.relative should have a default value"); |
| 351 |
|
| 352 |
let relativize_paths = relative_status || { |
| 353 |
// See in Python code with `getuipathfn` usage in `commands.py`. |
| 354 |
let legacy_relative_behavior = args.contains_id("file"); |
| 355 |
match relative_paths(invocation.config)? { |
| 356 |
RelativePaths::Legacy => legacy_relative_behavior, |
| 357 |
RelativePaths::Bool(v) => v, |
| 358 |
} |
| 359 |
}; |
| 360 |
|
| 361 |
let mut output = DisplayStatusPaths { |
| 362 |
ui, |
| 363 |
no_status, |
| 364 |
relativize: if relativize_paths { |
| 365 |
Some(RelativizePaths::new(repo)?) |
| 366 |
} else { |
| 367 |
None |
| 368 |
}, |
| 369 |
print0, |
| 370 |
}; |
| 371 |
|
| 372 |
let after_status = |res: StatusResult, |
| 373 |
warnings| |
| 374 |
-> Result<_, CommandError> { |
| 375 |
print_warnings(ui, warnings, repo.working_directory_path()); |
| 376 |
|
| 377 |
let mut ds_status = res?; |
| 378 |
|
| 379 |
for (path, error) in take(&mut ds_status.bad) { |
| 380 |
let error = match error { |
| 381 |
BadMatch::OsError(code) => { |
| 382 |
std::io::Error::from_raw_os_error(code).to_string() |
| 383 |
} |
| 384 |
BadMatch::BadType(ty) => { |
| 385 |
format!("unsupported file type (type is {})", ty) |
| 386 |
} |
| 387 |
}; |
| 388 |
ui.write_stderr(&format_bytes!( |
| 389 |
b"{}: {}\n", |
| 390 |
path.as_bytes(), |
| 391 |
error.as_bytes() |
| 392 |
))? |
| 393 |
} |
| 394 |
if !ds_status.unsure.is_empty() { |
| 395 |
info!( |
| 396 |
"Files to be rechecked by retrieval from filelog: {:?}", |
| 397 |
ds_status.unsure.iter().map(|s| &s.path).collect::<Vec<_>>() |
| 398 |
); |
| 399 |
} |
| 400 |
let mut fixup = Vec::new(); |
| 401 |
if !ds_status.unsure.is_empty() |
| 402 |
&& (display_states.modified || display_states.clean) |
| 403 |
{ |
| 404 |
let p1 = repo.dirstate_parents()?.p1; |
| 405 |
let manifest = repo.manifest_for_node(p1)?; |
| 406 |
let working_directory_vfs = repo.working_directory_vfs(); |
| 407 |
let store_vfs = repo.store_vfs(); |
| 408 |
let filelog_open_options = default_revlog_options( |
| 409 |
repo.config(), |
| 410 |
repo.requirements(), |
| 411 |
RevlogType::Filelog, |
| 412 |
)?; |
| 413 |
let res: Vec<_> = take(&mut ds_status.unsure) |
| 414 |
.into_par_iter() |
| 415 |
.map(|to_check| { |
| 416 |
// The compiler seems to get a bit confused with complex |
| 417 |
// inference when using a parallel iterator + map |
| 418 |
// + map_err + collect, so let's just inline some of the |
| 419 |
// logic. |
| 420 |
|
| 421 |
// Check we are unsure if the file has changed because |
| 422 |
// our filesystem is not precise enough and the rest of |
| 423 |
// the metadata was ambiguous, so check the data for real. |
| 424 |
match is_file_modified( |
| 425 |
&working_directory_vfs, |
| 426 |
&store_vfs, |
| 427 |
check_exec, |
| 428 |
&manifest, |
| 429 |
&to_check.path, |
| 430 |
filelog_open_options, |
| 431 |
) { |
| 432 |
Err(HgError::IoError { .. }) => { |
| 433 |
// IO errors most likely stem from the file being |
| 434 |
// deleted even though we know it's in the |
| 435 |
// dirstate. |
| 436 |
Ok((to_check, FileCompOutcome::Deleted)) |
| 437 |
} |
| 438 |
Ok(outcome) => Ok((to_check, outcome)), |
| 439 |
Err(e) => Err(e), |
| 440 |
} |
| 441 |
}) |
| 442 |
.collect::<Result<_, _>>()?; |
| 443 |
for (status_path, outcome) in res.into_iter() { |
| 444 |
match outcome { |
| 445 |
FileCompOutcome::Clean => { |
| 446 |
if display_states.clean { |
| 447 |
ds_status.clean.push(status_path.clone()); |
| 448 |
} |
| 449 |
fixup.push(status_path.path.into_owned()) |
| 450 |
} |
| 451 |
FileCompOutcome::Modified => { |
| 452 |
if display_states.modified { |
| 453 |
ds_status.modified.push(status_path); |
| 454 |
} |
| 455 |
} |
| 456 |
FileCompOutcome::Deleted => { |
| 457 |
if display_states.deleted { |
| 458 |
ds_status.deleted.push(status_path); |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
let dirstate_write_needed = ds_status.dirty; |
| 466 |
let filesystem_time_at_status_start = |
| 467 |
ds_status.filesystem_time_at_status_start; |
| 468 |
|
| 469 |
output.output(display_states, ds_status)?; |
| 470 |
|
| 471 |
Ok((fixup, dirstate_write_needed, filesystem_time_at_status_start)) |
| 472 |
}; |
| 473 |
|
| 474 |
let warning_context = HgWarningContext::new(); |
| 475 |
let warnings_sender = warning_context.sender(); |
| 476 |
|
| 477 |
let narrow_matcher = narrow::matcher(repo, warnings_sender)?; |
| 478 |
let sparse_matcher = sparse::matcher(repo, None, warnings_sender)?; |
| 479 |
// Sparse is only applicable for the working copy, not history. |
| 480 |
let sparse_is_applicable = revpair.is_none() && change.is_none(); |
| 481 |
let matcher = |
| 482 |
match (repo.has_narrow(), repo.has_sparse() && sparse_is_applicable) { |
| 483 |
(true, true) => Box::new(IntersectionMatcher::new( |
| 484 |
narrow_matcher, |
| 485 |
sparse_matcher, |
| 486 |
)), |
| 487 |
(true, false) => narrow_matcher, |
| 488 |
(false, true) => sparse_matcher, |
| 489 |
(false, false) => Box::new(AlwaysMatcher), |
| 490 |
}; |
| 491 |
let matcher = match args.get_many::<std::ffi::OsString>("file") { |
| 492 |
None => matcher, |
| 493 |
Some(files) => { |
| 494 |
let patterns: Vec<Vec<u8>> = files |
| 495 |
.filter(|s| !s.is_empty()) |
| 496 |
.map(get_bytes_from_os_str) |
| 497 |
.collect(); |
| 498 |
for file in &patterns { |
| 499 |
if file.starts_with(b"set:") { |
| 500 |
return Err(CommandError::unsupported("fileset")); |
| 501 |
} |
| 502 |
} |
| 503 |
let cwd = hg::utils::current_dir()?; |
| 504 |
let root = repo.working_directory_path(); |
| 505 |
let file_patterns = parse_pattern_args(patterns, &cwd, root)?; |
| 506 |
let files_matcher = |
| 507 |
hg::matchers::PatternMatcher::new(file_patterns)?; |
| 508 |
Box::new(IntersectionMatcher::new(Box::new(files_matcher), matcher)) |
| 509 |
} |
| 510 |
}; |
| 511 |
|
| 512 |
// Displayed in two steps to we get the warnings from narrow/sparse before |
| 513 |
// actually starting the status, which could be long if the repo is huge |
| 514 |
// and/or the disk is slow. |
| 515 |
print_warnings(ui, warning_context, repo.working_directory_path()); |
| 516 |
|
| 517 |
if revpair.is_some() || change.is_some() { |
| 518 |
let mut ds_status = DirstateStatus::default(); |
| 519 |
let list_copies = if list_copies { |
| 520 |
if config.get_bool(b"devel", b"copy-tracing.trace-all-files")? { |
| 521 |
Some(hg::operations::ListCopies::AddedOrModified) |
| 522 |
} else { |
| 523 |
Some(hg::operations::ListCopies::Added) |
| 524 |
} |
| 525 |
} else { |
| 526 |
None |
| 527 |
}; |
| 528 |
let stat = if let Some((rev1, rev2)) = revpair { |
| 529 |
if list_copies.is_some() { |
| 530 |
return Err(CommandError::unsupported( |
| 531 |
"status --rev --rev with copy information is not implemented yet", |
| 532 |
)); |
| 533 |
} |
| 534 |
hg::operations::status_rev_rev_no_copies(repo, rev1, rev2, matcher)? |
| 535 |
} else if let Some(rev) = change { |
| 536 |
hg::operations::status_change(repo, rev, matcher, list_copies)? |
| 537 |
} else { |
| 538 |
unreachable!(); |
| 539 |
}; |
| 540 |
for entry in stat.iter() { |
| 541 |
let (path, status) = entry?; |
| 542 |
match status { |
| 543 |
hg::operations::DiffStatus::Removed => { |
| 544 |
if display_states.removed { |
| 545 |
ds_status.removed.push(path) |
| 546 |
} |
| 547 |
} |
| 548 |
hg::operations::DiffStatus::Added => { |
| 549 |
if display_states.added { |
| 550 |
ds_status.added.push(path) |
| 551 |
} |
| 552 |
} |
| 553 |
hg::operations::DiffStatus::Modified => { |
| 554 |
if display_states.modified { |
| 555 |
ds_status.modified.push(path) |
| 556 |
} |
| 557 |
} |
| 558 |
hg::operations::DiffStatus::Matching => { |
| 559 |
if display_states.clean { |
| 560 |
ds_status.clean.push(path) |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
output.output(display_states, ds_status)?; |
| 566 |
return Ok(()); |
| 567 |
} |
| 568 |
|
| 569 |
let (fixup, mut dirstate_write_needed, filesystem_time_at_status_start) = |
| 570 |
dmap.with_status( |
| 571 |
&matcher, |
| 572 |
repo.working_directory_path().to_owned(), |
| 573 |
get_ignore_files(repo), |
| 574 |
options, |
| 575 |
after_status, |
| 576 |
)?; |
| 577 |
|
| 578 |
// Development config option to test write races |
| 579 |
if let Err(e) = |
| 580 |
debug_wait_for_file(config, "status.pre-dirstate-write-file") |
| 581 |
{ |
| 582 |
ui.write_stderr(e.as_bytes()).ok(); |
| 583 |
} |
| 584 |
|
| 585 |
if (fixup.is_empty() || filesystem_time_at_status_start.is_none()) |
| 586 |
&& !dirstate_write_needed |
| 587 |
{ |
| 588 |
// Nothing to update |
| 589 |
return Ok(()); |
| 590 |
} |
| 591 |
|
| 592 |
// Update the dirstate on disk if we can |
| 593 |
let with_lock_result = |
| 594 |
repo.try_with_wlock_no_wait(|| -> Result<(), CommandError> { |
| 595 |
if let Some(mtime_boundary) = filesystem_time_at_status_start { |
| 596 |
for hg_path in fixup { |
| 597 |
use std::os::unix::fs::MetadataExt; |
| 598 |
let fs_path = hg_path_to_path_buf(&hg_path) |
| 599 |
.expect("HgPath conversion"); |
| 600 |
// Specifically do not reuse `fs_metadata` from |
| 601 |
// `unsure_is_clean` which was needed before reading |
| 602 |
// contents. Here we access metadata again after reading |
| 603 |
// content, in case it changed in the meantime. |
| 604 |
let metadata_res = |
| 605 |
repo.working_directory_vfs().symlink_metadata(&fs_path); |
| 606 |
let fs_metadata = match metadata_res { |
| 607 |
Ok(meta) => meta, |
| 608 |
Err(err) => match err { |
| 609 |
HgError::IoError { .. } => { |
| 610 |
// The file has probably been deleted. In any |
| 611 |
// case, it was in the dirstate before, so |
| 612 |
// let's ignore the error. |
| 613 |
continue; |
| 614 |
} |
| 615 |
_ => return Err(err.into()), |
| 616 |
}, |
| 617 |
}; |
| 618 |
if let Some(mtime) = |
| 619 |
TruncatedTimestamp::for_reliable_mtime_of( |
| 620 |
&fs_metadata, |
| 621 |
&mtime_boundary, |
| 622 |
) |
| 623 |
.when_reading_file(&fs_path)? |
| 624 |
{ |
| 625 |
let mode = fs_metadata.mode(); |
| 626 |
let size = fs_metadata.len(); |
| 627 |
dmap.set_clean(&hg_path, mode, size as u32, mtime)?; |
| 628 |
dirstate_write_needed = true |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
drop(dmap); // Avoid "already mutably borrowed" RefCell panics |
| 633 |
if dirstate_write_needed { |
| 634 |
repo.write_dirstate()? |
| 635 |
} |
| 636 |
Ok(()) |
| 637 |
}); |
| 638 |
match with_lock_result { |
| 639 |
Ok(closure_result) => closure_result?, |
| 640 |
Err(LockError::AlreadyHeld) => { |
| 641 |
// Not updating the dirstate is not ideal but not critical: |
| 642 |
// don’t keep our caller waiting until some other Mercurial |
| 643 |
// process releases the lock. |
| 644 |
tracing::info!("not writing dirstate from `status`: lock is held") |
| 645 |
} |
| 646 |
Err(LockError::Other(HgError::IoError { error, .. })) |
| 647 |
if error.kind() == io::ErrorKind::PermissionDenied |
| 648 |
|| match error.raw_os_error() { |
| 649 |
None => false, |
| 650 |
Some(errno) => libc::EROFS == errno, |
| 651 |
} => |
| 652 |
{ |
| 653 |
// `hg status` on a read-only repository is fine |
| 654 |
} |
| 655 |
Err(LockError::Other(error)) => { |
| 656 |
// Report other I/O errors |
| 657 |
Err(error)? |
| 658 |
} |
| 659 |
} |
| 660 |
Ok(()) |
| 661 |
} |
| 662 |
|
| 663 |
struct DisplayStatusPaths<'a> { |
| 664 |
ui: &'a Ui, |
| 665 |
no_status: bool, |
| 666 |
relativize: Option<RelativizePaths>, |
| 667 |
print0: bool, |
| 668 |
} |
| 669 |
|
| 670 |
impl DisplayStatusPaths<'_> { |
| 671 |
// Probably more elegant to use a Deref or Borrow trait rather than |
| 672 |
// harcode HgPathBuf, but probably not really useful at this point |
| 673 |
fn display( |
| 674 |
&self, |
| 675 |
status_prefix: &[u8], |
| 676 |
label: &'static str, |
| 677 |
mut paths: Vec<StatusPath<'_>>, |
| 678 |
) -> Result<(), CommandError> { |
| 679 |
paths.sort_unstable(); |
| 680 |
let mut stdout = self.ui.stdout_buffer(); |
| 681 |
for StatusPath { path, copy_source } in paths { |
| 682 |
let relative_path; |
| 683 |
let relative_source; |
| 684 |
let (path, copy_source) = if let Some(relativize) = &self.relativize |
| 685 |
{ |
| 686 |
relative_path = relativize.relativize(&path); |
| 687 |
relative_source = |
| 688 |
copy_source.as_ref().map(|s| relativize.relativize(s)); |
| 689 |
(&*relative_path, relative_source.as_deref()) |
| 690 |
} else { |
| 691 |
(path.as_bytes(), copy_source.as_ref().map(|s| s.as_bytes())) |
| 692 |
}; |
| 693 |
// TODO: Add a way to use `write_bytes!` instead of |
| 694 |
// `format_bytes!` in order to stream to stdout |
| 695 |
// instead of allocating an itermediate |
| 696 |
// `Vec<u8>`. |
| 697 |
if !self.no_status { |
| 698 |
stdout.write_stdout_labelled(status_prefix, label)? |
| 699 |
} |
| 700 |
let linebreak = if self.print0 { b"\x00" } else { b"\n" }; |
| 701 |
stdout.write_stdout_labelled( |
| 702 |
&format_bytes!(b"{}{}", path, linebreak), |
| 703 |
label, |
| 704 |
)?; |
| 705 |
if let Some(source) = copy_source { |
| 706 |
let label = "status.copied"; |
| 707 |
stdout.write_stdout_labelled( |
| 708 |
&format_bytes!(b" {}{}", source, linebreak), |
| 709 |
label, |
| 710 |
)? |
| 711 |
} |
| 712 |
} |
| 713 |
stdout.flush()?; |
| 714 |
Ok(()) |
| 715 |
} |
| 716 |
|
| 717 |
fn output( |
| 718 |
&mut self, |
| 719 |
display_states: DisplayStates, |
| 720 |
ds_status: DirstateStatus, |
| 721 |
) -> Result<(), CommandError> { |
| 722 |
if display_states.modified { |
| 723 |
self.display(b"M ", "status.modified", ds_status.modified)?; |
| 724 |
} |
| 725 |
if display_states.added { |
| 726 |
self.display(b"A ", "status.added", ds_status.added)?; |
| 727 |
} |
| 728 |
if display_states.removed { |
| 729 |
self.display(b"R ", "status.removed", ds_status.removed)?; |
| 730 |
} |
| 731 |
if display_states.deleted { |
| 732 |
self.display(b"! ", "status.deleted", ds_status.deleted)?; |
| 733 |
} |
| 734 |
if display_states.unknown { |
| 735 |
self.display(b"? ", "status.unknown", ds_status.unknown)?; |
| 736 |
} |
| 737 |
if display_states.ignored { |
| 738 |
self.display(b"I ", "status.ignored", ds_status.ignored)?; |
| 739 |
} |
| 740 |
if display_states.clean { |
| 741 |
self.display(b"C ", "status.clean", ds_status.clean)?; |
| 742 |
} |
| 743 |
Ok(()) |
| 744 |
} |
| 745 |
} |