diff options
author | xenozoid <xenozoid@gmail.com> | 2019-06-05 09:16:24 +0300 |
---|---|---|
committer | xenozoid <xenozoid@gmail.com> | 2019-06-05 09:16:24 +0300 |
commit | cebfc18939cb3dd3b06469510e63adc1fc77d385 (patch) | |
tree | 145df783206e795475305e5e2bd03955c1df88d6 | |
parent | 274eb3c9f256bf1b868ae78f4b953f7182207443 (diff) | |
parent | a749743455ea9b36406f509575e7bdde4aeee070 (diff) |
Merge remote-tracking branch 'origin/master'0.6.3
-rw-r--r-- | src/gitignore.rs | 93 | ||||
-rw-r--r-- | src/parser.rs | 1 |
2 files changed, 42 insertions, 52 deletions
diff --git a/src/gitignore.rs b/src/gitignore.rs index 458becc..36bc14e 100644 --- a/src/gitignore.rs +++ b/src/gitignore.rs @@ -32,7 +32,8 @@ pub fn matches_gitignore_filter(gitignore_filters: &Option<Vec<GitignoreFilter>> continue; } - let is_match = gitignore_filter.regex.is_match(file_name); + let file_name_prepared = convert_file_name_for_matcher(file_name); + let is_match = gitignore_filter.regex.is_match(&file_name_prepared); if is_match && gitignore_filter.negate { return false; @@ -49,6 +50,18 @@ pub fn matches_gitignore_filter(gitignore_filters: &Option<Vec<GitignoreFilter>> } } +fn convert_file_name_for_matcher(file_name: &str) -> String { + #[cfg(windows)] + { + return String::from(file_name).replace("\\", "/"); + } + + #[cfg(not(windows))] + { + return String::from(file_name); + } +} + pub fn parse_gitignore(file_path: &Path, dir_path: &Path) -> Vec<GitignoreFilter> { let mut result = vec![]; @@ -114,15 +127,7 @@ fn convert_gitignore_pattern(pattern: &str, file_path: &Path) -> Vec<GitignoreFi result.push(GitignoreFilter::new(regex.unwrap(), true, negate)); } - #[cfg(not(windows))] - { - pattern = pattern.add("/**"); - } - - #[cfg(windows)] - { - pattern = pattern.add("\\\\**"); - } + pattern = pattern.add("/**"); } let regex = convert_gitignore_glob(&pattern, file_path); @@ -134,45 +139,31 @@ fn convert_gitignore_pattern(pattern: &str, file_path: &Path) -> Vec<GitignoreFi } fn convert_gitignore_glob(glob: &str, file_path: &Path) -> Result<Regex, Error> { - #[cfg(not(windows))] - { - let replace_regex = Regex::new("(\\*\\*|\\?|\\.|\\*)").unwrap(); - let mut pattern = replace_regex.replace_all(&glob, |c: &Captures| { - match c.index(0) { - "**" => ".*", - "." => "\\.", - "*" => "[^/]*", - "?" => "[^/]+", - _ => panic!("Error parsing pattern") - }.to_string() - }).to_string(); - - pattern = file_path.to_string_lossy().to_string() - .replace("\\", "\\\\") - .add("/([^/]+/)*").add(&pattern); - - Regex::new(&pattern) - } + let replace_regex = Regex::new("(\\*\\*|\\?|\\.|\\*)").unwrap(); + let mut pattern = replace_regex.replace_all(&glob, |c: &Captures| { + match c.index(0) { + "**" => ".*", + "." => "\\.", + "*" => "[^/]*", + "?" => "[^/]+", + _ => panic!("Error parsing pattern") + }.to_string() + }).to_string(); + + while pattern.starts_with("/") || pattern.starts_with("\\") { + pattern.remove(0); + } + + pattern = file_path.to_string_lossy().to_string() + .replace("\\", "\\\\") + .add("/([^/]+/)*").add(&pattern); #[cfg(windows)] - { - let replace_regex = Regex::new("(\\*\\*|\\?|\\.|\\*)").unwrap(); - let mut pattern = replace_regex.replace_all(&glob, |c: &Captures| { - match c.index(0) { - "**" => ".*", - "." => "\\.", - "*" => "[^\\\\]*", - "?" => "[^\\\\]+", - _ => panic!("Error parsing pattern") - }.to_string() - }).to_string(); - - pattern = file_path.to_string_lossy().to_string() - .replace("\\", "\\\\") - .add("\\\\([^\\\\]+\\\\)*").add(&pattern); - - Regex::new(&pattern) - } + { + pattern = pattern.replace("\\", "/").replace("//", "/"); + } + + Regex::new(&pattern) } #[cfg(test)] @@ -252,7 +243,7 @@ mod tests { let filter = &result[0]; - assert_eq!(filter.regex.as_str(), "C:\\\\Projects\\\\testprj\\\\([^\\\\]+\\\\)*foo"); + assert_eq!(filter.regex.as_str(), "C:/Projects/testprj/([^/]+/)*foo"); assert_eq!(filter.only_dir, false); assert_eq!(filter.negate, false); } @@ -269,13 +260,13 @@ mod tests { let filter = &result[0]; - assert_eq!(filter.regex.as_str(), "C:\\\\Projects\\\\testprj\\\\([^\\\\]+\\\\)*foo"); + assert_eq!(filter.regex.as_str(), "C:/Projects/testprj/([^/]+/)*foo"); assert_eq!(filter.only_dir, true); assert_eq!(filter.negate, false); let filter = &result[1]; - assert_eq!(filter.regex.as_str(), "C:\\\\Projects\\\\testprj\\\\([^\\\\]+\\\\)*foo\\\\.*"); + assert_eq!(filter.regex.as_str(), "C:/Projects/testprj/([^/]+/)*foo/.*"); assert_eq!(filter.only_dir, false); assert_eq!(filter.negate, false); } @@ -292,7 +283,7 @@ mod tests { let filter = &result[0]; - assert_eq!(filter.regex.as_str(), "C:\\\\Projects\\\\testprj\\\\([^\\\\]+\\\\)*foo"); + assert_eq!(filter.regex.as_str(), "C:/Projects/testprj/([^/]+/)*foo"); assert_eq!(filter.only_dir, false); assert_eq!(filter.negate, true); } diff --git a/src/parser.rs b/src/parser.rs index 446fa5f..378ced2 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -466,7 +466,6 @@ impl Parser { if let Some(Lexem::Order) = self.get_lexem() { if let Some(Lexem::By) = self.get_lexem() { loop { - use std::str::FromStr; match self.get_lexem() { Some(Lexem::Comma) => {}, Some(Lexem::RawString(ref ordering_field)) => { |