The .gitignore file is now searched for recursively.

Removed a warning if .gitignore is missing.
This commit is contained in:
Kim Hermansson 2019-10-04 22:56:56 +02:00
parent a3d1afdd1f
commit 930f730361
1 changed files with 36 additions and 21 deletions

View File

@ -53,33 +53,48 @@ fn remove_ignored_files(book_root: &PathBuf, paths: &[PathBuf]) -> Vec<PathBuf>
return vec![]; return vec![];
} }
let gitignore_path = book_root.join(".gitignore"); match find_gitignore(book_root) {
Some(gitignore_path) => {
match gitignore::File::new(gitignore_path.as_path()) { match gitignore::File::new(gitignore_path.as_path()) {
Ok(exclusion_checker) => paths Ok(exclusion_checker) => filter_ignored_files(exclusion_checker, paths),
.iter() Err(_) => {
.filter(|path| match exclusion_checker.is_excluded(path) { // We're unable to read the .gitignore file, so we'll silently allow everything.
Ok(exclude) => !exclude, // Please see discussion: https://github.com/rust-lang-nursery/mdBook/pull/1051
Err(error) => { paths.iter().map(|path| path.to_path_buf()).collect()
warn!(
"Unable to determine if {:?} is excluded: {:?}. Including it.",
&path, error
);
true
} }
}) }
.map(|path| path.to_path_buf()) }
.collect(), None => {
Err(error) => { // There is no .gitignore file.
warn!(
"Unable to read gitignore file at {:?} file: {:?}. All files will be allowed.",
gitignore_path, error
);
paths.iter().map(|path| path.to_path_buf()).collect() paths.iter().map(|path| path.to_path_buf()).collect()
} }
} }
} }
fn find_gitignore(book_root: &PathBuf) -> Option<PathBuf> {
book_root
.ancestors()
.map(|p| p.join(".gitignore"))
.find(|p| p.exists())
}
fn filter_ignored_files(exclusion_checker: gitignore::File, 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
}
})
.map(|path| path.to_path_buf())
.collect()
}
/// Calls the closure when a book source file is changed, blocking indefinitely. /// Calls the closure when a book source file is changed, blocking indefinitely.
pub fn trigger_on_change<F>(book: &MDBook, closure: F) pub fn trigger_on_change<F>(book: &MDBook, closure: F)
where where