wsb 0.203.4400

Workspace - A tool suite for file operations, version management, and development workflow automation
Documentation
use crate::fleet::config::GitModules;
use crate::fleet::error::{Result, SuperGitError};
use crate::fleet::git::GitOps;
use crate::fleet::output::Formatter;
use crate::git_backend::{self, PushOutcome};
use crate::prelude::*;

pub fn execute<P: AsRef<Path>>(
    repo_root: P,
    force_with_lease: bool,
    force_all: bool,
    dry_run: bool,
    verbose: bool,
) -> Result<()> {
    let gitmodules = GitModules::parse(&repo_root)?;

    Formatter::print_info(&format!(
        "Pushing changes in superproject + {} submodule(s){}",
        gitmodules.submodules.len(),
        if force_with_lease {
            " (with --force-with-lease)"
        } else {
            ""
        }
    ));

    if dry_run {
        Formatter::print_warning("DRY RUN: No changes will be made");
    }

    let mut successful = 0;
    let mut failed = 0;
    let mut skipped = 0;

    for submodule in &gitmodules.submodules {
        if verbose {
            Formatter::print_submodule_header(&submodule.name);
        }

        let submodule_path = repo_root.as_ref().join(&submodule.path);

        // Check if submodule exists
        if !submodule_path.exists() {
            Formatter::print_warning(&format!(
                "Submodule '{}' not initialized, skipping",
                submodule.name
            ));
            skipped += 1;
            continue;
        }

        // Check if in detached HEAD
        let repo = match GitOps::open_repo(&submodule_path) {
            Ok(r) => r,
            Err(e) => {
                Formatter::print_error(&format!("Failed to open '{}': {}", submodule.name, e));
                failed += 1;
                continue;
            }
        };

        let is_detached = match GitOps::is_detached_head(&repo) {
            Ok(d) => d,
            Err(e) => {
                Formatter::print_error(&format!(
                    "Failed to check HEAD state of '{}': {}",
                    submodule.name, e
                ));
                failed += 1;
                continue;
            }
        };

        if is_detached {
            Formatter::print_warning(&format!(
                "Submodule '{}' is in detached HEAD state, skipping",
                submodule.name
            ));
            skipped += 1;
            continue;
        }

        let has_outgoing = match git_backend::has_outgoing_commits(&repo, Some("origin")) {
            Ok(value) => value,
            Err(e) => {
                Formatter::print_error(&format!(
                    "Failed to determine outgoing commits in '{}': {}",
                    submodule.name, e
                ));
                failed += 1;
                continue;
            }
        };

        if !force_all && !has_outgoing {
            if verbose {
                Formatter::print_info(&format!("No outgoing commits in '{}'", submodule.name));
            }
            skipped += 1;
            continue;
        }

        if dry_run {
            Formatter::print_info("Would push changes");
            successful += 1;
            continue;
        }

        match git_backend::push_current_branch(&repo, Some("origin"), false, force_with_lease) {
            Ok(PushOutcome::Updated) => {
                Formatter::print_success(&format!("Pushed changes in '{}'", submodule.name));
                successful += 1;
            }
            Ok(PushOutcome::UpToDate) => {
                Formatter::print_info(&format!("'{}' is up to date", submodule.name));
                successful += 1;
            }
            Err(e) => {
                Formatter::print_error(&format!("Failed to push in '{}': {}", submodule.name, e));
                failed += 1;
            }
        }
    }

    // Push superproject after submodules
    if !dry_run {
        if verbose {
            Formatter::print_submodule_header("[SUPERPROJECT]");
        }

        let repo = git_backend::open_repo(repo_root.as_ref())?;
        let has_outgoing = git_backend::has_outgoing_commits(&repo, Some("origin"))?;
        if !force_all && !has_outgoing {
            Formatter::print_info("No outgoing commits in superproject, skipping push");
            skipped += 1;
            Formatter::print_summary(successful, failed, skipped);
        } else {
            match git_backend::push_current_branch(&repo, Some("origin"), true, force_with_lease) {
                Ok(PushOutcome::Updated) => {
                    Formatter::print_success("Pushed superproject");
                    successful += 1;
                }
                Ok(PushOutcome::UpToDate) => {
                    Formatter::print_info("Superproject is up to date");
                    successful += 1;
                }
                Err(err) => {
                    Formatter::print_error(&format!("Failed to push superproject: {}", err));
                    failed += 1;
                }
            }

            Formatter::print_summary(successful, failed, skipped);
        }
    }

    if failed > 0 {
        return Err(SuperGitError::Other(format!(
            "{} repository/submodule(s) failed to push",
            failed
        )));
    }

    Ok(())
}