Skip to content

Commit 313cd39

Browse files
committed
Use the ignore crate to filter files in watch command
Due to a bug in the `gitignore` create we switched to the `ignore` crate which does also provide more features to filter different files.
1 parent f8df8ed commit 313cd39

File tree

3 files changed

+55
-33
lines changed

3 files changed

+55
-33
lines changed

Cargo.lock

Lines changed: 45 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ topological-sort = "0.1.0"
3737

3838
# Watch feature
3939
notify = { version = "4.0", optional = true }
40-
gitignore = { version = "1.0", optional = true }
40+
ignore = { version = "0.4", optional = true }
4141

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

5959
[features]
6060
default = ["watch", "serve", "search"]
61-
watch = ["notify", "gitignore"]
61+
watch = ["notify", "ignore"]
6262
serve = ["futures-util", "tokio", "warp"]
6363
search = ["elasticlunr-rs", "ammonia"]
6464

src/cmd/watch.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{get_book_dir, open};
22
use clap::{arg, App, Arg, ArgMatches};
3+
use ignore::gitignore::Gitignore;
34
use mdbook::errors::Result;
45
use mdbook::utils;
56
use mdbook::MDBook;
@@ -75,16 +76,7 @@ fn remove_ignored_files(book_root: &Path, paths: &[PathBuf]) -> Vec<PathBuf> {
7576
}
7677

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

102-
fn filter_ignored_files(exclusion_checker: gitignore::File, paths: &[PathBuf]) -> Vec<PathBuf> {
94+
fn filter_ignored_files(ignore: &Gitignore, paths: &[PathBuf]) -> Vec<PathBuf> {
10395
paths
10496
.iter()
105-
.filter(|path| match exclusion_checker.is_excluded(path) {
106-
Ok(exclude) => !exclude,
107-
Err(error) => {
108-
warn!(
109-
"Unable to determine if {:?} is excluded: {:?}. Including it.",
110-
&path, error
111-
);
112-
true
113-
}
97+
.filter(|path| {
98+
let canonicalized = path.canonicalize();
99+
let p = canonicalized.as_ref().unwrap_or(path);
100+
101+
!ignore.matched_path_or_any_parents(p, p.is_dir()).is_ignore()
114102
})
115103
.map(|path| path.to_path_buf())
116104
.collect()

0 commit comments

Comments
 (0)