From 4d55a8f99f2a0b7c0c4ed70a615b7e58b5bee04b Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 6 Apr 2022 20:00:03 +0800 Subject: [PATCH] Enforce path conversion on windows gnu, it doesn't seem to like slashes (#298) --- git-features/src/path.rs | 17 +++++++++++++++++ git-ref/src/store/file/find.rs | 9 +++++---- git-ref/src/store/file/loose/iter.rs | 3 --- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/git-features/src/path.rs b/git-features/src/path.rs index 0869fae106..541ee1d50d 100644 --- a/git-features/src/path.rs +++ b/git-features/src/path.rs @@ -195,6 +195,23 @@ pub mod convert { p } + /// Convert paths with slashes to backslashes on windows and do nothing on unix. + pub fn to_windows_separators_on_windows_or_panic<'a>(path: &std::path::Path) -> Cow<'_, std::path::Path> { + #[cfg(not(windows))] + { + path.into() + } + #[cfg(windows)] + { + crate::path::from_byte_slice_or_panic_on_windows( + crate::path::convert::to_windows_separators(crate::path::into_bytes_or_panic_on_windows(path.as_ref())) + .as_ref(), + ) + .to_owned() + .into() + } + } + /// Replaces windows path separators with slashes. pub fn to_unix_separators<'a>(path: impl Into>) -> Cow<'a, [u8]> { replace(path, b'\\', b'/') diff --git a/git-ref/src/store/file/find.rs b/git-ref/src/store/file/find.rs index f60c85b8be..deccd32eb6 100644 --- a/git-ref/src/store/file/find.rs +++ b/git-ref/src/store/file/find.rs @@ -111,7 +111,7 @@ impl file::Store { packed: Option<&packed::Buffer>, transform: Transform, ) -> Result, Error> { - let (base, is_definitely_absolute) = match transform { + let (base, is_definitely_full_path) = match transform { Transform::EnforceRefsPrefix => ( if relative_path.starts_with("refs") { PathBuf::new() @@ -124,14 +124,15 @@ impl file::Store { }; let relative_path = base.join(inbetween).join(relative_path); + let path_to_open = git_features::path::convert::to_windows_separators_on_windows_or_panic(&relative_path); let contents = match self - .ref_contents(&relative_path) + .ref_contents(&path_to_open) .map_err(|err| Error::ReadFileContents { err, - path: relative_path.to_owned(), + path: path_to_open.into_owned(), })? { None => { - if is_definitely_absolute { + if is_definitely_full_path { if let Some(packed) = packed { let full_name = path_to_name(match &self.namespace { None => relative_path, diff --git a/git-ref/src/store/file/loose/iter.rs b/git-ref/src/store/file/loose/iter.rs index f745e3c1b4..1545e7bc37 100644 --- a/git-ref/src/store/file/loose/iter.rs +++ b/git-ref/src/store/file/loose/iter.rs @@ -71,9 +71,6 @@ impl Iterator for SortedLoosePaths { }; if git_validate::reference::name_partial(full_name.as_bstr()).is_ok() { - #[cfg(not(windows))] - let name = FullName(full_name.into()); - #[cfg(windows)] let name = FullName(full_name.into()); return Some(Ok((full_path, name))); } else {