chore(repo/find): Exlude paths based on regex
This commit is contained in:
@@ -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<String>,
|
||||
|
||||
#[clap(
|
||||
value_enum,
|
||||
short,
|
||||
|
||||
@@ -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);
|
||||
|
||||
21
src/lib.rs
21
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<Option<(Vec<repo::Repo>, Vec<String>, bool)>, String> {
|
||||
fn find_repos(
|
||||
root: &Path,
|
||||
exclusion_pattern: &Option<String>,
|
||||
) -> Result<Option<(Vec<repo::Repo>, Vec<String>, bool)>, String> {
|
||||
let mut repos: Vec<repo::Repo> = 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<Option<(Vec<repo::Repo>, Vec<String>, bool)
|
||||
Ok(Some((repos, warnings, repo_in_root)))
|
||||
}
|
||||
|
||||
pub fn find_in_tree(path: &Path) -> Result<(tree::Tree, Vec<String>), String> {
|
||||
pub fn find_in_tree(
|
||||
path: &Path,
|
||||
exclusion_pattern: &Option<String>,
|
||||
) -> Result<(tree::Tree, Vec<String>), String> {
|
||||
let mut warnings = Vec::new();
|
||||
|
||||
let (repos, repo_in_root): (Vec<repo::Repo>, bool) = match find_repos(path)? {
|
||||
let (repos, repo_in_root): (Vec<repo::Repo>, bool) = match find_repos(path, exclusion_pattern)?
|
||||
{
|
||||
Some((vec, mut repo_warnings, repo_in_root)) => {
|
||||
warnings.append(&mut repo_warnings);
|
||||
(vec, repo_in_root)
|
||||
|
||||
Reference in New Issue
Block a user