rust/rhg/src/utils/path_utils.rs
changeset 48342 10c32e1b892a
parent 48174 9ecf802b06e0
child 48453 9b0e1f64656f
equal deleted inserted replaced
48341:51f26c8088b2 48342:10c32e1b892a
     3 // This software may be used and distributed according to the terms of the
     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.
     4 // GNU General Public License version 2 or any later version.
     5 
     5 
     6 use crate::error::CommandError;
     6 use crate::error::CommandError;
     7 use crate::ui::UiError;
     7 use crate::ui::UiError;
       
     8 use hg::errors::HgError;
     8 use hg::repo::Repo;
     9 use hg::repo::Repo;
     9 use hg::utils::current_dir;
    10 use hg::utils::current_dir;
    10 use hg::utils::files::{get_bytes_from_path, relativize_path};
    11 use hg::utils::files::{get_bytes_from_path, relativize_path};
    11 use hg::utils::hg_path::HgPath;
    12 use hg::utils::hg_path::HgPath;
    12 use hg::utils::hg_path::HgPathBuf;
    13 use hg::utils::hg_path::HgPathBuf;
    13 use std::borrow::Cow;
    14 use std::borrow::Cow;
    14 
    15 
    15 pub fn relativize_paths(
    16 pub fn relativize_paths(
    16     repo: &Repo,
    17     repo: &Repo,
    17     paths: impl IntoIterator<Item = impl AsRef<HgPath>>,
    18     paths: impl IntoIterator<Item = Result<impl AsRef<HgPath>, HgError>>,
    18     mut callback: impl FnMut(Cow<[u8]>) -> Result<(), UiError>,
    19     mut callback: impl FnMut(Cow<[u8]>) -> Result<(), UiError>,
    19 ) -> Result<(), CommandError> {
    20 ) -> Result<(), CommandError> {
    20     let cwd = current_dir()?;
    21     let cwd = current_dir()?;
    21     let repo_root = repo.working_directory_path();
    22     let repo_root = repo.working_directory_path();
    22     let repo_root = cwd.join(repo_root); // Make it absolute
    23     let repo_root = cwd.join(repo_root); // Make it absolute
    36         cwd_hgpath = HgPathBuf::from(get_bytes_from_path(cwd));
    37         cwd_hgpath = HgPathBuf::from(get_bytes_from_path(cwd));
    37     }
    38     }
    38 
    39 
    39     for file in paths {
    40     for file in paths {
    40         if outside_repo {
    41         if outside_repo {
    41             let file = repo_root_hgpath.join(file.as_ref());
    42             let file = repo_root_hgpath.join(file?.as_ref());
    42             callback(relativize_path(&file, &cwd_hgpath))?;
    43             callback(relativize_path(&file, &cwd_hgpath))?;
    43         } else {
    44         } else {
    44             callback(relativize_path(file.as_ref(), &cwd_hgpath))?;
    45             callback(relativize_path(file?.as_ref(), &cwd_hgpath))?;
    45         }
    46         }
    46     }
    47     }
    47     Ok(())
    48     Ok(())
    48 }
    49 }