Skip to content

Commit 7991734

Browse files
committed
Prefere .bookignore when build too
Reuse watch command experience and process .bookignore (as .gitignore) if prezent.
1 parent 3a24f10 commit 7991734

File tree

4 files changed

+60
-47
lines changed

4 files changed

+60
-47
lines changed

src/cmd/watch.rs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::utils::ignore::remove_ignored_files;
12
use crate::{get_book_dir, open};
23
use clap::{arg, App, Arg, ArgMatches};
34
use mdbook::errors::Result;
@@ -69,53 +70,6 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
6970
Ok(())
7071
}
7172

72-
fn remove_ignored_files(book_root: &Path, paths: &[PathBuf]) -> Vec<PathBuf> {
73-
if paths.is_empty() {
74-
return vec![];
75-
}
76-
77-
match find_gitignore(book_root) {
78-
Some(gitignore_path) => {
79-
match gitignore::File::new(gitignore_path.as_path()) {
80-
Ok(exclusion_checker) => filter_ignored_files(exclusion_checker, paths),
81-
Err(_) => {
82-
// We're unable to read the .gitignore file, so we'll silently allow everything.
83-
// Please see discussion: https://github.com/rust-lang/mdBook/pull/1051
84-
paths.iter().map(|path| path.to_path_buf()).collect()
85-
}
86-
}
87-
}
88-
None => {
89-
// There is no .gitignore file.
90-
paths.iter().map(|path| path.to_path_buf()).collect()
91-
}
92-
}
93-
}
94-
95-
fn find_gitignore(book_root: &Path) -> Option<PathBuf> {
96-
book_root
97-
.ancestors()
98-
.map(|p| p.join(".gitignore"))
99-
.find(|p| p.exists())
100-
}
101-
102-
fn filter_ignored_files(exclusion_checker: gitignore::File, paths: &[PathBuf]) -> Vec<PathBuf> {
103-
paths
104-
.iter()
105-
.filter(|path| match exclusion_checker.is_excluded(path) {
106-
Ok(exclude) => !exclude,
107-
Err(error) => {
108-
warn!(
109-
"Unable to determine if {:?} is excluded: {:?}. Including it.",
110-
&path, error
111-
);
112-
true
113-
}
114-
})
115-
.map(|path| path.to_path_buf())
116-
.collect()
117-
}
118-
11973
/// Calls the closure when a book source file is changed, blocking indefinitely.
12074
pub fn trigger_on_change<F>(book: &MDBook, closure: F)
12175
where

src/utils/fs.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::errors::*;
2+
use crate::utils::ignore::remove_ignored_files;
23
use log::{debug, trace};
34
use std::convert::Into;
45
use std::fs::{self, File};
@@ -111,6 +112,14 @@ pub fn copy_files_except_ext(
111112

112113
for entry in fs::read_dir(from)? {
113114
let entry = entry?;
115+
116+
// Check if entry is ignored
117+
let paths = vec![entry.path()];
118+
let paths = remove_ignored_files(from, &paths[..]);
119+
if paths.is_empty() {
120+
continue;
121+
}
122+
114123
let metadata = entry
115124
.path()
116125
.metadata()

src/utils/ignore.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use log::warn;
2+
use std::path::{Path, PathBuf};
3+
4+
pub fn remove_ignored_files(book_root: &Path, paths: &[PathBuf]) -> Vec<PathBuf> {
5+
if paths.is_empty() {
6+
return vec![];
7+
}
8+
9+
match find_ignorefile(book_root) {
10+
Some(gitignore_path) => {
11+
match gitignore::File::new(gitignore_path.as_path()) {
12+
Ok(exclusion_checker) => filter_ignored_files(exclusion_checker, paths),
13+
Err(_) => {
14+
// We're unable to read the .gitignore file, so we'll silently allow everything.
15+
// Please see discussion: https://github.com/rust-lang/mdBook/pull/1051
16+
paths.iter().map(|path| path.to_path_buf()).collect()
17+
}
18+
}
19+
}
20+
None => {
21+
// There is no .gitignore file.
22+
paths.iter().map(|path| path.to_path_buf()).collect()
23+
}
24+
}
25+
}
26+
27+
fn find_ignorefile(book_root: &Path) -> Option<PathBuf> {
28+
book_root
29+
.ancestors()
30+
.flat_map(|p| vec![p.join(".bookignore"), p.join(".gitignore")].into_iter())
31+
.find(|p| p.exists())
32+
}
33+
34+
fn filter_ignored_files(exclusion_checker: gitignore::File<'_>, paths: &[PathBuf]) -> Vec<PathBuf> {
35+
paths
36+
.iter()
37+
.filter(|path| match exclusion_checker.is_excluded(path) {
38+
Ok(exclude) => !exclude,
39+
Err(error) => {
40+
warn!(
41+
"Unable to determine if {:?} is excluded: {:?}. Including it.",
42+
&path, error
43+
);
44+
true
45+
}
46+
})
47+
.map(|path| path.to_path_buf())
48+
.collect()
49+
}

src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(missing_docs)] // FIXME: Document this
22

33
pub mod fs;
4+
pub mod ignore;
45
mod string;
56
pub(crate) mod toml_ext;
67
use crate::errors::Error;

0 commit comments

Comments
 (0)