diff --git a/rust/rhg/src/commands/status.rs b/rust/rhg/src/commands/status.rs --- a/rust/rhg/src/commands/status.rs +++ b/rust/rhg/src/commands/status.rs @@ -7,6 +7,7 @@ use crate::error::CommandError; use crate::ui::{Ui, UiError}; +use crate::utils::path_utils::relativize_paths; use clap::{Arg, SubCommand}; use hg; use hg::config::Config; @@ -17,13 +18,11 @@ use hg::operations::cat; use hg::repo::Repo; use hg::revlog::node::Node; -use hg::utils::current_dir; -use hg::utils::files::{get_bytes_from_path, relativize_path}; -use hg::utils::hg_path::HgPathBuf; use hg::utils::hg_path::{hg_path_to_os_string, HgPath}; use hg::StatusError; use hg::{HgPathCow, StatusOptions}; use log::{info, warn}; +use std::borrow::Cow; use std::convert::TryInto; use std::fs; use std::io::BufReader; @@ -291,46 +290,12 @@ .get_bool(b"commands", b"status.relative") .unwrap_or(relative); if relative && !ui.plain() { - let cwd = current_dir()?; - let working_directory = repo.working_directory_path(); - let working_directory = cwd.join(working_directory); // Make it absolute - let working_directory_hgpath = - HgPathBuf::from(get_bytes_from_path(working_directory.to_owned())); - let outside_repo: bool; - let cwd_hgpath: HgPathBuf; - - if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&working_directory) - { - // The current directory is inside the repo, so we can work with - // relative paths - outside_repo = false; - cwd_hgpath = - HgPathBuf::from(get_bytes_from_path(cwd_relative_to_repo)); - } else { - outside_repo = true; - cwd_hgpath = HgPathBuf::from(get_bytes_from_path(cwd)); - } - - let print_path = |path: &HgPath| -> Result<(), UiError> { + let print_path = |path: Cow<[u8]>| -> Result<(), UiError> { ui.write_stdout( - &[ - status_prefix, - b" ", - relativize_path(path, &cwd_hgpath).as_ref(), - b"\n", - ] - .concat(), + &[status_prefix, b" ", path.as_ref(), b"\n"].concat(), ) }; - - for file in paths { - if outside_repo { - let file = working_directory_hgpath.join(file); - print_path(&file)?; - } else { - print_path(file)?; - } - } + relativize_paths(repo, paths, &print_path)?; } else { for path in paths { // Same TODO as in commands::root diff --git a/rust/rhg/src/main.rs b/rust/rhg/src/main.rs --- a/rust/rhg/src/main.rs +++ b/rust/rhg/src/main.rs @@ -17,6 +17,9 @@ mod blackbox; mod error; mod ui; +pub mod utils { + pub mod path_utils; +} use error::CommandError; fn main_with_result( diff --git a/rust/rhg/src/utils/path_utils.rs b/rust/rhg/src/utils/path_utils.rs new file mode 100644 --- /dev/null +++ b/rust/rhg/src/utils/path_utils.rs @@ -0,0 +1,48 @@ +// path utils module +// +// This software may be used and distributed according to the terms of the +// GNU General Public License version 2 or any later version. + +use crate::error::CommandError; +use crate::ui::UiError; +use hg::repo::Repo; +use hg::utils::current_dir; +use hg::utils::files::{get_bytes_from_path, relativize_path}; +use hg::utils::hg_path::HgPathBuf; +use hg::HgPathCow; +use std::borrow::Cow; + +pub fn relativize_paths( + repo: &Repo, + paths: &[HgPathCow], + callback: &dyn Fn(Cow<[u8]>) -> Result<(), UiError>, +) -> Result<(), CommandError> { + let cwd = current_dir()?; + let working_directory = repo.working_directory_path(); + let working_directory = cwd.join(working_directory); // Make it absolute + let working_directory_hgpath = + HgPathBuf::from(get_bytes_from_path(working_directory.to_owned())); + let outside_repo: bool; + let cwd_hgpath: HgPathBuf; + + if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&working_directory) { + // The current directory is inside the repo, so we can work with + // relative paths + outside_repo = false; + cwd_hgpath = + HgPathBuf::from(get_bytes_from_path(cwd_relative_to_repo)); + } else { + outside_repo = true; + cwd_hgpath = HgPathBuf::from(get_bytes_from_path(cwd)); + } + + for file in paths { + if outside_repo { + let file = working_directory_hgpath.join(file); + callback(relativize_path(&file, &cwd_hgpath))?; + } else { + callback(relativize_path(file, &cwd_hgpath))?; + } + } + Ok(()) +}