use tga::collect::CollectionPipeline;
use tga::core::config::Config;
use tga::core::db::Database;
use crate::commands::date_range::resolve_date_range;
use crate::CollectArgs;
pub async fn run(config: Config, db: &mut Database, args: CollectArgs) -> anyhow::Result<()> {
let mut cfg = config;
if !args.repos.is_empty() {
cfg.repositories.retain(|r| {
let name = r.name.clone().unwrap_or_default();
args.repos.contains(&name)
});
if cfg.repositories.is_empty() {
tracing::warn!(
"no repositories matched --repos filter ({:?}); nothing to do",
args.repos
);
}
}
let legacy_since = args.since.clone();
let (resolved_since, resolved_until) = resolve_date_range(
args.weeks,
args.from.as_deref(),
args.to.as_deref(),
legacy_since.as_deref(),
)?;
let effective_until = resolved_until.or_else(|| args.until.clone());
if let Some(since) = resolved_since.as_ref() {
tracing::info!(since = %since, "applying collection lower bound");
for repo in &mut cfg.repositories {
repo.since_date = Some(since.clone());
}
}
if let Some(until) = effective_until.as_ref() {
tracing::info!(until = %until, "applying collection upper bound");
for repo in &mut cfg.repositories {
repo.until_date = Some(until.clone());
}
}
let pipeline = CollectionPipeline::new(cfg)
.with_force(args.force)
.with_no_fetch(args.no_fetch)
.with_force_refresh_prs(args.force_refresh_prs);
let stats = if args.dry_run {
tracing::info!("Dry run — no database writes will occur");
let mut shadow = Database::open_in_memory()?;
pipeline.run(&mut shadow).await?
} else {
pipeline.run(db).await?
};
if args.dry_run {
println!(
"Dry run complete. Would have written {} commits, {} authors, \
{} PRs ({} weeks collected, {} weeks skipped). No changes persisted.",
stats.commits_collected,
stats.authors_resolved,
stats.prs_fetched,
stats.weeks_collected,
stats.weeks_skipped,
);
} else {
println!(
"Collected {} commits from {} authors ({} PRs fetched, \
{} weeks collected, {} weeks skipped)",
stats.commits_collected,
stats.authors_resolved,
stats.prs_fetched,
stats.weeks_collected,
stats.weeks_skipped,
);
}
if !stats.errors.is_empty() {
eprintln!(
"Encountered {} warnings during collection:",
stats.errors.len()
);
for e in &stats.errors {
eprintln!(" warning: {e}");
}
}
Ok(())
}