Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefere .bookignore when build too #1912

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ shlex = "1"
tempfile = "3.0"
toml = "0.5.1"
topological-sort = "0.1.0"
gitignore = "1.0"

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

# Serve feature
futures-util = { version = "0.3.4", optional = true }
Expand All @@ -58,7 +58,7 @@ walkdir = "2.0"

[features]
default = ["watch", "serve", "search"]
watch = ["notify", "gitignore"]
watch = ["notify"]
serve = ["futures-util", "tokio", "warp"]
search = ["elasticlunr-rs", "ammonia"]

Expand Down
48 changes: 1 addition & 47 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::utils::ignore::remove_ignored_files;
use crate::{get_book_dir, open};
use clap::{arg, App, Arg, ArgMatches};
use mdbook::errors::Result;
Expand Down Expand Up @@ -69,53 +70,6 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
Ok(())
}

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

match find_gitignore(book_root) {
Some(gitignore_path) => {
match gitignore::File::new(gitignore_path.as_path()) {
Ok(exclusion_checker) => filter_ignored_files(exclusion_checker, paths),
Err(_) => {
// We're unable to read the .gitignore file, so we'll silently allow everything.
// Please see discussion: https://github.com/rust-lang/mdBook/pull/1051
paths.iter().map(|path| path.to_path_buf()).collect()
}
}
}
None => {
// There is no .gitignore file.
paths.iter().map(|path| path.to_path_buf()).collect()
}
}
}

fn find_gitignore(book_root: &Path) -> 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.
pub fn trigger_on_change<F>(book: &MDBook, closure: F)
where
Expand Down
9 changes: 9 additions & 0 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::errors::*;
use crate::utils::ignore::remove_ignored_files;
use log::{debug, trace};
use std::convert::Into;
use std::fs::{self, File};
Expand Down Expand Up @@ -111,6 +112,14 @@ pub fn copy_files_except_ext(

for entry in fs::read_dir(from)? {
let entry = entry?;

// Check if entry is ignored
let paths = vec![entry.path()];
let paths = remove_ignored_files(from, &paths[..]);
if paths.is_empty() {
continue;
}

let metadata = entry
.path()
.metadata()
Expand Down
49 changes: 49 additions & 0 deletions src/utils/ignore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use log::warn;
use std::path::{Path, PathBuf};

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

match find_ignorefile(book_root) {
Some(gitignore_path) => {
match gitignore::File::new(gitignore_path.as_path()) {
Ok(exclusion_checker) => filter_ignored_files(exclusion_checker, paths),
Err(_) => {
// We're unable to read the .gitignore file, so we'll silently allow everything.
// Please see discussion: https://github.com/rust-lang/mdBook/pull/1051
paths.iter().map(|path| path.to_path_buf()).collect()
}
}
}
None => {
// There is no .gitignore file.
paths.iter().map(|path| path.to_path_buf()).collect()
}
}
}

fn find_ignorefile(book_root: &Path) -> Option<PathBuf> {
book_root
.ancestors()
.flat_map(|p| vec![p.join(".bookignore"), p.join(".gitignore")].into_iter())
.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()
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(missing_docs)] // FIXME: Document this

pub mod fs;
pub mod ignore;
mod string;
pub(crate) mod toml_ext;
use crate::errors::Error;
Expand Down