eazygit 0.5.1

A fast TUI for Git with staging, conflicts, rebase, and palette-first UX
Documentation
//! Rebase builder state handling within commit reducer.

use crate::app::{actions::Action, state::AppState};
use crate::app::state::{RebaseAction, RebaseEntry};

#![allow(dead_code)]
/// Handle rebase builder related actions.
pub fn handle(state: &mut AppState, action: &Action) -> bool {
    match action {
        Action::StartInteractiveRebase(hashes) => {
            // Build rebase entries from selected commits
            let entries: Vec<RebaseEntry> = hashes
                .iter()
                .map(|hash| {
                    let msg = state
                        .commits
                        .iter()
                        .find(|c| &c.hash == hash)
                        .map(|c| c.message.clone())
                        .unwrap_or_default();
                    RebaseEntry {
                        action: RebaseAction::Pick,
                        hash: hash.clone(),
                        message: msg,
                    }
                })
                .collect();
            state.rebase_entries = entries;
            state.rebase_entry_selected = 0;
            state.rebase_builder_open = true;
            true
        }
        Action::RebaseCancel => {
            state.rebase_builder_open = false;
            state.rebase_entries.clear();
            state.rebase_entry_selected = 0;
            true
        }
        Action::RebaseNext => {
            if state.rebase_entry_selected + 1 < state.rebase_entries.len() {
                state.rebase_entry_selected += 1;
            }
            true
        }
        Action::RebasePrev => {
            if state.rebase_entry_selected > 0 {
                state.rebase_entry_selected -= 1;
            }
            true
        }
        Action::RebaseMoveUp => {
            if state.rebase_entry_selected > 0 {
                state.rebase_entries.swap(
                    state.rebase_entry_selected,
                    state.rebase_entry_selected - 1,
                );
                state.rebase_entry_selected -= 1;
            }
            true
        }
        Action::RebaseMoveDown => {
            if state.rebase_entry_selected + 1 < state.rebase_entries.len() {
                state.rebase_entries.swap(
                    state.rebase_entry_selected,
                    state.rebase_entry_selected + 1,
                );
                state.rebase_entry_selected += 1;
            }
            true
        }
        Action::RebaseCycleAction => {
            if let Some(entry) = state.rebase_entries.get_mut(state.rebase_entry_selected) {
                entry.action = match entry.action {
                    RebaseAction::Pick => RebaseAction::Reword,
                    RebaseAction::Reword => RebaseAction::Edit,
                    RebaseAction::Edit => RebaseAction::Squash,
                    RebaseAction::Squash => RebaseAction::Fixup,
                    RebaseAction::Fixup => RebaseAction::Drop,
                    RebaseAction::Drop => RebaseAction::Pick,
                };
            }
            true
        }
        Action::RebaseSetAction(new_action) => {
            if let Some(entry) = state.rebase_entries.get_mut(state.rebase_entry_selected) {
                entry.action = new_action.clone();
            }
            true
        }
        _ => false,
    }
}