From 82bf53ac3c18e15b75852a48b2e5b432c75a5c7f Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 7 Dec 2023 09:57:43 +0000 Subject: feat(pull) pull commits for checked out pr branch - find pr event which matches branch name - fetch and apply latest commits --- src/sub_commands/mod.rs | 1 + src/sub_commands/pull.rs | 114 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/sub_commands/pull.rs (limited to 'src/sub_commands') diff --git a/src/sub_commands/mod.rs b/src/sub_commands/mod.rs index 6e99ca5..12a7f0f 100644 --- a/src/sub_commands/mod.rs +++ b/src/sub_commands/mod.rs @@ -1,3 +1,4 @@ pub mod claim; pub mod login; pub mod prs; +pub mod pull; diff --git a/src/sub_commands/pull.rs b/src/sub_commands/pull.rs new file mode 100644 index 0000000..a6513e8 --- /dev/null +++ b/src/sub_commands/pull.rs @@ -0,0 +1,114 @@ +use anyhow::{bail, Context, Result}; + +#[cfg(not(test))] +use crate::client::Client; +#[cfg(test)] +use crate::client::MockConnect; +use crate::{ + client::Connect, + git::{Repo, RepoActions}, + repo_ref, + sub_commands::prs::{ + create::{PATCH_KIND, PR_KIND}, + list::{get_most_recent_patch_with_ancestors, tag_value}, + }, +}; + +pub async fn launch() -> Result<()> { + let git_repo = Repo::discover().context("cannot find a git repository")?; + + let (main_or_master_branch_name, _) = git_repo + .get_main_or_master_branch() + .context("no main or master branch")?; + + let root_commit = git_repo + .get_root_commit(main_or_master_branch_name) + .context("failed to get root commit of the repository")?; + + let branch_name = git_repo + .get_checked_out_branch_name() + .context("cannot get checked out branch name")?; + + if branch_name == main_or_master_branch_name { + bail!("checkout a branch associated with a PR first") + } + #[cfg(not(test))] + let client = Client::default(); + #[cfg(test)] + let client = ::default(); + + let repo_ref = repo_ref::fetch( + root_commit.to_string(), + &client, + client.get_more_fallback_relays().clone(), + ) + .await?; + + println!("finding PR event..."); + + let pr_event: nostr::Event = client + .get_events( + repo_ref.relays.clone(), + vec![ + nostr::Filter::default() + .kind(nostr::Kind::Custom(PR_KIND)) + .reference(format!("r-{root_commit}")), + ], + ) + .await? + .iter() + .find(|e| { + e.kind.as_u64() == PR_KIND + && e.tags + .iter() + .any(|t| t.as_vec().len() > 1 && t.as_vec()[1].eq(&format!("r-{root_commit}"))) + && tag_value(e, "branch-name") + .unwrap_or_default() + .eq(&branch_name) + }) + .context("cannot find a PR event associated with the checked out branch name")? + .to_owned(); + + println!("found PR event. finding commits..."); + + let commits_events: Vec = client + .get_events( + repo_ref.relays.clone(), + vec![ + nostr::Filter::default() + .kind(nostr::Kind::Custom(PATCH_KIND)) + .event(pr_event.id) + .reference(format!("r-{root_commit}")), + ], + ) + .await? + .iter() + .filter(|e| { + e.kind.as_u64() == PATCH_KIND + && e.tags + .iter() + .any(|t| t.as_vec().len() > 2 && t.as_vec()[1].eq(&pr_event.id.to_string())) + && e.tags + .iter() + .any(|t| t.as_vec().len() > 1 && t.as_vec()[1].eq(&format!("r-{root_commit}"))) + }) + .map(std::borrow::ToOwned::to_owned) + .collect(); + + // TODO: are there outstanding changes to prevent checking out a new branch? + + let most_recent_pr_patch_chain = get_most_recent_patch_with_ancestors(commits_events) + .context("cannot get most recent patch for PR")?; + + let applied = git_repo + .apply_patch_chain(&branch_name, most_recent_pr_patch_chain) + .context("cannot apply patch chain")?; + + if applied.is_empty() { + println!("branch already up-to-date"); + } else { + println!("applied {} new commits", applied.len(),); + } + + Ok(()) +} -- cgit v1.2.3