use globset crate for find functionality
This commit is contained in:
@@ -360,21 +360,21 @@ impl CodeTool {
|
|||||||
|
|
||||||
/// Find files matching a glob pattern (like `find`).
|
/// Find files matching a glob pattern (like `find`).
|
||||||
///
|
///
|
||||||
/// Supports simple glob patterns with `*` wildcards.
|
/// Supports glob patterns using the `globset` crate syntax.
|
||||||
pub async fn find(&self, pattern: Option<&str>) -> Result<String, String> {
|
pub async fn find(&self, pattern: Option<&str>) -> Result<String, String> {
|
||||||
let cache = self.get_current_tarball().await;
|
let cache = self.get_current_tarball().await;
|
||||||
let cached = cache.as_ref().ok_or("source code not available")?;
|
let cached = cache.as_ref().ok_or("source code not available")?;
|
||||||
|
|
||||||
let pattern = pattern.unwrap_or("*");
|
let pattern = pattern.unwrap_or("*");
|
||||||
|
|
||||||
// Convert glob pattern to regex
|
let glob = Glob::new(pattern)
|
||||||
let regex_pattern = glob_to_regex(pattern);
|
.map_err(|e| format!("Invalid glob pattern: {e}"))?
|
||||||
let regex = Regex::new(®ex_pattern).map_err(|e| format!("Invalid pattern: {e}"))?;
|
.compile_matcher();
|
||||||
|
|
||||||
let mut matches: Vec<&str> = cached
|
let mut matches: Vec<&str> = cached
|
||||||
.files
|
.files
|
||||||
.keys()
|
.keys()
|
||||||
.filter(|path| regex.is_match(path))
|
.filter(|path| glob.is_match(path))
|
||||||
.map(|s| s.as_str())
|
.map(|s| s.as_str())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@@ -548,24 +548,6 @@ impl CodeTool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert a simple glob pattern to a regex.
|
|
||||||
fn glob_to_regex(pattern: &str) -> String {
|
|
||||||
let mut regex = String::from("^");
|
|
||||||
for c in pattern.chars() {
|
|
||||||
match c {
|
|
||||||
'*' => regex.push_str(".*"),
|
|
||||||
'?' => regex.push('.'),
|
|
||||||
'.' | '+' | '(' | ')' | '[' | ']' | '{' | '}' | '^' | '$' | '|' | '\\' => {
|
|
||||||
regex.push('\\');
|
|
||||||
regex.push(c);
|
|
||||||
}
|
|
||||||
_ => regex.push(c),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
regex.push('$');
|
|
||||||
regex
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Check if content looks like binary data.
|
/// Check if content looks like binary data.
|
||||||
fn looks_binary(content: &str) -> bool {
|
fn looks_binary(content: &str) -> bool {
|
||||||
// Check first 1000 chars for null bytes or high ratio of non-printable chars
|
// Check first 1000 chars for null bytes or high ratio of non-printable chars
|
||||||
|
|||||||
Reference in New Issue
Block a user