##// END OF EJS Templates
rhg: refactor relativize_path into a struct + method...
Simon Sapin -
r49284:9b0e1f64 default
parent child Browse files
Show More
@@ -1,14 +1,12 b''
1 1 use crate::error::CommandError;
2 2 use crate::ui::Ui;
3 use crate::ui::UiError;
4 use crate::utils::path_utils::relativize_paths;
3 use crate::utils::path_utils::RelativizePaths;
5 4 use clap::Arg;
6 5 use hg::errors::HgError;
7 6 use hg::operations::list_rev_tracked_files;
8 7 use hg::operations::Dirstate;
9 8 use hg::repo::Repo;
10 9 use hg::utils::hg_path::HgPath;
11 use std::borrow::Cow;
12 10
13 11 pub const HELP_TEXT: &str = "
14 12 List tracked files.
@@ -86,11 +84,14 b" fn display_files<'a>("
86 84 let mut stdout = ui.stdout_buffer();
87 85 let mut any = false;
88 86
89 relativize_paths(repo, files, |path: Cow<[u8]>| -> Result<(), UiError> {
87 let relativize = RelativizePaths::new(repo)?;
88 for result in files {
89 let path = result?;
90 stdout.write_all(&relativize.relativize(path))?;
91 stdout.write_all(b"\n")?;
90 92 any = true;
91 stdout.write_all(path.as_ref())?;
92 stdout.write_all(b"\n")
93 })?;
93 }
94
94 95 stdout.flush()?;
95 96 if any {
96 97 Ok(())
@@ -7,7 +7,7 b''
7 7
8 8 use crate::error::CommandError;
9 9 use crate::ui::Ui;
10 use crate::utils::path_utils::relativize_paths;
10 use crate::utils::path_utils::RelativizePaths;
11 11 use clap::{Arg, SubCommand};
12 12 use format_bytes::format_bytes;
13 13 use hg;
@@ -261,9 +261,12 b' pub fn run(invocation: &crate::CliInvoca'
261 261 .unwrap_or(config.get_bool(b"ui", b"relative-paths")?);
262 262 let output = DisplayStatusPaths {
263 263 ui,
264 repo,
265 264 no_status,
266 relative_paths,
265 relativize: if relative_paths {
266 Some(RelativizePaths::new(repo)?)
267 } else {
268 None
269 },
267 270 };
268 271 if display_states.modified {
269 272 output.display(b"M", ds_status.modified)?;
@@ -379,9 +382,8 b' fn ignore_files(repo: &Repo, config: &Co'
379 382
380 383 struct DisplayStatusPaths<'a> {
381 384 ui: &'a Ui,
382 repo: &'a Repo,
383 385 no_status: bool,
384 relative_paths: bool,
386 relativize: Option<RelativizePaths>,
385 387 }
386 388
387 389 impl DisplayStatusPaths<'_> {
@@ -393,27 +395,24 b" impl DisplayStatusPaths<'_> {"
393 395 mut paths: Vec<HgPathCow>,
394 396 ) -> Result<(), CommandError> {
395 397 paths.sort_unstable();
396 let print_path = |path: &[u8]| {
398 for path in paths {
399 let relative;
400 let path = if let Some(relativize) = &self.relativize {
401 relative = relativize.relativize(&path);
402 &*relative
403 } else {
404 path.as_bytes()
405 };
397 406 // TODO optim, probably lots of unneeded copies here, especially
398 407 // if out stream is buffered
399 408 if self.no_status {
400 self.ui.write_stdout(&format_bytes!(b"{}\n", path))
409 self.ui.write_stdout(&format_bytes!(b"{}\n", path))?
401 410 } else {
402 411 self.ui.write_stdout(&format_bytes!(
403 412 b"{} {}\n",
404 413 status_prefix,
405 414 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 })?;
414 } else {
415 for path in paths {
416 print_path(path.as_bytes())?
415 ))?
417 416 }
418 417 }
419 418 Ok(())
@@ -3,8 +3,6 b''
3 3 // This software may be used and distributed according to the terms of the
4 4 // GNU General Public License version 2 or any later version.
5 5
6 use crate::error::CommandError;
7 use crate::ui::UiError;
8 6 use hg::errors::HgError;
9 7 use hg::repo::Repo;
10 8 use hg::utils::current_dir;
@@ -13,37 +11,45 b' use hg::utils::hg_path::HgPath;'
13 11 use hg::utils::hg_path::HgPathBuf;
14 12 use std::borrow::Cow;
15 13
16 pub fn relativize_paths(
17 repo: &Repo,
18 paths: impl IntoIterator<Item = Result<impl AsRef<HgPath>, HgError>>,
19 mut callback: impl FnMut(Cow<[u8]>) -> Result<(), UiError>,
20 ) -> Result<(), CommandError> {
21 let cwd = current_dir()?;
22 let repo_root = repo.working_directory_path();
23 let repo_root = cwd.join(repo_root); // Make it absolute
24 let repo_root_hgpath =
25 HgPathBuf::from(get_bytes_from_path(repo_root.to_owned()));
26 let outside_repo: bool;
27 let cwd_hgpath: HgPathBuf;
14 pub struct RelativizePaths {
15 repo_root: HgPathBuf,
16 cwd: HgPathBuf,
17 outside_repo: bool,
18 }
19
20 impl RelativizePaths {
21 pub fn new(repo: &Repo) -> Result<Self, HgError> {
22 let cwd = current_dir()?;
23 let repo_root = repo.working_directory_path();
24 let repo_root = cwd.join(repo_root); // Make it absolute
25 let repo_root_hgpath =
26 HgPathBuf::from(get_bytes_from_path(repo_root.to_owned()));
28 27
29 if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&repo_root) {
30 // The current directory is inside the repo, so we can work with
31 // relative paths
32 outside_repo = false;
33 cwd_hgpath =
34 HgPathBuf::from(get_bytes_from_path(cwd_relative_to_repo));
35 } else {
36 outside_repo = true;
37 cwd_hgpath = HgPathBuf::from(get_bytes_from_path(cwd));
28 if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&repo_root) {
29 // The current directory is inside the repo, so we can work with
30 // relative paths
31 Ok(Self {
32 repo_root: repo_root_hgpath,
33 cwd: HgPathBuf::from(get_bytes_from_path(
34 cwd_relative_to_repo,
35 )),
36 outside_repo: false,
37 })
38 } else {
39 Ok(Self {
40 repo_root: repo_root_hgpath,
41 cwd: HgPathBuf::from(get_bytes_from_path(cwd)),
42 outside_repo: true,
43 })
44 }
38 45 }
39 46
40 for file in paths {
41 if outside_repo {
42 let file = repo_root_hgpath.join(file?.as_ref());
43 callback(relativize_path(&file, &cwd_hgpath))?;
47 pub fn relativize<'a>(&self, path: &'a HgPath) -> Cow<'a, [u8]> {
48 if self.outside_repo {
49 let joined = self.repo_root.join(path);
50 Cow::Owned(relativize_path(&joined, &self.cwd).into_owned())
44 51 } else {
45 callback(relativize_path(file?.as_ref(), &cwd_hgpath))?;
52 relativize_path(path, &self.cwd)
46 53 }
47 54 }
48 Ok(())
49 55 }
General Comments 0
You need to be logged in to leave comments. Login now