comparison rust/rhg/src/utils/path_utils.rs @ 48453:9b0e1f64656f

rhg: refactor relativize_path into a struct + method ? instead of a function that takes an iterator and a callback. Differential Revision: https://phab.mercurial-scm.org/D11898
author Simon Sapin <simon.sapin@octobus.net>
date Fri, 10 Dec 2021 16:57:39 +0100
parents 10c32e1b892a
children 58074252db3c
comparison
equal deleted inserted replaced
48452:2afaa0145584 48453:9b0e1f64656f
1 // path utils module 1 // path utils module
2 // 2 //
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;
7 use crate::ui::UiError;
8 use hg::errors::HgError; 6 use hg::errors::HgError;
9 use hg::repo::Repo; 7 use hg::repo::Repo;
10 use hg::utils::current_dir; 8 use hg::utils::current_dir;
11 use hg::utils::files::{get_bytes_from_path, relativize_path}; 9 use hg::utils::files::{get_bytes_from_path, relativize_path};
12 use hg::utils::hg_path::HgPath; 10 use hg::utils::hg_path::HgPath;
13 use hg::utils::hg_path::HgPathBuf; 11 use hg::utils::hg_path::HgPathBuf;
14 use std::borrow::Cow; 12 use std::borrow::Cow;
15 13
16 pub fn relativize_paths( 14 pub struct RelativizePaths {
17 repo: &Repo, 15 repo_root: HgPathBuf,
18 paths: impl IntoIterator<Item = Result<impl AsRef<HgPath>, HgError>>, 16 cwd: HgPathBuf,
19 mut callback: impl FnMut(Cow<[u8]>) -> Result<(), UiError>, 17 outside_repo: bool,
20 ) -> Result<(), CommandError> { 18 }
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;
28 19
29 if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&repo_root) { 20 impl RelativizePaths {
30 // The current directory is inside the repo, so we can work with 21 pub fn new(repo: &Repo) -> Result<Self, HgError> {
31 // relative paths 22 let cwd = current_dir()?;
32 outside_repo = false; 23 let repo_root = repo.working_directory_path();
33 cwd_hgpath = 24 let repo_root = cwd.join(repo_root); // Make it absolute
34 HgPathBuf::from(get_bytes_from_path(cwd_relative_to_repo)); 25 let repo_root_hgpath =
35 } else { 26 HgPathBuf::from(get_bytes_from_path(repo_root.to_owned()));
36 outside_repo = true; 27
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 { 47 pub fn relativize<'a>(&self, path: &'a HgPath) -> Cow<'a, [u8]> {
41 if outside_repo { 48 if self.outside_repo {
42 let file = repo_root_hgpath.join(file?.as_ref()); 49 let joined = self.repo_root.join(path);
43 callback(relativize_path(&file, &cwd_hgpath))?; 50 Cow::Owned(relativize_path(&joined, &self.cwd).into_owned())
44 } else { 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 }