From 6f44c872a347907737cc1d2f9e49da201142044e Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Sun, 21 May 2023 11:21:01 +0000 Subject: merge, patch, pull requests and repo_config --- src/patch.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/patch.rs (limited to 'src/patch.rs') diff --git a/src/patch.rs b/src/patch.rs new file mode 100644 index 0000000..fc1137c --- /dev/null +++ b/src/patch.rs @@ -0,0 +1,65 @@ +use nostr::{Event, EventBuilder, Keys }; +use std::str; + +use crate::{ngit_tag::{tag_repo, tag_branch, tag_commit_parent, tag_commit, tag_initial_commit, tag_patch_parent, tag_is_commit, tag_extract_value, tag_commit_message, tag_hashtag, tag_into_event}, kind::Kind}; + +pub fn initialize_patch( + keys: &Keys, + repoistory:&String, + branch: &String, + patch:&[u8], + message: &String, + commit_ids: &Vec, + patch_parent_id:Option, + parent_commit_id:Option, +) -> Event { + let mut tags = vec![ + tag_repo(repoistory), + tag_into_event(tag_repo(repoistory)), + tag_branch(branch), + tag_into_event(tag_branch(branch)), + tag_commit_message(message), + tag_hashtag("ngit-event"), + tag_hashtag("ngit-format-0.0.1"), +]; + for id in commit_ids { + tags.push(tag_commit(id)); + } + match parent_commit_id { + None => { tags.push(tag_initial_commit()); }, + Some(id) => { tags.push(tag_commit_parent(&id)); } + }; + match patch_parent_id { + None => (), + Some(id) => { + tags.push(tag_patch_parent(&id)); + tags.push(tag_into_event(tag_patch_parent(&id))); + } + }; + let content = str::from_utf8(patch) + .expect("patch Vec to convert to string"); + EventBuilder::new( + Kind::Patch.into_sdk_custom_kind(), + content, + &tags, + ) + .to_unsigned_event(keys.public_key()) + .sign(&keys) + .unwrap() +} + +pub fn patch_is_commit(event:&Event, oid:&String) -> bool { + event.tags.iter().any( + |t|tag_is_commit(t) + && tag_extract_value(t) == oid.clone() + ) +} + +pub fn patch_commit_id(event:&Event) -> String { + match event.tags.iter().find( + |t|tag_is_commit(t) + ) { + None => { String::new() }, + Some(t) => { tag_extract_value(t) }, + } +} \ No newline at end of file -- cgit v1.2.3