Skip to content

Commit

Permalink
Normalize path separator before filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Oct 29, 2024
1 parent 07ca424 commit cd836ed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
)?
Expand Down
24 changes: 24 additions & 0 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -181,3 +182,26 @@ pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> 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
}

0 comments on commit cd836ed

Please sign in to comment.