##// END OF EJS Templates
rhg: refactor function to relativize paths in utils...
Pulkit Goyal -
r48988:9ecf802b default
parent child Browse files
Show More
@@ -0,0 +1,48 b''
1 // path utils module
2 //
3 // This software may be used and distributed according to the terms of the
4 // GNU General Public License version 2 or any later version.
5
6 use crate::error::CommandError;
7 use crate::ui::UiError;
8 use hg::repo::Repo;
9 use hg::utils::current_dir;
10 use hg::utils::files::{get_bytes_from_path, relativize_path};
11 use hg::utils::hg_path::HgPath;
12 use hg::utils::hg_path::HgPathBuf;
13 use std::borrow::Cow;
14
15 pub fn relativize_paths(
16 repo: &Repo,
17 paths: impl IntoIterator<Item = impl AsRef<HgPath>>,
18 mut callback: impl FnMut(Cow<[u8]>) -> Result<(), UiError>,
19 ) -> Result<(), CommandError> {
20 let cwd = current_dir()?;
21 let repo_root = repo.working_directory_path();
22 let repo_root = cwd.join(repo_root); // Make it absolute
23 let repo_root_hgpath =
24 HgPathBuf::from(get_bytes_from_path(repo_root.to_owned()));
25 let outside_repo: bool;
26 let cwd_hgpath: HgPathBuf;
27
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 outside_repo = false;
32 cwd_hgpath =
33 HgPathBuf::from(get_bytes_from_path(cwd_relative_to_repo));
34 } else {
35 outside_repo = true;
36 cwd_hgpath = HgPathBuf::from(get_bytes_from_path(cwd));
37 }
38
39 for file in paths {
40 if outside_repo {
41 let file = repo_root_hgpath.join(file.as_ref());
42 callback(relativize_path(&file, &cwd_hgpath))?;
43 } else {
44 callback(relativize_path(file.as_ref(), &cwd_hgpath))?;
45 }
46 }
47 Ok(())
48 }
@@ -1,12 +1,13 b''
1 use crate::error::CommandError;
1 use crate::error::CommandError;
2 use crate::ui::Ui;
2 use crate::ui::Ui;
3 use crate::ui::UiError;
4 use crate::utils::path_utils::relativize_paths;
3 use clap::Arg;
5 use clap::Arg;
4 use hg::operations::list_rev_tracked_files;
6 use hg::operations::list_rev_tracked_files;
5 use hg::operations::Dirstate;
7 use hg::operations::Dirstate;
6 use hg::repo::Repo;
8 use hg::repo::Repo;
7 use hg::utils::current_dir;
9 use hg::utils::hg_path::HgPath;
8 use hg::utils::files::{get_bytes_from_path, relativize_path};
10 use std::borrow::Cow;
9 use hg::utils::hg_path::{HgPath, HgPathBuf};
10
11
11 pub const HELP_TEXT: &str = "
12 pub const HELP_TEXT: &str = "
12 List tracked files.
13 List tracked files.
@@ -54,34 +55,13 b" fn display_files<'a>("
54 files: impl IntoIterator<Item = &'a HgPath>,
55 files: impl IntoIterator<Item = &'a HgPath>,
55 ) -> Result<(), CommandError> {
56 ) -> Result<(), CommandError> {
56 let mut stdout = ui.stdout_buffer();
57 let mut stdout = ui.stdout_buffer();
57
58 let mut any = false;
58 let cwd = current_dir()?;
59 let working_directory = repo.working_directory_path();
60 let working_directory = cwd.join(working_directory); // Make it absolute
61
59
62 let mut any = false;
60 relativize_paths(repo, files, |path: Cow<[u8]>| -> Result<(), UiError> {
63 if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&working_directory) {
61 any = true;
64 // The current directory is inside the repo, so we can work with
62 stdout.write_all(path.as_ref())?;
65 // relative paths
63 stdout.write_all(b"\n")
66 let cwd = HgPathBuf::from(get_bytes_from_path(cwd_relative_to_repo));
64 })?;
67 for file in files {
68 any = true;
69 stdout.write_all(relativize_path(&file, &cwd).as_ref())?;
70 stdout.write_all(b"\n")?;
71 }
72 } else {
73 let working_directory =
74 HgPathBuf::from(get_bytes_from_path(working_directory));
75 let cwd = HgPathBuf::from(get_bytes_from_path(cwd));
76 for file in files {
77 any = true;
78 // Absolute path in the filesystem
79 let file = working_directory.join(file);
80 stdout.write_all(relativize_path(&file, &cwd).as_ref())?;
81 stdout.write_all(b"\n")?;
82 }
83 }
84
85 stdout.flush()?;
65 stdout.flush()?;
86 if any {
66 if any {
87 Ok(())
67 Ok(())
@@ -17,6 +17,9 b' use std::process::Command;'
17 mod blackbox;
17 mod blackbox;
18 mod error;
18 mod error;
19 mod ui;
19 mod ui;
20 pub mod utils {
21 pub mod path_utils;
22 }
20 use error::CommandError;
23 use error::CommandError;
21
24
22 fn main_with_result(
25 fn main_with_result(
General Comments 0
You need to be logged in to leave comments. Login now