Skip to content

Commit

Permalink
Use git_path::realpath in all places that allow it right now
Browse files Browse the repository at this point in the history
It seems better to be able to avoid questionmark errors, even though
they might be specific to some ways tests are run as well.

Note that test failures can happen with realpath and git-repository
equality comparisons for some reason.
  • Loading branch information
Byron committed Jun 21, 2022
1 parent 3d16c36 commit 229dc91
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
3 changes: 2 additions & 1 deletion git-discover/src/upwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ pub(crate) mod function {
cursor = if cursor.as_os_str().is_empty() {
cwd.clone()
} else {
cursor.canonicalize().ok()
// TODO: realpath or absolutize? No test runs into this.
Some(git_path::absolutize(&cursor, cwd.as_deref()).into_owned())
}
.ok_or(Error::InaccessibleDirectory { path: cursor })?;
}
Expand Down
7 changes: 5 additions & 2 deletions git-odb/src/alternate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub enum Error {
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Realpath(#[from] git_path::realpath::Error),
#[error(transparent)]
Parse(#[from] parse::Error),
#[error("Alternates form a cycle: {} -> {}", .0.iter().map(|p| format!("'{}'", p.display())).collect::<Vec<_>>().join(" -> "), .0.first().expect("more than one directories").display())]
Cycle(Vec<PathBuf>),
Expand All @@ -42,13 +44,14 @@ pub fn resolve(objects_directory: impl Into<PathBuf>) -> Result<Vec<PathBuf>, Er
let relative_base = objects_directory.into();
let mut dirs = vec![(0, relative_base.clone())];
let mut out = Vec::new();
let mut seen = vec![relative_base.canonicalize()?];
let cwd = std::env::current_dir()?;
let mut seen = vec![git_path::realpath(&relative_base, &cwd)?];
while let Some((depth, dir)) = dirs.pop() {
match fs::read(dir.join("info").join("alternates")) {
Ok(input) => {
for path in parse::content(&input)?.into_iter() {
let path = relative_base.join(path);
let path_canonicalized = path.canonicalize()?;
let path_canonicalized = git_path::realpath(&path, &cwd)?;
if seen.contains(&path_canonicalized) {
return Err(Error::Cycle(seen));
}
Expand Down
7 changes: 1 addition & 6 deletions git-path/tests/realpath/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,6 @@ fn create_symlink(from: impl AsRef<Path>, to: impl AsRef<Path>) -> std::io::Resu
}

fn canonicalized_tempdir() -> crate::Result<tempfile::TempDir> {
#[cfg(windows)]
let canonicalized_tempdir = std::env::temp_dir();

#[cfg(not(windows))]
let canonicalized_tempdir = std::env::temp_dir().canonicalize()?;

let canonicalized_tempdir = git_path::realpath(std::env::temp_dir(), std::env::current_dir()?)?;
Ok(tempfile::tempdir_in(canonicalized_tempdir)?)
}
32 changes: 17 additions & 15 deletions git-repository/src/repository/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,23 @@ impl crate::Repository {
// TODO: tests, details - there is a lot about environment variables to change things around.
pub fn prefix(&self) -> Option<std::io::Result<std::path::PathBuf>> {
self.work_tree.as_ref().map(|root| {
root.canonicalize().and_then(|root| {
std::env::current_dir().and_then(|cwd| {
cwd.strip_prefix(&root)
.map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"CWD '{}' isn't within the work tree '{}'",
cwd.display(),
root.display()
),
)
})
.map(ToOwned::to_owned)
})
std::env::current_dir().and_then(|cwd| {
git_path::realpath(root, &cwd)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
.and_then(|root| {
cwd.strip_prefix(&root)
.map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"CWD '{}' isn't within the work tree '{}'",
cwd.display(),
root.display()
),
)
})
.map(ToOwned::to_owned)
})
})
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/porcelain/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ pub struct EstimateHours {
}

mod validator {
use git_repository as git;
use std::{ffi::OsStr, path::PathBuf};

use anyhow::Context;

fn is_repo_inner(dir: &OsStr) -> anyhow::Result<()> {
let git_dir = PathBuf::from(dir).join(".git");
let p = git_dir
.canonicalize()
let p = git::path::realpath(&git_dir, std::env::current_dir()?)
.with_context(|| format!("Could not canonicalize git repository at '{}'", git_dir.display()))?;
if p.extension().unwrap_or_default() == "git"
|| p.file_name().unwrap_or_default() == ".git"
Expand Down

0 comments on commit 229dc91

Please sign in to comment.