Skip to content

Commit

Permalink
Use the ignore crate to filter files in watch command
Browse files Browse the repository at this point in the history
Due to a bug in the `gitignore` create we switched to the `ignore`
crate which does also provide more features to filter different files.
  • Loading branch information
Bergmann89 committed Oct 14, 2022
1 parent f8df8ed commit 313cd39
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
56 changes: 45 additions & 11 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ topological-sort = "0.1.0"

# Watch feature
notify = { version = "4.0", optional = true }
gitignore = { version = "1.0", optional = true }
ignore = { version = "0.4", optional = true }

# Serve feature
futures-util = { version = "0.3.4", optional = true }
Expand All @@ -58,7 +58,7 @@ walkdir = "2.0"

[features]
default = ["watch", "serve", "search"]
watch = ["notify", "gitignore"]
watch = ["notify", "ignore"]
serve = ["futures-util", "tokio", "warp"]
search = ["elasticlunr-rs", "ammonia"]

Expand Down
28 changes: 8 additions & 20 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{get_book_dir, open};
use clap::{arg, App, Arg, ArgMatches};
use ignore::gitignore::Gitignore;
use mdbook::errors::Result;
use mdbook::utils;
use mdbook::MDBook;
Expand Down Expand Up @@ -75,16 +76,7 @@ fn remove_ignored_files(book_root: &Path, paths: &[PathBuf]) -> Vec<PathBuf> {
}

match find_gitignore(book_root) {
Some(gitignore_path) => {
match gitignore::File::new(gitignore_path.as_path()) {
Ok(exclusion_checker) => filter_ignored_files(exclusion_checker, paths),
Err(_) => {
// We're unable to read the .gitignore file, so we'll silently allow everything.
// Please see discussion: https://github.com/rust-lang/mdBook/pull/1051
paths.iter().map(|path| path.to_path_buf()).collect()
}
}
}
Some(gitignore_path) => filter_ignored_files(&Gitignore::new(gitignore_path).0, paths),
None => {
// There is no .gitignore file.
paths.iter().map(|path| path.to_path_buf()).collect()
Expand All @@ -99,18 +91,14 @@ fn find_gitignore(book_root: &Path) -> Option<PathBuf> {
.find(|p| p.exists())
}

fn filter_ignored_files(exclusion_checker: gitignore::File, paths: &[PathBuf]) -> Vec<PathBuf> {
fn filter_ignored_files(ignore: &Gitignore, paths: &[PathBuf]) -> Vec<PathBuf> {
paths
.iter()
.filter(|path| match exclusion_checker.is_excluded(path) {
Ok(exclude) => !exclude,
Err(error) => {
warn!(
"Unable to determine if {:?} is excluded: {:?}. Including it.",
&path, error
);
true
}
.filter(|path| {
let canonicalized = path.canonicalize();
let p = canonicalized.as_ref().unwrap_or(path);

!ignore.matched_path_or_any_parents(p, p.is_dir()).is_ignore()
})
.map(|path| path.to_path_buf())
.collect()
Expand Down

0 comments on commit 313cd39

Please sign in to comment.