upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/funcs/find_latest_patch.rs
blob: 717d6c15c51dd274297a3f6f65097b2e003d8d1d (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::path::PathBuf;

use nostr::{ Event };

use crate::{ngit_tag::{tag_is_branch, tag_extract_value, tag_is_commit}, branch_refs::BranchRefs, utils::load_event, kind::Kind};

/// finds latest patch that needs applying. It might not be the latest created_at date if an earlier patch was 'merged' more recently
pub fn find_latest_patch(
    branch_id: &String,
    patch_events:&Vec<Event>,
    merges_into_branch: &Vec<Event>,
    branch_refs:&BranchRefs,
    repo_dir_path: &PathBuf,
) -> Option<Event> {

    // ensure only patch events make it into patch_events - we cant rely on relays for this
    let patch_events:Vec<Event> = patch_events.iter().filter(|p|
        // kind is patch
        p.kind == nostr_sdk::Kind::Custom(u64::from(Kind::Patch))
    ).map(|p|p.clone()).collect();

    let directly_authorised_patches: Vec<Event> = patch_events.iter().filter(|p|
        // kind is patch
        p.kind == nostr_sdk::Kind::Custom(u64::from(Kind::Patch))
        // on branch
        && p.tags.iter().any(
            |t|tag_is_branch(t)
            && tag_extract_value(t) == branch_id.clone()
        )
        // authorized
        && match &branch_refs.is_authorized(Some(&branch_id), &p.pubkey) {
            None => { false },
            Some(is_authorized) => { is_authorized.clone() }
        }
    ).map(|p|p.clone()).collect();

    let latest_authorized_patch = find_latest_event(&directly_authorised_patches);

    let authorised_merges: Vec<Event> = merges_into_branch.iter().filter(|m|
        // kind is merge
        m.kind == nostr_sdk::Kind::Custom(u64::from(Kind::Merge))
        // into branch
        && m.tags.iter().any(
            |t|tag_is_branch(t)
            && tag_extract_value(t) == branch_id.clone()
        )
        // merge authorized
        && match &branch_refs.is_authorized(Some(&branch_id), &m.pubkey) {
            None => { false },
            Some(is_authorized) => { is_authorized.clone() }
        }
    ).map(|p|p.clone()).collect();

    let latest_authorised_merge = find_latest_event(&authorised_merges);

    // find latest patch

    match latest_authorised_merge {
        // no merge - return patch or None
        None => latest_authorized_patch,
        Some(m) => {
            match latest_authorized_patch {
                // merge but no patch, return the patch related to the merge
                None => {
                    Some(get_merge_patch(
                        &m,
                        &patch_events,
                        repo_dir_path,
                    ))
                },
                // a merge and a patch
                Some(p) => {
                    // return the patch if it is later than merge
                    if m.created_at < p.created_at {
                        Some(p.clone())
                    }
                    // return the patch related to the merge if the merge is later
                    else {
                        Some(get_merge_patch(
                            &m,
                            &patch_events,
                            repo_dir_path,
                        ))
                    }
                }
            }
        }
    }
}

fn find_latest_event(events:&Vec<Event>) -> Option<Event> {
    let mut latest = match events.get(0) {
        None => { return None },
        Some(e) => e.clone(),
    };
    for e in events.iter() {
        if e.created_at > latest.created_at {
            latest = e.clone();
        }
    }
    Some(latest)
}

fn get_merge_patch(
    merge: &Event,
    patch_events: &Vec<Event>,
    repo_dir_path: &PathBuf,
) -> Event{
    let commit_id = tag_extract_value(
        merge.tags.iter().find(|tag| tag_is_commit(tag))
        .expect("merge event will have a commit tag")
    );
    // search in patch_events vector
    match patch_events.iter().find(|p|
        tag_extract_value(
            p.tags.iter().find(|tag| tag_is_commit(tag))
                .expect("patch event will have a commit tag")
        ) == *commit_id
    ) {
        // found merge patch in patch_events
        Some(patch) => patch.clone(),
        None => {
            let patch_path = repo_dir_path.join(format!(
                ".ngit/patches/{}.json",
                commit_id
            ));
            if patch_path.exists() {
                // found merge patch in .ngit/patches
                load_event(patch_path)
                    .expect("patch at path that exists renders as event")
            }
            else {
                panic!("cannot find patch from merge event in event vector or .ngit folder");
            }
        },
    }
}