Mercurial > public > mercurial-scm > hg-stable
annotate rust/rhg/src/utils/path_utils.rs @ 48492: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 |
rev | line source |
---|---|
48186
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
1 // path utils module |
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
2 // |
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
3 // This software may be used and distributed according to the terms of the |
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
4 // GNU General Public License version 2 or any later version. |
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
5 |
48391
10c32e1b892a
rhg: Propogate manifest parse errors instead of panicking
Simon Sapin <simon.sapin@octobus.net>
parents:
48186
diff
changeset
|
6 use hg::errors::HgError; |
48186
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
7 use hg::repo::Repo; |
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
8 use hg::utils::current_dir; |
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
9 use hg::utils::files::{get_bytes_from_path, relativize_path}; |
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
10 use hg::utils::hg_path::HgPath; |
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
11 use hg::utils::hg_path::HgPathBuf; |
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
12 use std::borrow::Cow; |
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
13 |
48492
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
14 pub struct RelativizePaths { |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
15 repo_root: HgPathBuf, |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
16 cwd: HgPathBuf, |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
17 outside_repo: bool, |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
18 } |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
19 |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
20 impl RelativizePaths { |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
21 pub fn new(repo: &Repo) -> Result<Self, HgError> { |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
22 let cwd = current_dir()?; |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
23 let repo_root = repo.working_directory_path(); |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
24 let repo_root = cwd.join(repo_root); // Make it absolute |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
25 let repo_root_hgpath = |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
26 HgPathBuf::from(get_bytes_from_path(repo_root.to_owned())); |
48186
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
27 |
48492
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
28 if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&repo_root) { |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
29 // The current directory is inside the repo, so we can work with |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
30 // relative paths |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
31 Ok(Self { |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
32 repo_root: repo_root_hgpath, |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
33 cwd: HgPathBuf::from(get_bytes_from_path( |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
34 cwd_relative_to_repo, |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
35 )), |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
36 outside_repo: false, |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
37 }) |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
38 } else { |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
39 Ok(Self { |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
40 repo_root: repo_root_hgpath, |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
41 cwd: HgPathBuf::from(get_bytes_from_path(cwd)), |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
42 outside_repo: true, |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
43 }) |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
44 } |
48186
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
45 } |
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
46 |
48492
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
47 pub fn relativize<'a>(&self, path: &'a HgPath) -> Cow<'a, [u8]> { |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
48 if self.outside_repo { |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
49 let joined = self.repo_root.join(path); |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
50 Cow::Owned(relativize_path(&joined, &self.cwd).into_owned()) |
48186
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
51 } else { |
48492
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48391
diff
changeset
|
52 relativize_path(path, &self.cwd) |
48186
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
53 } |
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
54 } |
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
55 } |