wsb 0.275.7622

Workspace tools for every project under $PRJ: git across submodules, template stamping, GitHub, and the workbench
Documentation
use crate::fleet::error::Result;
use crate::fleet::git::{GitOps, SubmoduleTree};
use crate::fleet::output::Formatter;
use crate::prelude::*;

pub fn execute<P: AsRef<Path>>(repo_root: P, all: bool, _verbose: bool) -> Result<()> {
    let tree = SubmoduleTree::discover(&repo_root)?;

    Formatter::print_info(&format!(
        "Listing branches for superproject + {} submodule(s)",
        tree.len()
    ));

    // List branches for superproject first
    let repo = match GitOps::open_repo(&repo_root) {
        Ok(r) => r,
        Err(e) => {
            Formatter::print_error(&format!("Failed to open superproject: {}", e));
            return Err(e);
        }
    };

    let current_branch = GitOps::current_branch(&repo).ok();

    let branches = if all {
        let mut local = GitOps::get_local_branches(&repo).unwrap_or_default();
        let remote = GitOps::get_remote_branches(&repo).unwrap_or_default();
        local.extend(remote);
        local
    } else {
        GitOps::get_local_branches(&repo).unwrap_or_default()
    };

    Formatter::print_branches("[SUPERPROJECT]", &branches, current_branch.as_deref());

    for node in tree.top_down() {
        if !node.initialized {
            Formatter::print_warning(&format!("'{}' not initialized", node.label()));
            continue;
        }

        let repo = match GitOps::open_repo(&node.abs_path) {
            Ok(r) => r,
            Err(e) => {
                Formatter::print_error(&format!("Failed to open '{}': {}", node.label(), e));
                continue;
            }
        };

        let current_branch = GitOps::current_branch(&repo).ok();

        let branches = if all {
            let mut local = GitOps::get_local_branches(&repo).unwrap_or_default();
            let remote = GitOps::get_remote_branches(&repo).unwrap_or_default();
            local.extend(remote);
            local
        } else {
            GitOps::get_local_branches(&repo).unwrap_or_default()
        };

        Formatter::print_branches(node.label(), &branches, current_branch.as_deref());
    }

    Ok(())
}