diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-02-08 15:57:05 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-02-09 06:51:39 +0000 |
| commit | 4b0a35583644e703eb615e0724d33fe93aec932b (patch) | |
| tree | e2b724d7b932c99e4346867a899452b839b57cbc | |
| parent | 0b9c7521b035e60ad7c458c1e782623e18a5cc21 (diff) | |
feat(prs-create): add `PATCH n/n` to content
format patch as a series in the patch event content unless it is 1/1
and there is no pr (cover letter) event
| -rw-r--r-- | src/git.rs | 57 | ||||
| -rw-r--r-- | src/sub_commands/prs/create.rs | 15 | ||||
| -rw-r--r-- | src/sub_commands/push.rs | 1 | ||||
| -rw-r--r-- | tests/prs_create.rs | 7 |
4 files changed, 70 insertions, 10 deletions
| @@ -54,7 +54,11 @@ pub trait RepoActions { | |||
| 54 | ) -> Result<(Vec<Sha1Hash>, Vec<Sha1Hash>)>; | 54 | ) -> Result<(Vec<Sha1Hash>, Vec<Sha1Hash>)>; |
| 55 | // including (un)staged changes and (un)tracked files | 55 | // including (un)staged changes and (un)tracked files |
| 56 | fn has_outstanding_changes(&self) -> Result<bool>; | 56 | fn has_outstanding_changes(&self) -> Result<bool>; |
| 57 | fn make_patch_from_commit(&self, commit: &Sha1Hash) -> Result<String>; | 57 | fn make_patch_from_commit( |
| 58 | &self, | ||
| 59 | commit: &Sha1Hash, | ||
| 60 | series_count: &Option<(u64, u64)>, | ||
| 61 | ) -> Result<String>; | ||
| 58 | fn extract_commit_pgp_signature(&self, commit: &Sha1Hash) -> Result<String>; | 62 | fn extract_commit_pgp_signature(&self, commit: &Sha1Hash) -> Result<String>; |
| 59 | fn checkout(&self, ref_name: &str) -> Result<Sha1Hash>; | 63 | fn checkout(&self, ref_name: &str) -> Result<Sha1Hash>; |
| 60 | fn create_branch_at_commit(&self, branch_name: &str, commit: &str) -> Result<()>; | 64 | fn create_branch_at_commit(&self, branch_name: &str, commit: &str) -> Result<()>; |
| @@ -212,7 +216,11 @@ impl RepoActions for Repo { | |||
| 212 | Ok(git_sig_to_tag_vec(&sig)) | 216 | Ok(git_sig_to_tag_vec(&sig)) |
| 213 | } | 217 | } |
| 214 | 218 | ||
| 215 | fn make_patch_from_commit(&self, commit: &Sha1Hash) -> Result<String> { | 219 | fn make_patch_from_commit( |
| 220 | &self, | ||
| 221 | commit: &Sha1Hash, | ||
| 222 | series_count: &Option<(u64, u64)>, | ||
| 223 | ) -> Result<String> { | ||
| 216 | let c = self | 224 | let c = self |
| 217 | .git_repo | 225 | .git_repo |
| 218 | .find_commit(Oid::from_bytes(commit.as_byte_array()).context(format!( | 226 | .find_commit(Oid::from_bytes(commit.as_byte_array()).context(format!( |
| @@ -220,7 +228,11 @@ impl RepoActions for Repo { | |||
| 220 | &commit | 228 | &commit |
| 221 | ))?) | 229 | ))?) |
| 222 | .context(format!("failed to find commit {}", &commit))?; | 230 | .context(format!("failed to find commit {}", &commit))?; |
| 223 | let patch = git2::Email::from_commit(&c, &mut git2::EmailCreateOptions::default()) | 231 | let mut options = git2::EmailCreateOptions::default(); |
| 232 | if let Some((n, total)) = series_count { | ||
| 233 | options.subject_prefix(format!("PATCH {n}/{total}")); | ||
| 234 | } | ||
| 235 | let patch = git2::Email::from_commit(&c, &mut options) | ||
| 224 | .context(format!("failed to create patch from commit {}", &commit))?; | 236 | .context(format!("failed to create patch from commit {}", &commit))?; |
| 225 | 237 | ||
| 226 | Ok(std::str::from_utf8(patch.as_slice()) | 238 | Ok(std::str::from_utf8(patch.as_slice()) |
| @@ -859,7 +871,43 @@ mod tests { | |||
| 859 | libgit2 1.7.1\n\ | 871 | libgit2 1.7.1\n\ |
| 860 | \n\ | 872 | \n\ |
| 861 | ", | 873 | ", |
| 862 | git_repo.make_patch_from_commit(&oid_to_sha1(&oid))?, | 874 | git_repo.make_patch_from_commit(&oid_to_sha1(&oid), &None)?, |
| 875 | ); | ||
| 876 | Ok(()) | ||
| 877 | } | ||
| 878 | |||
| 879 | #[test] | ||
| 880 | fn series_count() -> Result<()> { | ||
| 881 | let test_repo = GitTestRepo::default(); | ||
| 882 | let oid = test_repo.populate()?; | ||
| 883 | |||
| 884 | let git_repo = Repo::from_path(&test_repo.dir)?; | ||
| 885 | |||
| 886 | assert_eq!( | ||
| 887 | "\ | ||
| 888 | From 431b84edc0d2fa118d63faa3c2db9c73d630a5ae Mon Sep 17 00:00:00 2001\n\ | ||
| 889 | From: Joe Bloggs <joe.bloggs@pm.me>\n\ | ||
| 890 | Date: Thu, 1 Jan 1970 00:00:00 +0000\n\ | ||
| 891 | Subject: [PATCH 3/5] add t2.md\n\ | ||
| 892 | \n\ | ||
| 893 | ---\n \ | ||
| 894 | t2.md | 1 +\n \ | ||
| 895 | 1 file changed, 1 insertion(+)\n \ | ||
| 896 | create mode 100644 t2.md\n\ | ||
| 897 | \n\ | ||
| 898 | diff --git a/t2.md b/t2.md\n\ | ||
| 899 | new file mode 100644\n\ | ||
| 900 | index 0000000..a66525d\n\ | ||
| 901 | --- /dev/null\n\ | ||
| 902 | +++ b/t2.md\n\ | ||
| 903 | @@ -0,0 +1 @@\n\ | ||
| 904 | +some content1\n\\ \ | ||
| 905 | No newline at end of file\n\ | ||
| 906 | --\n\ | ||
| 907 | libgit2 1.7.1\n\ | ||
| 908 | \n\ | ||
| 909 | ", | ||
| 910 | git_repo.make_patch_from_commit(&oid_to_sha1(&oid), &Some((3, 5)))?, | ||
| 863 | ); | 911 | ); |
| 864 | Ok(()) | 912 | Ok(()) |
| 865 | } | 913 | } |
| @@ -1217,6 +1265,7 @@ mod tests { | |||
| 1217 | &TEST_KEY_1_KEYS, | 1265 | &TEST_KEY_1_KEYS, |
| 1218 | &RepoRef::try_from(generate_repo_ref_event()).unwrap(), | 1266 | &RepoRef::try_from(generate_repo_ref_event()).unwrap(), |
| 1219 | None, | 1267 | None, |
| 1268 | None, | ||
| 1220 | ) | 1269 | ) |
| 1221 | } | 1270 | } |
| 1222 | fn test_patch_applies_to_repository(patch_event: nostr::Event) -> Result<()> { | 1271 | fn test_patch_applies_to_repository(patch_event: nostr::Event) -> Result<()> { |
diff --git a/src/sub_commands/prs/create.rs b/src/sub_commands/prs/create.rs index 0f7cbda..82824a1 100644 --- a/src/sub_commands/prs/create.rs +++ b/src/sub_commands/prs/create.rs | |||
| @@ -80,7 +80,9 @@ pub async fn launch( | |||
| 80 | 80 | ||
| 81 | let title = match &args.title { | 81 | let title = match &args.title { |
| 82 | Some(t) => t.clone(), | 82 | Some(t) => t.clone(), |
| 83 | None => Interactor::default().input(PromptInputParms::default().with_prompt("title"))?, | 83 | None => Interactor::default() |
| 84 | .input(PromptInputParms::default().with_prompt("title"))? | ||
| 85 | .clone(), | ||
| 84 | }; | 86 | }; |
| 85 | 87 | ||
| 86 | let description = match &args.description { | 88 | let description = match &args.description { |
| @@ -350,7 +352,7 @@ pub fn generate_pr_and_patch_events( | |||
| 350 | let pr_event_id = pr_event.id; | 352 | let pr_event_id = pr_event.id; |
| 351 | 353 | ||
| 352 | let mut events = vec![pr_event]; | 354 | let mut events = vec![pr_event]; |
| 353 | for commit in commits { | 355 | for (i, commit) in commits.iter().enumerate() { |
| 354 | events.push( | 356 | events.push( |
| 355 | generate_patch_event( | 357 | generate_patch_event( |
| 356 | git_repo, | 358 | git_repo, |
| @@ -360,6 +362,11 @@ pub fn generate_pr_and_patch_events( | |||
| 360 | keys, | 362 | keys, |
| 361 | repo_ref, | 363 | repo_ref, |
| 362 | events.last().map(nostr::Event::id), | 364 | events.last().map(nostr::Event::id), |
| 365 | if events.is_empty() { | ||
| 366 | None | ||
| 367 | } else { | ||
| 368 | Some(((i + 1).try_into()?, commits.len().try_into()?)) | ||
| 369 | }, | ||
| 363 | ) | 370 | ) |
| 364 | .context("failed to generate patch event")?, | 371 | .context("failed to generate patch event")?, |
| 365 | ); | 372 | ); |
| @@ -367,6 +374,7 @@ pub fn generate_pr_and_patch_events( | |||
| 367 | Ok(events) | 374 | Ok(events) |
| 368 | } | 375 | } |
| 369 | 376 | ||
| 377 | #[allow(clippy::too_many_arguments)] | ||
| 370 | pub fn generate_patch_event( | 378 | pub fn generate_patch_event( |
| 371 | git_repo: &Repo, | 379 | git_repo: &Repo, |
| 372 | root_commit: &Sha1Hash, | 380 | root_commit: &Sha1Hash, |
| @@ -375,6 +383,7 @@ pub fn generate_patch_event( | |||
| 375 | keys: &nostr::Keys, | 383 | keys: &nostr::Keys, |
| 376 | repo_ref: &RepoRef, | 384 | repo_ref: &RepoRef, |
| 377 | parent_patch_event_id: Option<nostr::EventId>, | 385 | parent_patch_event_id: Option<nostr::EventId>, |
| 386 | series_count: Option<(u64, u64)>, | ||
| 378 | ) -> Result<nostr::Event> { | 387 | ) -> Result<nostr::Event> { |
| 379 | let commit_parent = git_repo | 388 | let commit_parent = git_repo |
| 380 | .get_commit_parent(commit) | 389 | .get_commit_parent(commit) |
| @@ -383,7 +392,7 @@ pub fn generate_patch_event( | |||
| 383 | EventBuilder::new( | 392 | EventBuilder::new( |
| 384 | nostr::event::Kind::Custom(PATCH_KIND), | 393 | nostr::event::Kind::Custom(PATCH_KIND), |
| 385 | git_repo | 394 | git_repo |
| 386 | .make_patch_from_commit(commit) | 395 | .make_patch_from_commit(commit,&series_count) |
| 387 | .context(format!("cannot make patch for commit {commit}"))?, | 396 | .context(format!("cannot make patch for commit {commit}"))?, |
| 388 | [ | 397 | [ |
| 389 | vec![ | 398 | vec![ |
diff --git a/src/sub_commands/push.rs b/src/sub_commands/push.rs index fac746f..de2a92f 100644 --- a/src/sub_commands/push.rs +++ b/src/sub_commands/push.rs | |||
| @@ -110,6 +110,7 @@ pub async fn launch(cli_args: &Cli) -> Result<()> { | |||
| 110 | &keys, | 110 | &keys, |
| 111 | &repo_ref, | 111 | &repo_ref, |
| 112 | patch_events.last().map(nostr::Event::id), | 112 | patch_events.last().map(nostr::Event::id), |
| 113 | None, | ||
| 113 | ) | 114 | ) |
| 114 | .context("cannot make patch event from commit")?, | 115 | .context("cannot make patch event from commit")?, |
| 115 | ); | 116 | ); |
diff --git a/tests/prs_create.rs b/tests/prs_create.rs index 4f71e5a..8db45d7 100644 --- a/tests/prs_create.rs +++ b/tests/prs_create.rs | |||
| @@ -339,7 +339,8 @@ mod sends_pr_and_2_patches_to_3_relays { | |||
| 339 | 339 | ||
| 340 | #[tokio::test] | 340 | #[tokio::test] |
| 341 | #[serial] | 341 | #[serial] |
| 342 | async fn patch_content_contains_patch_in_email_format() -> Result<()> { | 342 | async fn patch_content_contains_patch_in_email_format_with_patch_series_numbers() -> Result<()> |
| 343 | { | ||
| 343 | let (_, _, r53, r55, r56) = prep_run_create_pr().await?; | 344 | let (_, _, r53, r55, r56) = prep_run_create_pr().await?; |
| 344 | for relay in [&r53, &r55, &r56] { | 345 | for relay in [&r53, &r55, &r56] { |
| 345 | let patch_events: Vec<&nostr::Event> = relay | 346 | let patch_events: Vec<&nostr::Event> = relay |
| @@ -354,7 +355,7 @@ mod sends_pr_and_2_patches_to_3_relays { | |||
| 354 | From fe973a840fba2a8ab37dd505c154854a69a6505c Mon Sep 17 00:00:00 2001\n\ | 355 | From fe973a840fba2a8ab37dd505c154854a69a6505c Mon Sep 17 00:00:00 2001\n\ |
| 355 | From: Joe Bloggs <joe.bloggs@pm.me>\n\ | 356 | From: Joe Bloggs <joe.bloggs@pm.me>\n\ |
| 356 | Date: Thu, 1 Jan 1970 00:00:00 +0000\n\ | 357 | Date: Thu, 1 Jan 1970 00:00:00 +0000\n\ |
| 357 | Subject: [PATCH] add t4.md\n\ | 358 | Subject: [PATCH 1/2] add t4.md\n\ |
| 358 | \n\ | 359 | \n\ |
| 359 | ---\n \ | 360 | ---\n \ |
| 360 | t4.md | 1 +\n \ | 361 | t4.md | 1 +\n \ |
| @@ -380,7 +381,7 @@ mod sends_pr_and_2_patches_to_3_relays { | |||
| 380 | From 232efb37ebc67692c9e9ff58b83c0d3d63971a0a Mon Sep 17 00:00:00 2001\n\ | 381 | From 232efb37ebc67692c9e9ff58b83c0d3d63971a0a Mon Sep 17 00:00:00 2001\n\ |
| 381 | From: Joe Bloggs <joe.bloggs@pm.me>\n\ | 382 | From: Joe Bloggs <joe.bloggs@pm.me>\n\ |
| 382 | Date: Thu, 1 Jan 1970 00:00:00 +0000\n\ | 383 | Date: Thu, 1 Jan 1970 00:00:00 +0000\n\ |
| 383 | Subject: [PATCH] add t3.md\n\ | 384 | Subject: [PATCH 2/2] add t3.md\n\ |
| 384 | \n\ | 385 | \n\ |
| 385 | ---\n \ | 386 | ---\n \ |
| 386 | t3.md | 1 +\n \ | 387 | t3.md | 1 +\n \ |