1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//! # Gitignored
//!
//! `gitignored` is a Rust implementation of gitignore algorithm. Compliant with the format defined [here](https://git-scm.com/docs/gitignore).

use globset::GlobBuilder;
use regex::Regex;
use std::env;
use std::path::{Path, PathBuf};

fn first_char(string: &str) -> char {
    string.chars().nth(0).unwrap()
}

fn negate(string: &str) -> String {
    format!("!{}", string)
}

fn has_no_middle_separators(string: &str) -> bool {
    let segments: Vec<&str> = string.split("/").filter(|s| !s.is_empty()).collect();
    segments.len() <= 1
}

fn remove_whitespace(s: &str) -> String {
    s.chars().filter(|c| !c.is_whitespace()).collect()
}

#[derive(Debug)]
enum Match {
    Anywhere,
    Relative,
}

#[derive(Debug)]
enum PathKind {
    Dir,
    File,
    Both,
}

/// Represents a glob pattern and meta information about it.
pub struct Pattern {
    pub string: String,
    match_type: Match,
    path_kind: PathKind,
    negated: bool,
}

impl Pattern {
    /// Creates a new Pattern that can be passed to <a href="/struct.Gitignore.html#method.ignores_path">ignores_path</a>.
    /// Example:
    /// ```
    /// let ptn = Pattern::new("**/dist/*.js");
    /// ```
    pub fn new(glob: &str) -> Self {
        let has_extension = Regex::new(r"\.[^\*/\\]+$").unwrap();
        let negated = glob.starts_with("!");
        let without_neg = if negated { &glob[1..] } else { glob };
        let normalized_glob = remove_whitespace(without_neg);

        let match_type = if !normalized_glob.starts_with("**")
            && first_char(&normalized_glob) != '/'
            && has_no_middle_separators(&normalized_glob)
        {
            Match::Anywhere
        } else {
            Match::Relative
        };

        let path_kind = if has_extension.is_match(&normalized_glob) {
            PathKind::File
        } else {
            if normalized_glob.ends_with("/") {
                PathKind::Dir
            } else {
                PathKind::Both
            }
        };

        Self {
            string: String::from(normalized_glob),
            negated,
            match_type,
            path_kind,
        }
    }

    fn get_parents(&self) -> Vec<String> {
        let mut segments: Vec<&str> = self.string.split("/").collect();
        let mut parents: Vec<String> = Vec::new();
        while segments.len() > 1 {
            let mut joined = segments[..segments.len() - 1].join("/");
            joined.push_str("/");
            if joined.starts_with("/") {
                parents.push(joined);
            } else {
                parents.push(format!("/{}", joined));
                parents.push(joined);
            }
            segments.pop();
        }

        parents.into_iter().filter(|p| !p.is_empty()).collect()
    }
}

/// Used to match globs against user-provided paths.
pub struct Gitignore<P: AsRef<Path>> {
    /// Current working directory if created with `Gitignore::default()`.
    pub root: P,
    require_literal_separator: bool,
    case_insensitive: bool,
}

impl Default for Gitignore<PathBuf> {
    /// Creates a new instance using current working directory.
    fn default() -> Self {
        Self {
            root: env::current_dir().unwrap(),
            require_literal_separator: true,
            case_insensitive: true,
        }
    }
}

impl<P: AsRef<Path>> Gitignore<P> {
    /// Creates a new instance. Requires a path that serves as a root for all path calculations and
    /// matching options as defined in the <a href="https://docs.rs/glob/0.3.0/glob/">glob</a> crate.
    /// # Examples
    ///
    /// ```
    /// let cwd = env::current_dir().unwrap();
    /// let ig = Gitignore::new(cwd, true, true);
    /// ```
    pub fn new(root: P, require_literal_separator: bool, case_insensitive: bool) -> Gitignore<P> {
        Gitignore {
            root,
            require_literal_separator,
            case_insensitive,
        }
    }

    fn make_relative(&mut self, p: &str) -> String {
        self.require_literal_separator = true;
        let root_str = self.root.as_ref().display();
        let mut unformatted = p;

        if unformatted.ends_with("*") {
            unformatted = &p[..p.len() - 1];
        }

        if p.starts_with("**/") {
            return String::from(unformatted);
        }

        if p.starts_with("/") {
            return format!("{}{}", root_str, unformatted);
        }

        format!("{}/{}", root_str, unformatted)
    }

    fn make_matchable_anywhere(&mut self, p: &str) -> String {
        self.require_literal_separator = false;
        let mut unformatted = p;
        let root_str = self.root.as_ref().display();

        if unformatted.ends_with("*") {
            unformatted = &p[..p.len() - 1];
        }

        format!("{}{}{}", root_str, "/**/", unformatted)
    }

    fn make_full_path(&mut self, glob: &Pattern, from: &str) -> String {
        match (&glob.path_kind, &glob.match_type) {
            (PathKind::Both, Match::Anywhere) => self.make_matchable_anywhere(from) + "*",
            (PathKind::File, Match::Anywhere) => self.make_matchable_anywhere(from),
            (PathKind::Dir, Match::Anywhere) => self.make_matchable_anywhere(from) + "**/*",
            (PathKind::Both, Match::Relative) => self.make_relative(from) + "*",
            (PathKind::File, Match::Relative) => self.make_relative(from),
            (PathKind::Dir, Match::Relative) => self.make_relative(from) + "**/*",
        }
    }

