From fbb629c02e78821a24ccf9c1a6afb82057da4bf6 Mon Sep 17 00:00:00 2001 From: leonzchang Date: Tue, 31 Oct 2023 12:13:25 +0800 Subject: [PATCH 1/8] normalize path in watch cmd --- src/cmd/watch.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 9fd5085d..271af265 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -4,6 +4,7 @@ use ignore::gitignore::Gitignore; use mdbook::errors::Result; use mdbook::utils; use mdbook::MDBook; +use std::env; use std::path::{Path, PathBuf}; use std::sync::mpsc::channel; use std::thread::sleep; @@ -87,11 +88,16 @@ fn find_gitignore(book_root: &Path) -> Option { } fn filter_ignored_files(ignore: Gitignore, paths: &[PathBuf]) -> Vec { + let current_dir = env::current_dir().expect("Unable to determine the current directory"); + paths .iter() .filter(|path| { + let normalized_path = path + .strip_prefix(¤t_dir) + .expect("Could not normalize path"); !ignore - .matched_path_or_any_parents(path, path.is_dir()) + .matched_path_or_any_parents(normalized_path, normalized_path.is_dir()) .is_ignore() }) .map(|path| path.to_path_buf()) From 621ffc46c053147176e9e1589c7131f3b6ac2f57 Mon Sep 17 00:00:00 2001 From: leonzchang Date: Tue, 31 Oct 2023 12:14:28 +0800 Subject: [PATCH 2/8] bump version v0.4.36 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2346f5b9..ca6068d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -955,7 +955,7 @@ dependencies = [ [[package]] name = "mdbook" -version = "0.4.35" +version = "0.4.36" dependencies = [ "ammonia", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 9f93cf08..ee4a46e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mdbook" -version = "0.4.35" +version = "0.4.36" authors = [ "Mathieu David ", "Michael-F-Bryan ", From 3ab19f32956d71d4435dd37c78ff233a7e0096e4 Mon Sep 17 00:00:00 2001 From: leonzchang Date: Tue, 31 Oct 2023 12:22:08 +0800 Subject: [PATCH 3/8] Revert "bump version v0.4.36" This reverts commit 621ffc46c053147176e9e1589c7131f3b6ac2f57. --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ca6068d9..2346f5b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -955,7 +955,7 @@ dependencies = [ [[package]] name = "mdbook" -version = "0.4.36" +version = "0.4.35" dependencies = [ "ammonia", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index ee4a46e5..9f93cf08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mdbook" -version = "0.4.36" +version = "0.4.35" authors = [ "Mathieu David ", "Michael-F-Bryan ", From 722c55f85f2f258c93ddafd8dc7f13e50377619a Mon Sep 17 00:00:00 2001 From: leonzchang Date: Wed, 1 Nov 2023 12:33:13 +0800 Subject: [PATCH 4/8] normalize path to relative --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/cmd/watch.rs | 8 ++++---- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2346f5b9..664e5230 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -974,6 +974,7 @@ dependencies = [ "notify-debouncer-mini", "once_cell", "opener", + "pathdiff", "predicates", "pretty_assertions", "pulldown-cmark", @@ -1151,6 +1152,12 @@ dependencies = [ "windows-targets 0.48.1", ] +[[package]] +name = "pathdiff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" + [[package]] name = "percent-encoding" version = "2.3.0" diff --git a/Cargo.toml b/Cargo.toml index 9f93cf08..033fcbd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ log = "0.4.17" memchr = "2.5.0" opener = "0.6.1" pulldown-cmark = { version = "0.9.3", default-features = false } +pathdiff = "0.2.1" regex = "1.8.1" serde = { version = "1.0.163", features = ["derive"] } serde_json = "1.0.96" diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 271af265..d112a75f 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -4,6 +4,7 @@ use ignore::gitignore::Gitignore; use mdbook::errors::Result; use mdbook::utils; use mdbook::MDBook; +use pathdiff::diff_paths; use std::env; use std::path::{Path, PathBuf}; use std::sync::mpsc::channel; @@ -93,11 +94,10 @@ fn filter_ignored_files(ignore: Gitignore, paths: &[PathBuf]) -> Vec { paths .iter() .filter(|path| { - let normalized_path = path - .strip_prefix(¤t_dir) - .expect("Could not normalize path"); + let relative_path = + diff_paths(¤t_dir, &path).expect("One of the paths should be an absolute"); !ignore - .matched_path_or_any_parents(normalized_path, normalized_path.is_dir()) + .matched_path_or_any_parents(&relative_path, relative_path.is_dir()) .is_ignore() }) .map(|path| path.to_path_buf()) From b0a001c6a4c8e170ee291ab7effd027f69e959e1 Mon Sep 17 00:00:00 2001 From: leonzchang Date: Wed, 8 Nov 2023 13:35:35 +0800 Subject: [PATCH 5/8] create relative path through ignore root and path --- src/cmd/watch.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index d112a75f..9a6168bf 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -5,7 +5,6 @@ use mdbook::errors::Result; use mdbook::utils; use mdbook::MDBook; use pathdiff::diff_paths; -use std::env; use std::path::{Path, PathBuf}; use std::sync::mpsc::channel; use std::thread::sleep; @@ -89,13 +88,16 @@ fn find_gitignore(book_root: &Path) -> Option { } fn filter_ignored_files(ignore: Gitignore, paths: &[PathBuf]) -> Vec { - let current_dir = env::current_dir().expect("Unable to determine the current directory"); + let ignore_root = ignore + .path() + .canonicalize() + .expect("ignore root canonicalize error"); paths .iter() .filter(|path| { let relative_path = - diff_paths(¤t_dir, &path).expect("One of the paths should be an absolute"); + diff_paths(&path, &ignore_root).expect("One of the paths should be an absolute"); !ignore .matched_path_or_any_parents(&relative_path, relative_path.is_dir()) .is_ignore() @@ -185,3 +187,50 @@ where } } } + +#[cfg(test)] +mod tests { + use super::*; + use ignore::gitignore::GitignoreBuilder; + use std::env; + + fn path_to_buf(root: &str, file_name: &str) -> PathBuf { + let mut path = PathBuf::from(root); + path.push(file_name); + path + } + + #[test] + fn test_filter_ignored_files() { + let current_dir = env::current_dir().unwrap(); + + let ignore = GitignoreBuilder::new(¤t_dir) + .add_line(None, "*.html") + .unwrap() + .build() + .unwrap(); + let should_remain = path_to_buf(current_dir.to_str().unwrap(), "record.text"); + let should_filter = path_to_buf(current_dir.to_str().unwrap(), "index.html"); + + let remain = filter_ignored_files(ignore, &[should_remain.clone(), should_filter]); + assert_eq!(remain, vec![should_remain]) + } + + #[test] + fn filter_ignored_files_should_handle_parent_dir() { + let current_dir = env::current_dir().unwrap(); + + let ignore = GitignoreBuilder::new(¤t_dir) + .add_line(None, "*.html") + .unwrap() + .build() + .unwrap(); + + let parent_dir = format!("{}/../", current_dir.to_str().unwrap()); + let should_remain = path_to_buf(&parent_dir, "record.text"); + let should_filter = path_to_buf(&parent_dir, "index.html"); + + let remain = filter_ignored_files(ignore, &[should_remain.clone(), should_filter]); + assert_eq!(remain, vec![should_remain]) + } +} From 40c034ed3f0282ec7aee794e041ea600936e29b4 Mon Sep 17 00:00:00 2001 From: leonzchang Date: Wed, 29 Nov 2023 11:34:08 +0800 Subject: [PATCH 6/8] apply suggest changes --- src/cmd/watch.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 9a6168bf..9e8fe89a 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -194,12 +194,6 @@ mod tests { use ignore::gitignore::GitignoreBuilder; use std::env; - fn path_to_buf(root: &str, file_name: &str) -> PathBuf { - let mut path = PathBuf::from(root); - path.push(file_name); - path - } - #[test] fn test_filter_ignored_files() { let current_dir = env::current_dir().unwrap(); @@ -209,8 +203,8 @@ mod tests { .unwrap() .build() .unwrap(); - let should_remain = path_to_buf(current_dir.to_str().unwrap(), "record.text"); - let should_filter = path_to_buf(current_dir.to_str().unwrap(), "index.html"); + let should_remain = current_dir.join("record.text"); + let should_filter = current_dir.join("index.html"); let remain = filter_ignored_files(ignore, &[should_remain.clone(), should_filter]); assert_eq!(remain, vec![should_remain]) @@ -226,9 +220,9 @@ mod tests { .build() .unwrap(); - let parent_dir = format!("{}/../", current_dir.to_str().unwrap()); - let should_remain = path_to_buf(&parent_dir, "record.text"); - let should_filter = path_to_buf(&parent_dir, "index.html"); + let parent_dir = current_dir.join(".."); + let should_remain = parent_dir.join("record.text"); + let should_filter = parent_dir.join("index.html"); let remain = filter_ignored_files(ignore, &[should_remain.clone(), should_filter]); assert_eq!(remain, vec![should_remain]) From 70826898663ee3ccd6b39b982fb197cf99fa31a5 Mon Sep 17 00:00:00 2001 From: leonzchang Date: Wed, 29 Nov 2023 11:50:07 +0800 Subject: [PATCH 7/8] add comment --- src/cmd/watch.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 9e8fe89a..9d9960f7 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -87,6 +87,8 @@ fn find_gitignore(book_root: &Path) -> Option { .find(|p| p.exists()) } +// Using `canonicalize` can fail on Windows sometimes, might be a potential risk. +// see ([https://github.com/rust-lang/mdBook/pull/2229#discussion_r1408665981). fn filter_ignored_files(ignore: Gitignore, paths: &[PathBuf]) -> Vec { let ignore_root = ignore .path() From 8d4ae388fa237f2406e57b9ea37f449a95884b43 Mon Sep 17 00:00:00 2001 From: leonzchang Date: Wed, 29 Nov 2023 11:51:23 +0800 Subject: [PATCH 8/8] update comment --- src/cmd/watch.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 9d9960f7..1e336b29 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -87,8 +87,8 @@ fn find_gitignore(book_root: &Path) -> Option { .find(|p| p.exists()) } -// Using `canonicalize` can fail on Windows sometimes, might be a potential risk. -// see ([https://github.com/rust-lang/mdBook/pull/2229#discussion_r1408665981). +// Note: The usage of `canonicalize` may encounter occasional failures on the Windows platform, presenting a potential risk. +// For more details, refer to [Pull Request #2229](https://github.com/rust-lang/mdBook/pull/2229#discussion_r1408665981). fn filter_ignored_files(ignore: Gitignore, paths: &[PathBuf]) -> Vec { let ignore_root = ignore .path()