From a645273433fe3a716dd9c43ad9276da066af6127 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 16 Feb 2024 20:41:59 +0000 Subject: refactor: remove reliance on 'commit' tag as part of nip34 compliance --- src/sub_commands/list.rs | 19 +++++++++++++++---- src/sub_commands/push.rs | 6 +++--- 2 files changed, 18 insertions(+), 7 deletions(-) (limited to 'src/sub_commands') diff --git a/src/sub_commands/list.rs b/src/sub_commands/list.rs index 413406a..be5b97f 100644 --- a/src/sub_commands/list.rs +++ b/src/sub_commands/list.rs @@ -131,6 +131,18 @@ pub fn tag_value(event: &nostr::Event, tag_name: &str) -> Result { .clone()) } +pub fn get_commit_id_from_patch(event: &nostr::Event) -> Result { + let value = tag_value(event, "commit"); + + if value.is_ok() { + value + } else if event.content.starts_with("From ") && event.content.len().gt(&45) { + Ok(event.content[5..45].to_string()) + } else { + bail!("event is not a patch") + } +} + pub fn get_most_recent_patch_with_ancestors( mut patches: Vec, ) -> Result> { @@ -143,14 +155,14 @@ pub fn get_most_recent_patch_with_ancestors( .filter(|p| p.created_at.eq(&first_patch.created_at)) .collect(); - let latest_commit_id = tag_value( + let latest_commit_id = get_commit_id_from_patch( // get the first patch which isn't a parent of a patch event created at the same // time patches_with_youngest_created_at .clone() .iter() .find(|p| { - if let Ok(commit) = tag_value(p, "commit") { + if let Ok(commit) = get_commit_id_from_patch(p) { !patches_with_youngest_created_at.iter().any(|p2| { if let Ok(parent) = tag_value(p2, "parent-commit") { commit.eq(&parent) @@ -163,7 +175,6 @@ pub fn get_most_recent_patch_with_ancestors( } }) .context("cannot find patches_with_youngest_created_at")?, - "commit", )?; let mut res = vec![]; @@ -171,7 +182,7 @@ pub fn get_most_recent_patch_with_ancestors( let mut commit_id_to_search = latest_commit_id; while let Some(event) = patches.iter().find(|e| { - if let Ok(commit) = tag_value(e, "commit") { + if let Ok(commit) = get_commit_id_from_patch(e) { commit.eq(&commit_id_to_search) } else { false // skip diff --git a/src/sub_commands/push.rs b/src/sub_commands/push.rs index cc1f480..7c6b95b 100644 --- a/src/sub_commands/push.rs +++ b/src/sub_commands/push.rs @@ -12,8 +12,8 @@ use crate::{ repo_ref::{self, RepoRef}, sub_commands::{ list::{ - find_commits_for_pr_event, find_pr_events, get_most_recent_patch_with_ancestors, - tag_value, + find_commits_for_pr_event, find_pr_events, get_commit_id_from_patch, + get_most_recent_patch_with_ancestors, tag_value, }, send::{event_to_cover_letter, generate_patch_event, send_events}, }, @@ -66,7 +66,7 @@ pub async fn launch(cli_args: &Cli) -> Result<()> { let branch_tip = git_repo.get_tip_of_local_branch(&branch_name)?; let most_recent_patch_commit_id = str_to_sha1( - &tag_value(&most_recent_pr_patch_chain[0], "commit") + &get_commit_id_from_patch(&most_recent_pr_patch_chain[0]) .context("latest patch event doesnt have a commit tag")?, ) .context("latest patch event commit tag isn't a valid SHA1 hash")?; -- cgit v1.2.3