Hacky .gitignore handling

This commit is contained in:
Matt Green 2016-09-14 16:50:14 -04:00
parent fdc6564bb1
commit 5b97663990
2 changed files with 41 additions and 9 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "dirwatcher"
version = "0.1.0"
version = "0.4.0"
authors = ["Matt Green <mattgreenrocks@gmail.com>"]
[dependencies]

View File

@ -5,6 +5,9 @@ use std::env;
use libc::system;
use notify::{Event, RecommendedWatcher, Watcher};
use std::ffi::CString;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::path::{Path,PathBuf};
use std::string::String;
use std::sync::mpsc::{channel, Receiver, RecvError};
@ -18,9 +21,12 @@ fn clear() {
}
fn ignored(relpath: &Path) -> bool {
if relpath.to_str().unwrap().starts_with(".") {
return true;
fn ignored(relpath: &Path, ignores: &Vec<String>) -> bool {
for i in ignores.iter() {
if relpath.to_str().unwrap().starts_with(i) {
//println!("Ignoring {} because {}", relpath.to_str().unwrap(), i);
return true;
}
}
false
@ -33,14 +39,33 @@ fn invoke(cmd: &String) {
}
}
fn wait(rx: &Receiver<Event>, cwd: &PathBuf) -> Result<Event, RecvError> {
fn read_gitignore(path: &str) -> Result<Vec<String>, std::io::Error> {
let f = try!(File::open(path));
let reader = BufReader::new(f);
let mut entries = vec![];
for line in reader.lines() {
let l = try!(line).trim().to_string();
if l.starts_with("#") || l.len() == 0 {
continue;
}
//println!("Read {}", l);
entries.push(l);
}
Ok(entries)
}
fn wait(rx: &Receiver<Event>, cwd: &PathBuf, ignore: &Vec<String>) -> Result<Event, RecvError> {
loop {
let e = try!(rx.recv());
let ignored = match e.path {
Some(ref path) => {
let stripped = path.strip_prefix(cwd).unwrap();
ignored(stripped)
ignored(stripped, &ignore)
},
None => false
};
@ -65,6 +90,13 @@ fn main() {
let cmd = env::args().nth(1).expect("Argument 1 needs to be a command");
let cwd = env::current_dir().unwrap();
let mut ignored = vec![];
ignored.push(String::from("."));
match read_gitignore(".gitignore") {
Ok(gitignores) => ignored.extend(gitignores),
Err(_) => ()
}
let (tx, rx) = channel();
let mut watcher: RecommendedWatcher = Watcher::new(tx)
.expect("unable to create watcher");
@ -72,11 +104,11 @@ fn main() {
.expect("unable to start watching directory");
loop {
clear();
let e = wait(&rx, &cwd)
//clear();
let e = wait(&rx, &cwd, &ignored)
.expect("error when waiting for filesystem changes");
println!("{:?} {:?}", e.op, e.path);
//println!("{:?} {:?}", e.op, e.path);
invoke(&cmd);
}
}