diff options
author | Gabriel de Perthuis <g2p.code+github@gmail.com> | 2019-09-07 17:05:25 +0200 |
---|---|---|
committer | Matan Kushner <hello@matchai.me> | 2019-09-07 11:05:25 -0400 |
commit | 69ebab46a49c57b3d5c9e8e603c9eb0d9dd9cbca (patch) | |
tree | 75a4cf66901020e820a69a8a20fb3b9e0c7f7280 | |
parent | 61abe6dd7acc7bcf1b32a3b34d64fdb88a1feb87 (diff) |
fix: Don't trigger an extension match on hidden files (#299)
Addresses #52, closes #280.
-rw-r--r-- | src/context.rs | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/src/context.rs b/src/context.rs index 3a663da42..6ccda8495 100644 --- a/src/context.rs +++ b/src/context.rs @@ -177,20 +177,20 @@ pub fn path_has_name<'a>(dir_entry: &PathBuf, names: &'a [&'a str]) -> bool { } } -/// checks if pathbuf matches the extension provided +/// checks if pathbuf doesn't start with a dot and matches any provided extension pub fn has_extension<'a>(dir_entry: &PathBuf, extensions: &'a [&'a str]) -> bool { - let found_ext = extensions.iter().find(|ext| { - dir_entry - .extension() - .and_then(OsStr::to_str) - .unwrap_or_default() - == **ext - }); - - match found_ext { - Some(extension) => !extension.is_empty(), - None => false, + if let Some(file_name) = dir_entry.file_name() { + if file_name.to_string_lossy().starts_with('.') { + return false; + } + return extensions.iter().any(|ext| { + dir_entry + .extension() + .and_then(OsStr::to_str) + .map_or(false, |e| e == *ext) + }); } + false } fn get_current_branch(repository: &Repository) -> Option<String> { @@ -228,6 +228,9 @@ mod tests { buf.set_file_name("some-file.rs"); assert_eq!(has_extension(&buf, &extensions), false); + buf.set_file_name(".some-file.js"); + assert_eq!(has_extension(&buf, &extensions), false); + buf.set_file_name("some-file.js"); assert_eq!(has_extension(&buf, &extensions), true) } |