git-next/src/server/gitforge/forgejo/branch/reset.rs

49 lines
1.5 KiB
Rust
Raw Normal View History

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 {
let user = &repo_details.forge.user;
let hostname = &repo_details.forge.hostname;
let path = &repo_details.repo;
use secrecy::ExposeSecret;
let expose_secret = &repo_details.forge.token;
let token = expose_secret.expose_secret();
let origin = format!("https://{user}:{token}@{hostname}/{path}.git");
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'
let command = format!("/usr/bin/git push {origin} {to_commit}:{branch_name} {force}");
drop(origin);
info!("Resetting {branch_name} to {to_commit}");
match gix::command::prepare(command)
.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(())
}
}
}