Skip to content

Commit

Permalink
adapt to all changes in git-path with bstr support (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Apr 27, 2022
1 parent 9380e99 commit f158648
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 30 deletions.
8 changes: 4 additions & 4 deletions git-attributes/src/match_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ where
.and_then(|base| {
(!base.as_os_str().is_empty()).then(|| {
let mut base: BString =
git_path::to_unix_separators_on_windows(git_path::into_bytes_or_panic_on_windows(base))
.into_owned()
.into();
git_path::to_unix_separators_on_windows(git_path::into_bstr_or_panic_on_windows(base))
.into_owned();

base.push_byte(b'/');
base
})
Expand Down Expand Up @@ -309,7 +309,7 @@ impl PatternList<Ignore> {
.map(Into::into)
.enumerate()
.filter_map(|(seq_id, pattern)| {
let pattern = git_path::into_bytes(PathBuf::from(pattern)).ok()?;
let pattern = git_path::into_bstr(PathBuf::from(pattern)).ok()?;
git_glob::parse(pattern.as_ref()).map(|p| PatternMapping {
pattern: p,
value: (),
Expand Down
23 changes: 17 additions & 6 deletions git-config/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,17 @@ pub mod path {
})?;
let (_prefix, path_without_trailing_slash) = self.split_at(PREFIX.len());
let path_without_trailing_slash =
git_path::from_byte_vec(path_without_trailing_slash).context("path past %(prefix)")?;
git_path::from_bstring(path_without_trailing_slash).context("path past %(prefix)")?;
Ok(git_install_dir.join(path_without_trailing_slash).into())
} else if self.starts_with(USER_HOME) {
let home_path = dirs::home_dir().ok_or(interpolate::Error::Missing { what: "home dir" })?;
let (_prefix, val) = self.split_at(USER_HOME.len());
let val = git_path::from_bytes(val).context("path past ~/")?;
let val = git_path::from_byte_slice(val).context("path past ~/")?;
Ok(home_path.join(val).into())
} else if self.starts_with(b"~") && self.contains(&b'/') {
self.interpolate_user()
} else {
Ok(git_path::from_bytes(self.value).context("unexpanded path")?)
Ok(git_path::from_bstr_or_panic_on_windows(self.value))
}
}

Expand Down Expand Up @@ -306,11 +306,11 @@ pub mod path {
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Path<'a> {
/// The path string, un-interpolated
pub value: Cow<'a, [u8]>,
pub value: Cow<'a, BStr>,
}

impl<'a> std::ops::Deref for Path<'a> {
type Target = [u8];
type Target = BStr;

fn deref(&self) -> &Self::Target {
self.value.as_ref()
Expand All @@ -323,10 +323,21 @@ impl<'a> AsRef<[u8]> for Path<'a> {
}
}

impl<'a> AsRef<BStr> for Path<'a> {
fn as_ref(&self) -> &BStr {
self.value.as_ref()
}
}

impl<'a> From<Cow<'a, [u8]>> for Path<'a> {
#[inline]
fn from(value: Cow<'a, [u8]>) -> Self {
Path { value }
Path {
value: match value {
Cow::Borrowed(v) => Cow::Borrowed(v.into()),
Cow::Owned(v) => Cow::Owned(v.into()),
},
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion git-ref/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl<'a> TryFrom<&'a OsStr> for PartialNameRef<'a> {
type Error = Error;

fn try_from(v: &'a OsStr) -> Result<Self, Self::Error> {
let v = git_path::os_str_into_bytes(v)
let v = git_path::os_str_into_bstr(v)
.map_err(|_| Error::Tag(git_validate::tag::name::Error::InvalidByte("<unknown encoding>".into())))?;
Ok(PartialNameRef(
git_validate::reference::name_partial(v.as_bstr())?.into(),
Expand Down
8 changes: 2 additions & 6 deletions git-ref/src/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ impl Namespace {
/// Append the given `prefix` to this namespace so it becomes usable for prefixed iteration.
pub fn into_namespaced_prefix(mut self, prefix: impl AsRef<Path>) -> PathBuf {
self.0
.push_str(git_path::into_bytes_or_panic_on_windows(prefix.as_ref()));
git_path::to_windows_separators_on_windows_or_panic({
let v: Vec<_> = self.0.into();
v
})
.into_owned()
.push_str(git_path::into_bstr_or_panic_on_windows(prefix.as_ref()).as_ref());
git_path::to_windows_separators_on_windows_or_panic(self.0.clone()).into_owned()
}
}

Expand Down
2 changes: 1 addition & 1 deletion git-ref/src/store/file/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl file::Store {
let relative_path = base.join(inbetween).join(relative_path);

let path_to_open = git_path::to_windows_separators_on_windows_or_panic(
git_path::into_bytes_or_panic_on_windows(&relative_path),
git_path::into_bstr_or_panic_on_windows(&relative_path),
);
let contents = match self
.ref_contents(&path_to_open)
Expand Down
10 changes: 5 additions & 5 deletions git-ref/src/store/file/loose/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Iterator for SortedLoosePaths {
.as_deref()
.and_then(|prefix| full_path.file_name().map(|name| (prefix, name)))
{
match git_path::os_str_into_bytes(name) {
match git_path::os_str_into_bstr(name) {
Ok(name) => {
if !name.starts_with(prefix) {
continue;
Expand All @@ -61,7 +61,7 @@ impl Iterator for SortedLoosePaths {
let full_name = full_path
.strip_prefix(&self.base)
.expect("prefix-stripping cannot fail as prefix is our root");
let full_name = match git_path::into_bytes(full_name) {
let full_name = match git_path::into_bstr(full_name) {
Ok(name) => {
let name = git_path::to_unix_separators_on_windows(name);
name.into_owned()
Expand All @@ -70,7 +70,7 @@ impl Iterator for SortedLoosePaths {
};

if git_validate::reference::name_partial(full_name.as_bstr()).is_ok() {
let name = FullName(full_name.into());
let name = FullName(full_name);
return Some(Ok((full_path, name)));
} else {
continue;
Expand Down Expand Up @@ -200,8 +200,8 @@ impl file::Store {
base.file_name()
.map(ToOwned::to_owned)
.map(|p| {
git_path::into_bytes(PathBuf::from(p))
.map(|p| BString::from(p.into_owned()))
git_path::into_bstr(PathBuf::from(p))
.map(|p| p.into_owned())
.map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
Expand Down
6 changes: 2 additions & 4 deletions git-ref/src/store/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ pub struct Transaction<'s> {
}

pub(in crate::store_impl::file) fn path_to_name<'a>(path: impl Into<Cow<'a, Path>>) -> Cow<'a, BStr> {
let path = git_path::into_bytes_or_panic_on_windows(path.into());

let path = git_path::to_unix_separators_on_windows(path);
git_path::into_bstr(path)
let path = git_path::into_bstr_or_panic_on_windows(path.into());
git_path::to_unix_separators_on_windows(path)
}

///
Expand Down
2 changes: 1 addition & 1 deletion git-worktree/src/fs/cache/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'a, 'paths> Platform<'a, 'paths> {
/// If the cache was configured without exclude patterns.
pub fn matching_exclude_pattern(&self) -> Option<git_attributes::Match<'_, ()>> {
let ignore = self.parent.state.ignore_or_panic();
let relative_path = git_path::to_unix_separators_on_windows(git_path::into_bytes_or_panic_on_windows(
let relative_path = git_path::to_unix_separators_on_windows(git_path::into_bstr_or_panic_on_windows(
self.parent.stack.current_relative.as_path(),
));
ignore.matching_exclude_pattern(relative_path.as_bstr(), self.is_dir, self.parent.case)
Expand Down
4 changes: 2 additions & 2 deletions git-worktree/src/fs/cache/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Ignore {
Find: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Result<git_object::BlobRef<'b>, E>,
E: std::error::Error + Send + Sync + 'static,
{
let ignore_path_relative = git_path::to_unix_separators_on_windows(git_path::into_bytes_or_panic_on_windows(
let ignore_path_relative = git_path::to_unix_separators_on_windows(git_path::into_bstr_or_panic_on_windows(
dir.strip_prefix(root).expect("dir in root").join(".gitignore"),
));
let ignore_file_in_index =
Expand All @@ -89,7 +89,7 @@ impl Ignore {
Ok(idx) => {
let ignore_blob = find(&attribute_files_in_index[idx].1, buf)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?;
let ignore_path = git_path::from_byte_vec_or_panic_on_windows(ignore_path_relative.into_owned());
let ignore_path = git_path::from_bstring_or_panic_on_windows(ignore_path_relative.into_owned());
self.stack
.add_patterns_buffer(ignore_blob.data, ignore_path, Some(root));
}
Expand Down

0 comments on commit f158648

Please sign in to comment.