2016-10-12 04:43:53 +02:00
|
|
|
extern crate glob;
|
|
|
|
|
|
|
|
use std::fs;
|
|
|
|
use std::io;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
2016-10-12 15:06:01 +02:00
|
|
|
// Immutable, ordered set of Patterns
|
|
|
|
// Used to implement whitelisting
|
|
|
|
pub struct PatternSet {
|
2016-10-24 02:12:48 +02:00
|
|
|
patterns: Vec<Pattern>,
|
2016-10-12 04:43:53 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 15:06:01 +02:00
|
|
|
// Represents a single gitignore rule
|
|
|
|
//
|
|
|
|
// Currently we ignore rules about whether to match
|
|
|
|
// only a directory since it's a bit weird for what
|
|
|
|
// we want to use a gitignore file for.
|
2016-10-12 04:43:53 +02:00
|
|
|
struct Pattern {
|
|
|
|
pattern: glob::Pattern,
|
|
|
|
str: String,
|
|
|
|
root: PathBuf,
|
2016-10-12 15:06:01 +02:00
|
|
|
whitelist: bool,
|
2016-10-12 04:43:53 +02:00
|
|
|
#[allow(dead_code)]
|
|
|
|
directory: bool,
|
2016-10-24 02:12:48 +02:00
|
|
|
anchored: bool,
|
2016-10-12 04:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
Glob(glob::PatternError),
|
|
|
|
Io(io::Error),
|
|
|
|
}
|
|
|
|
|
2016-10-12 15:06:01 +02:00
|
|
|
pub fn parse(path: &Path) -> Result<PatternSet, Error> {
|
|
|
|
let mut file = try!(fs::File::open(path));
|
|
|
|
let mut contents = String::new();
|
|
|
|
try!(file.read_to_string(&mut contents));
|
|
|
|
|
|
|
|
// If we've opened the file, we'll have at least one other path component
|
|
|
|
let root = path.parent().unwrap();
|
2016-10-24 02:12:48 +02:00
|
|
|
let patterns = try!(contents.lines()
|
2016-10-12 15:06:01 +02:00
|
|
|
.filter(|l| !l.is_empty())
|
|
|
|
.filter(|l| !l.starts_with("#"))
|
|
|
|
.map(|l| Pattern::new(l, root))
|
|
|
|
.collect());
|
|
|
|
|
|
|
|
Ok(PatternSet::new(patterns))
|
|
|
|
}
|
2016-10-12 04:43:53 +02:00
|
|
|
|
2016-10-12 15:06:01 +02:00
|
|
|
impl PatternSet {
|
|
|
|
fn new(patterns: Vec<Pattern>) -> PatternSet {
|
2016-10-24 02:12:48 +02:00
|
|
|
PatternSet { patterns: patterns }
|
2016-10-12 04:43:53 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 15:06:01 +02:00
|
|
|
// Apply the patterns to the path one-by-one
|
|
|
|
//
|
|
|
|
// If there are whitelisting, we need to run through the whole set.
|
|
|
|
// Otherwise, we can stop at the first exclusion.
|
2016-10-12 04:43:53 +02:00
|
|
|
pub fn is_excluded(&self, path: &Path) -> bool {
|
|
|
|
let mut excluded = false;
|
2016-10-12 15:06:01 +02:00
|
|
|
let has_whitelistings = self.patterns.iter().any(|p| p.whitelist);
|
2016-10-12 04:43:53 +02:00
|
|
|
|
2016-10-18 15:39:40 +02:00
|
|
|
for pattern in &self.patterns {
|
2016-10-12 04:43:53 +02:00
|
|
|
let matched = pattern.matches(path);
|
|
|
|
|
|
|
|
if matched {
|
2016-10-12 15:06:01 +02:00
|
|
|
if pattern.whitelist {
|
2016-10-12 04:43:53 +02:00
|
|
|
excluded = false;
|
2016-10-24 02:12:48 +02:00
|
|
|
} else {
|
2016-10-12 04:43:53 +02:00
|
|
|
excluded = true;
|
2016-10-12 15:06:01 +02:00
|
|
|
|
|
|
|
// We can stop running rules in this case
|
|
|
|
if !has_whitelistings {
|
|
|
|
break;
|
|
|
|
}
|
2016-10-12 04:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
excluded
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Pattern {
|
|
|
|
fn new(pattern: &str, root: &Path) -> Result<Pattern, Error> {
|
|
|
|
let mut normalized = String::from(pattern);
|
|
|
|
|
2016-10-18 15:39:40 +02:00
|
|
|
let whitelisted = if normalized.starts_with('!') {
|
2016-10-12 04:43:53 +02:00
|
|
|
normalized.remove(0);
|
2016-10-18 15:39:40 +02:00
|
|
|
true
|
2016-10-24 02:12:48 +02:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2016-10-12 04:43:53 +02:00
|
|
|
|
2016-10-18 15:39:40 +02:00
|
|
|
let anchored = if normalized.starts_with('/') {
|
2016-10-12 04:43:53 +02:00
|
|
|
normalized.remove(0);
|
2016-10-18 15:39:40 +02:00
|
|
|
true
|
2016-10-24 02:12:48 +02:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2016-10-12 04:43:53 +02:00
|
|
|
|
2016-10-18 15:39:40 +02:00
|
|
|
let directory = if normalized.ends_with('/') {
|
2016-10-12 04:43:53 +02:00
|
|
|
normalized.pop();
|
2016-10-18 15:39:40 +02:00
|
|
|
true
|
2016-10-24 02:12:48 +02:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2016-10-12 04:43:53 +02:00
|
|
|
|
|
|
|
if normalized.starts_with("\\#") || normalized.starts_with("\\!") {
|
|
|
|
normalized.remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
let pat = try!(glob::Pattern::new(&normalized));
|
|
|
|
|
|
|
|
Ok(Pattern {
|
|
|
|
pattern: pat,
|
|
|
|
str: String::from(normalized),
|
|
|
|
root: root.to_path_buf(),
|
2016-10-12 15:06:01 +02:00
|
|
|
whitelist: whitelisted,
|
2016-10-12 04:43:53 +02:00
|
|
|
directory: directory,
|
2016-10-24 02:12:48 +02:00
|
|
|
anchored: anchored,
|
2016-10-12 04:43:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn matches(&self, path: &Path) -> bool {
|
|
|
|
let options = glob::MatchOptions {
|
|
|
|
case_sensitive: false,
|
|
|
|
require_literal_separator: true,
|
2016-10-24 02:12:48 +02:00
|
|
|
require_literal_leading_dot: false,
|
2016-10-12 04:43:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let stripped_path = match path.strip_prefix(&self.root) {
|
2016-10-24 02:12:48 +02:00
|
|
|
Ok(p) => p,
|
|
|
|
Err(_) => return false,
|
2016-10-12 04:43:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut result = false;
|
|
|
|
|
|
|
|
if self.anchored {
|
|
|
|
let first_component = stripped_path.iter().next();
|
|
|
|
result = match first_component {
|
2016-10-24 02:12:48 +02:00
|
|
|
Some(s) => self.pattern.matches_path_with(Path::new(&s), &options),
|
|
|
|
None => false,
|
2016-10-12 04:43:53 +02:00
|
|
|
}
|
2016-10-24 02:12:48 +02:00
|
|
|
} else if !self.str.contains('/') {
|
|
|
|
result = stripped_path.iter()
|
|
|
|
.any(|c| self.pattern.matches_path_with(Path::new(c), &options));
|
|
|
|
} else if self.pattern.matches_path_with(stripped_path, &options) {
|
2016-10-18 15:39:40 +02:00
|
|
|
result = true;
|
2016-10-12 04:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<glob::PatternError> for Error {
|
|
|
|
fn from(error: glob::PatternError) -> Error {
|
|
|
|
Error::Glob(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(error: io::Error) -> Error {
|
|
|
|
Error::Io(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 02:12:48 +02:00
|
|
|
// fn main() {
|
|
|
|
// let cwd = env::current_dir().unwrap();
|
|
|
|
// let gitignore_file = cwd.join(".gitignore");
|
|
|
|
// let file = File::new(&gitignore_file).unwrap();
|
2016-10-12 04:43:53 +02:00
|
|
|
|
2016-10-24 02:12:48 +02:00
|
|
|
// for arg in env::args().skip(1) {
|
|
|
|
// let path = cwd.join(&arg);
|
|
|
|
// let matches = file.is_excluded(&path);
|
|
|
|
// println!("File: {}, Excluded: {}", arg, matches);
|
|
|
|
// }
|
|
|
|
// }
|
2016-10-12 04:43:53 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Pattern;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
fn base_dir() -> PathBuf {
|
|
|
|
PathBuf::from("/home/user/dir")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_pattern(pattern: &str) -> Pattern {
|
|
|
|
Pattern::new(pattern, &base_dir()).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_matches_exact() {
|
|
|
|
let pattern = build_pattern("Cargo.toml");
|
|
|
|
|
|
|
|
assert!(pattern.matches(&base_dir().join("Cargo.toml")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_matches_simple_wildcard() {
|
|
|
|
let pattern = build_pattern("targ*");
|
|
|
|
|
|
|
|
assert!(pattern.matches(&base_dir().join("target")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_does_not_match() {
|
|
|
|
let pattern = build_pattern("Cargo.toml");
|
|
|
|
|
|
|
|
assert!(!pattern.matches(&base_dir().join("src").join("main.rs")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_matches_subdir() {
|
|
|
|
let pattern = build_pattern("target");
|
|
|
|
|
|
|
|
assert!(pattern.matches(&base_dir().join("target").join("file")));
|
|
|
|
assert!(pattern.matches(&base_dir().join("target").join("subdir").join("file")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_wildcard_with_dir() {
|
|
|
|
let pattern = build_pattern("target/f*");
|
|
|
|
|
|
|
|
assert!(pattern.matches(&base_dir().join("target").join("file")));
|
|
|
|
assert!(!pattern.matches(&base_dir().join("target").join("subdir").join("file")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_leading_slash() {
|
|
|
|
let pattern = build_pattern("/*.c");
|
|
|
|
|
|
|
|
assert!(pattern.matches(&base_dir().join("cat-file.c")));
|
|
|
|
assert!(!pattern.matches(&base_dir().join("mozilla-sha1").join("sha1.c")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_leading_double_wildcard() {
|
|
|
|
let pattern = build_pattern("**/foo");
|
|
|
|
|
|
|
|
assert!(pattern.matches(&base_dir().join("foo")));
|
|
|
|
assert!(pattern.matches(&base_dir().join("target").join("foo")));
|
|
|
|
assert!(pattern.matches(&base_dir().join("target").join("subdir").join("foo")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_trailing_double_wildcard() {
|
|
|
|
let pattern = build_pattern("abc/**");
|
|
|
|
|
|
|
|
assert!(!pattern.matches(&base_dir().join("def").join("foo")));
|
|
|
|
assert!(pattern.matches(&base_dir().join("abc").join("foo")));
|
|
|
|
assert!(pattern.matches(&base_dir().join("abc").join("subdir").join("foo")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sandwiched_double_wildcard() {
|
|
|
|
let pattern = build_pattern("a/**/b");
|
|
|
|
|
|
|
|
assert!(pattern.matches(&base_dir().join("a").join("b")));
|
|
|
|
assert!(pattern.matches(&base_dir().join("a").join("x").join("b")));
|
|
|
|
assert!(pattern.matches(&base_dir().join("a").join("x").join("y").join("b")));
|
|
|
|
}
|
|
|
|
|
2016-10-12 15:06:01 +02:00
|
|
|
use super::PatternSet;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty_pattern_set_never_excludes() {
|
|
|
|
let set = PatternSet::new(vec![]);
|
|
|
|
|
|
|
|
assert!(!set.is_excluded(&base_dir().join("target")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_set_tests_all_patterns() {
|
|
|
|
let patterns = vec![build_pattern("target"), build_pattern("target2")];
|
|
|
|
let set = PatternSet::new(patterns);
|
|
|
|
|
|
|
|
assert!(set.is_excluded(&base_dir().join("target").join("foo.txt")));
|
|
|
|
assert!(set.is_excluded(&base_dir().join("target2").join("bar.txt")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_set_handles_whitelisting() {
|
|
|
|
let patterns = vec![build_pattern("target"), build_pattern("!target/foo.txt")];
|
|
|
|
let set = PatternSet::new(patterns);
|
|
|
|
|
|
|
|
assert!(!set.is_excluded(&base_dir().join("target").join("foo.txt")));
|
|
|
|
}
|
2016-10-12 04:43:53 +02:00
|
|
|
}
|