eazygit 0.5.1

A fast TUI for Git with staging, conflicts, rebase, and palette-first UX
Documentation
use crate::app::AppState;
use crate::commands::{Command, StageFilesCommand, UnstageFilesCommand, CommitCommand};
use crate::app::commit_template;

/// Creates a stage or unstage command for selected files.
pub fn create_stage_command(state: &AppState, stage: bool) -> Option<Box<dyn Command>> {
    // 1. Multi-selected files
    if !state.selected_files.is_empty() {
        let paths: Vec<String> = state.selected_files.iter().cloned().collect();
        return if stage {
            Some(Box::new(StageFilesCommand { paths }))
        } else {
            Some(Box::new(UnstageFilesCommand { paths }))
        };
    }

    // 2. Currently selected file
    if !state.status_selected_path.is_empty() {
        let paths = vec![state.status_selected_path.clone()];
        return if stage {
            Some(Box::new(StageFilesCommand { paths }))
        } else {
            Some(Box::new(UnstageFilesCommand { paths }))
        };
    }

    // 3. No selection
    None
}

/// Creates a commit command with template substitution applied.
pub fn create_commit_command(state: &AppState, amend: bool) -> Option<Box<dyn Command>> {
    let mut message = state.commit_input.clone();
    
    // Apply template substitution to the message if template exists
    if let Some(ref template) = state.commit_template {
        message = commit_template::substitute_template(template, state, &message);
    }

    if message.trim().is_empty() && !amend {
        return None;
    }

    let (author, email) = if amend {
        (
            if state.amend_author_name.is_empty() { None } else { Some(state.amend_author_name.clone()) },
            if state.amend_author_email.is_empty() { None } else { Some(state.amend_author_email.clone()) }
        )
    } else {
        (None, None)
    };

    Some(Box::new(CommitCommand {
        message,
        amend,
        author_name: author,
        author_email: email,
    }))
}