diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-12-08 20:15:43 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-12-08 20:15:43 +0000 |
| commit | 6d3c9218d2d3320f5d7fb9b9ede8750e947b70e8 (patch) | |
| tree | 8b2da4f27c393dec9f01690162cd2705c21d6c26 /src | |
| parent | 82bf53ac3c18e15b75852a48b2e5b432c75a5c7f (diff) | |
feat(push) push commits to existing pr
- find pr with a branch-name that matches checked out branch
- check branch isnt behind latest patch on pr
- push new commits a patches associated with pr
Diffstat (limited to 'src')
| -rw-r--r-- | src/git.rs | 6 | ||||
| -rw-r--r-- | src/main.rs | 3 | ||||
| -rw-r--r-- | src/sub_commands/mod.rs | 1 | ||||
| -rw-r--r-- | src/sub_commands/push.rs | 182 |
4 files changed, 192 insertions, 0 deletions
| @@ -384,6 +384,12 @@ fn sha1_to_oid(hash: &Sha1Hash) -> Result<Oid> { | |||
| 384 | Oid::from_bytes(hash.as_byte_array()).context("Sha1Hash bytes failed to produce a valid Oid") | 384 | Oid::from_bytes(hash.as_byte_array()).context("Sha1Hash bytes failed to produce a valid Oid") |
| 385 | } | 385 | } |
| 386 | 386 | ||
| 387 | pub fn str_to_sha1(s: &str) -> Result<Sha1Hash> { | ||
| 388 | Ok(oid_to_sha1( | ||
| 389 | &Oid::from_str(s).context("string is not a sha1 hash")?, | ||
| 390 | )) | ||
| 391 | } | ||
| 392 | |||
| 387 | fn git_sig_to_tag_vec(sig: &git2::Signature) -> Vec<String> { | 393 | fn git_sig_to_tag_vec(sig: &git2::Signature) -> Vec<String> { |
| 388 | vec![ | 394 | vec![ |
| 389 | sig.name().unwrap_or("").to_string(), | 395 | sig.name().unwrap_or("").to_string(), |
diff --git a/src/main.rs b/src/main.rs index 8c6f0d0..996b697 100644 --- a/src/main.rs +++ b/src/main.rs | |||
| @@ -40,6 +40,8 @@ enum Commands { | |||
| 40 | Prs(sub_commands::prs::SubCommandArgs), | 40 | Prs(sub_commands::prs::SubCommandArgs), |
| 41 | /// pull latest commits in pr linked to checked out branch | 41 | /// pull latest commits in pr linked to checked out branch |
| 42 | Pull, | 42 | Pull, |
| 43 | /// push commits to current checked out pr branch | ||
| 44 | Push, | ||
| 43 | } | 45 | } |
| 44 | 46 | ||
| 45 | #[tokio::main] | 47 | #[tokio::main] |
| @@ -54,5 +56,6 @@ async fn main() -> Result<()> { | |||
| 54 | } | 56 | } |
| 55 | Commands::Prs(args) => futures::executor::block_on(sub_commands::prs::launch(&cli, args)), | 57 | Commands::Prs(args) => futures::executor::block_on(sub_commands::prs::launch(&cli, args)), |
| 56 | Commands::Pull => futures::executor::block_on(sub_commands::pull::launch()), | 58 | Commands::Pull => futures::executor::block_on(sub_commands::pull::launch()), |
| 59 | Commands::Push => futures::executor::block_on(sub_commands::push::launch(&cli)), | ||
| 57 | } | 60 | } |
| 58 | } | 61 | } |
diff --git a/src/sub_commands/mod.rs b/src/sub_commands/mod.rs index 12a7f0f..8be9004 100644 --- a/src/sub_commands/mod.rs +++ b/src/sub_commands/mod.rs | |||
| @@ -2,3 +2,4 @@ pub mod claim; | |||
| 2 | pub mod login; | 2 | pub mod login; |
| 3 | pub mod prs; | 3 | pub mod prs; |
| 4 | pub mod pull; | 4 | pub mod pull; |
| 5 | pub mod push; | ||
diff --git a/src/sub_commands/push.rs b/src/sub_commands/push.rs new file mode 100644 index 0000000..968aa0a --- /dev/null +++ b/src/sub_commands/push.rs | |||
| @@ -0,0 +1,182 @@ | |||
| 1 | use anyhow::{bail, Context, Result}; | ||
| 2 | use nostr::prelude::sha1::Hash as Sha1Hash; | ||
| 3 | |||
| 4 | #[cfg(not(test))] | ||
| 5 | use crate::client::Client; | ||
| 6 | #[cfg(test)] | ||
| 7 | use crate::client::MockConnect; | ||
| 8 | use crate::{ | ||
| 9 | client::Connect, | ||
| 10 | git::{str_to_sha1, Repo, RepoActions}, | ||
| 11 | login, | ||
| 12 | repo_ref::{self, RepoRef}, | ||
| 13 | sub_commands::prs::{ | ||
| 14 | create::{generate_patch_event, send_events, PATCH_KIND, PR_KIND}, | ||
| 15 | list::{get_most_recent_patch_with_ancestors, tag_value}, | ||
| 16 | }, | ||
| 17 | Cli, | ||
| 18 | }; | ||
| 19 | |||
| 20 | pub async fn launch(cli_args: &Cli) -> Result<()> { | ||
| 21 | let git_repo = Repo::discover().context("cannot find a git repository")?; | ||
| 22 | |||
| 23 | let (main_or_master_branch_name, _) = git_repo | ||
| 24 | .get_main_or_master_branch() | ||
| 25 | .context("no main or master branch")?; | ||
| 26 | |||
| 27 | let root_commit = git_repo | ||
| 28 | .get_root_commit(main_or_master_branch_name) | ||
| 29 | .context("failed to get root commit of the repository")?; | ||
| 30 | |||
| 31 | let branch_name = git_repo | ||
| 32 | .get_checked_out_branch_name() | ||
| 33 | .context("cannot get checked out branch name")?; | ||
| 34 | |||
| 35 | if branch_name == main_or_master_branch_name { | ||
| 36 | bail!("checkout a branch associated with a PR first") | ||
| 37 | } | ||
| 38 | #[cfg(not(test))] | ||
| 39 | let mut client = Client::default(); | ||
| 40 | #[cfg(test)] | ||
| 41 | let mut client = <MockConnect as std::default::Default>::default(); | ||
| 42 | |||
| 43 | let repo_ref = repo_ref::fetch( | ||
| 44 | root_commit.to_string(), | ||
| 45 | &client, | ||
| 46 | client.get_more_fallback_relays().clone(), | ||
| 47 | ) | ||
| 48 | .await?; | ||
| 49 | |||
| 50 | let (pr_event, commit_events) = | ||
| 51 | fetch_pr_and_most_recent_patch_chain(&client, &repo_ref, &root_commit, &branch_name) | ||
| 52 | .await?; | ||
| 53 | |||
| 54 | // TODO: fix these scenarios: | ||
| 55 | // - local PR branch is 2 behind and 1 ahead. intructions: ... | ||
| 56 | // - PR has been rebased. (against commit in main) instructions: ... | ||
| 57 | // - PR has been rebased. (against commit not in repo) instructions: .. | ||
| 58 | |||
| 59 | let most_recent_pr_patch_chain = get_most_recent_patch_with_ancestors(commit_events) | ||
| 60 | .context("cannot get most recent patch for PR")?; | ||
| 61 | |||
| 62 | let branch_tip = git_repo.get_tip_of_local_branch(&branch_name)?; | ||
| 63 | |||
| 64 | let most_recent_patch_commit_id = str_to_sha1( | ||
| 65 | &tag_value(&most_recent_pr_patch_chain[0], "commit") | ||
| 66 | .context("latest patch event doesnt have a commit tag")?, | ||
| 67 | ) | ||
| 68 | .context("latest patch event commit tag isn't a valid SHA1 hash")?; | ||
| 69 | |||
| 70 | if most_recent_patch_commit_id.eq(&branch_tip) { | ||
| 71 | bail!("nostr pr already up-to-date with local branch"); | ||
| 72 | } | ||
| 73 | |||
| 74 | if most_recent_pr_patch_chain.iter().any(|e| { | ||
| 75 | let c = tag_value(e, "parent-commit").unwrap_or_default(); | ||
| 76 | c.eq(&branch_tip.to_string()) | ||
| 77 | }) { | ||
| 78 | bail!("nostr pr is ahead of local branch"); | ||
| 79 | } | ||
| 80 | |||
| 81 | let (ahead, behind) = git_repo | ||
| 82 | .get_commits_ahead_behind(&most_recent_patch_commit_id, &branch_tip) | ||
| 83 | .context("the latest patch in pr doesnt share an ancestor with your branch.")?; | ||
| 84 | |||
| 85 | if !behind.is_empty() { | ||
| 86 | bail!( | ||
| 87 | "your local pr branch is {} behind patches on nostr. consider rebasing or force pushing", | ||
| 88 | behind.len() | ||
| 89 | ) | ||
| 90 | } | ||
| 91 | |||
| 92 | println!( | ||
| 93 | "{} commits ahead. preparing to create creating patch events.", | ||
| 94 | ahead.len() | ||
| 95 | ); | ||
| 96 | |||
| 97 | let (keys, user_ref) = login::launch(&cli_args.nsec, &cli_args.password, Some(&client)).await?; | ||
| 98 | |||
| 99 | client.set_keys(&keys).await; | ||
| 100 | |||
| 101 | let mut patch_events: Vec<nostr::Event> = vec![]; | ||
| 102 | for commit in &ahead { | ||
| 103 | patch_events.push( | ||
| 104 | generate_patch_event(&git_repo, &root_commit, commit, pr_event.id, &keys) | ||
| 105 | .context("cannot make patch event from commit")?, | ||
| 106 | ); | ||
| 107 | } | ||
| 108 | println!("pushing {} commits", ahead.len()); | ||
| 109 | |||
| 110 | send_events( | ||
| 111 | &client, | ||
| 112 | patch_events, | ||
| 113 | user_ref.relays.write(), | ||
| 114 | repo_ref.relays.clone(), | ||
| 115 | !cli_args.disable_cli_spinners, | ||
| 116 | ) | ||
| 117 | .await?; | ||
| 118 | |||
| 119 | println!("pushed {} commits", ahead.len()); | ||
| 120 | |||
| 121 | Ok(()) | ||
| 122 | } | ||
| 123 | |||
| 124 | async fn fetch_pr_and_most_recent_patch_chain( | ||
| 125 | #[cfg(test)] client: &crate::client::MockConnect, | ||
| 126 | #[cfg(not(test))] client: &Client, | ||
| 127 | repo_ref: &RepoRef, | ||
| 128 | root_commit: &Sha1Hash, | ||
| 129 | branch_name: &String, | ||
| 130 | ) -> Result<(nostr::Event, Vec<nostr::Event>)> { | ||
| 131 | println!("finding PR event..."); | ||
| 132 | |||
| 133 | let pr_event: nostr::Event = client | ||
| 134 | .get_events( | ||
| 135 | repo_ref.relays.clone(), | ||
| 136 | vec![ | ||
| 137 | nostr::Filter::default() | ||
| 138 | .kind(nostr::Kind::Custom(PR_KIND)) | ||
| 139 | .reference(format!("r-{root_commit}")), | ||
| 140 | ], | ||
| 141 | ) | ||
| 142 | .await? | ||
| 143 | .iter() | ||
| 144 | .find(|e| { | ||
| 145 | e.kind.as_u64() == PR_KIND | ||
| 146 | && e.tags | ||
| 147 | .iter() | ||
| 148 | .any(|t| t.as_vec().len() > 1 && t.as_vec()[1].eq(&format!("r-{root_commit}"))) | ||
| 149 | && tag_value(e, "branch-name") | ||
| 150 | .unwrap_or_default() | ||
| 151 | .eq(branch_name) | ||
| 152 | }) | ||
| 153 | .context("cannot find a PR event associated with the checked out branch name")? | ||
| 154 | .to_owned(); | ||
| 155 | |||
| 156 | println!("found PR event. finding commits..."); | ||
| 157 | |||
| 158 | let commits_events: Vec<nostr::Event> = client | ||
| 159 | .get_events( | ||
| 160 | repo_ref.relays.clone(), | ||
| 161 | vec![ | ||
| 162 | nostr::Filter::default() | ||
| 163 | .kind(nostr::Kind::Custom(PATCH_KIND)) | ||
| 164 | .event(pr_event.id) | ||
| 165 | .reference(format!("r-{root_commit}")), | ||
| 166 | ], | ||
| 167 | ) | ||
| 168 | .await? | ||
| 169 | .iter() | ||
| 170 | .filter(|e| { | ||
| 171 | e.kind.as_u64() == PATCH_KIND | ||
| 172 | && e.tags | ||
| 173 | .iter() | ||
| 174 | .any(|t| t.as_vec().len() > 2 && t.as_vec()[1].eq(&pr_event.id.to_string())) | ||
| 175 | && e.tags | ||
| 176 | .iter() | ||
| 177 | .any(|t| t.as_vec().len() > 1 && t.as_vec()[1].eq(&format!("r-{root_commit}"))) | ||
| 178 | }) | ||
| 179 | .map(std::borrow::ToOwned::to_owned) | ||
| 180 | .collect(); | ||
| 181 | Ok((pr_event, commits_events)) | ||
| 182 | } | ||