Skip to content

Commit

Permalink
refactor: rename GitIgnorer into Ignorer
Browse files Browse the repository at this point in the history
because it doesn't handle just gitignore
  • Loading branch information
Canop committed Jan 12, 2025
1 parent f423190 commit 3611b0a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 43 deletions.
1 change: 1 addition & 0 deletions bacon.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ command = [
"-A", "clippy::neg_multiply",
"-A", "clippy::vec_init_then_push",
"-W", "clippy::explicit_iter_loop",
"-A", "clippy::unnecessary_map_or",
]
need_stdout = false

Expand Down
2 changes: 1 addition & 1 deletion src/command/panel_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl PanelInput {
}
if !verb.file_extensions.is_empty() {
let extension = sel_info.extension();
if !extension.map_or(false, |ext| verb.file_extensions.iter().any(|ve| ve == ext)) {
if !extension.is_some_and(|ext| verb.file_extensions.iter().any(|ve| ve == ext)) {
continue;
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/command/parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl CommandParts {
pub fn has_not_empty_verb_invocation(&self) -> bool {
self.verb_invocation
.as_ref()
.map_or(false, |vi| !vi.is_empty())
.is_some_and(|vi| !vi.is_empty())
}
pub fn from<S: Into<String>>(raw: S) -> Self {
let mut raw = raw.into();
Expand All @@ -41,8 +41,7 @@ impl CommandParts {
// we loop on chars and build the pattern tree until we reach an unescaped ' ' or ':'
while let Some((pos, cur_char)) = chars.next() {
let between_slashes = pt.current_atom()
.map_or(
false,
.is_some_and(
|pp: &PatternParts| pp.is_between_slashes(),
);
match cur_char {
Expand Down
60 changes: 30 additions & 30 deletions src/git/ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,35 @@ use {


#[derive(Default)]
pub struct GitIgnorer {
files: Arena<GitIgnoreFile>,
pub struct Ignorer {
files: Arena<IgnoreFile>,
}
#[derive(Debug, Clone, Default)]
pub struct GitIgnoreChain {
pub struct IgnoreChain {
in_repo: bool,
file_ids: Vec<Id<GitIgnoreFile>>,
file_ids: Vec<Id<IgnoreFile>>,
}
/// The rules of a gitignore file
#[derive(Debug, Clone)]
pub struct GitIgnoreFile {
rules: Vec<GitIgnoreRule>,
pub struct IgnoreFile {
rules: Vec<IgnoreRule>,
/// whether this is a git dedicated file (as opposed to a .ignore file)
git: bool,
local_git_ignore: bool,
}
/// a simple rule of a gitignore file
#[derive(Clone)]
struct GitIgnoreRule {
struct IgnoreRule {
ok: bool, // does this rule when matched means the file is good? (usually false)
directory: bool, // whether this rule only applies to directories
filename: bool, // does this rule apply to just the filename
pattern: glob::Pattern,
pattern_options: glob::MatchOptions,
}

impl fmt::Debug for GitIgnoreRule {
impl fmt::Debug for IgnoreRule {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GitIgnoreRule")
f.debug_struct("IgnoreRule")
.field("ok", &self.ok)
.field("directory", &self.directory)
.field("filename", &self.filename)
Expand All @@ -55,10 +55,10 @@ impl fmt::Debug for GitIgnoreRule {
}
}

impl GitIgnoreRule {
impl IgnoreRule {
/// parse a line of a .gitignore file.
/// The ref_dir is used if the line starts with '/'
fn from(line: &str, ref_dir: &Path) -> Option<GitIgnoreRule> {
fn from(line: &str, ref_dir: &Path) -> Option<IgnoreRule> {
if line.starts_with('#') {
return None; // comment line
}
Expand Down Expand Up @@ -91,7 +91,7 @@ impl GitIgnoreRule {
require_literal_leading_dot: false,
require_literal_separator: has_separator,
};
return Some(GitIgnoreRule {
return Some(IgnoreRule {
ok: c.get(1).is_some(), // if negation
pattern,
directory: c.get(3).is_some(),
Expand All @@ -109,7 +109,7 @@ impl GitIgnoreRule {
}
}

impl GitIgnoreFile {
impl IgnoreFile {
/// build a new gitignore file, from either a global ignore file or
/// a .gitignore file found inside a git repository.
/// The ref_dir is either:
Expand All @@ -119,30 +119,30 @@ impl GitIgnoreFile {
file_path: &Path,
ref_dir: &Path,
local_git_ignore: bool,
) -> Result<GitIgnoreFile> {
) -> Result<IgnoreFile> {
let f = File::open(file_path)?;
let git = file_path.file_name().map_or(false, |f| f == ".gitignore");
let mut rules: Vec<GitIgnoreRule> = Vec::new();
let mut rules: Vec<IgnoreRule> = Vec::new();
for line in BufReader::new(f).lines() {
if let Some(rule) = GitIgnoreRule::from(&line?, ref_dir) {
if let Some(rule) = IgnoreRule::from(&line?, ref_dir) {
rules.push(rule);
}
}
// the last rule applicable to a path is the right one. So
// we reverse the list to easily iterate from the last one to the first one
rules.reverse();
Ok(GitIgnoreFile {
Ok(IgnoreFile {
git,
rules,
local_git_ignore,
})
}
/// return the global gitignore file interpreted for
/// the given repo dir
pub fn global(repo_dir: &Path) -> Option<GitIgnoreFile> {
pub fn global(repo_dir: &Path) -> Option<IgnoreFile> {
static GLOBAL_GI_PATH: Lazy<Option<PathBuf>> = Lazy::new(find_global_ignore);
if let Some(path) = &*GLOBAL_GI_PATH {
GitIgnoreFile::new(path, repo_dir, true).ok()
IgnoreFile::new(path, repo_dir, true).ok()
} else {
None
}
Expand All @@ -162,19 +162,19 @@ pub fn find_global_ignore() -> Option<PathBuf> {
})
}

impl GitIgnoreChain {
pub fn push(&mut self, id: Id<GitIgnoreFile>) {
impl IgnoreChain {
pub fn push(&mut self, id: Id<IgnoreFile>) {
self.file_ids.push(id);
}
}

impl GitIgnorer {
pub fn root_chain(&mut self, mut dir: &Path) -> GitIgnoreChain {
let mut chain = GitIgnoreChain::default();
impl Ignorer {
pub fn root_chain(&mut self, mut dir: &Path) -> IgnoreChain {
let mut chain = IgnoreChain::default();
loop {
let is_repo = is_repo(dir);
if is_repo {
if let Some(gif) = GitIgnoreFile::global(dir) {
if let Some(gif) = IgnoreFile::global(dir) {
chain.push(self.files.alloc(gif));
}
}
Expand All @@ -188,7 +188,7 @@ impl GitIgnorer {
continue;
}
let file = dir.join(filename);
if let Ok(gif) = GitIgnoreFile::new(&file, dir, local_git_ignore) {
if let Ok(gif) = IgnoreFile::new(&file, dir, local_git_ignore) {
chain.push(self.files.alloc(gif));
}
}
Expand All @@ -214,9 +214,9 @@ impl GitIgnorer {
///
/// Deeper file have a bigger priority.
/// .ignore files have a bigger priority than .gitignore files.
pub fn deeper_chain(&mut self, parent_chain: &GitIgnoreChain, dir: &Path) -> GitIgnoreChain {
pub fn deeper_chain(&mut self, parent_chain: &IgnoreChain, dir: &Path) -> IgnoreChain {
let mut chain = if is_repo(dir) {
let mut chain = GitIgnoreChain::default();
let mut chain = IgnoreChain::default();
for &id in &parent_chain.file_ids {
if !self.files[id].local_git_ignore {
chain.file_ids.push(id);
Expand All @@ -236,7 +236,7 @@ impl GitIgnorer {
continue;
}
let ignore_file = dir.join(filename);
if let Ok(gif) = GitIgnoreFile::new(&ignore_file, dir, local_git_ignore) {
if let Ok(gif) = IgnoreFile::new(&ignore_file, dir, local_git_ignore) {
debug!("pushing GIF {:#?}", &gif);
chain.push(self.files.alloc(gif));
}
Expand All @@ -246,7 +246,7 @@ impl GitIgnorer {
/// return true if the given path should not be ignored
pub fn accepts(
&self,
chain: &GitIgnoreChain,
chain: &IgnoreChain,
path: &Path,
filename: &str,
directory: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod status;
mod status_computer;

pub use {
ignore::{GitIgnoreChain, GitIgnorer},
ignore::{IgnoreChain, Ignorer},
status::{LineGitStatus, LineStatusComputer, TreeGitStatus},
status_computer::{clear_status_computer_cache, get_tree_status},
};
Expand Down
6 changes: 3 additions & 3 deletions src/tree_build/bline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {
super::bid::BId,
crate::{
errors::TreeBuildError,
git::GitIgnoreChain,
git::IgnoreChain,
path::{normalize_path, Directive, SpecialHandling},
tree::*,
},
Expand All @@ -29,7 +29,7 @@ pub struct BLine {
pub direct_match: bool,
pub score: i32,
pub nb_kept_children: i32, // used during the trimming step
pub git_ignore_chain: GitIgnoreChain,
pub git_ignore_chain: IgnoreChain,
pub special_handling: SpecialHandling,
}

Expand All @@ -47,7 +47,7 @@ impl BLine {
pub fn from_root(
blines: &mut Arena<BLine>,
path: PathBuf,
git_ignore_chain: GitIgnoreChain,
git_ignore_chain: IgnoreChain,
_options: &TreeOptions,
) -> Result<BId, TreeBuildError> {
if let Ok(md) = fs::metadata(&path) {
Expand Down
10 changes: 5 additions & 5 deletions src/tree_build/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use {
app::AppContext,
errors::TreeBuildError,
git::{
GitIgnoreChain,
GitIgnorer,
IgnoreChain,
Ignorer,
LineStatusComputer,
},
path::Directive,
Expand Down Expand Up @@ -75,7 +75,7 @@ pub struct TreeBuilder<'c> {
root_id: BId,
subpath_offset: usize,
total_search: bool,
git_ignorer: GitIgnorer,
git_ignorer: Ignorer,
line_status_computer: Option<LineStatusComputer>,
con: &'c AppContext,
pub matches_max: Option<usize>, // optional hard limit
Expand All @@ -92,7 +92,7 @@ impl<'c> TreeBuilder<'c> {
) -> Result<TreeBuilder<'c>, TreeBuildError> {
let mut blines = Arena::new();
let subpath_offset = path.components().count();
let mut git_ignorer = time!(GitIgnorer::default());
let mut git_ignorer = time!(Ignorer::default());
let root_ignore_chain = git_ignorer.root_chain(&path);
let line_status_computer = if options.filter_by_git_status || options.show_git_file_info {
time!(
Expand Down Expand Up @@ -239,7 +239,7 @@ impl<'c> TreeBuilder<'c> {
direct_match,
score,
nb_kept_children: 0,
git_ignore_chain: GitIgnoreChain::default(),
git_ignore_chain: IgnoreChain::default(),
special_handling,
})
}
Expand Down

0 comments on commit 3611b0a

Please sign in to comment.