From a3d1afdd1f474812209c727744c9d44709a1cc9f Mon Sep 17 00:00:00 2001 From: Kim Hermansson Date: Fri, 4 Oct 2019 19:44:36 +0200 Subject: [PATCH 1/2] This fixes a small bug where the gitignore location can be misinterpreted to be in the folder "above" where it actually is. --- src/cmd/watch.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index fe1f188c..09524f65 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -53,7 +53,7 @@ fn remove_ignored_files(book_root: &PathBuf, paths: &[PathBuf]) -> Vec return vec![]; } - let gitignore_path = book_root.with_file_name(".gitignore"); + let gitignore_path = book_root.join(".gitignore"); match gitignore::File::new(gitignore_path.as_path()) { Ok(exclusion_checker) => paths From 930f73036161d193e125a478e0d53d216e619bcc Mon Sep 17 00:00:00 2001 From: Kim Hermansson Date: Fri, 4 Oct 2019 22:56:56 +0200 Subject: [PATCH 2/2] The .gitignore file is now searched for recursively. Removed a warning if .gitignore is missing. --- src/cmd/watch.rs | 57 ++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 09524f65..a5846b21 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -53,33 +53,48 @@ fn remove_ignored_files(book_root: &PathBuf, paths: &[PathBuf]) -> Vec return vec![]; } - let gitignore_path = book_root.join(".gitignore"); - - match gitignore::File::new(gitignore_path.as_path()) { - Ok(exclusion_checker) => 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 + 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-nursery/mdBook/pull/1051 + paths.iter().map(|path| path.to_path_buf()).collect() } - }) - .map(|path| path.to_path_buf()) - .collect(), - Err(error) => { - warn!( - "Unable to read gitignore file at {:?} file: {:?}. All files will be allowed.", - gitignore_path, error - ); + } + } + None => { + // There is no .gitignore file. paths.iter().map(|path| path.to_path_buf()).collect() } } } +fn find_gitignore(book_root: &PathBuf) -> Option { + book_root + .ancestors() + .map(|p| p.join(".gitignore")) + .find(|p| p.exists()) +} + +fn filter_ignored_files(exclusion_checker: gitignore::File, paths: &[PathBuf]) -> Vec { + 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 + } + }) + .map(|path| path.to_path_buf()) + .collect() +} + /// Calls the closure when a book source file is changed, blocking indefinitely. pub fn trigger_on_change(book: &MDBook, closure: F) where