pets_tools 0.2.0

MusKitty multi-repo source management CLI — one command to clone, build, test, push, and publish 9 independent crate repositories
//! `pets push <project>` — push all repos with confirmation.

use crate::config::Config;
use crate::git;
use crate::ui;
use crate::PushArgs;
use std::io::{self, Write};

pub fn run(config: Config, args: &PushArgs) {
    // First, gather status for all repos
    let mut repos_to_push: Vec<(&str, &std::path::Path)> = Vec::new();

    // Check main repo
    if git::is_git_repo(&config.main_repo.dir) {
        if let Ok((ahead, _behind)) = git::ahead_behind(&config.main_repo.dir) {
            if ahead > 0 || !git::is_clean(&config.main_repo.dir) {
                repos_to_push.push((
                    config.main_repo.name.as_str(),
                    config.main_repo.dir.as_path(),
                ));
            }
        }
    }

    // Check crate repos
    for repo in &config.repos {
        if let Ok((ahead, _behind)) = git::ahead_behind(&repo.abs_dir) {
            if ahead > 0 || !git::is_clean(&repo.abs_dir) {
                repos_to_push.push((repo.name.as_str(), repo.abs_dir.as_path()));
            }
        }
    }

    if repos_to_push.is_empty() {
        ui::info("Nothing to push — all repos up-to-date.");
        return;
    }

    // Show what will be pushed
    ui::heading("Repos with unpushed changes:");
    for (name, dir) in &repos_to_push {
        let (ahead, behind) = git::ahead_behind(dir).unwrap_or((0, 0));
        let clean = if git::is_clean(dir) { "clean" } else { "dirty" };
        ui::info(&format!(
            "  {} — ahead: {}, behind: {}, {}",
            name, ahead, behind, clean
        ));
    }

    if args.dry_run {
        ui::info("--dry-run: exiting without pushing.");
        return;
    }

    // Confirm
    if !args.all {
        print!("\n  Push {} repos? [y/N]: ", repos_to_push.len());
        io::stdout().flush().ok();
        let mut input = String::new();
        io::stdin().read_line(&mut input).ok();
        let input = input.trim().to_lowercase();
        if input != "y" && input != "yes" {
            ui::warn("Push cancelled.");
            return;
        }
    }

    // Push
    let mut ok = 0;
    let mut fail = 0;
    for (name, dir) in repos_to_push {
        match git::push(dir) {
            Ok(_) => {
                ui::success(&format!("{} — pushed ✓", name));
                ok += 1;
            }
            Err(e) => {
                ui::error(&format!("{} — {}", name, e));
                fail += 1;
            }
        }
    }

    ui::separator();
    ui::summary(ok, 0, fail);

    if fail > 0 {
        std::process::exit(1);
    }
}