From 0848d57c7654d09c3041022c231dfbbbfac17600 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Tue, 30 Jul 2024 14:14:59 +0100 Subject: refactor: add struct `RepoState` to more easily access state details --- src/client.rs | 22 ++++++++++------------ src/git_remote_helper.rs | 1 + src/main.rs | 1 + src/repo_state.rs | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 src/repo_state.rs (limited to 'src') diff --git a/src/client.rs b/src/client.rs index 6fce7a5..db41f99 100644 --- a/src/client.rs +++ b/src/client.rs @@ -37,6 +37,7 @@ use crate::{ config::get_dirs, login::{get_logged_in_user, get_user_ref_from_cache}, repo_ref::RepoRef, + repo_state::RepoState, sub_commands::{ list::status_kinds, send::{event_is_patch_set_root, event_is_revision_root}, @@ -841,17 +842,14 @@ pub async fn get_repo_ref_from_cache( }) } -pub async fn get_state_from_cache( - git_repo_path: &Path, - repo_ref: &RepoRef, -) -> Result> { - let mut state_events = get_events_from_cache( - git_repo_path, - vec![get_filter_state_events(&repo_ref.coordinates())], +pub async fn get_state_from_cache(git_repo_path: &Path, repo_ref: &RepoRef) -> Result { + RepoState::try_from( + get_events_from_cache( + git_repo_path, + vec![get_filter_state_events(&repo_ref.coordinates())], + ) + .await?, ) - .await?; - state_events.sort_by_key(|e| e.created_at); - Ok(state_events.first().map(std::borrow::ToOwned::to_owned)) } #[allow(clippy::too_many_lines)] @@ -1062,8 +1060,8 @@ async fn create_relays_request( .collect() }, state: if let Ok(repo_ref) = &repo_ref { - if let Ok(Some(existing_state)) = get_state_from_cache(git_repo_path, repo_ref).await { - Some((existing_state.created_at, existing_state.id)) + if let Ok(existing_state) = get_state_from_cache(git_repo_path, repo_ref).await { + Some((existing_state.event.created_at, existing_state.event.id)) } else { None } diff --git a/src/git_remote_helper.rs b/src/git_remote_helper.rs index 44f7fb5..ba8ab61 100644 --- a/src/git_remote_helper.rs +++ b/src/git_remote_helper.rs @@ -37,6 +37,7 @@ mod git; mod key_handling; mod login; mod repo_ref; +mod repo_state; mod sub_commands; #[tokio::main] diff --git a/src/main.rs b/src/main.rs index add26f1..e833e4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ mod git; mod key_handling; mod login; mod repo_ref; +mod repo_state; mod sub_commands; #[tokio::main] diff --git a/src/repo_state.rs b/src/repo_state.rs new file mode 100644 index 0000000..33bc90f --- /dev/null +++ b/src/repo_state.rs @@ -0,0 +1,33 @@ +use anyhow::{Context, Result}; +use git2::Oid; + +pub struct RepoState { + pub state: Vec<(String, String)>, + pub event: nostr::Event, +} + +impl RepoState { + pub fn try_from(mut state_events: Vec) -> Result { + state_events.sort_by_key(|e| e.created_at); + let event = state_events.first().context("no state events")?; + let mut state = vec![]; + for tag in &event.tags { + if let Some(name) = tag.as_vec().first() { + if ["refs/heads/", "refs/tags", "HEAD"] + .iter() + .any(|s| name.starts_with(*s)) + { + if let Some(value) = tag.as_vec().get(1) { + if Oid::from_str(value).is_ok() { + state.push((name.to_owned(), value.to_owned())); + } + } + } + } + } + Ok(RepoState { + state, + event: event.clone(), + }) + } +} -- cgit v1.2.3