Skip to content

Commit

Permalink
fix(python_file_finder): correctly handle gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
mkniewallner committed Oct 21, 2024
1 parent e7212eb commit d5d737f
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions src/python_file_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use path_slash::PathExt;
use pyo3::types::PyList;
use pyo3::{pyfunction, PyObject, PyResult, Python};
use regex::Regex;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

#[pyfunction]
#[pyo3(signature = (directories, exclude, extend_exclude, using_default_exclude, ignore_notebooks=false))]
Expand Down Expand Up @@ -46,12 +46,7 @@ fn build_walker(
use_git_ignore: bool,
ignore_notebooks: bool,
) -> Walk {
let (first_directory, additional_directories) = directories.split_first().unwrap();

let mut walk_builder = WalkBuilder::new(first_directory);
for path in additional_directories {
walk_builder.add(path);
}
let mut walk_builder = WalkBuilder::new(Path::new("."));

let re: Option<Regex> = if excluded_patterns.is_empty() {
None
Expand All @@ -63,8 +58,18 @@ fn build_walker(
.types(build_types(ignore_notebooks).unwrap())
.hidden(false)
.git_ignore(use_git_ignore)
.parents(false)
.require_git(false)
.filter_entry(move |entry| entry_satisfies_predicate(entry, re.as_ref()))
.filter_entry(move |entry| {
entry_satisfies_predicates(
entry,
directories
.iter()
.map(|d| d.canonicalize().unwrap())
.collect(),
re.as_ref(),
)
})
.build()
}

Expand All @@ -81,7 +86,15 @@ fn build_types(ignore_notebooks: bool) -> Result<Types, ignore::Error> {
types_builder.build()
}

fn entry_satisfies_predicate(entry: &DirEntry, regex: Option<&Regex>) -> bool {
fn entry_satisfies_predicates(
entry: &DirEntry,
directories: Vec<PathBuf>,
regex: Option<&Regex>,
) -> bool {
if !path_satisfies_directories(entry.path().canonicalize().unwrap(), directories) {
return false;
}

if regex.is_none() {
return true;
}
Expand All @@ -91,3 +104,13 @@ fn entry_satisfies_predicate(entry: &DirEntry, regex: Option<&Regex>) -> bool {
.unwrap()
.is_match(path_str.strip_prefix("./").unwrap_or(&path_str).as_ref())
}

fn path_satisfies_directories(path: PathBuf, directories: Vec<PathBuf>) -> bool {
for directory in directories {
if path.starts_with(directory) {
return true;
}
}

false
}

0 comments on commit d5d737f

Please sign in to comment.