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/git.rs | 13 ++++++++----- src/sub_commands/list.rs | 19 +++++++++++++++---- src/sub_commands/push.rs | 6 +++--- 3 files changed, 26 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/git.rs b/src/git.rs index 113a63c..51b27fa 100644 --- a/src/git.rs +++ b/src/git.rs @@ -6,7 +6,7 @@ use anyhow::{bail, Context, Result}; use git2::{DiffOptions, Oid, Revwalk}; use nostr::prelude::{sha1::Hash as Sha1Hash, Hash}; -use crate::sub_commands::list::tag_value; +use crate::sub_commands::list::{get_commit_id_from_patch, tag_value}; pub struct Repo { git_repo: git2::Repository, @@ -363,7 +363,7 @@ impl RepoActions for Repo { .into_iter() .filter(|e| { !self - .does_commit_exist(&tag_value(e, "commit").unwrap()) + .does_commit_exist(&get_commit_id_from_patch(e).unwrap()) .unwrap() }) .collect(); @@ -517,7 +517,10 @@ fn apply_patch(git_repo: &Repo, patch: &nostr::Event) -> Result<()> { git2::ResetType::Mixed, None, )?; - if gpg_commit_id.to_string().eq(&tag_value(patch, "commit")?) { + if gpg_commit_id + .to_string() + .eq(&get_commit_id_from_patch(patch)?) + { return Ok(()); } } else { @@ -575,12 +578,12 @@ fn validate_patch_applied(git_repo: &Repo, patch: &nostr::Event) -> Result<()> { if !updated_commit_oid .to_string() - .eq(&tag_value(patch, "commit")?) + .eq(&get_commit_id_from_patch(patch)?) { bail!( "when applied the patch commit id ({}) doesn't match the one specified in the event tag ({})", updated_commit_oid.to_string(), - tag_value(patch, "commit")?, + get_commit_id_from_patch(patch)?, ) } } 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