Skip to content

Commit

Permalink
Added support for gitignore files. (rust-lang#1044)
Browse files Browse the repository at this point in the history
* Added support for gitignore files.
The watch command will now ignore files based on gitignore. This can be useful for when your editor creates cache or swap files.

* Ran cargo fmt.

* Made the code a bit tidier based on input from other Rust programmers.
Changed the type of the closure back to use PathBuf, not &PathBuf.
Reduced nesting.
  • Loading branch information
segfaultsourcery authored and Dylan-DPC committed Oct 4, 2019
1 parent bb51f50 commit 04e41cd
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ book-example/book
.vscode
tests/dummy_book/book/

# Ignore Jetbrains specific files.
.idea/
16 changes: 16 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ toml-query = "0.9"

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

# Serve feature
iron = { version = "0.6", optional = true }
Expand All @@ -57,7 +58,7 @@ walkdir = "2.0"
default = ["output", "watch", "serve", "search"]
debug = []
output = []
watch = ["notify"]
watch = ["notify", "gitignore"]
serve = ["iron", "staticfile", "ws"]
search = ["elasticlunr-rs", "ammonia"]

Expand Down
40 changes: 38 additions & 2 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
Ok(())
}

fn remove_ignored_files(book_root: &PathBuf, paths: &[PathBuf]) -> Vec<PathBuf> {
if paths.is_empty() {
return vec![];
}

let gitignore_path = book_root.with_file_name(".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
}
})
.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
);
paths.iter().map(|path| path.to_path_buf()).collect()
}
}
}

/// Calls the closure when a book source file is changed, blocking indefinitely.
pub fn trigger_on_change<F>(book: &MDBook, closure: F)
where
Expand Down Expand Up @@ -96,8 +128,12 @@ where
_ => None,
}
})
.collect();
.collect::<Vec<_>>();

closure(paths, &book.root);
let paths = remove_ignored_files(&book.root, &paths[..]);

if !paths.is_empty() {
closure(paths, &book.root);
}
}
}

0 comments on commit 04e41cd

Please sign in to comment.