2024-05-03 17:50:50 +01:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
2024-04-19 18:56:42 +01:00
|
|
|
use secrecy::ExposeSecret;
|
2024-04-16 22:21:55 +01:00
|
|
|
use tracing::{info, warn};
|
|
|
|
|
|
|
|
use crate::server::{
|
|
|
|
config::{BranchName, RepoDetails},
|
2024-05-03 17:50:50 +01:00
|
|
|
gitforge::{BranchResetError, BranchResetResult, Force},
|
2024-04-16 22:21:55 +01:00
|
|
|
types::GitRef,
|
|
|
|
};
|
|
|
|
|
2024-05-03 17:50:50 +01:00
|
|
|
// TODO: (#72) reimplement using `gix`
|
|
|
|
#[tracing::instrument]
|
2024-04-16 22:21:55 +01:00
|
|
|
pub fn reset(
|
|
|
|
repo_details: &RepoDetails,
|
|
|
|
branch_name: BranchName,
|
|
|
|
to_commit: GitRef,
|
|
|
|
force: Force,
|
|
|
|
) -> BranchResetResult {
|
2024-05-03 17:50:50 +01:00
|
|
|
let repository = gix::ThreadSafeRepository::open(repo_details.gitdir.deref())
|
|
|
|
.map_err(Box::new)?
|
|
|
|
.to_thread_local();
|
|
|
|
let gitdir = repository.git_dir();
|
2024-04-19 18:56:42 +01:00
|
|
|
let origin = repo_details.origin();
|
2024-04-16 22:21:55 +01:00
|
|
|
let force = match force {
|
|
|
|
Force::No => "".to_string(),
|
|
|
|
Force::From(old_ref) => format!("--force-with-lease={branch_name}:{old_ref}"),
|
|
|
|
};
|
|
|
|
// INFO: never log the command as it contains the API token within the 'origin'
|
2024-04-19 18:56:42 +01:00
|
|
|
let command: secrecy::Secret<String> = format!(
|
|
|
|
"/usr/bin/git push {} {to_commit}:{branch_name} {force}",
|
|
|
|
origin.expose_secret()
|
|
|
|
)
|
|
|
|
.into();
|
2024-04-16 22:21:55 +01:00
|
|
|
info!("Resetting {branch_name} to {to_commit}");
|
2024-05-03 17:50:50 +01:00
|
|
|
let ctx = gix::diff::command::Context {
|
|
|
|
git_dir: Some(gitdir.to_path_buf()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
// info!(?ctx, command = command.expose_secret(), "prepare");
|
2024-04-19 18:56:42 +01:00
|
|
|
match gix::command::prepare(command.expose_secret())
|
2024-05-03 17:50:50 +01:00
|
|
|
.with_context(ctx)
|
2024-04-16 22:21:55 +01:00
|
|
|
.with_shell_allow_argument_splitting()
|
2024-05-03 17:50:50 +01:00
|
|
|
// .stdout(std::process::Stdio::null())
|
|
|
|
// .stderr(std::process::Stdio::null())
|
2024-04-16 22:21:55 +01:00
|
|
|
.spawn()
|
|
|
|
{
|
|
|
|
Ok(mut child) => match child.wait() {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(err) => {
|
|
|
|
warn!(?err, "Failed (wait)");
|
2024-05-03 17:50:50 +01:00
|
|
|
Err(BranchResetError::Push)
|
2024-04-16 22:21:55 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
warn!(?err, "Failed (spawn)");
|
2024-05-03 17:50:50 +01:00
|
|
|
Err(BranchResetError::Push)
|
2024-04-16 22:21:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|