use crate::cli::UI;
use crate::ops::oplog;
use crate::ops::utils::short_oid;
use anyhow::Result;
use std::path::Path;
pub fn execute(path: &Path, target: &str, mode: &str, ui: &UI) -> Result<()> {
oplog::with_oplog(
path,
"reset",
&format!("reset --{} {}", mode, target),
|| execute_inner(path, target, mode, ui),
)
}
fn execute_inner(path: &Path, target: &str, mode: &str, ui: &UI) -> Result<()> {
let repo = crate::ops::open_repo(path)?;
let obj = repo.revparse_single(target)?;
let commit = obj.peel_to_commit()?;
let reset_type = match mode {
"soft" => git2::ResetType::Soft,
"mixed" => git2::ResetType::Mixed,
"hard" => git2::ResetType::Hard,
_ => git2::ResetType::Mixed,
};
repo.reset(commit.as_object(), reset_type, None)?;
ui.info(format!(
"HEAD is now at {} {}",
short_oid(&commit.id()),
commit.summary().unwrap_or("")
));
Ok(())
}