From ed1ab6faa519969c39c054af560a0b7ee08c18c5 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 11 Apr 2024 14:20:59 +0100 Subject: [PATCH] feat: add branch::reset function --- src/server/actors/repo/branch.rs | 56 +++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/src/server/actors/repo/branch.rs b/src/server/actors/repo/branch.rs index 66e7b199..179af93b 100644 --- a/src/server/actors/repo/branch.rs +++ b/src/server/actors/repo/branch.rs @@ -1,9 +1,14 @@ +use std::fmt::Display; + use actix::prelude::*; use kxio::network; use tracing::{error, info, warn}; -use crate::server::{config, forge}; +use crate::server::{ + config::{self, BranchName}, + forge, +}; use super::{RepoActor, StartMonitoring, StartRepo}; @@ -177,6 +182,55 @@ pub async fn advance_main( }; } +#[derive(Clone, Debug)] +pub struct GitRef(pub String); +impl From for GitRef { + fn from(value: forge::Commit) -> Self { + Self(value.sha) + } +} +impl From 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, + 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)] mod tests { use super::*;