Skip to content

Commit 8a33407

Browse files
authored
Merge pull request #1051 from segfaultsourcery/fix-small-gitignore-bug
I fixed a small gitignore bug
2 parents 09c7384 + 930f730 commit 8a33407

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

src/cmd/watch.rs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,33 +53,48 @@ fn remove_ignored_files(book_root: &PathBuf, paths: &[PathBuf]) -> Vec<PathBuf>
5353
return vec![];
5454
}
5555

56-
let gitignore_path = book_root.with_file_name(".gitignore");
57-
58-
match gitignore::File::new(gitignore_path.as_path()) {
59-
Ok(exclusion_checker) => paths
60-
.iter()
61-
.filter(|path| match exclusion_checker.is_excluded(path) {
62-
Ok(exclude) => !exclude,
63-
Err(error) => {
64-
warn!(
65-
"Unable to determine if {:?} is excluded: {:?}. Including it.",
66-
&path, error
67-
);
68-
true
56+
match find_gitignore(book_root) {
57+
Some(gitignore_path) => {
58+
match gitignore::File::new(gitignore_path.as_path()) {
59+
Ok(exclusion_checker) => filter_ignored_files(exclusion_checker, paths),
60+
Err(_) => {
61+
// We're unable to read the .gitignore file, so we'll silently allow everything.
62+
// Please see discussion: https://github.com/rust-lang-nursery/mdBook/pull/1051
63+
paths.iter().map(|path| path.to_path_buf()).collect()
6964
}
70-
})
71-
.map(|path| path.to_path_buf())
72-
.collect(),
73-
Err(error) => {
74-
warn!(
75-
"Unable to read gitignore file at {:?} file: {:?}. All files will be allowed.",
76-
gitignore_path, error
77-
);
65+
}
66+
}
67+
None => {
68+
// There is no .gitignore file.
7869
paths.iter().map(|path| path.to_path_buf()).collect()
7970
}
8071
}
8172
}
8273

74+
fn find_gitignore(book_root: &PathBuf) -> Option<PathBuf> {
75+
book_root
76+
.ancestors()
77+
.map(|p| p.join(".gitignore"))
78+
.find(|p| p.exists())
79+
}
80+
81+
fn filter_ignored_files(exclusion_checker: gitignore::File, paths: &[PathBuf]) -> Vec<PathBuf> {
82+
paths
83+
.iter()
84+
.filter(|path| match exclusion_checker.is_excluded(path) {
85+
Ok(exclude) => !exclude,
86+
Err(error) => {
87+
warn!(
88+
"Unable to determine if {:?} is excluded: {:?}. Including it.",
89+
&path, error
90+
);
91+
true
92+
}
93+
})
94+
.map(|path| path.to_path_buf())
95+
.collect()
96+
}
97+
8398
/// Calls the closure when a book source file is changed, blocking indefinitely.
8499
pub fn trigger_on_change<F>(book: &MDBook, closure: F)
85100
where

0 commit comments

Comments
 (0)