diff --git a/src/grm/cmd.rs b/src/grm/cmd.rs index 4b0fd1e..d269610 100644 --- a/src/grm/cmd.rs +++ b/src/grm/cmd.rs @@ -63,6 +63,14 @@ pub struct FindLocalArgs { #[clap(help = "The path to search through")] pub path: String, + #[clap( + short, + long, + help = "Exclude repositories that match the given regex", + name = "REGEX" + )] + pub exclude: Option, + #[clap( value_enum, short, diff --git a/src/grm/main.rs b/src/grm/main.rs index aa522b4..4b293dc 100644 --- a/src/grm/main.rs +++ b/src/grm/main.rs @@ -199,7 +199,7 @@ fn main() { } }; - let (found_repos, warnings) = match find_in_tree(&path) { + let (found_repos, warnings) = match find_in_tree(&path, &args.exclude) { Ok((repos, warnings)) => (repos, warnings), Err(error) => { print_error(&error); diff --git a/src/lib.rs b/src/lib.rs index cdfc596..3cd48f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,12 +19,23 @@ pub mod worktree; /// The bool in the return value specifies whether there is a repository /// in root itself. #[allow(clippy::type_complexity)] -fn find_repos(root: &Path) -> Result, Vec, bool)>, String> { +fn find_repos( + root: &Path, + exclusion_pattern: &Option, +) -> Result, Vec, bool)>, String> { let mut repos: Vec = Vec::new(); let mut repo_in_root = false; let mut warnings = Vec::new(); + let exlusion_regex: regex::Regex = + regex::Regex::new(&exclusion_pattern.clone().unwrap_or(r"^$".to_string())) + .unwrap_or_else(|_| regex::Regex::new(r"^$").unwrap()); for path in tree::find_repo_paths(root)? { + if exclusion_pattern.is_some() && exlusion_regex.is_match(&path::path_as_string(&path)) { + warnings.push(format!("[skipped] {}", &path::path_as_string(&path))); + continue; + } + let is_worktree = repo::RepoHandle::detect_worktree(&path); if path == root { repo_in_root = true; @@ -130,10 +141,14 @@ fn find_repos(root: &Path) -> Result, Vec, bool) Ok(Some((repos, warnings, repo_in_root))) } -pub fn find_in_tree(path: &Path) -> Result<(tree::Tree, Vec), String> { +pub fn find_in_tree( + path: &Path, + exclusion_pattern: &Option, +) -> Result<(tree::Tree, Vec), String> { let mut warnings = Vec::new(); - let (repos, repo_in_root): (Vec, bool) = match find_repos(path)? { + let (repos, repo_in_root): (Vec, bool) = match find_repos(path, exclusion_pattern)? + { Some((vec, mut repo_warnings, repo_in_root)) => { warnings.append(&mut repo_warnings); (vec, repo_in_root)