Skip to content

Commit

Permalink
Make serve and watch respect .gitignore
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fmanco committed Oct 2, 2019
1 parent a6f317e commit 96aaab0
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 6 deletions.
115 changes: 115 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ clap = "2.24"
env_logger = "0.6"
error-chain = "0.12"
handlebars = { version = "2.0", default-features = false, features = ["no_dir_source"] }
ignore = "0.1.8"
itertools = "0.8"
lazy_static = "1.0"
log = "0.4"
Expand Down
12 changes: 11 additions & 1 deletion src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use iron::{status, AfterMiddleware, Chain, Iron, IronError, IronResult, Request,
use mdbook::errors::*;
use mdbook::utils;
use mdbook::MDBook;
use std::path::PathBuf;

struct ErrorRecover;

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

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

let gitignore_path: Option<PathBuf> = if args.is_present("gitignore") {
let mut path = book_dir.clone();
path.push(".gitignore");
Some(path)
} else {
None
};

#[cfg(feature = "watch")]
watch::trigger_on_change(&book, move |paths, book_dir| {
watch::trigger_on_change(&book, gitignore_path, move |paths, book_dir| {
info!("Files changed: {:?}", paths);
info!("Building book...");

Expand Down
Loading

0 comments on commit 96aaab0

Please sign in to comment.