feat: add branch::reset function

This commit is contained in:
Paul Campbell 2024-04-11 14:20:59 +01:00
parent 1c9f7cb4ea
commit ed1ab6faa5

View file

@ -1,9 +1,14 @@
use std::fmt::Display;
use actix::prelude::*; use actix::prelude::*;
use kxio::network; use kxio::network;
use tracing::{error, info, warn}; use tracing::{error, info, warn};
use crate::server::{config, forge}; use crate::server::{
config::{self, BranchName},
forge,
};
use super::{RepoActor, StartMonitoring, StartRepo}; use super::{RepoActor, StartMonitoring, StartRepo};
@ -177,6 +182,55 @@ pub async fn advance_main(
}; };
} }
#[derive(Clone, Debug)]
pub struct GitRef(pub String);
impl From<forge::Commit> for GitRef {
fn from(value: forge::Commit) -> Self {
Self(value.sha)
}
}
impl From<BranchName> for GitRef {
fn from(value: BranchName) -> Self {
Self(value.0)
}
}
impl Display for GitRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
#[allow(dead_code)] // TODO: (#21) use to force-update next to main when it isn't a dev ancestor
pub fn reset(
branch: &BranchName,
gitref: impl Into<GitRef>,
repo_details: &config::RepoDetails,
) -> Result<(), std::io::Error> {
let gitref: GitRef = gitref.into();
let user = &repo_details.forge.user;
let token = &repo_details.forge.token;
let hostname = &repo_details.forge.hostname;
let path = &repo_details.repo;
let command =
format!("/usr/bin/git push https://{user}:{token}@{hostname}/{path}.git {gitref}:{branch}");
// info!("Running command: {}", command);
match gix::command::prepare(command)
.with_shell_allow_argument_splitting()
.spawn()
{
Ok(mut child) => match child.wait() {
Ok(_) => Ok(()),
Err(err) => {
warn!(?err, "Advance Next Failed (wait)");
Err(err)
}
},
Err(err) => {
warn!(?err, "Advance Next Failed (spawn)");
Err(err)
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;