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.rs45
1 files changed, 22 insertions, 23 deletions
diff --git a/src/git.rs b/src/git.rs
index e832c23..5b78b34 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -41,11 +41,11 @@ pub trait RepoActions {
41 fn get_head_commit(&self) -> Result<Sha1Hash>; 41 fn get_head_commit(&self) -> Result<Sha1Hash>;
42 fn get_commit_parent(&self, commit: &Sha1Hash) -> Result<Sha1Hash>; 42 fn get_commit_parent(&self, commit: &Sha1Hash) -> Result<Sha1Hash>;
43 fn get_commit_message(&self, commit: &Sha1Hash) -> Result<String>; 43 fn get_commit_message(&self, commit: &Sha1Hash) -> Result<String>;
44 /// returns vector ["name", "email", "unixtime"] 44 /// returns vector ["name", "email", "unixtime", "offset"]
45 /// eg ["joe bloggs", "joe@pm.me", "12176,-300"] 45 /// eg ["joe bloggs", "joe@pm.me", "12176","-300"]
46 fn get_commit_author(&self, commit: &Sha1Hash) -> Result<Vec<String>>; 46 fn get_commit_author(&self, commit: &Sha1Hash) -> Result<Vec<String>>;
47 /// returns vector ["name", "email", "unixtime"] 47 /// returns vector ["name", "email", "unixtime", "offset"]
48 /// eg ["joe bloggs", "joe@pm.me", "12176,-300"] 48 /// eg ["joe bloggs", "joe@pm.me", "12176","-300"]
49 fn get_commit_comitter(&self, commit: &Sha1Hash) -> Result<Vec<String>>; 49 fn get_commit_comitter(&self, commit: &Sha1Hash) -> Result<Vec<String>>;
50 fn get_commits_ahead_behind( 50 fn get_commits_ahead_behind(
51 &self, 51 &self,
@@ -442,7 +442,8 @@ fn git_sig_to_tag_vec(sig: &git2::Signature) -> Vec<String> {
442 vec![ 442 vec![
443 sig.name().unwrap_or("").to_string(), 443 sig.name().unwrap_or("").to_string(),
444 sig.email().unwrap_or("").to_string(), 444 sig.email().unwrap_or("").to_string(),
445 format!("{},{}", sig.when().seconds(), sig.when().offset_minutes()), 445 format!("{}", sig.when().seconds()),
446 format!("{}", sig.when().offset_minutes()),
446 ] 447 ]
447} 448}
448 449
@@ -477,7 +478,7 @@ fn apply_patch(git_repo: &Repo, patch: &nostr::Event) -> Result<()> {
477 index.add_all(["."], git2::IndexAddOption::DEFAULT, None)?; 478 index.add_all(["."], git2::IndexAddOption::DEFAULT, None)?;
478 index.write()?; 479 index.write()?;
479 480
480 let pgp_sig = if let Ok(pgp_sig) = tag_value(patch, "commit-sig") { 481 let pgp_sig = if let Ok(pgp_sig) = tag_value(patch, "commit-pgp-sig") {
481 if pgp_sig.is_empty() { 482 if pgp_sig.is_empty() {
482 None 483 None
483 } else { 484 } else {
@@ -585,22 +586,15 @@ fn extract_sig_from_patch_tags<'a>(
585 .find(|t| t.as_vec()[0].eq(tag_name)) 586 .find(|t| t.as_vec()[0].eq(tag_name))
586 .context(format!("tag '{tag_name}' not present in patch"))? 587 .context(format!("tag '{tag_name}' not present in patch"))?
587 .as_vec(); 588 .as_vec();
588 if v.len() != 4 { 589 if v.len() != 5 {
589 bail!("tag '{tag_name}' is incorrectly formatted") 590 bail!("tag '{tag_name}' is incorrectly formatted")
590 } 591 }
591 let git_time: Vec<&str> = v[3].split(',').collect();
592 if git_time.len() != 2 {
593 bail!("tag '{tag_name}' time is incorrectly formatted")
594 }
595 git2::Signature::new( 592 git2::Signature::new(
596 v[1].as_str(), 593 v[1].as_str(),
597 v[2].as_str(), 594 v[2].as_str(),
598 &git2::Time::new( 595 &git2::Time::new(
599 git_time[0] 596 v[3].parse().context("tag time is incorrectly formatted")?,
600 .parse() 597 v[4].parse()
601 .context("tag time is incorrectly formatted")?,
602 git_time[1]
603 .parse()
604 .context("tag time offset is incorrectly formatted")?, 598 .context("tag time offset is incorrectly formatted")?,
605 ), 599 ),
606 ) 600 )
@@ -611,7 +605,7 @@ fn extract_sig_from_patch_tags<'a>(
611mod tests { 605mod tests {
612 use std::fs; 606 use std::fs;
613 607
614 use test_utils::git::GitTestRepo; 608 use test_utils::{generate_repo_ref_event, git::GitTestRepo, TEST_KEY_1_KEYS};
615 609
616 use super::*; 610 use super::*;
617 611
@@ -706,19 +700,22 @@ mod tests {
706 #[test] 700 #[test]
707 fn no_offset() -> Result<()> { 701 fn no_offset() -> Result<()> {
708 let res = prep(&git2::Time::new(5000, 0))?; 702 let res = prep(&git2::Time::new(5000, 0))?;
709 assert_eq!("5000,0", res[2]); 703 assert_eq!("5000", res[2]);
704 assert_eq!("0", res[3]);
710 Ok(()) 705 Ok(())
711 } 706 }
712 #[test] 707 #[test]
713 fn positive_offset() -> Result<()> { 708 fn positive_offset() -> Result<()> {
714 let res = prep(&git2::Time::new(5000, 300))?; 709 let res = prep(&git2::Time::new(5000, 300))?;
715 assert_eq!("5000,300", res[2]); 710 assert_eq!("5000", res[2]);
711 assert_eq!("300", res[3]);
716 Ok(()) 712 Ok(())
717 } 713 }
718 #[test] 714 #[test]
719 fn negative_offset() -> Result<()> { 715 fn negative_offset() -> Result<()> {
720 let res = prep(&git2::Time::new(5000, -300))?; 716 let res = prep(&git2::Time::new(5000, -300))?;
721 assert_eq!("5000,-300", res[2]); 717 assert_eq!("5000", res[2]);
718 assert_eq!("-300", res[3]);
722 Ok(()) 719 Ok(())
723 } 720 }
724 } 721 }
@@ -1205,10 +1202,9 @@ mod tests {
1205 } 1202 }
1206 1203
1207 mod apply_patch { 1204 mod apply_patch {
1208 use test_utils::TEST_KEY_1_KEYS;
1209 1205
1210 use super::*; 1206 use super::*;
1211 use crate::sub_commands::prs::create::generate_patch_event; 1207 use crate::{repo_ref::RepoRef, sub_commands::prs::create::generate_patch_event};
1212 1208
1213 fn generate_patch_from_head_commit(test_repo: &GitTestRepo) -> Result<nostr::Event> { 1209 fn generate_patch_from_head_commit(test_repo: &GitTestRepo) -> Result<nostr::Event> {
1214 let original_oid = test_repo.git_repo.head()?.peel_to_commit()?.id(); 1210 let original_oid = test_repo.git_repo.head()?.peel_to_commit()?.id();
@@ -1219,6 +1215,8 @@ mod tests {
1219 &oid_to_sha1(&original_oid), 1215 &oid_to_sha1(&original_oid),
1220 nostr::EventId::all_zeros(), 1216 nostr::EventId::all_zeros(),
1221 &TEST_KEY_1_KEYS, 1217 &TEST_KEY_1_KEYS,
1218 &RepoRef::try_from(generate_repo_ref_event()).unwrap(),
1219 None,
1222 ) 1220 )
1223 } 1221 }
1224 fn test_patch_applies_to_repository(patch_event: nostr::Event) -> Result<()> { 1222 fn test_patch_applies_to_repository(patch_event: nostr::Event) -> Result<()> {
@@ -1358,7 +1356,7 @@ mod tests {
1358 use test_utils::TEST_KEY_1_KEYS; 1356 use test_utils::TEST_KEY_1_KEYS;
1359 1357
1360 use super::*; 1358 use super::*;
1361 use crate::sub_commands::prs::create::generate_pr_and_patch_events; 1359 use crate::{repo_ref::RepoRef, sub_commands::prs::create::generate_pr_and_patch_events};
1362 1360
1363 static BRANCH_NAME: &str = "add-example-feature"; 1361 static BRANCH_NAME: &str = "add-example-feature";
1364 // returns original_repo, pr_event, patch_events 1362 // returns original_repo, pr_event, patch_events
@@ -1378,6 +1376,7 @@ mod tests {
1378 &git_repo, 1376 &git_repo,
1379 &vec![oid_to_sha1(&oid1), oid_to_sha1(&oid2), oid_to_sha1(&oid3)], 1377 &vec![oid_to_sha1(&oid1), oid_to_sha1(&oid2), oid_to_sha1(&oid3)],
1380 &TEST_KEY_1_KEYS, 1378 &TEST_KEY_1_KEYS,
1379 &RepoRef::try_from(generate_repo_ref_event()).unwrap(),
1381 )?; 1380 )?;
1382 1381
1383 events.reverse(); 1382 events.reverse();