upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/patch.rs
blob: fc1137c45a5b9d83ac3d140459baeca2c128cafa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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<String>,
    patch_parent_id:Option<String>,
    parent_commit_id:Option<String>,
) -> 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<u8> 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) },
    }
}