From 0d85d8275aa3e0c0bf99da97c04c352734816038 Mon Sep 17 00:00:00 2001 From: Matt Green Date: Tue, 8 Nov 2016 17:52:15 -0500 Subject: [PATCH] Fix gitignore searching not terminating in some cases; closes #25 --- src/main.rs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 79e5fb4..e242589 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,21 +39,27 @@ use notification_filter::NotificationFilter; use process::{Process, ProcessReaper}; use watcher::{Event, Watcher}; -// Starting at the specified path, search for gitignore files, -// stopping at the first one found. -fn find_gitignore_file(path: &Path) -> Option { - let mut gitignore_path = path.join(".gitignore"); - if gitignore_path.exists() { - return Some(gitignore_path); - } +fn find_gitignore(path: &Path) -> Option { + let mut p = path.to_owned(); - let p = path.to_owned(); - - while let Some(p) = p.parent() { - gitignore_path = p.join(".gitignore"); + loop { + let gitignore_path = p.join(".gitignore"); if gitignore_path.exists() { return Some(gitignore_path); } + + // Stop if we see a .git directory + if let Ok(metadata) = p.join(".git").metadata() { + if metadata.is_dir() { + break; + } + } + + if p.parent().is_none() { + break; + } + + p.pop(); } None @@ -85,7 +91,7 @@ fn main() { let mut gitignore_file = None; if !args.no_vcs_ignore { - if let Some(gitignore_path) = find_gitignore_file(&cwd) { + if let Some(gitignore_path) = find_gitignore(&cwd) { debug!("Found .gitignore file: {:?}", gitignore_path); gitignore_file = gitignore::parse(&gitignore_path).ok();