diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-19 21:20:53 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-19 21:20:53 +0100 |
| commit | 8531328558c7f5870be3571f63a952743eb0b9e6 (patch) | |
| tree | f72b57d2a6e571181d21a55fb03aca70d899cec3 /src/sub_commands | |
| parent | fca1d193a46eba17255b5372adffac2396e48c04 (diff) | |
feat: integrate `fetch` into `push`
as part of a project to use `fetch` and the stored cache everywhere
Diffstat (limited to 'src/sub_commands')
| -rw-r--r-- | src/sub_commands/push.rs | 98 | ||||
| -rw-r--r-- | src/sub_commands/send.rs | 6 |
2 files changed, 28 insertions, 76 deletions
diff --git a/src/sub_commands/push.rs b/src/sub_commands/push.rs index 9e3e041..111a14a 100644 --- a/src/sub_commands/push.rs +++ b/src/sub_commands/push.rs | |||
| @@ -1,25 +1,22 @@ | |||
| 1 | use anyhow::{bail, Context, Result}; | 1 | use anyhow::{bail, Context, Result}; |
| 2 | use nostr_sdk::hashes::sha1::Hash as Sha1Hash; | ||
| 3 | 2 | ||
| 4 | #[cfg(not(test))] | 3 | #[cfg(not(test))] |
| 5 | use crate::client::Client; | 4 | use crate::client::Client; |
| 6 | #[cfg(test)] | 5 | #[cfg(test)] |
| 7 | use crate::client::MockConnect; | 6 | use crate::client::MockConnect; |
| 8 | use crate::{ | 7 | use crate::{ |
| 9 | client::Connect, | 8 | client::{fetching_with_report, get_repo_ref_from_cache, Connect}, |
| 10 | git::{str_to_sha1, Repo, RepoActions}, | 9 | git::{str_to_sha1, Repo, RepoActions}, |
| 11 | login, | 10 | login, |
| 12 | repo_ref::{self, RepoRef}, | 11 | repo_ref::get_repo_coordinates, |
| 13 | sub_commands::{ | 12 | sub_commands::{ |
| 14 | self, | 13 | self, |
| 15 | list::{ | 14 | list::{ |
| 16 | find_commits_for_proposal_root_events, find_proposal_and_status_events, | 15 | get_all_proposal_patch_events_from_cache, get_commit_id_from_patch, |
| 17 | get_commit_id_from_patch, get_most_recent_patch_with_ancestors, tag_value, | 16 | get_most_recent_patch_with_ancestors, get_proposals_and_revisions_from_cache, |
| 18 | }, | 17 | tag_value, |
| 19 | send::{ | ||
| 20 | event_is_revision_root, event_to_cover_letter, generate_patch_event, send_events, | ||
| 21 | PATCH_KIND, | ||
| 22 | }, | 18 | }, |
| 19 | send::{event_to_cover_letter, generate_patch_event, send_events}, | ||
| 23 | }, | 20 | }, |
| 24 | Cli, | 21 | Cli, |
| 25 | }; | 22 | }; |
| @@ -37,6 +34,7 @@ pub struct SubCommandArgs { | |||
| 37 | #[allow(clippy::too_many_lines)] | 34 | #[allow(clippy::too_many_lines)] |
| 38 | pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | 35 | pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { |
| 39 | let git_repo = Repo::discover().context("cannot find a git repository")?; | 36 | let git_repo = Repo::discover().context("cannot find a git repository")?; |
| 37 | let git_repo_path = git_repo.get_path()?; | ||
| 40 | 38 | ||
| 41 | let (main_or_master_branch_name, _) = git_repo | 39 | let (main_or_master_branch_name, _) = git_repo |
| 42 | .get_main_or_master_branch() | 40 | .get_main_or_master_branch() |
| @@ -58,20 +56,24 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | |||
| 58 | #[cfg(test)] | 56 | #[cfg(test)] |
| 59 | let mut client = <MockConnect as std::default::Default>::default(); | 57 | let mut client = <MockConnect as std::default::Default>::default(); |
| 60 | 58 | ||
| 61 | let repo_ref = repo_ref::fetch( | 59 | let repo_coordinates = get_repo_coordinates(&git_repo, &client).await?; |
| 62 | &git_repo, | ||
| 63 | root_commit.to_string(), | ||
| 64 | &client, | ||
| 65 | client.get_fallback_relays().clone(), | ||
| 66 | true, | ||
| 67 | ) | ||
| 68 | .await?; | ||
| 69 | 60 | ||
| 70 | let (proposal_root_event, commit_events) = fetch_proposal_root_and_most_recent_patch_chain( | 61 | fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; |
| 71 | &client, | 62 | |
| 63 | let repo_ref = get_repo_ref_from_cache(git_repo_path, &repo_coordinates).await?; | ||
| 64 | |||
| 65 | let proposal_root_event = | ||
| 66 | get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates()) | ||
| 67 | .await? | ||
| 68 | .iter() | ||
| 69 | .find(|e| event_to_cover_letter(e).is_ok_and(|cl| cl.branch_name.eq(&branch_name))) | ||
| 70 | .context("cannot find proposal that matches the current branch name")? | ||
| 71 | .clone(); | ||
| 72 | |||
| 73 | let commit_events = get_all_proposal_patch_events_from_cache( | ||
| 74 | git_repo_path, | ||
| 72 | &repo_ref, | 75 | &repo_ref, |
| 73 | &root_commit, | 76 | &proposal_root_event.id(), |
| 74 | &branch_name, | ||
| 75 | ) | 77 | ) |
| 76 | .await?; | 78 | .await?; |
| 77 | 79 | ||
| @@ -116,6 +118,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | |||
| 116 | description: None, | 118 | description: None, |
| 117 | no_cover_letter: args.no_cover_letter, | 119 | no_cover_letter: args.no_cover_letter, |
| 118 | }, | 120 | }, |
| 121 | true, | ||
| 119 | ) | 122 | ) |
| 120 | .await?; | 123 | .await?; |
| 121 | println!("force pushed proposal revision"); | 124 | println!("force pushed proposal revision"); |
| @@ -199,56 +202,3 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | |||
| 199 | 202 | ||
| 200 | Ok(()) | 203 | Ok(()) |
| 201 | } | 204 | } |
| 202 | |||
| 203 | pub async fn fetch_proposal_root_and_most_recent_patch_chain( | ||
| 204 | #[cfg(test)] client: &crate::client::MockConnect, | ||
| 205 | #[cfg(not(test))] client: &Client, | ||
| 206 | repo_ref: &RepoRef, | ||
| 207 | root_commit: &Sha1Hash, | ||
| 208 | branch_name: &String, | ||
| 209 | ) -> Result<(nostr::Event, Vec<nostr::Event>)> { | ||
| 210 | println!("finding proposal root event..."); | ||
| 211 | |||
| 212 | let proposal_events_and_revisions: Vec<nostr::Event> = | ||
| 213 | find_proposal_and_status_events(client, repo_ref, &root_commit.to_string()) | ||
| 214 | .await | ||
| 215 | .context("cannot get proposal events for repo")?; | ||
| 216 | |||
| 217 | let proposal_events: Vec<&nostr::Event> = proposal_events_and_revisions | ||
| 218 | .iter() | ||
| 219 | .filter(|e| !event_is_revision_root(e) && e.kind().as_u16().eq(&PATCH_KIND)) | ||
| 220 | .collect::<Vec<&nostr::Event>>(); | ||
| 221 | |||
| 222 | let proposal_root_event: &nostr::Event = proposal_events | ||
| 223 | .iter() | ||
| 224 | .find(|e| { | ||
| 225 | event_to_cover_letter(e).is_ok_and(|cl| cl.branch_name.eq(branch_name)) | ||
| 226 | // TODO remove the dependancy on same branch name and replace with | ||
| 227 | // references stored in .git/ngit | ||
| 228 | }) | ||
| 229 | .context("cannot find a proposal root event associated with the checked out branch name")? | ||
| 230 | .to_owned(); | ||
| 231 | |||
| 232 | println!("found proposal root event. finding commits..."); | ||
| 233 | |||
| 234 | let commits_events: Vec<nostr::Event> = find_commits_for_proposal_root_events( | ||
| 235 | client, | ||
| 236 | &[ | ||
| 237 | vec![proposal_root_event], | ||
| 238 | proposal_events_and_revisions | ||
| 239 | .iter() | ||
| 240 | .filter(|e| { | ||
| 241 | e.tags.iter().any(|t| { | ||
| 242 | t.as_vec().len().gt(&1) | ||
| 243 | && t.as_vec()[1].eq(&proposal_root_event.id.to_string()) | ||
| 244 | }) | ||
| 245 | }) | ||
| 246 | .collect::<Vec<&nostr::Event>>(), | ||
| 247 | ] | ||
| 248 | .concat(), | ||
| 249 | repo_ref, | ||
| 250 | ) | ||
| 251 | .await?; | ||
| 252 | |||
| 253 | Ok((proposal_root_event.clone(), commits_events)) | ||
| 254 | } | ||
diff --git a/src/sub_commands/send.rs b/src/sub_commands/send.rs index d093cb6..f94eed3 100644 --- a/src/sub_commands/send.rs +++ b/src/sub_commands/send.rs | |||
| @@ -53,7 +53,7 @@ pub struct SubCommandArgs { | |||
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | #[allow(clippy::too_many_lines)] | 55 | #[allow(clippy::too_many_lines)] |
| 56 | pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | 56 | pub async fn launch(cli_args: &Cli, args: &SubCommandArgs, no_fetch: bool) -> Result<()> { |
| 57 | let git_repo = Repo::discover().context("cannot find a git repository")?; | 57 | let git_repo = Repo::discover().context("cannot find a git repository")?; |
| 58 | let git_repo_path = git_repo.get_path()?; | 58 | let git_repo_path = git_repo.get_path()?; |
| 59 | 59 | ||
| @@ -68,7 +68,9 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | |||
| 68 | 68 | ||
| 69 | let repo_coordinates = get_repo_coordinates(&git_repo, &client).await?; | 69 | let repo_coordinates = get_repo_coordinates(&git_repo, &client).await?; |
| 70 | 70 | ||
| 71 | fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; | 71 | if !no_fetch { |
| 72 | fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; | ||
| 73 | } | ||
| 72 | 74 | ||
| 73 | let (root_proposal_id, mention_tags) = | 75 | let (root_proposal_id, mention_tags) = |
| 74 | get_root_proposal_id_and_mentions_from_in_reply_to(git_repo.get_path()?, &args.in_reply_to) | 76 | get_root_proposal_id_and_mentions_from_in_reply_to(git_repo.get_path()?, &args.in_reply_to) |