diff --git a/bin/periphery/src/api/git.rs b/bin/periphery/src/api/git.rs index 9336e8a30..b771f4dcf 100644 --- a/bin/periphery/src/api/git.rs +++ b/bin/periphery/src/api/git.rs @@ -1,9 +1,30 @@ +use anyhow::anyhow; use monitor_client::entities::{to_monitor_name, update::Log}; -use periphery_client::api::git::{CloneRepo, DeleteRepo, PullRepo}; +use periphery_client::api::git::{ + CloneRepo, DeleteRepo, GetLatestCommit, GetLatestCommitResponse, + PullRepo, +}; use resolver_api::Resolve; use crate::{config::periphery_config, helpers::git, State}; +#[async_trait::async_trait] +impl Resolve for State { + async fn resolve( + &self, + GetLatestCommit { name }: GetLatestCommit, + _: (), + ) -> anyhow::Result { + let repo_path = periphery_config().repo_dir.join(name); + if !repo_path.is_dir() { + return Err(anyhow!( + "repo path is not directory. is it cloned?" + )); + } + git::get_commit_hash_info(&repo_path).await + } +} + #[async_trait::async_trait] impl Resolve for State { #[instrument(name = "CloneRepo", skip(self))] diff --git a/bin/periphery/src/helpers/git.rs b/bin/periphery/src/helpers/git.rs index 13ea68437..451ffcce4 100644 --- a/bin/periphery/src/helpers/git.rs +++ b/bin/periphery/src/helpers/git.rs @@ -6,6 +6,7 @@ use monitor_client::entities::{ monitor_timestamp, to_monitor_name, update::Log, CloneArgs, SystemCommand, }; +use periphery_client::api::git::GetLatestCommitResponse; use run_command::async_run_command; use crate::config::periphery_config; @@ -181,6 +182,26 @@ async fn clone_inner( } } +pub async fn get_commit_hash_info( + repo_dir: &Path, +) -> anyhow::Result { + let command = format!("cd {} && git rev-parse --short HEAD && git rev-parse HEAD && git log -1 --pretty=%B", repo_dir.display()); + let output = async_run_command(&command).await; + let mut split = output.stdout.split('\n'); + let (hash, _, message) = ( + split + .next() + .context("failed to get short commit hash")? + .to_string(), + split.next().context("failed to get long commit hash")?, + split + .next() + .context("failed to get commit message")? + .to_string(), + ); + Ok(GetLatestCommitResponse { hash, message }) +} + #[instrument] async fn get_commit_hash_log(repo_dir: &Path) -> anyhow::Result { let start_ts = monitor_timestamp(); diff --git a/client/periphery/rs/src/api/git.rs b/client/periphery/rs/src/api/git.rs index fa0e24f9e..0b87e57ef 100644 --- a/client/periphery/rs/src/api/git.rs +++ b/client/periphery/rs/src/api/git.rs @@ -4,6 +4,18 @@ use monitor_client::entities::{ use resolver_api::derive::Request; use serde::{Deserialize, Serialize}; +#[derive(Debug, Clone, Serialize, Deserialize, Request)] +#[response(GetLatestCommitResponse)] +pub struct GetLatestCommit { + pub name: String, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct GetLatestCommitResponse { + pub hash: String, + pub message: String, +} + #[derive(Serialize, Deserialize, Debug, Clone, Request)] #[response(Vec)] pub struct CloneRepo {