Skip to content

Commit

Permalink
Fix normalize_path
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Oct 29, 2024
1 parent 8c3f13d commit 624f5b3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
7 changes: 4 additions & 3 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,12 @@ pub(crate) fn normalize_path(path: String) -> String {
pub(crate) fn normalize_path(mut path: String) -> String {
use std::path::is_separator;

for i in 0..path.len() {
if path[i] == b'/' || !is_separator(char::from(path[i])) {
let bytes = unsafe { path.as_bytes_mut() };
for i in 0..bytes.len() {
if bytes[i] == b'/' || !is_separator(char::from(bytes[i])) {
continue;
}
path[i] = b'/';
bytes[i] = b'/';
}
path
}
Expand Down
25 changes: 11 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,17 @@ fn adjust_relative_paths(mut cli: Cli, new_cwd: &Path) -> Result<Cli> {
})
.transpose()?;

match cli.command {
Some(Command::Run(ref mut args)) | Some(Command::TryRepo(ref mut args)) => {
args.files = args
.files
.iter()
.map(|path| fs::relative_to(std::path::absolute(path)?, new_cwd))
.collect::<Result<Vec<PathBuf>, std::io::Error>>()?;
args.commit_msg_filename = args
.commit_msg_filename
.as_ref()
.map(|path| fs::relative_to(std::path::absolute(path)?, new_cwd))
.transpose()?;
}
_ => {}
if let Some(Command::Run(ref mut args) | Command::TryRepo(ref mut args)) = cli.command {
args.files = args
.files
.iter()
.map(|path| fs::relative_to(std::path::absolute(path)?, new_cwd))
.collect::<Result<Vec<PathBuf>, std::io::Error>>()?;
args.commit_msg_filename = args
.commit_msg_filename
.as_ref()
.map(|path| fs::relative_to(std::path::absolute(path)?, new_cwd))
.transpose()?;
}

Ok(cli)
Expand Down
4 changes: 2 additions & 2 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,13 @@ fn subdirectory() -> Result<()> {
context
.workdir()
.child(".pre-commit-config.yaml")
.write_str(indoc::indoc! {r#"
.write_str(indoc::indoc! {r"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
"#
"
})?;

Command::new("git")
Expand Down

0 comments on commit 624f5b3

Please sign in to comment.