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},
|
|
|
|
gitforge::{BranchResetResult, Force},
|
|
|
|
types::GitRef,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn reset(
|
|
|
|
repo_details: &RepoDetails,
|
|
|
|
branch_name: BranchName,
|
|
|
|
to_commit: GitRef,
|
|
|
|
force: Force,
|
|
|
|
) -> BranchResetResult {
|
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-04-19 18:56:42 +01:00
|
|
|
match gix::command::prepare(command.expose_secret())
|
2024-04-16 22:21:55 +01:00
|
|
|
.with_shell_allow_argument_splitting()
|
|
|
|
.stdout(std::process::Stdio::null())
|
|
|
|
.stderr(std::process::Stdio::null())
|
|
|
|
.spawn()
|
|
|
|
{
|
|
|
|
Ok(mut child) => match child.wait() {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(err) => {
|
|
|
|
warn!(?err, "Failed (wait)");
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
warn!(?err, "Failed (spawn)");
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|