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/bin/git_remote_nostr/utils.rs23
-rw-r--r--src/bin/ngit/sub_commands/pull.rs17
-rw-r--r--src/bin/ngit/sub_commands/push.rs18
-rw-r--r--src/lib/git_events.rs18
4 files changed, 33 insertions, 43 deletions
diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs
index 15a0d76..bbe7cfa 100644
--- a/src/bin/git_remote_nostr/utils.rs
+++ b/src/bin/git_remote_nostr/utils.rs
@@ -16,8 +16,8 @@ use ngit::{
16 Repo, RepoActions, 16 Repo, RepoActions,
17 }, 17 },
18 git_events::{ 18 git_events::{
19 event_is_revision_root, event_to_cover_letter, get_most_recent_patch_with_ancestors, 19 event_is_revision_root, get_most_recent_patch_with_ancestors,
20 status_kinds, 20 is_event_proposal_root_for_branch, status_kinds,
21 }, 21 },
22 repo_ref::RepoRef, 22 repo_ref::RepoRef,
23}; 23};
@@ -189,24 +189,7 @@ pub fn find_proposal_and_patches_by_branch_name<'a>(
189 current_user: &Option<PublicKey>, 189 current_user: &Option<PublicKey>,
190) -> Option<(&'a EventId, &'a (Event, Vec<Event>))> { 190) -> Option<(&'a EventId, &'a (Event, Vec<Event>))> {
191 open_proposals.iter().find(|(_, (proposal, _))| { 191 open_proposals.iter().find(|(_, (proposal, _))| {
192 if let Ok(cl) = event_to_cover_letter(proposal) { 192 is_event_proposal_root_for_branch(proposal, refstr, current_user).unwrap_or(false)
193 if let Ok(mut branch_name) = cl.get_branch_name() {
194 branch_name = if let Some(public_key) = current_user {
195 if proposal.author().eq(public_key) {
196 cl.branch_name.to_string()
197 } else {
198 branch_name
199 }
200 } else {
201 branch_name
202 };
203 branch_name.eq(&refstr.replace("refs/heads/", ""))
204 } else {
205 false
206 }
207 } else {
208 false
209 }
210 }) 193 })
211} 194}
212 195
diff --git a/src/bin/ngit/sub_commands/pull.rs b/src/bin/ngit/sub_commands/pull.rs
index eba6fc5..9a3d911 100644
--- a/src/bin/ngit/sub_commands/pull.rs
+++ b/src/bin/ngit/sub_commands/pull.rs
@@ -1,4 +1,5 @@
1use anyhow::{bail, Context, Result}; 1use anyhow::{bail, Context, Result};
2use ngit::git_events::is_event_proposal_root_for_branch;
2use nostr_sdk::PublicKey; 3use nostr_sdk::PublicKey;
3 4
4use crate::{ 5use crate::{
@@ -7,10 +8,7 @@ use crate::{
7 get_proposals_and_revisions_from_cache, get_repo_ref_from_cache, Client, Connect, 8 get_proposals_and_revisions_from_cache, get_repo_ref_from_cache, Client, Connect,
8 }, 9 },
9 git::{str_to_sha1, Repo, RepoActions}, 10 git::{str_to_sha1, Repo, RepoActions},
10 git_events::{ 11 git_events::{get_commit_id_from_patch, get_most_recent_patch_with_ancestors, tag_value},
11 event_is_revision_root, event_to_cover_letter, get_commit_id_from_patch,
12 get_most_recent_patch_with_ancestors, tag_value,
13 },
14 repo_ref::get_repo_coordinates, 12 repo_ref::get_repo_coordinates,
15}; 13};
16 14
@@ -37,7 +35,7 @@ pub async fn launch() -> Result<()> {
37 35
38 let repo_ref = get_repo_ref_from_cache(git_repo_path, &repo_coordinates).await?; 36 let repo_ref = get_repo_ref_from_cache(git_repo_path, &repo_coordinates).await?;
39 37
40 let public_key_if_known = 38 let logged_in_public_key =
41 if let Ok(Some(npub)) = git_repo.get_git_config_item("nostr.npub", None) { 39 if let Ok(Some(npub)) = git_repo.get_git_config_item("nostr.npub", None) {
42 PublicKey::parse(npub).ok() 40 PublicKey::parse(npub).ok()
43 } else { 41 } else {
@@ -49,15 +47,12 @@ pub async fn launch() -> Result<()> {
49 .await? 47 .await?
50 .iter() 48 .iter()
51 .find(|e| { 49 .find(|e| {
52 event_to_cover_letter(e).is_ok_and(|cl| { 50 is_event_proposal_root_for_branch(e, &branch_name, &logged_in_public_key)
53 (public_key_if_known.is_some_and(|public_key| e.author().eq(&public_key)) 51 .unwrap_or(false)
54 && (branch_name.eq(&format!("pr/{}", cl.branch_name))
55 || cl.branch_name.eq(&branch_name)))
56 || cl.get_branch_name().is_ok_and(|s| s.eq(&branch_name))
57 }) && !event_is_revision_root(e)
58 }) 52 })
59 .context("cannot find proposal that matches the current branch name")? 53 .context("cannot find proposal that matches the current branch name")?
60 .clone(); 54 .clone();
55
61 let commit_events = get_all_proposal_patch_events_from_cache( 56 let commit_events = get_all_proposal_patch_events_from_cache(
62 git_repo_path, 57 git_repo_path,
63 &repo_ref, 58 &repo_ref,
diff --git a/src/bin/ngit/sub_commands/push.rs b/src/bin/ngit/sub_commands/push.rs
index 7cdc655..66edfb4 100644
--- a/src/bin/ngit/sub_commands/push.rs
+++ b/src/bin/ngit/sub_commands/push.rs
@@ -1,5 +1,8 @@
1use anyhow::{bail, Context, Result}; 1use anyhow::{bail, Context, Result};
2use ngit::{client::send_events, git_events::tag_value}; 2use ngit::{
3 client::send_events,
4 git_events::{is_event_proposal_root_for_branch, tag_value},
5};
3use nostr_sdk::PublicKey; 6use nostr_sdk::PublicKey;
4 7
5use crate::{ 8use crate::{
@@ -10,8 +13,7 @@ use crate::{
10 }, 13 },
11 git::{identify_ahead_behind, str_to_sha1, Repo, RepoActions}, 14 git::{identify_ahead_behind, str_to_sha1, Repo, RepoActions},
12 git_events::{ 15 git_events::{
13 event_is_revision_root, event_to_cover_letter, generate_patch_event, 16 generate_patch_event, get_commit_id_from_patch, get_most_recent_patch_with_ancestors,
14 get_commit_id_from_patch, get_most_recent_patch_with_ancestors,
15 }, 17 },
16 login, 18 login,
17 repo_ref::get_repo_coordinates, 19 repo_ref::get_repo_coordinates,
@@ -53,7 +55,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
53 55
54 let repo_ref = get_repo_ref_from_cache(git_repo_path, &repo_coordinates).await?; 56 let repo_ref = get_repo_ref_from_cache(git_repo_path, &repo_coordinates).await?;
55 57
56 let public_key_if_known = 58 let logged_in_public_key =
57 if let Ok(Some(npub)) = git_repo.get_git_config_item("nostr.npub", None) { 59 if let Ok(Some(npub)) = git_repo.get_git_config_item("nostr.npub", None) {
58 PublicKey::parse(npub).ok() 60 PublicKey::parse(npub).ok()
59 } else { 61 } else {
@@ -65,12 +67,8 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
65 .await? 67 .await?
66 .iter() 68 .iter()
67 .find(|e| { 69 .find(|e| {
68 event_to_cover_letter(e).is_ok_and(|cl| { 70 is_event_proposal_root_for_branch(e, &branch_name, &logged_in_public_key)
69 (public_key_if_known.is_some_and(|public_key| e.author().eq(&public_key)) 71 .unwrap_or(false)
70 && (branch_name.eq(&format!("pr/{}", cl.branch_name))
71 || cl.branch_name.eq(&branch_name)))
72 || cl.get_branch_name().is_ok_and(|s| s.eq(&branch_name))
73 }) && !event_is_revision_root(e)
74 }) 72 })
75 .context("cannot find proposal that matches the current branch name")? 73 .context("cannot find proposal that matches the current branch name")?
76 .clone(); 74 .clone();
diff --git a/src/lib/git_events.rs b/src/lib/git_events.rs
index b4ac676..d818f4c 100644
--- a/src/lib/git_events.rs
+++ b/src/lib/git_events.rs
@@ -3,8 +3,8 @@ use std::str::FromStr;
3use anyhow::{bail, Context, Result}; 3use anyhow::{bail, Context, Result};
4use nostr::nips::{nip01::Coordinate, nip10::Marker, nip19::Nip19}; 4use nostr::nips::{nip01::Coordinate, nip10::Marker, nip19::Nip19};
5use nostr_sdk::{ 5use nostr_sdk::{
6 hashes::sha1::Hash as Sha1Hash, Event, EventBuilder, EventId, FromBech32, Kind, Tag, TagKind, 6 hashes::sha1::Hash as Sha1Hash, Event, EventBuilder, EventId, FromBech32, Kind, PublicKey, Tag,
7 TagStandard, UncheckedUrl, 7 TagKind, TagStandard, UncheckedUrl,
8}; 8};
9use nostr_signer::NostrSigner; 9use nostr_signer::NostrSigner;
10 10
@@ -594,6 +594,20 @@ fn get_event_parent_id(event: &nostr::Event) -> Result<String> {
594 .clone()) 594 .clone())
595} 595}
596 596
597pub fn is_event_proposal_root_for_branch(
598 e: &Event,
599 branch_name_or_refstr: &str,
600 logged_in_user: &Option<PublicKey>,
601) -> Result<bool> {
602 let branch_name = branch_name_or_refstr.replace("refs/heads/", "");
603 Ok(event_to_cover_letter(e).is_ok_and(|cl| {
604 (logged_in_user.is_some_and(|public_key| e.author().eq(&public_key))
605 && (branch_name.eq(&format!("pr/{}", cl.branch_name))
606 || cl.branch_name.eq(&branch_name)))
607 || cl.get_branch_name().is_ok_and(|s| s.eq(&branch_name))
608 }) && !event_is_revision_root(e))
609}
610
597#[cfg(test)] 611#[cfg(test)]
598mod tests { 612mod tests {
599 use super::*; 613 use super::*;