Skip to content

Commit 96aaab0

Browse files
committed
Make serve and watch respect .gitignore
When editing files with some editors (e.g. vim) temporary files are constantly being modified causing mdbook serve and watch to constantly rebuild the project. To avoid this leverage the ignore crate to load the project's root .gitignore file and skip changes on files ignored.
1 parent a6f317e commit 96aaab0

File tree

4 files changed

+158
-6
lines changed

4 files changed

+158
-6
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ clap = "2.24"
2121
env_logger = "0.6"
2222
error-chain = "0.12"
2323
handlebars = { version = "2.0", default-features = false, features = ["no_dir_source"] }
24+
ignore = "0.1.8"
2425
itertools = "0.8"
2526
lazy_static = "1.0"
2627
log = "0.4"

src/cmd/serve.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use iron::{status, AfterMiddleware, Chain, Iron, IronError, IronResult, Request,
66
use mdbook::errors::*;
77
use mdbook::utils;
88
use mdbook::MDBook;
9+
use std::path::PathBuf;
910

1011
struct ErrorRecover;
1112

@@ -59,6 +60,7 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
5960
.help("Port to use for WebSockets livereload connections"),
6061
)
6162
.arg_from_usage("-o, --open 'Opens the book server in a web browser'")
63+
.arg_from_usage("-i, --gitignore 'Respect .gitignore and skip changes to ignored files'")
6264
}
6365

6466
// Watch command implementation
@@ -107,8 +109,16 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
107109
open(serving_url);
108110
}
109111

112+
let gitignore_path: Option<PathBuf> = if args.is_present("gitignore") {
113+
let mut path = book_dir.clone();
114+
path.push(".gitignore");
115+
Some(path)
116+
} else {
117+
None
118+
};
119+
110120
#[cfg(feature = "watch")]
111-
watch::trigger_on_change(&book, move |paths, book_dir| {
121+
watch::trigger_on_change(&book, gitignore_path, move |paths, book_dir| {
112122
info!("Files changed: {:?}", paths);
113123
info!("Building book...");
114124

0 commit comments

Comments
 (0)