1
1
use crate :: { get_book_dir, open} ;
2
2
use clap:: { arg, App , Arg , ArgMatches } ;
3
+ use ignore:: gitignore:: Gitignore ;
3
4
use mdbook:: errors:: Result ;
4
5
use mdbook:: utils;
5
6
use mdbook:: MDBook ;
@@ -75,16 +76,7 @@ fn remove_ignored_files(book_root: &Path, paths: &[PathBuf]) -> Vec<PathBuf> {
75
76
}
76
77
77
78
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
- }
79
+ Some ( gitignore_path) => filter_ignored_files ( & Gitignore :: new ( gitignore_path) . 0 , paths) ,
88
80
None => {
89
81
// There is no .gitignore file.
90
82
paths. iter ( ) . map ( |path| path. to_path_buf ( ) ) . collect ( )
@@ -99,18 +91,14 @@ fn find_gitignore(book_root: &Path) -> Option<PathBuf> {
99
91
. find ( |p| p. exists ( ) )
100
92
}
101
93
102
- fn filter_ignored_files ( exclusion_checker : gitignore :: File , paths : & [ PathBuf ] ) -> Vec < PathBuf > {
94
+ fn filter_ignored_files ( ignore : & Gitignore , paths : & [ PathBuf ] ) -> Vec < PathBuf > {
103
95
paths
104
96
. 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
- }
97
+ . filter ( |path| {
98
+ let canonicalized = path. canonicalize ( ) ;
99
+ let p = canonicalized. as_ref ( ) . unwrap_or ( path) ;
100
+
101
+ !ignore. matched_path_or_any_parents ( p, p. is_dir ( ) ) . is_ignore ( )
114
102
} )
115
103
. map ( |path| path. to_path_buf ( ) )
116
104
. collect ( )
0 commit comments