clidispatch: make repo.shared_path non-lazy

Summary:
Move `repo.shared_path` handling to repo initialization time and store it in
the repo structure.

This makes `repo.shared_path()` cheap if it gets used frequently.

Reviewed By: sfilipco

Differential Revision: D16796401

fbshipit-source-id: e19f3381cc87b55500ea1d27fd918ccb16a71972
This commit is contained in:
Jun Wu 2019-08-21 12:57:32 -07:00 committed by Facebook Github Bot
parent 08a80a4bcc
commit 8f349dbb17
2 changed files with 43 additions and 35 deletions

View File

@ -13,6 +13,7 @@ pub struct Repo {
path: PathBuf,
config: ConfigSet,
bundle_path: Option<PathBuf>,
shared_path: Option<PathBuf>,
}
/// Either an optional [`Repo`] which owns a [`ConfigSet`], or a [`ConfigSet`]
@ -100,47 +101,21 @@ impl Repo {
if let Some(error) = errors.pop() {
Err(error.into())
} else {
let shared_path = read_sharedpath(&path)?;
Ok(Repo {
path,
config,
bundle_path: None,
shared_path,
})
}
}
pub fn sharedpath(&self) -> Fallible<Option<PathBuf>> {
let mut sharedpath = fs::read_to_string(self.path.join(".hg/sharedpath"))
.ok()
.map(|s| PathBuf::from(s))
.and_then(|p| Some(PathBuf::from(p.parent()?)));
if let Some(possible_path) = sharedpath {
if possible_path.is_absolute() && !possible_path.exists() {
return Err(errors::InvalidSharedPath(
possible_path.join(".hg").to_string_lossy().to_string(),
)
.into());
} else if possible_path.is_absolute() {
sharedpath = Some(possible_path)
} else {
// join relative path from the REPO/.hg path
let new_possible = self.path.join(".hg").join(possible_path);
if !new_possible.join(".hg").exists() {
return Err(errors::InvalidSharedPath(
new_possible
.canonicalize()
.ok()
.map(|r| r.to_string_lossy().to_string())
.unwrap_or("".to_string()),
)
.into());
}
sharedpath = Some(new_possible)
}
pub fn shared_path(&self) -> Option<&Path> {
match &self.shared_path {
Some(path) => Some(path),
None => None,
}
Ok(sharedpath)
}
pub fn path(&self) -> &Path {
@ -162,3 +137,36 @@ fn find_hg_repo_root(current_path: &Path) -> Option<PathBuf> {
None
}
}
fn read_sharedpath(path: &Path) -> Fallible<Option<PathBuf>> {
let mut sharedpath = fs::read_to_string(path.join(".hg/sharedpath"))
.ok()
.map(|s| PathBuf::from(s))
.and_then(|p| Some(PathBuf::from(p.parent()?)));
if let Some(possible_path) = sharedpath {
if possible_path.is_absolute() && !possible_path.is_dir() {
return Err(errors::InvalidSharedPath(
possible_path.join(".hg").to_string_lossy().to_string(),
)
.into());
} else if possible_path.is_absolute() {
sharedpath = Some(possible_path)
} else {
// join relative path from the REPO/.hg path
let new_possible = path.join(".hg").join(possible_path);
if !new_possible.join(".hg").exists() {
return Err(errors::InvalidSharedPath(
new_possible
.canonicalize()
.ok()
.map(|r| r.to_string_lossy().to_string())
.unwrap_or("".to_string()),
)
.into());
}
sharedpath = Some(new_possible)
}
}
Ok(sharedpath)
}

View File

@ -58,10 +58,10 @@ pub fn root(opts: RootOpts, io: &mut IO, repo: Repo) -> Fallible<u8> {
return Err(errors::InvalidArguments.into());
}
let shared = repo.sharedpath()?;
let path = if opts.shared {
shared.unwrap_or(repo.path().to_owned())
repo.shared_path()
.map(|p| p.to_owned())
.unwrap_or(repo.path().to_owned())
} else {
repo.path().to_owned()
};