Skip to content
Myr

ignore: refactor `is_hidden`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This does the more sensible thing of putting the implementation within the routine instead of repeating the docs. We also rename this routine since we're going to want a path variant for incremental checking.

authored by Andrew Gallant · committed by Andrew Gallant · parent c035d23 · Browse files · 2 files+47 added−42 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+47 added−42 removed

  • other:+47 added−42 removed

2 files+47 added−42 removed

modified:crates/ignore/src/dir.rs+2 added-2 removed
23 unmodified lines
2425use crate::{26 gitignore::{self, Gitignore, GitignoreBuilder},27 overrides::{self, Override},28 pathutil::{is_hidden, strip_prefix},28 pathutil::{is_hidden_entry, strip_prefix},29 types::{self, Types},30 walk::DirEntry,31 {Error, Match, PartialErrorBuilder},32};
453 unmodified lines
486 &'a self,487 dent: &DirEntry,488 ) -> Match<IgnoreMatch<'a>> {489 let m = self.matched(dent.path(), dent.is_dir());490 if m.is_none() && self.inner.opts.hidden && is_hidden(dent) {490 if m.is_none() && self.inner.opts.hidden && is_hidden_entry(dent) {491 return Match::Ignore(IgnoreMatch::hidden());492 }493 m494 }
1114 unmodified lines
modified:crates/ignore/src/pathutil.rs+45 added-40 removed
1use std::{ffi::OsStr, path::Path};23use crate::walk::DirEntry;45/// Returns true if and only if this entry is considered to be hidden.5/// Returns true if and only if this directory entry is considered to be6/// hidden.7///7/// This only returns true if the base name of the path starts with a `.`.8/// # Platform behavior9///9/// On Unix, this implements a more optimized check.10#[cfg(unix)]11pub(crate) fn is_hidden(dent: &DirEntry) -> bool {12 use std::os::unix::ffi::OsStrExt;1314 if let Some(name) = file_name(dent.path()) {15 name.as_bytes().get(0) == Some(&b'.')16 } else {17 false18 }19}2021/// Returns true if and only if this entry is considered to be hidden.10/// ## Windows11///23/// On Windows, this returns true if one of the following is true:12/// This returns true if one of the following is true:13///14/// * The base name of the path starts with a `.`.15/// * The file attributes have the `HIDDEN` property set.27#[cfg(windows)]28pub(crate) fn is_hidden(dent: &DirEntry) -> bool {29 use std::os::windows::fs::MetadataExt;30 use winapi_util::file;16///17/// ## All other platforms18///19/// This only returns true if the base name of the path starts with a `.`.20pub(crate) fn is_hidden_entry(dent: &DirEntry) -> bool {21 #[cfg(unix)]22 fn imp(dent: &DirEntry) -> bool {23 use std::os::unix::ffi::OsStrExt;2432 // This looks like we're doing an extra stat call, but on Windows, the33 // directory traverser reuses the metadata retrieved from each directory34 // entry and stores it on the DirEntry itself. So this is "free."35 if let Ok(md) = dent.metadata() {36 if file::is_hidden(md.file_attributes() as u64) {37 return true;25 if let Some(name) = file_name(dent.path()) {26 name.as_bytes().get(0) == Some(&b'.')27 } else {28 false29 }30 }3132 #[cfg(windows)]33 fn imp(dent: &DirEntry) -> bool {34 use std::os::windows::fs::MetadataExt;35 use winapi_util::file;3637 // This looks like we're doing an extra stat call, but on Windows, the38 // directory traverser reuses the metadata retrieved from each directory39 // entry and stores it on the DirEntry itself. So this is "free."40 if let Ok(md) = dent.metadata() {41 if file::is_hidden(md.file_attributes() as u64) {42 return true;43 }44 }40 if let Some(name) = file_name(dent.path()) {41 name.to_str().map(|s| s.starts_with(".")).unwrap_or(false)42 } else {43 false45 if let Some(name) = file_name(dent.path()) {46 name.to_str().map(|s| s.starts_with(".")).unwrap_or(false)47 } else {48 false49 }50 }45}5147/// Returns true if and only if this entry is considered to be hidden.48///49/// This only returns true if the base name of the path starts with a `.`.50#[cfg(not(any(unix, windows)))]51pub(crate) fn is_hidden(dent: &DirEntry) -> bool {52 if let Some(name) = file_name(dent.path()) {53 name.to_str().map(|s| s.starts_with(".")).unwrap_or(false)54 } else {55 false52 #[cfg(not(any(unix, windows)))]53 fn imp(dent: &DirEntry) -> bool {54 if let Some(name) = file_name(dent.path()) {55 name.to_str().map(|s| s.starts_with(".")).unwrap_or(false)56 } else {57 false58 }59 }6061 imp(dent)62}6364/// Strip `prefix` from the `path` and return the remainder.65///
82 unmodified lines