From 9386cc2e409cc4117ae1792411179a9cd986b3f3 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 19 Jun 2025 09:30:16 +0100 Subject: refactor: move build state function in preparation for enforcing the inclusion of HEAD --- src/bin/git_remote_nostr/push.rs | 37 +------------------------------------ src/lib/repo_state.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs index 56bf177..434dae9 100644 --- a/src/bin/git_remote_nostr/push.rs +++ b/src/bin/git_remote_nostr/push.rs @@ -8,9 +8,7 @@ use std::{ use anyhow::{Context, Result, anyhow, bail}; use auth_git2::GitAuthenticator; -use client::{ - STATE_KIND, get_events_from_local_cache, get_state_from_cache, send_events, sign_event, -}; +use client::{get_events_from_local_cache, get_state_from_cache, send_events, sign_event}; use console::Term; use git::{RepoActions, sha1_to_oid}; use git_events::{ @@ -1549,39 +1547,6 @@ fn get_refspecs_from_push_batch(stdin: &Stdin, initial_refspec: &str) -> Result< Ok(refspecs) } -trait BuildRepoState { - async fn build( - identifier: String, - state: HashMap, - signer: &Arc, - ) -> Result; -} -impl BuildRepoState for RepoState { - async fn build( - identifier: String, - state: HashMap, - signer: &Arc, - ) -> Result { - let mut tags = vec![Tag::identifier(identifier.clone())]; - for (name, value) in &state { - tags.push(Tag::custom(nostr_sdk::TagKind::Custom(name.into()), vec![ - value.clone(), - ])); - } - let event = sign_event( - EventBuilder::new(STATE_KIND, "").tags(tags), - signer, - "git state".to_string(), - ) - .await?; - Ok(RepoState { - identifier, - state, - event, - }) - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/lib/repo_state.rs b/src/lib/repo_state.rs index aa60f54..8eba1af 100644 --- a/src/lib/repo_state.rs +++ b/src/lib/repo_state.rs @@ -1,7 +1,13 @@ -use std::collections::HashMap; +use std::{collections::HashMap, sync::Arc}; use anyhow::{Context, Result}; use git2::Oid; +use nostr::{ + event::{EventBuilder, Tag}, + signer::NostrSigner, +}; + +use crate::client::{STATE_KIND, sign_event}; pub struct RepoState { pub identifier: String, @@ -58,4 +64,28 @@ impl RepoState { event: event.clone(), }) } + + pub async fn build( + identifier: String, + state: HashMap, + signer: &Arc, + ) -> Result { + let mut tags = vec![Tag::identifier(identifier.clone())]; + for (name, value) in &state { + tags.push(Tag::custom(nostr_sdk::TagKind::Custom(name.into()), vec![ + value.clone(), + ])); + } + let event = sign_event( + EventBuilder::new(STATE_KIND, "").tags(tags), + signer, + "git state".to_string(), + ) + .await?; + Ok(RepoState { + identifier, + state, + event, + }) + } } -- cgit v1.2.3