use crate::commands::{Command, CommandResult};
use crate::services::GitService;
use crate::app::{AppState, Action, reducer};
use crate::errors::CommandError;
use tracing::instrument;
pub struct CheckMergeStatusCommand {
pub base: String,
}
impl Command for CheckMergeStatusCommand {
#[instrument(skip(self, git, state), fields(base = %self.base))]
fn execute(
&self,
git: &GitService,
state: &AppState,
) -> Result<CommandResult, CommandError> {
let mut new_state = state.clone();
new_state = reducer(new_state, Action::SetOpStatus(Some(format!("checking {}…", self.base))));
match git.ahead_behind_remote(&state.repo_path, &self.base) {
Ok((ahead, behind)) => {
new_state = reducer(new_state, Action::SetMergeAhead(Some(ahead)));
new_state = reducer(new_state, Action::AppendOpLog(format!("{} ahead {}, behind {}", self.base, ahead, behind)));
new_state = reducer(new_state, Action::SetFeedback(Some(format!("{} ahead {}, behind {}", self.base, ahead, behind))));
new_state = reducer(
new_state,
Action::AppendMergeLog(format!("{} ahead {}, behind {}", self.base, ahead, behind)),
);
}
Err(e) => {
new_state = reducer(new_state, Action::AppendOpLog(format!("merge check error: {e}")));
new_state = reducer(new_state, Action::SetStatusError(Some(format!("merge check error: {e}"))));
new_state = reducer(new_state, Action::AppendMergeLog(format!("{}: error {}", self.base, e)));
}
}
new_state = reducer(new_state, Action::SetOpStatus(None));
Ok(CommandResult::StateUpdate(new_state))
}
}