diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-02-16 20:41:59 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-02-16 20:41:59 +0000 |
| commit | a645273433fe3a716dd9c43ad9276da066af6127 (patch) | |
| tree | 942e08c7de68112975269b64b6802dcfcb7c1fe0 | |
| parent | 614d2c8a3a0ac01b142aa83a142fc34d1ad56e9d (diff) | |
refactor: remove reliance on 'commit' tag
as part of nip34 compliance
| -rw-r--r-- | src/git.rs | 13 | ||||
| -rw-r--r-- | src/sub_commands/list.rs | 19 | ||||
| -rw-r--r-- | src/sub_commands/push.rs | 6 |
3 files changed, 26 insertions, 12 deletions
| @@ -6,7 +6,7 @@ use anyhow::{bail, Context, Result}; | |||
| 6 | use git2::{DiffOptions, Oid, Revwalk}; | 6 | use git2::{DiffOptions, Oid, Revwalk}; |
| 7 | use nostr::prelude::{sha1::Hash as Sha1Hash, Hash}; | 7 | use nostr::prelude::{sha1::Hash as Sha1Hash, Hash}; |
| 8 | 8 | ||
| 9 | use crate::sub_commands::list::tag_value; | 9 | use crate::sub_commands::list::{get_commit_id_from_patch, tag_value}; |
| 10 | 10 | ||
| 11 | pub struct Repo { | 11 | pub struct Repo { |
| 12 | git_repo: git2::Repository, | 12 | git_repo: git2::Repository, |
| @@ -363,7 +363,7 @@ impl RepoActions for Repo { | |||
| 363 | .into_iter() | 363 | .into_iter() |
| 364 | .filter(|e| { | 364 | .filter(|e| { |
| 365 | !self | 365 | !self |
| 366 | .does_commit_exist(&tag_value(e, "commit").unwrap()) | 366 | .does_commit_exist(&get_commit_id_from_patch(e).unwrap()) |
| 367 | .unwrap() | 367 | .unwrap() |
| 368 | }) | 368 | }) |
| 369 | .collect(); | 369 | .collect(); |
| @@ -517,7 +517,10 @@ fn apply_patch(git_repo: &Repo, patch: &nostr::Event) -> Result<()> { | |||
| 517 | git2::ResetType::Mixed, | 517 | git2::ResetType::Mixed, |
| 518 | None, | 518 | None, |
| 519 | )?; | 519 | )?; |
| 520 | if gpg_commit_id.to_string().eq(&tag_value(patch, "commit")?) { | 520 | if gpg_commit_id |
| 521 | .to_string() | ||
| 522 | .eq(&get_commit_id_from_patch(patch)?) | ||
| 523 | { | ||
| 521 | return Ok(()); | 524 | return Ok(()); |
| 522 | } | 525 | } |
| 523 | } else { | 526 | } else { |
| @@ -575,12 +578,12 @@ fn validate_patch_applied(git_repo: &Repo, patch: &nostr::Event) -> Result<()> { | |||
| 575 | 578 | ||
| 576 | if !updated_commit_oid | 579 | if !updated_commit_oid |
| 577 | .to_string() | 580 | .to_string() |
| 578 | .eq(&tag_value(patch, "commit")?) | 581 | .eq(&get_commit_id_from_patch(patch)?) |
| 579 | { | 582 | { |
| 580 | bail!( | 583 | bail!( |
| 581 | "when applied the patch commit id ({}) doesn't match the one specified in the event tag ({})", | 584 | "when applied the patch commit id ({}) doesn't match the one specified in the event tag ({})", |
| 582 | updated_commit_oid.to_string(), | 585 | updated_commit_oid.to_string(), |
| 583 | tag_value(patch, "commit")?, | 586 | get_commit_id_from_patch(patch)?, |
| 584 | ) | 587 | ) |
| 585 | } | 588 | } |
| 586 | } | 589 | } |
diff --git a/src/sub_commands/list.rs b/src/sub_commands/list.rs index 413406a..be5b97f 100644 --- a/src/sub_commands/list.rs +++ b/src/sub_commands/list.rs | |||
| @@ -131,6 +131,18 @@ pub fn tag_value(event: &nostr::Event, tag_name: &str) -> Result<String> { | |||
| 131 | .clone()) | 131 | .clone()) |
| 132 | } | 132 | } |
| 133 | 133 | ||
| 134 | pub fn get_commit_id_from_patch(event: &nostr::Event) -> Result<String> { | ||
| 135 | let value = tag_value(event, "commit"); | ||
| 136 | |||
| 137 | if value.is_ok() { | ||
| 138 | value | ||
| 139 | } else if event.content.starts_with("From ") && event.content.len().gt(&45) { | ||
| 140 | Ok(event.content[5..45].to_string()) | ||
| 141 | } else { | ||
| 142 | bail!("event is not a patch") | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 134 | pub fn get_most_recent_patch_with_ancestors( | 146 | pub fn get_most_recent_patch_with_ancestors( |
| 135 | mut patches: Vec<nostr::Event>, | 147 | mut patches: Vec<nostr::Event>, |
| 136 | ) -> Result<Vec<nostr::Event>> { | 148 | ) -> Result<Vec<nostr::Event>> { |
| @@ -143,14 +155,14 @@ pub fn get_most_recent_patch_with_ancestors( | |||
| 143 | .filter(|p| p.created_at.eq(&first_patch.created_at)) | 155 | .filter(|p| p.created_at.eq(&first_patch.created_at)) |
| 144 | .collect(); | 156 | .collect(); |
| 145 | 157 | ||
| 146 | let latest_commit_id = tag_value( | 158 | let latest_commit_id = get_commit_id_from_patch( |
| 147 | // get the first patch which isn't a parent of a patch event created at the same | 159 | // get the first patch which isn't a parent of a patch event created at the same |
| 148 | // time | 160 | // time |
| 149 | patches_with_youngest_created_at | 161 | patches_with_youngest_created_at |
| 150 | .clone() | 162 | .clone() |
| 151 | .iter() | 163 | .iter() |
| 152 | .find(|p| { | 164 | .find(|p| { |
| 153 | if let Ok(commit) = tag_value(p, "commit") { | 165 | if let Ok(commit) = get_commit_id_from_patch(p) { |
| 154 | !patches_with_youngest_created_at.iter().any(|p2| { | 166 | !patches_with_youngest_created_at.iter().any(|p2| { |
| 155 | if let Ok(parent) = tag_value(p2, "parent-commit") { | 167 | if let Ok(parent) = tag_value(p2, "parent-commit") { |
| 156 | commit.eq(&parent) | 168 | commit.eq(&parent) |
| @@ -163,7 +175,6 @@ pub fn get_most_recent_patch_with_ancestors( | |||
| 163 | } | 175 | } |
| 164 | }) | 176 | }) |
| 165 | .context("cannot find patches_with_youngest_created_at")?, | 177 | .context("cannot find patches_with_youngest_created_at")?, |
| 166 | "commit", | ||
| 167 | )?; | 178 | )?; |
| 168 | 179 | ||
| 169 | let mut res = vec![]; | 180 | let mut res = vec![]; |
| @@ -171,7 +182,7 @@ pub fn get_most_recent_patch_with_ancestors( | |||
| 171 | let mut commit_id_to_search = latest_commit_id; | 182 | let mut commit_id_to_search = latest_commit_id; |
| 172 | 183 | ||
| 173 | while let Some(event) = patches.iter().find(|e| { | 184 | while let Some(event) = patches.iter().find(|e| { |
| 174 | if let Ok(commit) = tag_value(e, "commit") { | 185 | if let Ok(commit) = get_commit_id_from_patch(e) { |
| 175 | commit.eq(&commit_id_to_search) | 186 | commit.eq(&commit_id_to_search) |
| 176 | } else { | 187 | } else { |
| 177 | false // skip | 188 | false // skip |
diff --git a/src/sub_commands/push.rs b/src/sub_commands/push.rs index cc1f480..7c6b95b 100644 --- a/src/sub_commands/push.rs +++ b/src/sub_commands/push.rs | |||
| @@ -12,8 +12,8 @@ use crate::{ | |||
| 12 | repo_ref::{self, RepoRef}, | 12 | repo_ref::{self, RepoRef}, |
| 13 | sub_commands::{ | 13 | sub_commands::{ |
| 14 | list::{ | 14 | list::{ |
| 15 | find_commits_for_pr_event, find_pr_events, get_most_recent_patch_with_ancestors, | 15 | find_commits_for_pr_event, find_pr_events, get_commit_id_from_patch, |
| 16 | tag_value, | 16 | get_most_recent_patch_with_ancestors, tag_value, |
| 17 | }, | 17 | }, |
| 18 | send::{event_to_cover_letter, generate_patch_event, send_events}, | 18 | send::{event_to_cover_letter, generate_patch_event, send_events}, |
| 19 | }, | 19 | }, |
| @@ -66,7 +66,7 @@ pub async fn launch(cli_args: &Cli) -> Result<()> { | |||
| 66 | let branch_tip = git_repo.get_tip_of_local_branch(&branch_name)?; | 66 | let branch_tip = git_repo.get_tip_of_local_branch(&branch_name)?; |
| 67 | 67 | ||
| 68 | let most_recent_patch_commit_id = str_to_sha1( | 68 | let most_recent_patch_commit_id = str_to_sha1( |
| 69 | &tag_value(&most_recent_pr_patch_chain[0], "commit") | 69 | &get_commit_id_from_patch(&most_recent_pr_patch_chain[0]) |
| 70 | .context("latest patch event doesnt have a commit tag")?, | 70 | .context("latest patch event doesnt have a commit tag")?, |
| 71 | ) | 71 | ) |
| 72 | .context("latest patch event commit tag isn't a valid SHA1 hash")?; | 72 | .context("latest patch event commit tag isn't a valid SHA1 hash")?; |