Skip to content

Commit a64ded1

Browse files
committed
refactor: generalize recursive walk to accept filter closure
Generated-by: Zed (Claude Sonnet 4.6)
1 parent fedd5fd commit a64ded1

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

  • plumbing/git-filter-tree/src

plumbing/git-filter-tree/src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pub mod cli;
33
pub mod exe;
44
pub use git2::{Error, Repository};
5-
use globset::{GlobSet, GlobSetBuilder};
5+
use globset::GlobSetBuilder;
66

77
pub trait FilterTree {
88
/// Filters tree entries by gitattributes-style patterns and returns a new tree with contents
@@ -62,7 +62,7 @@ impl FilterTree for git2::Repository {
6262
.map_err(|e| Error::from_str(&e.to_string()))?;
6363

6464
// Recursively filter the tree
65-
filter_tree_recursive(self, tree, None, &matcher)
65+
filter_tree_recursive(self, tree, None, &|_repo, path| matcher.is_match(path))
6666
}
6767

6868
fn filter_by_attributes<'a>(
@@ -76,12 +76,15 @@ impl FilterTree for git2::Repository {
7676

7777
/// Recursively filters a tree, matching patterns against full paths.
7878
/// Returns a new tree containing only entries that match or have matching descendants.
79-
fn filter_tree_recursive<'a>(
79+
fn filter_tree_recursive<'a, F>(
8080
repo: &'a Repository,
8181
tree: &'a git2::Tree<'a>,
8282
prefix: Option<&str>,
83-
matcher: &GlobSet,
84-
) -> Result<git2::Tree<'a>, Error> {
83+
predicate: &F,
84+
) -> Result<git2::Tree<'a>, Error>
85+
where
86+
F: Fn(&Repository, &str) -> bool,
87+
{
8588
let mut builder = repo.treebuilder(None)?;
8689

8790
for entry in tree.iter() {
@@ -96,14 +99,14 @@ fn filter_tree_recursive<'a>(
9699

97100
match entry.kind() {
98101
Some(git2::ObjectType::Blob) => {
99-
if matcher.is_match(&full_path) {
102+
if predicate(repo, &full_path) {
100103
builder.insert(name, entry.id(), entry.filemode())?;
101104
}
102105
}
103106
Some(git2::ObjectType::Tree) => {
104107
let subtree = entry.to_object(repo)?.peel_to_tree()?;
105108
let filtered_subtree =
106-
filter_tree_recursive(repo, &subtree, Some(&full_path), matcher)?;
109+
filter_tree_recursive(repo, &subtree, Some(&full_path), predicate)?;
107110
if !filtered_subtree.is_empty() {
108111
builder.insert(name, filtered_subtree.id(), entry.filemode())?;
109112
}

0 commit comments

Comments
 (0)