eazygit 0.5.1

A fast TUI for Git with staging, conflicts, rebase, and palette-first UX
Documentation
//! Commit and rebase action handlers.

use crate::app::{Action, reducer};
use crate::app::workflow::WorkflowState;
use crate::errors::ApplicationError;
use super::super::HandlerContext;

/// Handle commit-related special actions. Returns true if handled.
pub async fn handle(action: &Action, ctx: &mut HandlerContext<'_>) -> Result<bool, ApplicationError> {
    match action {
        Action::RebaseAbortFromBuilder => {
            if matches!(
                ctx.app.state.workflow_context.as_ref().map(|c| c.state.clone()),
                Some(WorkflowState::RebaseInProgress)
            ) {
                match ctx.app.git_service.rebase_abort(&ctx.app.state.repo_path) {
                    Ok(_) => {
                        ctx.app.state = reducer(ctx.app.state.clone(), Action::AppendOpLog("rebase --abort".into()));
                        ctx.app.state = reducer(ctx.app.state.clone(), Action::SetFeedback(Some("Rebase aborted".into())));
                    }
                    Err(e) => {
                        ctx.app.state = reducer(ctx.app.state.clone(), Action::SetStatusError(Some(format!("rebase --abort error: {e}"))));
                    }
                }
            }
            ctx.app.state = reducer(ctx.app.state.clone(), Action::RebaseCancel);
            Ok(true)
        }
        Action::StartInteractiveRebase(_) => {
            // Auto-enable --root if oldest commit is root
            if let Some(oldest_entry) = ctx.app.state.rebase_session.entries.first() {
                let oldest_hash = oldest_entry.hash.clone();
                let oldest_short = oldest_entry.short_hash.clone();
                
                if let Ok(root_hash) = ctx.app.git_service.root_commit(&ctx.app.state.repo_path) {
                    if !root_hash.is_empty() && root_hash == oldest_hash && !ctx.app.state.rebase_use_root {
                        ctx.app.state = reducer(ctx.app.state.clone(), Action::RebaseToggleUseRoot);
                        ctx.app.state = reducer(ctx.app.state.clone(), Action::SetFeedback(Some(
                            format!("Auto-enabled --root (commit {} is root). Press R to toggle.", oldest_short)
                        )));
                    }
                }
            }
            Ok(false) // Let other handlers continue
        }
        Action::ShowLogActionMenu(_) => {
            // Force immediate render for log action menu
            if let Err(e) = ctx.terminal.draw(|frame| {
                if let Err(re) = ctx.app.component_manager.render(frame, &ctx.app.state) {
                    tracing::error!(error = %re, "Render failed (log action menu)");
                }
            }) {
                tracing::error!(error = %e, "Immediate render failed after ShowLogActionMenu");
            }
            Ok(true)
        }
        Action::RequestRefreshStatus => {
            ctx.app.state = reducer(ctx.app.state.clone(), Action::RequestRefreshStatus);
            // Continue to handle RefreshStatus
            Ok(false)
        }
        _ => Ok(false),
    }
}