From ccdbc337ae16d7c70be44166269c8b2d5b9f5c09 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 2 Feb 2024 06:44:28 +0000 Subject: feat(prs-create)!: use nip34 patch kind and tags - change kind number - remove "r-" prefix from unique commit id r tag - rename tag commit-sig to commit-pgp-sig - a tag for repo identifer and pubkey. this serves as a vote for this pubkey being a maintainer - add relay hints - change format of committer tag - remove r references to parent commit id - tag parent patch event if its part of change request author and commit-message tags still need to be removed but they are required to apply patches with gitlib2. we will need to fallback to running the git client to apply patches. BREAKING CHANGE: change patch/commit event kind and tags to reflect nip34 draft. events with the older kind will no longer be found and will not be in a valid format --- src/git.rs | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) (limited to 'src/git.rs') diff --git a/src/git.rs b/src/git.rs index e832c23..5b78b34 100644 --- a/src/git.rs +++ b/src/git.rs @@ -41,11 +41,11 @@ pub trait RepoActions { fn get_head_commit(&self) -> Result; fn get_commit_parent(&self, commit: &Sha1Hash) -> Result; fn get_commit_message(&self, commit: &Sha1Hash) -> Result; - /// returns vector ["name", "email", "unixtime"] - /// eg ["joe bloggs", "joe@pm.me", "12176,-300"] + /// returns vector ["name", "email", "unixtime", "offset"] + /// eg ["joe bloggs", "joe@pm.me", "12176","-300"] fn get_commit_author(&self, commit: &Sha1Hash) -> Result>; - /// returns vector ["name", "email", "unixtime"] - /// eg ["joe bloggs", "joe@pm.me", "12176,-300"] + /// returns vector ["name", "email", "unixtime", "offset"] + /// eg ["joe bloggs", "joe@pm.me", "12176","-300"] fn get_commit_comitter(&self, commit: &Sha1Hash) -> Result>; fn get_commits_ahead_behind( &self, @@ -442,7 +442,8 @@ fn git_sig_to_tag_vec(sig: &git2::Signature) -> Vec { vec![ sig.name().unwrap_or("").to_string(), sig.email().unwrap_or("").to_string(), - format!("{},{}", sig.when().seconds(), sig.when().offset_minutes()), + format!("{}", sig.when().seconds()), + format!("{}", sig.when().offset_minutes()), ] } @@ -477,7 +478,7 @@ fn apply_patch(git_repo: &Repo, patch: &nostr::Event) -> Result<()> { index.add_all(["."], git2::IndexAddOption::DEFAULT, None)?; index.write()?; - let pgp_sig = if let Ok(pgp_sig) = tag_value(patch, "commit-sig") { + let pgp_sig = if let Ok(pgp_sig) = tag_value(patch, "commit-pgp-sig") { if pgp_sig.is_empty() { None } else { @@ -585,22 +586,15 @@ fn extract_sig_from_patch_tags<'a>( .find(|t| t.as_vec()[0].eq(tag_name)) .context(format!("tag '{tag_name}' not present in patch"))? .as_vec(); - if v.len() != 4 { + if v.len() != 5 { bail!("tag '{tag_name}' is incorrectly formatted") } - let git_time: Vec<&str> = v[3].split(',').collect(); - if git_time.len() != 2 { - bail!("tag '{tag_name}' time is incorrectly formatted") - } git2::Signature::new( v[1].as_str(), v[2].as_str(), &git2::Time::new( - git_time[0] - .parse() - .context("tag time is incorrectly formatted")?, - git_time[1] - .parse() + v[3].parse().context("tag time is incorrectly formatted")?, + v[4].parse() .context("tag time offset is incorrectly formatted")?, ), ) @@ -611,7 +605,7 @@ fn extract_sig_from_patch_tags<'a>( mod tests { use std::fs; - use test_utils::git::GitTestRepo; + use test_utils::{generate_repo_ref_event, git::GitTestRepo, TEST_KEY_1_KEYS}; use super::*; @@ -706,19 +700,22 @@ mod tests { #[test] fn no_offset() -> Result<()> { let res = prep(&git2::Time::new(5000, 0))?; - assert_eq!("5000,0", res[2]); + assert_eq!("5000", res[2]); + assert_eq!("0", res[3]); Ok(()) } #[test] fn positive_offset() -> Result<()> { let res = prep(&git2::Time::new(5000, 300))?; - assert_eq!("5000,300", res[2]); + assert_eq!("5000", res[2]); + assert_eq!("300", res[3]); Ok(()) } #[test] fn negative_offset() -> Result<()> { let res = prep(&git2::Time::new(5000, -300))?; - assert_eq!("5000,-300", res[2]); + assert_eq!("5000", res[2]); + assert_eq!("-300", res[3]); Ok(()) } } @@ -1205,10 +1202,9 @@ mod tests { } mod apply_patch { - use test_utils::TEST_KEY_1_KEYS; use super::*; - use crate::sub_commands::prs::create::generate_patch_event; + use crate::{repo_ref::RepoRef, sub_commands::prs::create::generate_patch_event}; fn generate_patch_from_head_commit(test_repo: &GitTestRepo) -> Result { let original_oid = test_repo.git_repo.head()?.peel_to_commit()?.id(); @@ -1219,6 +1215,8 @@ mod tests { &oid_to_sha1(&original_oid), nostr::EventId::all_zeros(), &TEST_KEY_1_KEYS, + &RepoRef::try_from(generate_repo_ref_event()).unwrap(), + None, ) } fn test_patch_applies_to_repository(patch_event: nostr::Event) -> Result<()> { @@ -1358,7 +1356,7 @@ mod tests { use test_utils::TEST_KEY_1_KEYS; use super::*; - use crate::sub_commands::prs::create::generate_pr_and_patch_events; + use crate::{repo_ref::RepoRef, sub_commands::prs::create::generate_pr_and_patch_events}; static BRANCH_NAME: &str = "add-example-feature"; // returns original_repo, pr_event, patch_events @@ -1378,6 +1376,7 @@ mod tests { &git_repo, &vec![oid_to_sha1(&oid1), oid_to_sha1(&oid2), oid_to_sha1(&oid3)], &TEST_KEY_1_KEYS, + &RepoRef::try_from(generate_repo_ref_event()).unwrap(), )?; events.reverse(); -- cgit v1.2.3