upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/git.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/git.rs')
-rw-r--r--src/git.rs57
1 files changed, 53 insertions, 4 deletions
diff --git a/src/git.rs b/src/git.rs
index 5b78b34..6e6091d 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -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<()> {