##// END OF EJS Templates
rhg: Skip reading the contents of ambiguous files in some cases...
Simon Sapin -
r49302:b005d07d default
parent child Browse files
Show More
@@ -1,494 +1,506
1 1 // status.rs
2 2 //
3 3 // Copyright 2020, Georges Racinet <georges.racinets@octobus.net>
4 4 //
5 5 // This software may be used and distributed according to the terms of the
6 6 // GNU General Public License version 2 or any later version.
7 7
8 8 use crate::error::CommandError;
9 9 use crate::ui::Ui;
10 10 use crate::utils::path_utils::RelativizePaths;
11 11 use clap::{Arg, SubCommand};
12 12 use format_bytes::format_bytes;
13 13 use hg;
14 14 use hg::config::Config;
15 15 use hg::dirstate::has_exec_bit;
16 16 use hg::dirstate::status::StatusPath;
17 17 use hg::dirstate::TruncatedTimestamp;
18 18 use hg::dirstate::RANGE_MASK_31BIT;
19 19 use hg::errors::{HgError, IoResultExt};
20 20 use hg::lock::LockError;
21 21 use hg::manifest::Manifest;
22 22 use hg::matchers::AlwaysMatcher;
23 23 use hg::repo::Repo;
24 24 use hg::utils::files::get_bytes_from_os_string;
25 25 use hg::utils::files::get_path_from_bytes;
26 26 use hg::utils::hg_path::{hg_path_to_path_buf, HgPath};
27 27 use hg::StatusOptions;
28 28 use log::{info, warn};
29 29 use std::io;
30 30 use std::path::PathBuf;
31 31
32 32 pub const HELP_TEXT: &str = "
33 33 Show changed files in the working directory
34 34
35 35 This is a pure Rust version of `hg status`.
36 36
37 37 Some options might be missing, check the list below.
38 38 ";
39 39
40 40 pub fn args() -> clap::App<'static, 'static> {
41 41 SubCommand::with_name("status")
42 42 .alias("st")
43 43 .about(HELP_TEXT)
44 44 .arg(
45 45 Arg::with_name("all")
46 46 .help("show status of all files")
47 47 .short("-A")
48 48 .long("--all"),
49 49 )
50 50 .arg(
51 51 Arg::with_name("modified")
52 52 .help("show only modified files")
53 53 .short("-m")
54 54 .long("--modified"),
55 55 )
56 56 .arg(
57 57 Arg::with_name("added")
58 58 .help("show only added files")
59 59 .short("-a")
60 60 .long("--added"),
61 61 )
62 62 .arg(
63 63 Arg::with_name("removed")
64 64 .help("show only removed files")
65 65 .short("-r")
66 66 .long("--removed"),
67 67 )
68 68 .arg(
69 69 Arg::with_name("clean")
70 70 .help("show only clean files")
71 71 .short("-c")
72 72 .long("--clean"),
73 73 )
74 74 .arg(
75 75 Arg::with_name("deleted")
76 76 .help("show only deleted files")
77 77 .short("-d")
78 78 .long("--deleted"),
79 79 )
80 80 .arg(
81 81 Arg::with_name("unknown")
82 82 .help("show only unknown (not tracked) files")
83 83 .short("-u")
84 84 .long("--unknown"),
85 85 )
86 86 .arg(
87 87 Arg::with_name("ignored")
88 88 .help("show only ignored files")
89 89 .short("-i")
90 90 .long("--ignored"),
91 91 )
92 92 .arg(
93 93 Arg::with_name("copies")
94 94 .help("show source of copied files (DEFAULT: ui.statuscopies)")
95 95 .short("-C")
96 96 .long("--copies"),
97 97 )
98 98 .arg(
99 99 Arg::with_name("no-status")
100 100 .help("hide status prefix")
101 101 .short("-n")
102 102 .long("--no-status"),
103 103 )
104 104 }
105 105
106 106 /// Pure data type allowing the caller to specify file states to display
107 107 #[derive(Copy, Clone, Debug)]
108 108 pub struct DisplayStates {
109 109 pub modified: bool,
110 110 pub added: bool,
111 111 pub removed: bool,
112 112 pub clean: bool,
113 113 pub deleted: bool,
114 114 pub unknown: bool,
115 115 pub ignored: bool,
116 116 }
117 117
118 118 pub const DEFAULT_DISPLAY_STATES: DisplayStates = DisplayStates {
119 119 modified: true,
120 120 added: true,
121 121 removed: true,
122 122 clean: false,
123 123 deleted: true,
124 124 unknown: true,
125 125 ignored: false,
126 126 };
127 127
128 128 pub const ALL_DISPLAY_STATES: DisplayStates = DisplayStates {
129 129 modified: true,
130 130 added: true,
131 131 removed: true,
132 132 clean: true,
133 133 deleted: true,
134 134 unknown: true,
135 135 ignored: true,
136 136 };
137 137
138 138 impl DisplayStates {
139 139 pub fn is_empty(&self) -> bool {
140 140 !(self.modified
141 141 || self.added
142 142 || self.removed
143 143 || self.clean
144 144 || self.deleted
145 145 || self.unknown
146 146 || self.ignored)
147 147 }
148 148 }
149 149
150 150 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
151 151 let status_enabled_default = false;
152 152 let status_enabled = invocation.config.get_option(b"rhg", b"status")?;
153 153 if !status_enabled.unwrap_or(status_enabled_default) {
154 154 return Err(CommandError::unsupported(
155 155 "status is experimental in rhg (enable it with 'rhg.status = true' \
156 156 or enable fallback with 'rhg.on-unsupported = fallback')"
157 157 ));
158 158 }
159 159
160 160 // TODO: lift these limitations
161 161 if invocation.config.get_bool(b"ui", b"tweakdefaults")? {
162 162 return Err(CommandError::unsupported(
163 163 "ui.tweakdefaults is not yet supported with rhg status",
164 164 ));
165 165 }
166 166 if invocation.config.get_bool(b"ui", b"statuscopies")? {
167 167 return Err(CommandError::unsupported(
168 168 "ui.statuscopies is not yet supported with rhg status",
169 169 ));
170 170 }
171 171 if invocation
172 172 .config
173 173 .get(b"commands", b"status.terse")
174 174 .is_some()
175 175 {
176 176 return Err(CommandError::unsupported(
177 177 "status.terse is not yet supported with rhg status",
178 178 ));
179 179 }
180 180
181 181 let ui = invocation.ui;
182 182 let config = invocation.config;
183 183 let args = invocation.subcommand_args;
184 184 let all = args.is_present("all");
185 185 let display_states = if all {
186 186 // TODO when implementing `--quiet`: it excludes clean files
187 187 // from `--all`
188 188 ALL_DISPLAY_STATES
189 189 } else {
190 190 let requested = DisplayStates {
191 191 modified: args.is_present("modified"),
192 192 added: args.is_present("added"),
193 193 removed: args.is_present("removed"),
194 194 clean: args.is_present("clean"),
195 195 deleted: args.is_present("deleted"),
196 196 unknown: args.is_present("unknown"),
197 197 ignored: args.is_present("ignored"),
198 198 };
199 199 if requested.is_empty() {
200 200 DEFAULT_DISPLAY_STATES
201 201 } else {
202 202 requested
203 203 }
204 204 };
205 205 let no_status = args.is_present("no-status");
206 206 let list_copies = all
207 207 || args.is_present("copies")
208 208 || config.get_bool(b"ui", b"statuscopies")?;
209 209
210 210 let repo = invocation.repo?;
211 211
212 212 if repo.has_sparse() || repo.has_narrow() {
213 213 return Err(CommandError::unsupported(
214 214 "rhg status is not supported for sparse checkouts or narrow clones yet"
215 215 ));
216 216 }
217 217
218 218 let mut dmap = repo.dirstate_map_mut()?;
219 219
220 220 let options = StatusOptions {
221 221 // we're currently supporting file systems with exec flags only
222 222 // anyway
223 223 check_exec: true,
224 224 list_clean: display_states.clean,
225 225 list_unknown: display_states.unknown,
226 226 list_ignored: display_states.ignored,
227 227 list_copies,
228 228 collect_traversed_dirs: false,
229 229 };
230 230 let (mut ds_status, pattern_warnings) = dmap.status(
231 231 &AlwaysMatcher,
232 232 repo.working_directory_path().to_owned(),
233 233 ignore_files(repo, config),
234 234 options,
235 235 )?;
236 236 if !pattern_warnings.is_empty() {
237 237 warn!("Pattern warnings: {:?}", &pattern_warnings);
238 238 }
239 239
240 240 for (path, error) in ds_status.bad {
241 241 let error = match error {
242 242 hg::BadMatch::OsError(code) => {
243 243 std::io::Error::from_raw_os_error(code).to_string()
244 244 }
245 245 hg::BadMatch::BadType(ty) => {
246 246 format!("unsupported file type (type is {})", ty)
247 247 }
248 248 };
249 249 ui.write_stderr(&format_bytes!(
250 250 b"{}: {}\n",
251 251 path.as_bytes(),
252 252 error.as_bytes()
253 253 ))?
254 254 }
255 255 if !ds_status.unsure.is_empty() {
256 256 info!(
257 257 "Files to be rechecked by retrieval from filelog: {:?}",
258 258 ds_status.unsure.iter().map(|s| &s.path).collect::<Vec<_>>()
259 259 );
260 260 }
261 261 let mut fixup = Vec::new();
262 262 if !ds_status.unsure.is_empty()
263 263 && (display_states.modified || display_states.clean)
264 264 {
265 265 let p1 = repo.dirstate_parents()?.p1;
266 266 let manifest = repo.manifest_for_node(p1).map_err(|e| {
267 267 CommandError::from((e, &*format!("{:x}", p1.short())))
268 268 })?;
269 269 for to_check in ds_status.unsure {
270 270 if unsure_is_modified(repo, &manifest, &to_check.path)? {
271 271 if display_states.modified {
272 272 ds_status.modified.push(to_check);
273 273 }
274 274 } else {
275 275 if display_states.clean {
276 276 ds_status.clean.push(to_check.clone());
277 277 }
278 278 fixup.push(to_check.path.into_owned())
279 279 }
280 280 }
281 281 }
282 282 let relative_paths = (!ui.plain())
283 283 && config
284 284 .get_option(b"commands", b"status.relative")?
285 285 .unwrap_or(config.get_bool(b"ui", b"relative-paths")?);
286 286 let output = DisplayStatusPaths {
287 287 ui,
288 288 no_status,
289 289 relativize: if relative_paths {
290 290 Some(RelativizePaths::new(repo)?)
291 291 } else {
292 292 None
293 293 },
294 294 };
295 295 if display_states.modified {
296 296 output.display(b"M", ds_status.modified)?;
297 297 }
298 298 if display_states.added {
299 299 output.display(b"A", ds_status.added)?;
300 300 }
301 301 if display_states.removed {
302 302 output.display(b"R", ds_status.removed)?;
303 303 }
304 304 if display_states.deleted {
305 305 output.display(b"!", ds_status.deleted)?;
306 306 }
307 307 if display_states.unknown {
308 308 output.display(b"?", ds_status.unknown)?;
309 309 }
310 310 if display_states.ignored {
311 311 output.display(b"I", ds_status.ignored)?;
312 312 }
313 313 if display_states.clean {
314 314 output.display(b"C", ds_status.clean)?;
315 315 }
316 316
317 317 let mut dirstate_write_needed = ds_status.dirty;
318 318 let filesystem_time_at_status_start = ds_status
319 319 .filesystem_time_at_status_start
320 320 .map(TruncatedTimestamp::from);
321 321
322 322 if (fixup.is_empty() || filesystem_time_at_status_start.is_none())
323 323 && !dirstate_write_needed
324 324 {
325 325 // Nothing to update
326 326 return Ok(());
327 327 }
328 328
329 329 // Update the dirstate on disk if we can
330 330 let with_lock_result =
331 331 repo.try_with_wlock_no_wait(|| -> Result<(), CommandError> {
332 332 if let Some(mtime_boundary) = filesystem_time_at_status_start {
333 333 for hg_path in fixup {
334 334 use std::os::unix::fs::MetadataExt;
335 335 let fs_path = hg_path_to_path_buf(&hg_path)
336 336 .expect("HgPath conversion");
337 337 // Specifically do not reuse `fs_metadata` from
338 338 // `unsure_is_clean` which was needed before reading
339 339 // contents. Here we access metadata again after reading
340 340 // content, in case it changed in the meantime.
341 341 let fs_metadata = repo
342 342 .working_directory_vfs()
343 343 .symlink_metadata(&fs_path)?;
344 344 if let Some(mtime) =
345 345 TruncatedTimestamp::for_reliable_mtime_of(
346 346 &fs_metadata,
347 347 &mtime_boundary,
348 348 )
349 349 .when_reading_file(&fs_path)?
350 350 {
351 351 let mode = fs_metadata.mode();
352 352 let size = fs_metadata.len() as u32 & RANGE_MASK_31BIT;
353 353 let mut entry = dmap
354 354 .get(&hg_path)?
355 355 .expect("ambiguous file not in dirstate");
356 356 entry.set_clean(mode, size, mtime);
357 357 dmap.add_file(&hg_path, entry)?;
358 358 dirstate_write_needed = true
359 359 }
360 360 }
361 361 }
362 362 drop(dmap); // Avoid "already mutably borrowed" RefCell panics
363 363 if dirstate_write_needed {
364 364 repo.write_dirstate()?
365 365 }
366 366 Ok(())
367 367 });
368 368 match with_lock_result {
369 369 Ok(closure_result) => closure_result?,
370 370 Err(LockError::AlreadyHeld) => {
371 371 // Not updating the dirstate is not ideal but not critical:
372 372 // don’t keep our caller waiting until some other Mercurial
373 373 // process releases the lock.
374 374 }
375 375 Err(LockError::Other(HgError::IoError { error, .. }))
376 376 if error.kind() == io::ErrorKind::PermissionDenied =>
377 377 {
378 378 // `hg status` on a read-only repository is fine
379 379 }
380 380 Err(LockError::Other(error)) => {
381 381 // Report other I/O errors
382 382 Err(error)?
383 383 }
384 384 }
385 385 Ok(())
386 386 }
387 387
388 388 fn ignore_files(repo: &Repo, config: &Config) -> Vec<PathBuf> {
389 389 let mut ignore_files = Vec::new();
390 390 let repo_ignore = repo.working_directory_vfs().join(".hgignore");
391 391 if repo_ignore.exists() {
392 392 ignore_files.push(repo_ignore)
393 393 }
394 394 for (key, value) in config.iter_section(b"ui") {
395 395 if key == b"ignore" || key.starts_with(b"ignore.") {
396 396 let path = get_path_from_bytes(value);
397 397 // TODO: expand "~/" and environment variable here, like Python
398 398 // does with `os.path.expanduser` and `os.path.expandvars`
399 399
400 400 let joined = repo.working_directory_path().join(path);
401 401 ignore_files.push(joined);
402 402 }
403 403 }
404 404 ignore_files
405 405 }
406 406
407 407 struct DisplayStatusPaths<'a> {
408 408 ui: &'a Ui,
409 409 no_status: bool,
410 410 relativize: Option<RelativizePaths>,
411 411 }
412 412
413 413 impl DisplayStatusPaths<'_> {
414 414 // Probably more elegant to use a Deref or Borrow trait rather than
415 415 // harcode HgPathBuf, but probably not really useful at this point
416 416 fn display(
417 417 &self,
418 418 status_prefix: &[u8],
419 419 mut paths: Vec<StatusPath<'_>>,
420 420 ) -> Result<(), CommandError> {
421 421 paths.sort_unstable();
422 422 for StatusPath { path, copy_source } in paths {
423 423 let relative;
424 424 let path = if let Some(relativize) = &self.relativize {
425 425 relative = relativize.relativize(&path);
426 426 &*relative
427 427 } else {
428 428 path.as_bytes()
429 429 };
430 430 // TODO optim, probably lots of unneeded copies here, especially
431 431 // if out stream is buffered
432 432 if self.no_status {
433 433 self.ui.write_stdout(&format_bytes!(b"{}\n", path))?
434 434 } else {
435 435 self.ui.write_stdout(&format_bytes!(
436 436 b"{} {}\n",
437 437 status_prefix,
438 438 path
439 439 ))?
440 440 }
441 441 if let Some(source) = copy_source {
442 442 self.ui.write_stdout(&format_bytes!(
443 443 b" {}\n",
444 444 source.as_bytes()
445 445 ))?
446 446 }
447 447 }
448 448 Ok(())
449 449 }
450 450 }
451 451
452 452 /// Check if a file is modified by comparing actual repo store and file system.
453 453 ///
454 454 /// This meant to be used for those that the dirstate cannot resolve, due
455 455 /// to time resolution limits.
456 456 fn unsure_is_modified(
457 457 repo: &Repo,
458 458 manifest: &Manifest,
459 459 hg_path: &HgPath,
460 460 ) -> Result<bool, HgError> {
461 461 let vfs = repo.working_directory_vfs();
462 462 let fs_path = hg_path_to_path_buf(hg_path).expect("HgPath conversion");
463 463 let fs_metadata = vfs.symlink_metadata(&fs_path)?;
464 464 let is_symlink = fs_metadata.file_type().is_symlink();
465 465 // TODO: Also account for `FALLBACK_SYMLINK` and `FALLBACK_EXEC` from the
466 466 // dirstate
467 467 let fs_flags = if is_symlink {
468 468 Some(b'l')
469 469 } else if has_exec_bit(&fs_metadata) {
470 470 Some(b'x')
471 471 } else {
472 472 None
473 473 };
474 474
475 475 let entry = manifest
476 476 .find_file(hg_path)?
477 477 .expect("ambgious file not in p1");
478 478 if entry.flags != fs_flags {
479 479 return Ok(true);
480 480 }
481 481 let filelog = repo.filelog(hg_path)?;
482 let fs_len = fs_metadata.len();
483 // TODO: check `fs_len` here like below, but based on
484 // `RevlogEntry::uncompressed_len` without decompressing the full filelog
485 // contents where possible. This is only valid if the revlog data does not
486 // contain metadata. See how Python’s `revlog.rawsize` calls
487 // `storageutil.filerevisioncopied`.
488 // (Maybe also check for content-modifying flags? See `revlog.size`.)
482 489 let filelog_entry =
483 490 filelog.data_for_node(entry.node_id()?).map_err(|_| {
484 491 HgError::corrupted("filelog missing node from manifest")
485 492 })?;
486 493 let contents_in_p1 = filelog_entry.data()?;
494 if contents_in_p1.len() as u64 != fs_len {
495 // No need to read the file contents:
496 // it cannot be equal if it has a different length.
497 return Ok(true);
498 }
487 499
488 500 let fs_contents = if is_symlink {
489 501 get_bytes_from_os_string(vfs.read_link(fs_path)?.into_os_string())
490 502 } else {
491 503 vfs.read(fs_path)?
492 504 };
493 505 Ok(contents_in_p1 != &*fs_contents)
494 506 }
General Comments 0
You need to be logged in to leave comments. Login now