upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/repo_state.rs
blob: 33bc90fed32cc2d1e3c40e1036eef2491633b903 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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<nostr::Event>) -> Result<Self> {
        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(),
        })
    }
}