Debian Codegraph @

rust/rhg/src/utils/path_utils.rs

raw
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 std::borrow::Cow;
7 use std::ffi::OsString;
8
9 use hg::errors::HgError;
10 use hg::repo::Repo;
11 use hg::utils::current_dir;
12 use hg::utils::files::get_bytes_from_path;
13 use hg::utils::files::relativize_path;
14 use hg::utils::hg_path::HgPath;
15 use hg::utils::hg_path::HgPathBuf;
16
17 use crate::error::CommandError;
18
19 pub struct RelativizePaths {
20 repo_root: HgPathBuf,
21 cwd: HgPathBuf,
22 outside_repo: bool,
23 }
24
25 impl RelativizePaths {
26 pub fn new(repo: &Repo) -> Result<Self, HgError> {
27 let cwd = current_dir()?;
28 let repo_root = repo.working_directory_path();
29 let repo_root = cwd.join(repo_root); // Make it absolute
30 let repo_root_hgpath = HgPathBuf::from(get_bytes_from_path(&repo_root));
31
32 if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&repo_root) {
33 // The current directory is inside the repo, so we can work with
34 // relative paths
35 Ok(Self {
36 repo_root: repo_root_hgpath,
37 cwd: HgPathBuf::from(get_bytes_from_path(cwd_relative_to_repo)),
38 outside_repo: false,
39 })
40 } else {
41 Ok(Self {
42 repo_root: repo_root_hgpath,
43 cwd: HgPathBuf::from(get_bytes_from_path(cwd)),
44 outside_repo: true,
45 })
46 }
47 }
48
49 pub fn relativize<'a>(&self, path: &'a HgPath) -> Cow<'a, [u8]> {
50 if self.outside_repo {
51 let joined = self.repo_root.join(path);
52 Cow::Owned(relativize_path(&joined, &self.cwd).into_owned())
53 } else {
54 relativize_path(path, &self.cwd)
55 }
56 }
57 }
58
59 /// Resolves `FILE ...` arguments to a list of paths in the repository.
60 pub fn resolve_file_args<'a>(
61 repo: &Repo,
62 file_args: impl Iterator<Item = &'a OsString>,
63 ) -> Result<Vec<HgPathBuf>, CommandError> {
64 let cwd = hg::utils::current_dir()?;
65 let root = cwd.join(repo.working_directory_path());
66 let mut result = Vec::new();
67 for pattern in file_args {
68 // TODO: Support all the formats in `hg help patterns`.
69 if pattern.as_encoded_bytes().contains(&b':') {
70 return Err(CommandError::unsupported(
71 "rhg does not support file patterns",
72 ));
73 }
74 // TODO: use hg::utils::files::canonical_path (currently doesn't work).
75 let path = cwd.join(pattern);
76 let dotted = path.components().any(|c| c.as_os_str() == "..");
77 if pattern.as_encoded_bytes() == b"." || dotted {
78 let message = "`..` or `.` path segment";
79 return Err(CommandError::unsupported(message));
80 }
81 let relative_path = root.strip_prefix(&cwd).unwrap_or(&root);
82 let stripped = path.strip_prefix(&root).map_err(|_| {
83 CommandError::abort(format!(
84 "abort: {} not under root '{}'\n(consider using '--cwd {}')",
85 String::from_utf8_lossy(pattern.as_encoded_bytes()),
86 root.display(),
87 relative_path.display(),
88 ))
89 })?;
90 let hg_file = HgPathBuf::try_from(stripped.to_path_buf())
91 .map_err(|e| CommandError::abort(e.to_string()))?;
92 result.push(hg_file);
93 }
94 Ok(result)
95 }