git working

This commit is contained in:
2025-05-08 09:05:44 +03:00
parent 21893ce225
commit 104097ee4b
36 changed files with 311 additions and 221 deletions

View File

@@ -197,40 +197,41 @@ impl GitTree {
/// * `Ok(Vec<String>)` - A vector of paths to matching repositories
/// * `Err(GitError)` - If no matching repositories are found,
/// or if multiple repositories match a non-wildcard pattern
pub fn find(&self, pattern: &str) -> Result<Vec<String>, GitError> {
// Get all repos
let repos = self.list()?;
if repos.is_empty() {
return Err(GitError::NoRepositoriesFound);
pub fn find(&self, pattern: &str) -> Result<Vec<GitRepo>, GitError> {
let repo_names = self.list()?; // list() already ensures these are git repo names
if repo_names.is_empty() {
return Ok(Vec::new()); // If no repos listed, find results in an empty list
}
// Check if pattern ends with wildcard
if pattern.ends_with('*') {
let search_pattern = &pattern[0..pattern.len()-1]; // Remove the *
let matching: Vec<String> = repos.iter()
.filter(|repo| repo.contains(search_pattern))
.cloned()
.collect();
if matching.is_empty() {
return Err(GitError::RepositoryNotFound(pattern.to_string()));
let mut matched_repos: Vec<GitRepo> = Vec::new();
if pattern == "*" {
for name in repo_names {
let full_path = format!("{}/{}", self.base_path, name);
matched_repos.push(GitRepo::new(full_path));
}
} else if pattern.ends_with('*') {
let prefix = &pattern[0..pattern.len()-1];
for name in repo_names {
if name.starts_with(prefix) {
let full_path = format!("{}/{}", self.base_path, name);
matched_repos.push(GitRepo::new(full_path));
}
}
Ok(matching)
} else {
// No wildcard, need to find exactly one match
let matching: Vec<String> = repos.iter()
.filter(|repo| repo.contains(pattern))
.cloned()
.collect();
match matching.len() {
0 => Err(GitError::RepositoryNotFound(pattern.to_string())),
1 => Ok(matching),
_ => Err(GitError::MultipleRepositoriesFound(pattern.to_string(), matching.len())),
// Exact match for the name
for name in repo_names {
if name == pattern {
let full_path = format!("{}/{}", self.base_path, name);
matched_repos.push(GitRepo::new(full_path));
// `find` returns all exact matches. If names aren't unique (unlikely from `list`),
// it could return more than one. For an exact name, typically one is expected.
}
}
}
Ok(matched_repos)
}
/// Gets one or more GitRepo objects based on a path pattern or URL.
@@ -281,14 +282,9 @@ impl GitTree {
Err(GitError::GitCommandFailed(format!("Git clone error: {}", error)))
}
} else {
// It's a path pattern, find matching repositories
let repo_paths = self.find(path_or_url)?;
// Convert paths to GitRepo objects
let repos: Vec<GitRepo> = repo_paths.into_iter()
.map(GitRepo::new)
.collect();
// It's a path pattern, find matching repositories using the updated self.find()
// which now directly returns Result<Vec<GitRepo>, GitError>.
let repos = self.find(path_or_url)?;
Ok(repos)
}
}