upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/merge.rs35
-rw-r--r--src/patch.rs65
-rw-r--r--src/pull_request.rs39
-rw-r--r--src/repo_config.rs125
4 files changed, 264 insertions, 0 deletions
diff --git a/src/merge.rs b/src/merge.rs
new file mode 100644
index 0000000..5f5b15f
--- /dev/null
+++ b/src/merge.rs
@@ -0,0 +1,35 @@
1use nostr::{Event, EventBuilder, Keys };
2
3
4use crate::{ngit_tag::{tag_repo, tag_branch, tag_commit, tag_branch_merge_from, tag_patch, tag_hashtag, tag_into_event}, kind::Kind};
5
6pub fn initialize_merge(
7 keys: &Keys,
8 repoistory:&String,
9 to_branch_id: &String,
10 from_branch_id: &String,
11 commit_id: &String,
12 patch_id: &String,
13) -> Event {
14 let tags = vec![
15 tag_repo(repoistory),
16 tag_into_event(tag_repo(repoistory)),
17 tag_branch(to_branch_id),
18 tag_into_event(tag_branch(to_branch_id)),
19 tag_branch_merge_from(from_branch_id),
20 tag_into_event(tag_branch_merge_from(from_branch_id)),
21 tag_patch(patch_id),
22 tag_into_event(tag_patch(patch_id)),
23 tag_commit(commit_id),
24 tag_hashtag("ngit-event"),
25 tag_hashtag("ngit-format-0.0.1"),
26];
27 EventBuilder::new(
28 Kind::Merge.into_sdk_custom_kind(),
29 "MERGE",
30 &tags,
31 )
32 .to_unsigned_event(keys.public_key())
33 .sign(&keys)
34 .unwrap()
35}
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 @@
1use nostr::{Event, EventBuilder, Keys };
2use std::str;
3
4use 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
6pub 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
51pub 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
58pub 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
diff --git a/src/pull_request.rs b/src/pull_request.rs
new file mode 100644
index 0000000..a87fa45
--- /dev/null
+++ b/src/pull_request.rs
@@ -0,0 +1,39 @@
1use nostr::{Event, EventBuilder, Keys };
2
3
4use crate::{ngit_tag::{tag_repo, tag_branch, tag_branch_merge_from, tag_hashtag, tag_into_event}, kind::Kind};
5
6pub fn initialize_pull_request(
7 keys: &Keys,
8 repoistory:&String,
9 to_branch_id: &String,
10 from_branch_id: &String,
11 title: &String,
12 description: &String,
13 hashtags: Vec<String>,
14
15) -> Event {
16 let mut tags = vec![
17 tag_repo(repoistory),
18 tag_into_event(tag_repo(repoistory)),
19 tag_branch(to_branch_id),
20 tag_into_event(tag_branch(to_branch_id)),
21 tag_branch_merge_from(from_branch_id),
22 tag_into_event(tag_branch_merge_from(from_branch_id)),
23 tag_hashtag("ngit-event"),
24 tag_hashtag("ngit-format-0.0.1"),
25];
26 for t in hashtags.iter() {
27 tags.push(
28 nostr::Tag::Hashtag(t.to_string())
29 )
30 }
31 EventBuilder::new(
32 Kind::PullRequest.into_sdk_custom_kind(),
33 format!("PullRequest\n{title}\n\n{description}"),
34 &tags,
35 )
36 .to_unsigned_event(keys.public_key())
37 .sign(&keys)
38 .unwrap()
39}
diff --git a/src/repo_config.rs b/src/repo_config.rs
new file mode 100644
index 0000000..a26575f
--- /dev/null
+++ b/src/repo_config.rs
@@ -0,0 +1,125 @@
1use std::{path::PathBuf, fs::File, io::Write};
2
3use nostr::Timestamp;
4use serde::{Deserialize, Serialize};
5
6use crate::utils::load_file;
7
8#[derive(Serialize, Deserialize)]
9pub struct RepoConfig {
10 version: u8,
11 branch_mappings: Vec<(String, String, Option<Timestamp>)>,
12 last_branch_ref_update_time: Option<Timestamp>,
13 repo_dir_path: PathBuf,
14}
15
16
17impl RepoConfig {
18
19 pub fn open(repo_dir_path: &PathBuf) -> Self {
20 let path = repo_dir_path.join(".ngit/config.json");
21 if path.exists() {
22 let repo_config: Self = serde_json::from_str(
23 load_file(path)
24 .expect("config json to load from file")
25 .as_str()
26 )
27 .expect("config.json to deserialize into RepoConfig");
28 repo_config
29 }
30 else {
31 Self {
32 version: 0,
33 branch_mappings: vec![],
34 last_branch_ref_update_time: None,
35 repo_dir_path:repo_dir_path.clone(),
36
37 }
38 }
39 }
40
41 fn save(&self) {
42 let path = self.repo_dir_path.join(".ngit/config.json");
43 let mut f = File::create(path)
44 .expect("config.json to open using File::Create");
45 f.write_all(
46 serde_json::json!(self).to_string().as_bytes()
47 )
48 .expect("write_all to write serialized RepoConfig to config.json");
49 }
50
51 pub fn set_mapping(&mut self, branch_name:&String, branch_id:&String) {
52 for i in 0..self.branch_mappings.len() {
53 if branch_name.clone() == self.branch_mappings[i].0 {
54 self.branch_mappings[i].1 = branch_id.clone();
55 self.save();
56 return;
57 }
58 }
59 self.branch_mappings.push(
60 (branch_name.clone(), branch_id.clone(), None)
61 );
62 self.save();
63 }
64
65 pub fn set_last_branch_ref_update_time(&mut self, timestamp:Timestamp) {
66 self.last_branch_ref_update_time = Some(timestamp);
67 self.save();
68 }
69
70 pub fn last_branch_ref_update_time(&self) -> &Option<Timestamp> {
71 &self.last_branch_ref_update_time
72 }
73
74 pub fn set_last_patch_update_time(&mut self, branch_id: String, timestamp:Timestamp) {
75 for i in 0..self.branch_mappings.len() {
76 if branch_id.clone() == self.branch_mappings[i].1 {
77 self.branch_mappings[i].2 = Some(timestamp);
78 self.save();
79 return;
80 }
81 }
82 }
83
84 pub fn last_patch_update_time(&self, branch_id: String) -> &Option<Timestamp> {
85 for mapping in self.branch_mappings.iter() {
86 if branch_id.clone() == mapping.1 {
87 return &mapping.2;
88 }
89 }
90 return &None;
91 }
92
93 pub fn branch_id_from_name (&self,branch_name:&String) -> Option<&String> {
94 for mapping in self.branch_mappings.iter() {
95 if branch_name.clone() == mapping.0
96 && self.check_local_branch_exists(&mapping.0)
97 {
98 return Some(&mapping.1);
99 }
100 }
101 return None;
102 }
103
104 pub fn branch_name_from_id (&self,branch_id:&String) -> Option<&String> {
105 for mapping in self.branch_mappings.iter() {
106 if branch_id.clone() == mapping.1
107 && self.check_local_branch_exists(&mapping.0)
108 {
109 return Some(&mapping.0);
110 }
111 }
112 return None;
113 }
114
115 fn check_local_branch_exists(&self, branch_name: &String) -> bool {
116 match git2::Repository::open(&self.repo_dir_path)
117 .expect("git repo not initialized. run ngit init first")
118 .find_branch(branch_name, git2::BranchType::Local)
119 {
120 Ok(_) => true,
121 Err(_) => false,
122 }
123 }
124
125}