upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/git_remote_helper.rs40
-rw-r--r--src/repo_state.rs10
2 files changed, 18 insertions, 32 deletions
diff --git a/src/git_remote_helper.rs b/src/git_remote_helper.rs
index 68aa681..08a59c1 100644
--- a/src/git_remote_helper.rs
+++ b/src/git_remote_helper.rs
@@ -6,7 +6,7 @@
6 6
7use core::str; 7use core::str;
8use std::{ 8use std::{
9 collections::HashSet, 9 collections::{HashMap, HashSet},
10 env, 10 env,
11 io::{self, Stdin}, 11 io::{self, Stdin},
12 path::PathBuf, 12 path::PathBuf,
@@ -261,41 +261,25 @@ async fn generate_updated_state(
261 git_repo: &Repo, 261 git_repo: &Repo,
262 repo_ref: &RepoRef, 262 repo_ref: &RepoRef,
263 refspecs: &Vec<String>, 263 refspecs: &Vec<String>,
264) -> Result<Vec<(String, String)>> { 264) -> Result<HashMap<String, String>> {
265 let new_state = { 265 let new_state = {
266 if let Ok(mut repo_state) = get_state_from_cache(git_repo.get_path()?, repo_ref).await { 266 if let Ok(mut repo_state) = get_state_from_cache(git_repo.get_path()?, repo_ref).await {
267 for refspec in refspecs { 267 for refspec in refspecs {
268 let (from, to) = refspec_to_from_to(refspec)?; 268 let (from, to) = refspec_to_from_to(refspec)?;
269 if to.is_empty() { 269 if from.is_empty() {
270 // delete 270 // delete
271 repo_state.state.retain(|(name, _)| !name.eq(to)); 271 repo_state.state.remove(to);
272 } else if repo_state.state.iter().any(|(name, _)| name.eq(from)) {
273 // update
274 repo_state.state = repo_state
275 .state
276 .iter()
277 .map(|(name, value)| {
278 (
279 name.clone(),
280 if name.eq(to) {
281 reference_to_ref_value(&git_repo.git_repo, to).unwrap()
282 } else {
283 value.to_string()
284 },
285 )
286 })
287 .collect();
288 } else { 272 } else {
289 // add 273 // add or update
290 repo_state.state.push(( 274 repo_state.state.insert(
291 to.to_string(), 275 to.to_string(),
292 reference_to_ref_value(&git_repo.git_repo, to).unwrap(), 276 reference_to_ref_value(&git_repo.git_repo, to).unwrap(),
293 )); 277 );
294 } 278 }
295 } 279 }
296 repo_state.state 280 repo_state.state
297 } else { 281 } else {
298 let mut state = vec![]; 282 let mut state = HashMap::new();
299 let git_server_url = repo_ref 283 let git_server_url = repo_ref
300 .git_server 284 .git_server
301 .first() 285 .first()
@@ -303,14 +287,14 @@ async fn generate_updated_state(
303 let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?; 287 let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?;
304 git_server_remote.connect(git2::Direction::Fetch)?; 288 git_server_remote.connect(git2::Direction::Fetch)?;
305 for head in git_server_remote.list()? { 289 for head in git_server_remote.list()? {
306 state.push(( 290 state.insert(
307 head.name().to_string(), 291 head.name().to_string(),
308 if let Some(symbolic_ref) = head.symref_target() { 292 if let Some(symbolic_ref) = head.symref_target() {
309 format!("ref: {}", symbolic_ref) 293 format!("ref: {symbolic_ref}")
310 } else { 294 } else {
311 head.oid().to_string() 295 head.oid().to_string()
312 }, 296 },
313 )); 297 );
314 } 298 }
315 git_server_remote.disconnect()?; 299 git_server_remote.disconnect()?;
316 state 300 state
@@ -460,7 +444,7 @@ fn get_refspecs_from_push_batch(stdin: &Stdin, initial_refspec: &str) -> Result<
460impl RepoState { 444impl RepoState {
461 pub async fn build( 445 pub async fn build(
462 identifier: String, 446 identifier: String,
463 state: Vec<(String, String)>, 447 state: HashMap<String, String>,
464 signer: &NostrSigner, 448 signer: &NostrSigner,
465 ) -> Result<RepoState> { 449 ) -> Result<RepoState> {
466 let mut tags = vec![Tag::identifier(identifier.clone())]; 450 let mut tags = vec![Tag::identifier(identifier.clone())];
diff --git a/src/repo_state.rs b/src/repo_state.rs
index 0c1aa30..a5cebab 100644
--- a/src/repo_state.rs
+++ b/src/repo_state.rs
@@ -1,9 +1,11 @@
1use std::collections::HashMap;
2
1use anyhow::{Context, Result}; 3use anyhow::{Context, Result};
2use git2::Oid; 4use git2::Oid;
3 5
4pub struct RepoState { 6pub struct RepoState {
5 pub identifier: String, 7 pub identifier: String,
6 pub state: Vec<(String, String)>, 8 pub state: HashMap<String, String>,
7 pub event: nostr::Event, 9 pub event: nostr::Event,
8} 10}
9 11
@@ -11,7 +13,7 @@ impl RepoState {
11 pub fn try_from(mut state_events: Vec<nostr::Event>) -> Result<Self> { 13 pub fn try_from(mut state_events: Vec<nostr::Event>) -> Result<Self> {
12 state_events.sort_by_key(|e| e.created_at); 14 state_events.sort_by_key(|e| e.created_at);
13 let event = state_events.first().context("no state events")?; 15 let event = state_events.first().context("no state events")?;
14 let mut state = vec![]; 16 let mut state = HashMap::new();
15 for tag in &event.tags { 17 for tag in &event.tags {
16 if let Some(name) = tag.as_vec().first() { 18 if let Some(name) = tag.as_vec().first() {
17 if ["refs/heads/", "refs/tags", "HEAD"] 19 if ["refs/heads/", "refs/tags", "HEAD"]
@@ -19,8 +21,8 @@ impl RepoState {
19 .any(|s| name.starts_with(*s)) 21 .any(|s| name.starts_with(*s))
20 { 22 {
21 if let Some(value) = tag.as_vec().get(1) { 23 if let Some(value) = tag.as_vec().get(1) {
22 if Oid::from_str(value).is_ok() { 24 if Oid::from_str(value).is_ok() || value.contains("ref: refs/") {
23 state.push((name.to_owned(), value.to_owned())); 25 state.insert(name.to_owned(), value.to_owned());
24 } 26 }
25 } 27 }
26 } 28 }