From a625be66cfbced5f96cb0123a286937543e7273c Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 1 Aug 2025 09:39:36 +0100 Subject: refactor: move patch size evaluation fn to lib so we can use it in ngit as well as remote helper --- src/bin/git_remote_nostr/push.rs | 17 ++--------------- src/lib/git/mod.rs | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs index 9ba7c30..38b6fc4 100644 --- a/src/bin/git_remote_nostr/push.rs +++ b/src/bin/git_remote_nostr/push.rs @@ -357,7 +357,7 @@ async fn process_proposal_refspecs( ); } if proposal.kind.eq(&KIND_PULL_REQUEST) - || are_commits_too_big_for_patches(git_repo, &ahead) + || git_repo.are_commits_too_big_for_patches(&ahead) { for event in generate_patches_or_pr_event_or_pr_updates( git_repo, @@ -441,19 +441,6 @@ async fn process_proposal_refspecs( Ok((events, rejected_proposal_refspecs)) } -fn are_commits_too_big_for_patches(git_repo: &Repo, commits: &[Sha1Hash]) -> bool { - commits.iter().any(|commit| { - if let Ok(patch) = git_repo.make_patch_from_commit(commit, &None) { - patch.len() - > ((65 // max recomended patch event size specified in nip34 in kb - // allownace for nostr event wrapper (id, pubkey, tags, sig) - - 1) * 1024) - } else { - true - } - }) -} - #[allow(clippy::too_many_lines)] async fn generate_patches_or_pr_event_or_pr_updates( git_repo: &Repo, @@ -466,7 +453,7 @@ async fn generate_patches_or_pr_event_or_pr_updates( ) -> Result> { let mut events: Vec = vec![]; let use_pr = root_proposal.is_some_and(|proposal| proposal.kind.eq(&KIND_PULL_REQUEST)) - || are_commits_too_big_for_patches(git_repo, ahead); + || git_repo.are_commits_too_big_for_patches(ahead); if use_pr { let repo_grasps = repo_ref.grasp_servers(); diff --git a/src/lib/git/mod.rs b/src/lib/git/mod.rs index b275b49..3d5297f 100644 --- a/src/lib/git/mod.rs +++ b/src/lib/git/mod.rs @@ -75,6 +75,7 @@ pub trait RepoActions { commit: &Sha1Hash, series_count: &Option<(u64, u64)>, ) -> Result; + fn are_commits_too_big_for_patches(&self, commits: &[Sha1Hash]) -> bool; fn extract_commit_pgp_signature(&self, commit: &Sha1Hash) -> Result; fn checkout(&self, ref_name: &str) -> Result; fn create_branch_at_commit(&self, branch_name: &str, commit: &str) -> Result<()>; @@ -380,6 +381,19 @@ impl RepoActions for Repo { .to_owned()) } + fn are_commits_too_big_for_patches(&self, commits: &[Sha1Hash]) -> bool { + commits.iter().any(|commit| { + if let Ok(patch) = self.make_patch_from_commit(commit, &None) { + patch.len() + > ((65 // max recomended patch event size specified in nip34 in kb + // allownace for nostr event wrapper (id, pubkey, tags, sig) + - 1) * 1024) + } else { + true + } + }) + } + fn extract_commit_pgp_signature(&self, commit: &Sha1Hash) -> Result { let oid = Oid::from_bytes(commit.as_byte_array()).context(format!( "failed to convert commit_id format for {}", -- cgit v1.2.3