diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-05-21 11:21:01 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-05-21 11:21:01 +0000 |
| commit | 6f44c872a347907737cc1d2f9e49da201142044e (patch) | |
| tree | 28d7e56ddd396b7a40d3b1c2bd83e2509f848228 /src/patch.rs | |
| parent | a579e90666d49a0f9353319525b1790ad7b26c4a (diff) | |
merge, patch, pull requests and repo_config
Diffstat (limited to 'src/patch.rs')
| -rw-r--r-- | src/patch.rs | 65 |
1 files changed, 65 insertions, 0 deletions
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 @@ | |||
| 1 | use nostr::{Event, EventBuilder, Keys }; | ||
| 2 | use std::str; | ||
| 3 | |||
| 4 | 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}; | ||
| 5 | |||
| 6 | pub fn initialize_patch( | ||
| 7 | keys: &Keys, | ||
| 8 | repoistory:&String, | ||
| 9 | branch: &String, | ||
| 10 | patch:&[u8], | ||
| 11 | message: &String, | ||
| 12 | commit_ids: &Vec<String>, | ||
| 13 | patch_parent_id:Option<String>, | ||
| 14 | parent_commit_id:Option<String>, | ||
| 15 | ) -> Event { | ||
| 16 | let mut tags = vec![ | ||
| 17 | tag_repo(repoistory), | ||
| 18 | tag_into_event(tag_repo(repoistory)), | ||
| 19 | tag_branch(branch), | ||
| 20 | tag_into_event(tag_branch(branch)), | ||
| 21 | tag_commit_message(message), | ||
| 22 | tag_hashtag("ngit-event"), | ||
| 23 | tag_hashtag("ngit-format-0.0.1"), | ||
| 24 | ]; | ||
| 25 | for id in commit_ids { | ||
| 26 | tags.push(tag_commit(id)); | ||
| 27 | } | ||
| 28 | match parent_commit_id { | ||
| 29 | None => { tags.push(tag_initial_commit()); }, | ||
| 30 | Some(id) => { tags.push(tag_commit_parent(&id)); } | ||
| 31 | }; | ||
| 32 | match patch_parent_id { | ||
| 33 | None => (), | ||
| 34 | Some(id) => { | ||
| 35 | tags.push(tag_patch_parent(&id)); | ||
| 36 | tags.push(tag_into_event(tag_patch_parent(&id))); | ||
| 37 | } | ||
| 38 | }; | ||
| 39 | let content = str::from_utf8(patch) | ||
| 40 | .expect("patch Vec<u8> to convert to string"); | ||
| 41 | EventBuilder::new( | ||
| 42 | Kind::Patch.into_sdk_custom_kind(), | ||
| 43 | content, | ||
| 44 | &tags, | ||
| 45 | ) | ||
| 46 | .to_unsigned_event(keys.public_key()) | ||
| 47 | .sign(&keys) | ||
| 48 | .unwrap() | ||
| 49 | } | ||
| 50 | |||
| 51 | pub fn patch_is_commit(event:&Event, oid:&String) -> bool { | ||
| 52 | event.tags.iter().any( | ||
| 53 | |t|tag_is_commit(t) | ||
| 54 | && tag_extract_value(t) == oid.clone() | ||
| 55 | ) | ||
| 56 | } | ||
| 57 | |||
| 58 | pub fn patch_commit_id(event:&Event) -> String { | ||
| 59 | match event.tags.iter().find( | ||
| 60 | |t|tag_is_commit(t) | ||
| 61 | ) { | ||
| 62 | None => { String::new() }, | ||
| 63 | Some(t) => { tag_extract_value(t) }, | ||
| 64 | } | ||
| 65 | } \ No newline at end of file | ||