repo.rs
288 lines
| 9.4 KiB
| application/rls-services+xml
|
RustLexer
|
r47215 | use crate::config::{Config, ConfigError, ConfigParseError}; | ||
|
r47341 | use crate::errors::{HgError, IoErrorContext, IoResultExt}; | ||
|
r48199 | use crate::exit_codes; | ||
|
r46782 | use crate::requirements; | ||
|
r47190 | use crate::utils::files::get_path_from_bytes; | ||
|
r47474 | use crate::utils::SliceExt; | ||
|
r46782 | use memmap::{Mmap, MmapOptions}; | ||
|
r47190 | use std::collections::HashSet; | ||
|
r46782 | use std::path::{Path, PathBuf}; | ||
/// A repository on disk | ||||
pub struct Repo { | ||||
working_directory: PathBuf, | ||||
dot_hg: PathBuf, | ||||
store: PathBuf, | ||||
|
r47190 | requirements: HashSet<String>, | ||
|
r47215 | config: Config, | ||
|
r46782 | } | ||
|
r47175 | #[derive(Debug, derive_more::From)] | ||
|
r47215 | pub enum RepoError { | ||
NotFound { | ||||
|
r47253 | at: PathBuf, | ||
|
r47175 | }, | ||
#[from] | ||||
|
r47215 | ConfigParseError(ConfigParseError), | ||
#[from] | ||||
|
r47175 | Other(HgError), | ||
} | ||||
|
r47215 | impl From<ConfigError> for RepoError { | ||
fn from(error: ConfigError) -> Self { | ||||
match error { | ||||
ConfigError::Parse(error) => error.into(), | ||||
ConfigError::Other(error) => error.into(), | ||||
} | ||||
} | ||||
} | ||||
|
r46782 | /// Filesystem access abstraction for the contents of a given "base" diretory | ||
#[derive(Clone, Copy)] | ||||
|
r47341 | pub struct Vfs<'a> { | ||
pub(crate) base: &'a Path, | ||||
|
r46782 | } | ||
impl Repo { | ||||
|
r48197 | /// tries to find nearest repository root in current working directory or | ||
/// its ancestors | ||||
pub fn find_repo_root() -> Result<PathBuf, RepoError> { | ||||
let current_directory = crate::utils::current_dir()?; | ||||
// ancestors() is inclusive: it first yields `current_directory` | ||||
// as-is. | ||||
for ancestor in current_directory.ancestors() { | ||||
if ancestor.join(".hg").is_dir() { | ||||
return Ok(ancestor.to_path_buf()); | ||||
} | ||||
} | ||||
return Err(RepoError::NotFound { | ||||
at: current_directory, | ||||
}); | ||||
} | ||||
|
r47255 | /// Find a repository, either at the given path (which must contain a `.hg` | ||
/// sub-directory) or by searching the current directory and its | ||||
/// ancestors. | ||||
|
r47253 | /// | ||
|
r47255 | /// A method with two very different "modes" like this usually a code smell | ||
/// to make two methods instead, but in this case an `Option` is what rhg | ||||
/// sub-commands get from Clap for the `-R` / `--repository` CLI argument. | ||||
/// Having two methods would just move that `if` to almost all callers. | ||||
|
r47253 | pub fn find( | ||
config: &Config, | ||||
|
r48196 | explicit_path: Option<PathBuf>, | ||
|
r47253 | ) -> Result<Self, RepoError> { | ||
if let Some(root) = explicit_path { | ||||
|
r47474 | if root.join(".hg").is_dir() { | ||
Self::new_at_path(root.to_owned(), config) | ||||
} else if root.is_file() { | ||||
|
r47464 | Err(HgError::unsupported("bundle repository").into()) | ||
|
r47253 | } else { | ||
Err(RepoError::NotFound { | ||||
at: root.to_owned(), | ||||
}) | ||||
|
r47175 | } | ||
|
r47253 | } else { | ||
|
r48197 | let root = Self::find_repo_root()?; | ||
Self::new_at_path(root, config) | ||||
|
r46782 | } | ||
} | ||||
|
r47190 | /// To be called after checking that `.hg` is a sub-directory | ||
|
r47214 | fn new_at_path( | ||
working_directory: PathBuf, | ||||
config: &Config, | ||||
|
r47215 | ) -> Result<Self, RepoError> { | ||
|
r47190 | let dot_hg = working_directory.join(".hg"); | ||
|
r47191 | |||
|
r47215 | let mut repo_config_files = Vec::new(); | ||
repo_config_files.push(dot_hg.join("hgrc")); | ||||
repo_config_files.push(dot_hg.join("hgrc-not-shared")); | ||||
|
r47190 | let hg_vfs = Vfs { base: &dot_hg }; | ||
|
r47191 | let mut reqs = requirements::load_if_exists(hg_vfs)?; | ||
|
r47190 | let relative = | ||
reqs.contains(requirements::RELATIVE_SHARED_REQUIREMENT); | ||||
let shared = | ||||
reqs.contains(requirements::SHARED_REQUIREMENT) || relative; | ||||
|
r47191 | |||
// From `mercurial/localrepo.py`: | ||||
// | ||||
// if .hg/requires contains the sharesafe requirement, it means | ||||
// there exists a `.hg/store/requires` too and we should read it | ||||
// NOTE: presence of SHARESAFE_REQUIREMENT imply that store requirement | ||||
// is present. We never write SHARESAFE_REQUIREMENT for a repo if store | ||||
// is not present, refer checkrequirementscompat() for that | ||||
// | ||||
// However, if SHARESAFE_REQUIREMENT is not present, it means that the | ||||
// repository was shared the old way. We check the share source | ||||
// .hg/requires for SHARESAFE_REQUIREMENT to detect whether the | ||||
// current repository needs to be reshared | ||||
let share_safe = reqs.contains(requirements::SHARESAFE_REQUIREMENT); | ||||
|
r47190 | let store_path; | ||
if !shared { | ||||
store_path = dot_hg.join("store"); | ||||
} else { | ||||
let bytes = hg_vfs.read("sharedpath")?; | ||||
|
r47427 | let mut shared_path = | ||
get_path_from_bytes(bytes.trim_end_newlines()).to_owned(); | ||||
|
r47190 | if relative { | ||
shared_path = dot_hg.join(shared_path) | ||||
} | ||||
if !shared_path.is_dir() { | ||||
return Err(HgError::corrupted(format!( | ||||
".hg/sharedpath points to nonexistent directory {}", | ||||
shared_path.display() | ||||
|
r47215 | )) | ||
.into()); | ||||
|
r47190 | } | ||
store_path = shared_path.join("store"); | ||||
|
r47191 | |||
let source_is_share_safe = | ||||
requirements::load(Vfs { base: &shared_path })? | ||||
.contains(requirements::SHARESAFE_REQUIREMENT); | ||||
if share_safe && !source_is_share_safe { | ||||
|
r47215 | return Err(match config | ||
|
r47469 | .get(b"share", b"safe-mismatch.source-not-safe") | ||
|
r47215 | { | ||
|
r47214 | Some(b"abort") | None => HgError::abort( | ||
|
r47469 | "abort: share source does not support share-safe requirement\n\ | ||
(see `hg help config.format.use-share-safe` for more information)", | ||||
|
r48199 | exit_codes::ABORT, | ||
|
r47214 | ), | ||
|
r47215 | _ => HgError::unsupported("share-safe downgrade"), | ||
} | ||||
.into()); | ||||
|
r47191 | } else if source_is_share_safe && !share_safe { | ||
|
r47214 | return Err( | ||
|
r47469 | match config.get(b"share", b"safe-mismatch.source-safe") { | ||
|
r47214 | Some(b"abort") | None => HgError::abort( | ||
|
r47469 | "abort: version mismatch: source uses share-safe \ | ||
functionality while the current share does not\n\ | ||||
(see `hg help config.format.use-share-safe` for more information)", | ||||
|
r48199 | exit_codes::ABORT, | ||
|
r47214 | ), | ||
_ => HgError::unsupported("share-safe upgrade"), | ||||
|
r47215 | } | ||
.into(), | ||||
|
r47214 | ); | ||
|
r47191 | } | ||
|
r47215 | |||
if share_safe { | ||||
repo_config_files.insert(0, shared_path.join("hgrc")) | ||||
} | ||||
|
r47190 | } | ||
|
r47357 | if share_safe { | ||
reqs.extend(requirements::load(Vfs { base: &store_path })?); | ||||
} | ||||
|
r47190 | |||
|
r47475 | let repo_config = if std::env::var_os("HGRCSKIPREPO").is_none() { | ||
config.combine_with_repo(&repo_config_files)? | ||||
} else { | ||||
config.clone() | ||||
}; | ||||
|
r47215 | |||
|
r47190 | let repo = Self { | ||
requirements: reqs, | ||||
working_directory, | ||||
store: store_path, | ||||
dot_hg, | ||||
|
r47215 | config: repo_config, | ||
|
r47190 | }; | ||
requirements::check(&repo)?; | ||||
Ok(repo) | ||||
} | ||||
|
r46782 | pub fn working_directory_path(&self) -> &Path { | ||
&self.working_directory | ||||
} | ||||
|
r47190 | pub fn requirements(&self) -> &HashSet<String> { | ||
&self.requirements | ||||
} | ||||
|
r47215 | pub fn config(&self) -> &Config { | ||
&self.config | ||||
} | ||||
|
r46782 | /// For accessing repository files (in `.hg`), except for the store | ||
/// (`.hg/store`). | ||||
|
r47341 | pub fn hg_vfs(&self) -> Vfs<'_> { | ||
|
r46782 | Vfs { base: &self.dot_hg } | ||
} | ||||
/// For accessing repository store files (in `.hg/store`) | ||||
|
r47341 | pub fn store_vfs(&self) -> Vfs<'_> { | ||
|
r46782 | Vfs { base: &self.store } | ||
} | ||||
/// For accessing the working copy | ||||
|
r47578 | pub fn working_directory_vfs(&self) -> Vfs<'_> { | ||
|
r46782 | Vfs { | ||
base: &self.working_directory, | ||||
} | ||||
} | ||||
|
r47343 | |||
|
r48165 | pub fn has_dirstate_v2(&self) -> bool { | ||
self.requirements | ||||
.contains(requirements::DIRSTATE_V2_REQUIREMENT) | ||||
} | ||||
|
r47343 | pub fn dirstate_parents( | ||
&self, | ||||
) -> Result<crate::dirstate::DirstateParents, HgError> { | ||||
let dirstate = self.hg_vfs().mmap_open("dirstate")?; | ||||
|
r48165 | if dirstate.is_empty() { | ||
return Ok(crate::dirstate::DirstateParents::NULL); | ||||
} | ||||
let parents = if self.has_dirstate_v2() { | ||||
|
r48474 | crate::dirstate_tree::on_disk::read_docket(&dirstate)?.parents() | ||
|
r48165 | } else { | ||
crate::dirstate::parsers::parse_dirstate_parents(&dirstate)? | ||||
|
r48474 | .clone() | ||
|
r48165 | }; | ||
|
r48474 | Ok(parents) | ||
|
r47343 | } | ||
|
r46782 | } | ||
impl Vfs<'_> { | ||||
|
r47341 | pub fn join(&self, relative_path: impl AsRef<Path>) -> PathBuf { | ||
|
r47175 | self.base.join(relative_path) | ||
} | ||||
|
r47341 | pub fn read( | ||
|
r46782 | &self, | ||
relative_path: impl AsRef<Path>, | ||||
|
r47172 | ) -> Result<Vec<u8>, HgError> { | ||
|
r47175 | let path = self.join(relative_path); | ||
|
r47341 | std::fs::read(&path).when_reading_file(&path) | ||
|
r46782 | } | ||
|
r47341 | pub fn mmap_open( | ||
|
r46782 | &self, | ||
relative_path: impl AsRef<Path>, | ||||
|
r47172 | ) -> Result<Mmap, HgError> { | ||
let path = self.base.join(relative_path); | ||||
|
r47341 | let file = std::fs::File::open(&path).when_reading_file(&path)?; | ||
|
r46782 | // TODO: what are the safety requirements here? | ||
|
r47341 | let mmap = unsafe { MmapOptions::new().map(&file) } | ||
.when_reading_file(&path)?; | ||||
|
r46782 | Ok(mmap) | ||
} | ||||
|
r47341 | |||
pub fn rename( | ||||
&self, | ||||
relative_from: impl AsRef<Path>, | ||||
relative_to: impl AsRef<Path>, | ||||
) -> Result<(), HgError> { | ||||
let from = self.join(relative_from); | ||||
let to = self.join(relative_to); | ||||
std::fs::rename(&from, &to) | ||||
.with_context(|| IoErrorContext::RenamingFile { from, to }) | ||||
} | ||||
|
r46782 | } | ||