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/repo_config.rs | |
| parent | a579e90666d49a0f9353319525b1790ad7b26c4a (diff) | |
merge, patch, pull requests and repo_config
Diffstat (limited to 'src/repo_config.rs')
| -rw-r--r-- | src/repo_config.rs | 125 |
1 files changed, 125 insertions, 0 deletions
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 @@ | |||
| 1 | use std::{path::PathBuf, fs::File, io::Write}; | ||
| 2 | |||
| 3 | use nostr::Timestamp; | ||
| 4 | use serde::{Deserialize, Serialize}; | ||
| 5 | |||
| 6 | use crate::utils::load_file; | ||
| 7 | |||
| 8 | #[derive(Serialize, Deserialize)] | ||
| 9 | pub 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 | |||
| 17 | impl 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 | } | ||