use crate::commands::{Command, CommandResult};
use crate::services::GitService;
use crate::app::{AppState, Action, reducer};
use crate::errors::CommandError;
use tracing::instrument;
use std::collections::HashSet;
use super::builder::build_lines_patch;
pub struct StageLinesCommand {
pub raw_diff: String,
pub path: String,
pub selected_lines: HashSet<usize>,
}
impl Command for StageLinesCommand {
#[instrument(skip(self, git, state))]
fn execute(
&self,
git: &GitService,
state: &AppState,
) -> Result<CommandResult, CommandError> {
let mut new_state = state.clone();
new_state = reducer(new_state, Action::SetOpStatus(Some("stage lines…".into())));
match build_lines_patch(&self.path, &self.raw_diff, &self.selected_lines) {
Ok(patch) => match git.apply_patch(&state.repo_path, &patch, true, false) {
Ok(_) => {
new_state = reducer(new_state, Action::AppendOpLog("lines staged".into()));
new_state = reducer(new_state, Action::SetRefreshing(true));
}
Err(e) => {
new_state = reducer(new_state, Action::AppendOpLog(format!("stage lines error: {e}")));
new_state = reducer(new_state, Action::SetStatusError(Some(format!("stage lines error: {e}"))));
}
},
Err(msg) => {
new_state = reducer(new_state, Action::SetStatusError(Some(msg)));
}
}
new_state = reducer(new_state, Action::SetOpStatus(None));
Ok(CommandResult::StateUpdate(new_state))
}
}
pub struct UnstageLinesCommand {
pub raw_diff: String,
pub path: String,
pub selected_lines: HashSet<usize>,
}
impl Command for UnstageLinesCommand {
#[instrument(skip(self, git, state))]
fn execute(
&self,
git: &GitService,
state: &AppState,
) -> Result<CommandResult, CommandError> {
let mut new_state = state.clone();
new_state = reducer(new_state, Action::SetOpStatus(Some("unstage lines…".into())));
match build_lines_patch(&self.path, &self.raw_diff, &self.selected_lines) {
Ok(patch) => match git.apply_patch(&state.repo_path, &patch, true, true) {
Ok(_) => {
new_state = reducer(new_state, Action::AppendOpLog("lines unstaged".into()));
new_state = reducer(new_state, Action::SetRefreshing(true));
}
Err(e) => {
new_state = reducer(new_state, Action::AppendOpLog(format!("unstage lines error: {e}")));
new_state = reducer(new_state, Action::SetStatusError(Some(format!("unstage lines error: {e}"))));
}
},
Err(msg) => {
new_state = reducer(new_state, Action::SetStatusError(Some(msg)));
}
}
new_state = reducer(new_state, Action::SetOpStatus(None));
Ok(CommandResult::StateUpdate(new_state))
}
}