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