From f05d32838e212fcb506d6b211137e9dc2457786c Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 19 Jun 2025 09:42:54 +0100 Subject: fix: add state HEAD on creation try and add the HEAD when the state event is built, rather than just when its parsed. --- src/lib/repo_state.rs | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/lib/repo_state.rs b/src/lib/repo_state.rs index 8eba1af..04f3cf2 100644 --- a/src/lib/repo_state.rs +++ b/src/lib/repo_state.rs @@ -36,24 +36,7 @@ impl RepoState { } } } - // Include a HEAD if one isn't listed to prevent errors when users git config - // default branch isn't in the state event - if !state.contains_key("HEAD") { - if state.contains_key("refs/heads/master") { - state.insert("HEAD".to_string(), "ref: refs/heads/master".to_string()); - } else if state.contains_key("refs/heads/main") { - state.insert("HEAD".to_string(), "ref: refs/heads/main".to_string()); - } else if let Some(tag) = event - .tags - .iter() - .find(|t| t.len() > 1 && t.as_slice()[0].starts_with("refs/heads/")) - { - state.insert( - "HEAD".to_string(), - format!("ref: {}", tag.clone().to_vec()[0]), - ); - } - } + add_head(&mut state); Ok(RepoState { identifier: event .tags @@ -67,9 +50,10 @@ impl RepoState { pub async fn build( identifier: String, - state: HashMap, + mut state: HashMap, signer: &Arc, ) -> Result { + add_head(&mut state); let mut tags = vec![Tag::identifier(identifier.clone())]; for (name, value) in &state { tags.push(Tag::custom(nostr_sdk::TagKind::Custom(name.into()), vec![ @@ -89,3 +73,17 @@ impl RepoState { }) } } + +// Include a HEAD if one isn't listed to prevent errors when users git config +// default branch isn't in the state event +fn add_head(state: &mut HashMap) { + if !state.contains_key("HEAD") { + if state.contains_key("refs/heads/master") { + state.insert("HEAD".to_string(), "ref: refs/heads/master".to_string()); + } else if state.contains_key("refs/heads/main") { + state.insert("HEAD".to_string(), "ref: refs/heads/main".to_string()); + } else if let Some(k) = state.keys().find(|k| k.starts_with("refs/heads/")) { + state.insert("HEAD".to_string(), format!("ref: {k}")); + } + } +} -- cgit v1.2.3