From cd836ed81d41062157f712f8c7a6c0c95713e7ac Mon Sep 17 00:00:00 2001 From: j178 <10510431+j178@users.noreply.github.com> Date: Tue, 29 Oct 2024 18:31:21 +0800 Subject: [PATCH] Normalize path separator before filtering --- src/cli/run.rs | 3 ++- src/fs.rs | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/cli/run.rs b/src/cli/run.rs index 5c7bb3c..f2ba736 100644 --- a/src/cli/run.rs +++ b/src/cli/run.rs @@ -16,6 +16,7 @@ use unicode_width::UnicodeWidthStr; use crate::cli::ExitStatus; use crate::config::Stage; +use crate::fs::normalize_path; use crate::git::{get_all_files, get_changed_files, get_diff, get_staged_files}; use crate::hook::{Hook, Project}; use crate::identify::tags_from_path; @@ -97,7 +98,7 @@ pub(crate) async fn run( let filenames = all_filenames(hook_stage, from_ref, to_ref, all_files, files).await?; let filenames = filter_filenames( - filenames.par_iter(), + filenames.par_iter().map(normalize_path), project.config().files.as_deref(), project.config().exclude.as_deref(), )? diff --git a/src/fs.rs b/src/fs.rs index ed80dd9..68ee0c0 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -20,6 +20,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +use std::borrow::Cow; use std::fmt::Display; use std::path::{Path, PathBuf}; use std::sync::LazyLock; @@ -181,3 +182,26 @@ pub fn copy_dir_all(src: impl AsRef, dst: impl AsRef) -> std::io::Re } Ok(()) } + +/// Normalizes a path to use `/` as a separator everywhere, even on platforms +/// that recognize other characters as separators. +#[cfg(unix)] +pub(crate) fn normalize_path(path: Cow<'_, [u8]>) -> Cow<'_, [u8]> { + // UNIX only uses /, so we're good. + path +} + +/// Normalizes a path to use `/` as a separator everywhere, even on platforms +/// that recognize other characters as separators. +#[cfg(not(unix))] +pub(crate) fn normalize_path(mut path: Cow<[u8]>) -> Cow<[u8]> { + use std::path::is_separator; + + for i in 0..path.len() { + if path[i] == b'/' || !is_separator(char::from(path[i])) { + continue; + } + path.to_mut()[i] = b'/'; + } + path +}