Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
57 changes: 56 additions & 1 deletion src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ignore::gitignore::Gitignore;
use mdbook::errors::Result;
use mdbook::utils;
use mdbook::MDBook;
use pathdiff::diff_paths;
use std::path::{Path, PathBuf};
use std::sync::mpsc::channel;
use std::thread::sleep;
Expand Down Expand Up @@ -87,11 +88,18 @@ fn find_gitignore(book_root: &Path) -> Option<PathBuf> {
}

fn filter_ignored_files(ignore: Gitignore, paths: &[PathBuf]) -> Vec<PathBuf> {
let ignore_root = ignore
.path()
.canonicalize()
.expect("ignore root canonicalize error");

paths
.iter()
.filter(|path| {
let relative_path =
diff_paths(&path, &ignore_root).expect("One of the paths should be an absolute");
!ignore
.matched_path_or_any_parents(path, path.is_dir())
.matched_path_or_any_parents(&relative_path, relative_path.is_dir())
.is_ignore()
})
.map(|path| path.to_path_buf())
Expand Down Expand Up @@ -179,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(&current_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(&current_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])
}
}