summaryrefslogtreecommitdiffstats
path: root/src/git_ignore.rs
blob: edfb14ebd806e426ad7475ddee48fb7cd96941c0 (plain)
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
//! Implements parsing and applying .gitignore files.
//! Also manages a stack of such files, because more than one
//!  can apply for a dir (i.e when entering a directory we
//!  may add a gitignore file to the stack)
use glob;
use regex::Regex;
use std::fs::File;
use std::io::{BufRead, BufReader, Result};
use std::path::{Path, PathBuf};

/// a simple rule of a gitignore file
#[derive(Clone)]
struct GitIgnoreRule {
    ok: bool,        // does this rule when matched means the file is good? (usually false)
    directory: bool, // whether this rule only applies to directories
    filename: bool,  // does this rule apply to just the filename
    pattern: glob::Pattern,
    pattern_options: glob::MatchOptions,
}

impl GitIgnoreRule {
    fn from(line: &str, dir: &Path) -> Option<GitIgnoreRule> {
        lazy_static! {
            static ref RE: Regex = Regex::new(
                r"(?x)
                ^\s*
                (!)?    # 1 : negation
                (.+?)   # 2 : pattern
                (/)?    # 3 : directory
                \s*$
                "
            )
            .unwrap();
        }
        if line.starts_with('#') {
            return None; // comment line
        }
        if let Some(c) = RE.captures(line) {
            if let Some(p) = c.get(2) {
                let mut p = p.as_str().to_string();
                let has_separator = p.contains('/');
                if has_separator && p.starts_with('/') {
                    p = dir.to_string_lossy().to_string() + &p;
                }
                if let Ok(pattern) = glob::Pattern::new(&p) {
                    let pattern_options = glob::MatchOptions {
                        case_sensitive: true, // not really sure about this one
                        require_literal_leading_dot: false,
                        require_literal_separator: has_separator,
                    };
                    return Some(GitIgnoreRule {
                        ok: c.get(1).is_some(), // if negation
                        pattern,
                        directory: c.get(3).is_some(),
                        filename: !has_separator,
                        pattern_options,
                    });
                }
            }
        }
        None
    }
}

/// The rules of a gitignore file
#[derive(Clone)]
pub struct GitIgnoreFile {
    rules: Vec<GitIgnoreRule>,
}
impl GitIgnoreFile {
    pub fn new(path: &Path) -> Result<GitIgnoreFile> {
        let f = File::open(path)?;
        let parent = path.parent().unwrap();
        let mut rules: Vec<GitIgnoreRule> = Vec::new();
        for line in BufReader::new(f).lines() {
            if let Some(rule) = GitIgnoreRule::from(&line?, &parent) {
                rules.push(rule);
            }
        }
        // the last rule applicable to a path is the right one. So
        // we reverse the list to easily iterate from the last one to the first one
        rules.reverse();
        debug!(
            "loaded .gitignore file {:?} with {} rules",
            path,
            rules.len()
        );
        Ok(GitIgnoreFile { rules })
    }
}

/// A stack of the gitignore files applying to a directory.
pub struct GitIgnoreFilter {
    pub files: Vec<GitIgnoreFile>, // the last one is the deepest one
}
impl GitIgnoreFilter {
    pub fn applicable_to(path: &Path) -> GitIgnoreFilter {
        let mut filter = GitIgnoreFilter { files: Vec::new() };
        for ignore_file in GitIgnoreFilesFinder::for_dir(path) {
            match GitIgnoreFile::new(&ignore_file) {
                Ok(gif) => {
                    filter.files.push(gif);
                }
                Err(e) => {
                    info!("reading GIF failed: {:?}", e);
                }
            }
        }
        filter
    }
    pub fn extended_to(&self, dir: &Path) -> GitIgnoreFilter {
        let mut files = self.files.clone();
        let ignore_file = dir.join(".gitignore");
        if let Ok(gif) = GitIgnoreFile::new(&ignore_file) {
            files.push(gif);
        }
        GitIgnoreFilter { files }
    }
    pub fn accepts(&self, path: &Path, filename: &str, directory: bool) -> bool {
        for file in &self.files {
            for rule in &file.rules {
                if rule.directory && !directory {
                    continue;
                }
                let ok = if rule.filename {
                    rule.pattern.matches_with(filename, &rule.pattern_options)
                } else {
                    rule.pattern.matches_path_with(path, &rule.pattern_options)
                };
                if ok {
                    return rule.ok;
                }
            }
        }
        true
    }
}

/// an iterator to find all applicable git_ignore files
pub struct GitIgnoreFilesFinder<'a> {
    dir: &'a Path,
}
impl<'a> GitIgnoreFilesFinder<'a> {
    fn for_dir(dir: &'a Path) -> GitIgnoreFilesFinder<'a> {
        GitIgnoreFilesFinder { dir }
    }
}
impl<'a> Iterator for GitIgnoreFilesFinder<'a> {
    type Item = PathBuf; // I don't really see a way to deal with only &'a Path as join makes a PathBuf
    fn next(&mut self) -> Option<PathBuf> {
        loop {
            let ignore_file = self.dir.join(".gitignore");
            match self.dir.parent() {
                Some(parent) => {
                    self.dir = parent;
                    if ignore_file.exists() {
                        return Some(ignore_file);
                    }
                }
                None => {
                    return None;
                }
            }
        }
    }
}