diff options
| -rw-r--r-- | src/client.rs | 22 | ||||
| -rw-r--r-- | src/git_remote_helper.rs | 1 | ||||
| -rw-r--r-- | src/main.rs | 1 | ||||
| -rw-r--r-- | src/repo_state.rs | 33 |
4 files changed, 45 insertions, 12 deletions
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::{ | |||
| 37 | config::get_dirs, | 37 | config::get_dirs, |
| 38 | login::{get_logged_in_user, get_user_ref_from_cache}, | 38 | login::{get_logged_in_user, get_user_ref_from_cache}, |
| 39 | repo_ref::RepoRef, | 39 | repo_ref::RepoRef, |
| 40 | repo_state::RepoState, | ||
| 40 | sub_commands::{ | 41 | sub_commands::{ |
| 41 | list::status_kinds, | 42 | list::status_kinds, |
| 42 | send::{event_is_patch_set_root, event_is_revision_root}, | 43 | send::{event_is_patch_set_root, event_is_revision_root}, |
| @@ -841,17 +842,14 @@ pub async fn get_repo_ref_from_cache( | |||
| 841 | }) | 842 | }) |
| 842 | } | 843 | } |
| 843 | 844 | ||
| 844 | pub async fn get_state_from_cache( | 845 | pub async fn get_state_from_cache(git_repo_path: &Path, repo_ref: &RepoRef) -> Result<RepoState> { |
| 845 | git_repo_path: &Path, | 846 | RepoState::try_from( |
| 846 | repo_ref: &RepoRef, | 847 | get_events_from_cache( |
| 847 | ) -> Result<Option<nostr::Event>> { | 848 | git_repo_path, |
| 848 | let mut state_events = get_events_from_cache( | 849 | vec![get_filter_state_events(&repo_ref.coordinates())], |
| 849 | git_repo_path, | 850 | ) |
| 850 | vec![get_filter_state_events(&repo_ref.coordinates())], | 851 | .await?, |
| 851 | ) | 852 | ) |
| 852 | .await?; | ||
| 853 | state_events.sort_by_key(|e| e.created_at); | ||
| 854 | Ok(state_events.first().map(std::borrow::ToOwned::to_owned)) | ||
| 855 | } | 853 | } |
| 856 | 854 | ||
| 857 | #[allow(clippy::too_many_lines)] | 855 | #[allow(clippy::too_many_lines)] |
| @@ -1062,8 +1060,8 @@ async fn create_relays_request( | |||
| 1062 | .collect() | 1060 | .collect() |
| 1063 | }, | 1061 | }, |
| 1064 | state: if let Ok(repo_ref) = &repo_ref { | 1062 | state: if let Ok(repo_ref) = &repo_ref { |
| 1065 | if let Ok(Some(existing_state)) = get_state_from_cache(git_repo_path, repo_ref).await { | 1063 | if let Ok(existing_state) = get_state_from_cache(git_repo_path, repo_ref).await { |
| 1066 | Some((existing_state.created_at, existing_state.id)) | 1064 | Some((existing_state.event.created_at, existing_state.event.id)) |
| 1067 | } else { | 1065 | } else { |
| 1068 | None | 1066 | None |
| 1069 | } | 1067 | } |
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; | |||
| 37 | mod key_handling; | 37 | mod key_handling; |
| 38 | mod login; | 38 | mod login; |
| 39 | mod repo_ref; | 39 | mod repo_ref; |
| 40 | mod repo_state; | ||
| 40 | mod sub_commands; | 41 | mod sub_commands; |
| 41 | 42 | ||
| 42 | #[tokio::main] | 43 | #[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; | |||
| 14 | mod key_handling; | 14 | mod key_handling; |
| 15 | mod login; | 15 | mod login; |
| 16 | mod repo_ref; | 16 | mod repo_ref; |
| 17 | mod repo_state; | ||
| 17 | mod sub_commands; | 18 | mod sub_commands; |
| 18 | 19 | ||
| 19 | #[tokio::main] | 20 | #[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 @@ | |||
| 1 | use anyhow::{Context, Result}; | ||
| 2 | use git2::Oid; | ||
| 3 | |||
| 4 | pub struct RepoState { | ||
| 5 | pub state: Vec<(String, String)>, | ||
| 6 | pub event: nostr::Event, | ||
| 7 | } | ||
| 8 | |||
| 9 | impl RepoState { | ||
| 10 | pub fn try_from(mut state_events: Vec<nostr::Event>) -> Result<Self> { | ||
| 11 | state_events.sort_by_key(|e| e.created_at); | ||
| 12 | let event = state_events.first().context("no state events")?; | ||
| 13 | let mut state = vec![]; | ||
| 14 | for tag in &event.tags { | ||
| 15 | if let Some(name) = tag.as_vec().first() { | ||
| 16 | if ["refs/heads/", "refs/tags", "HEAD"] | ||
| 17 | .iter() | ||
| 18 | .any(|s| name.starts_with(*s)) | ||
| 19 | { | ||
| 20 | if let Some(value) = tag.as_vec().get(1) { | ||
| 21 | if Oid::from_str(value).is_ok() { | ||
| 22 | state.push((name.to_owned(), value.to_owned())); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | } | ||
| 26 | } | ||
| 27 | } | ||
| 28 | Ok(RepoState { | ||
| 29 | state, | ||
| 30 | event: event.clone(), | ||
| 31 | }) | ||
| 32 | } | ||
| 33 | } | ||