use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use crate::config::{PerRepoConfig, Registry};
use crate::output;
use crate::scanner;
pub fn run(paths: &[String], dry_run: bool) -> Result<()> {
output::print_banner();
output::print_header("dev-prune init");
let mut registry = Registry::load()?;
let mut total_found = 0;
let mut newly_added_repos: Vec<PathBuf> = Vec::new();
for path_str in paths {
let path = Path::new(path_str)
.canonicalize()
.with_context(|| format!("Path not found: {path_str}"))?;
output::print_info(&format!(
"Scanning {} for Git repositories...",
output::clean_path(&path)
));
let repos = scanner::scan_for_repos(&path)?;
total_found += repos.len();
for repo in repos {
if registry.add_repo(repo.clone()) {
newly_added_repos.push(repo.clone());
let verb = if dry_run {
"Would register"
} else {
"Registered"
};
output::print_success(&format!("{verb}: {}", output::clean_path(&repo)));
} else {
output::print_info(&format!(
"Already registered: {}",
output::clean_path(&repo)
));
}
}
}
if !dry_run {
registry.last_added_repos = newly_added_repos.clone();
registry.save()?;
}
output::print_header("Summary");
output::print_info(&format!(
"Found {total_found} Git {}, {} {} new",
output::plural(total_found, "repo", "repos"),
if dry_run { "would add" } else { "added" },
newly_added_repos.len()
));
output::print_info(&format!("Total tracked: {}", registry.repo_count()));
if !dry_run {
if let Some(report) = crate::setup::ensure_integrations_if_enabled(®istry) {
if report.changed_anything() || report.needs_attention() {
output::print_header("Integrations");
report.print(false);
}
}
crate::setup::suppress_next_auto_setup();
for repo in registry.repositories.keys() {
if let Err(e) = PerRepoConfig::load_with_diagnostics(repo) {
output::print_warning(&format!(
"{}: `.devprune.json` could not be parsed and was left untouched — {e}",
output::clean_path(repo)
));
}
}
}
output::print_header("Version");
output::print_info(&format!("Installed: v{}", crate::constants::VERSION));
if registry.settings.update_check {
let mut checked = registry;
if crate::commands::update::check_now(&mut checked) && !dry_run {
let _ = checked.save();
}
registry = checked;
} else {
output::print_info(
"The release check is off (`devp config set update_check true` re-enables it).",
);
}
if dry_run {
output::print_header("Dry Run Complete");
output::print_info("Nothing was written. Re-run without `--dry-run` to register.");
return Ok(());
}
output::print_header("Initialization Complete");
output::print_success(&format!(
"dev-prune initialization complete! All {} tracked {} registered & verified.",
registry.repo_count(),
output::plural(registry.repo_count(), "repository", "repositories")
));
output::print_info(
"Optional extra: `devp icon` gives .devprune.json its own icon in your file manager.",
);
output::print_info("Review or undo the integrations with `devp setup --status`.");
Ok(())
}