From 69ebab46a49c57b3d5c9e8e603c9eb0d9dd9cbca Mon Sep 17 00:00:00 2001 From: Gabriel de Perthuis Date: Sat, 7 Sep 2019 17:05:25 +0200 Subject: [PATCH] fix: Don't trigger an extension match on hidden files (#299) Addresses #52, closes #280. --- src/context.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/context.rs b/src/context.rs index 3a663da4..6ccda849 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 { @@ -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) }