    fn make_relative_to_root(&mut self, glob: &Pattern, from: &str) -> String {
        match (&glob.path_kind, &glob.match_type) {
            (PathKind::Both, _) => self.make_relative(from) + "*",
            (PathKind::File, _) => self.make_relative(from),
            (PathKind::Dir, _) => self.make_relative(from) + "**/*",
        }
    }

    fn find_ignored_dirs(&self, lines: &[&str]) -> Vec<String> {
        let mut ignored_dirs: Vec<String> = Vec::new();

        for line in lines.iter() {
            let glob = Pattern::new(line);
            let parents: Vec<String> = glob.get_parents().into_iter().map(|p| negate(&p)).collect();
            let has_negated_parents = parents.iter().any(|p| lines.contains(&&p[..]));

            // Disallow re-include by negation if parent dir is ignored unless the same parent is negated, with or without /
            match glob.path_kind {
                PathKind::Both | PathKind::Dir => {
                    if !glob.negated && !has_negated_parents {
                        ignored_dirs.push(glob.string);
                    }
                }
                _ => (),
            }
        }

        ignored_dirs
    }

    /// Checks if the target is ignored by provided list of gitignore patterns.
    ///
    /// # Examples
    ///
    /// ```
    /// let globs = vec!["lib/*.js", "!lib/include.js"];
    /// assert!(!ig.ignores(&globs, ig.root.join("lib/include.js")));
    /// ```
    pub fn ignores(&mut self, lines: &[&str], target: impl AsRef<Path>) -> bool {
        let ignored_dirs = self.find_ignored_dirs(lines);

        let mut is_ignored = false;

        for line in lines.iter() {
            let glob = Pattern::new(line);

            let has_ignored_parent = ignored_dirs.iter().any(|dir| {
                let long_glob = self.make_relative_to_root(&glob, dir);
                let matcher = GlobBuilder::new(&long_glob)
                    .literal_separator(self.require_literal_separator)
                    .case_insensitive(self.case_insensitive)
                    .build()
                    .unwrap()
                    .compile_matcher();
                matcher.is_match(target.as_ref())
            });

            // Early return because nothing can re-include it
            if has_ignored_parent {
                return true;
            }

            // Avoid being re-included by irrelevant globs
            if is_ignored && !glob.negated {
                return true;
            }

            let full_path = self.make_full_path(&glob, &glob.string);
            let matcher = GlobBuilder::new(&full_path)
                .literal_separator(self.require_literal_separator)
                .case_insensitive(self.case_insensitive)
                .build()
                .unwrap()
                .compile_matcher();
            let is_match = matcher.is_match(target.as_ref());

            is_ignored = if is_match { !glob.negated } else { is_ignored };
        }

        is_ignored
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn multiple_lines() {
        let mut ig = Gitignore::default();

        let a = vec!["lib/", "!lib/*.js"];
        let b = vec!["lib", "!lib/*.js"];
        let c = vec!["!lib/*.js", "lib"];
        let d = vec!["lib/", "!lib/deep/include.js"];
        let e = vec!["/lib/", "!/lib/deep/"];

        let f = vec!["lib/", "!/lib/"];

        let g = vec!["!/lib/", "lib/"];
        let h = vec!["**/remove-items.js"];
        let i = vec!["remove-items*"];
        let j = vec!["remove*, !remove-items.js"];

        let k = vec!["lib/*.js", "!lib/include.js"];
        let l = vec!["lib/*.js", "!lib/"];
        let m = vec!["lib/", "!lib/"];
        let n = vec!["lib/", "!/lib/"];

        let o = vec!["*.js", "!lib.js"];
        let p = vec!["src/*.js", "target/"];

        assert!(ig.ignores(&a, ig.root.join("lib/include.js")));

        assert!(ig.ignores(&c, ig.root.join("lib/include.js")));
        assert!(ig.ignores(&d, ig.root.join("lib/deep/include.js")));
        assert!(ig.ignores(&e, ig.root.join("lib/deep/include.js")));

        assert!(ig.ignores(&g, ig.root.join("deep/lib/include.js")));
        assert!(ig.ignores(&h, ig.root.join("deep/lib/remove-items.js")));
        assert!(ig.ignores(&i, ig.root.join("deep/lib/remove-items.js")));
        assert!(ig.ignores(&p, ig.root.join("src/lib.js")));

        assert!(!ig.ignores(&j, ig.root.join("deep/lib/remove-items.js")));
        assert!(!ig.ignores(&k, ig.root.join("lib/include.js")));
        assert!(!ig.ignores(&l, ig.root.join("lib/include.js")));
        assert!(!ig.ignores(&m, ig.root.join("lib/include.js")));
        assert!(!ig.ignores(&n, ig.root.join("lib/include.js")));
        assert!(!ig.ignores(&o, ig.root.join("src/lib.js")));
        assert!(!ig.ignores(&b, ig.root.join("lib/include.js")));

        assert!(ig.ignores(&d, ig.root.join("lib/deep/ignored.js")));
        assert!(ig.ignores(&f, ig.root.join("deep/lib/include.js")));
    }
}