##// END OF EJS Templates
tracked-key: remove the dual write and rename to tracked-hint...
tracked-key: remove the dual write and rename to tracked-hint The dual-write approach was mostly useless. As explained in the previous version of the help, the key had to be read twice before we could cache a value. However this "read twice" limitation actually also apply to any usage of the key. If some operation wants to rely of the "same value == same tracked set" property it would need to read the value before, and after running that operation (or at least, after, in all cases). So it cannot be sure the operation it did is "valid" until checking the key after the operation. As a resultat such operation can only be read-only or rollbackable. This reduce the utility of the "same value == same tracked set" a lot. So it seems simpler to drop the double write and to update the documentation to highlight that this file does not garantee race-free operation. As a result the "key" is demoted to a "hint". Documentation is updated accordingly. Differential Revision: https://phab.mercurial-scm.org/D12201

File last commit:

r49284:9b0e1f64 default
r49644:6e559391 default
Show More
path_utils.rs
55 lines | 1.7 KiB | application/rls-services+xml | RustLexer
// 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 hg::errors::HgError;
use hg::repo::Repo;
use hg::utils::current_dir;
use hg::utils::files::{get_bytes_from_path, relativize_path};
use hg::utils::hg_path::HgPath;
use hg::utils::hg_path::HgPathBuf;
use std::borrow::Cow;
pub struct RelativizePaths {
repo_root: HgPathBuf,
cwd: HgPathBuf,
outside_repo: bool,
}
impl RelativizePaths {
pub fn new(repo: &Repo) -> Result<Self, HgError> {
let cwd = current_dir()?;
let repo_root = repo.working_directory_path();
let repo_root = cwd.join(repo_root); // Make it absolute
let repo_root_hgpath =
HgPathBuf::from(get_bytes_from_path(repo_root.to_owned()));
if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&repo_root) {
// The current directory is inside the repo, so we can work with
// relative paths
Ok(Self {
repo_root: repo_root_hgpath,
cwd: HgPathBuf::from(get_bytes_from_path(
cwd_relative_to_repo,
)),
outside_repo: false,
})
} else {
Ok(Self {
repo_root: repo_root_hgpath,
cwd: HgPathBuf::from(get_bytes_from_path(cwd)),
outside_repo: true,
})
}
}
pub fn relativize<'a>(&self, path: &'a HgPath) -> Cow<'a, [u8]> {
if self.outside_repo {
let joined = self.repo_root.join(path);
Cow::Owned(relativize_path(&joined, &self.cwd).into_owned())
} else {
relativize_path(path, &self.cwd)
}
}
}