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:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-06-28 15:16:43 +0100
committerDanConwayDev <DanConwayDev@protonmail.com>2024-06-28 15:16:43 +0100
commita82546b70303000b4fc053a1ee21d3d8c7d6ad66 (patch)
treef8c4238a5ae27759b3c1a6adb5c865b07e339a66 /src/git.rs
parent6b06e874119ceca1a9dac1b94dcfe6e06aacd7b9 (diff)
feat(login): login with nip46 remote signer
and save details in git config
Diffstat (limited to 'src/git.rs')
-rw-r--r--src/git.rs93
1 files changed, 81 insertions, 12 deletions
diff --git a/src/git.rs b/src/git.rs
index 46687ae..bb943a9 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -76,8 +76,9 @@ pub trait RepoActions {
76 ) -> Result<Vec<nostr::Event>>; 76 ) -> Result<Vec<nostr::Event>>;
77 fn parse_starting_commits(&self, starting_commits: &str) -> Result<Vec<Sha1Hash>>; 77 fn parse_starting_commits(&self, starting_commits: &str) -> Result<Vec<Sha1Hash>>;
78 fn ancestor_of(&self, decendant: &Sha1Hash, ancestor: &Sha1Hash) -> Result<bool>; 78 fn ancestor_of(&self, decendant: &Sha1Hash, ancestor: &Sha1Hash) -> Result<bool>;
79 fn get_git_config_item(&self, item: &str, global: bool) -> Result<Option<String>>; 79 fn get_git_config_item(&self, item: &str, global: Option<bool>) -> Result<Option<String>>;
80 fn save_git_config_item(&self, item: &str, value: &str, global: bool) -> Result<()>; 80 fn save_git_config_item(&self, item: &str, value: &str, global: bool) -> Result<()>;
81 fn remove_git_config_item(&self, item: &str, global: bool) -> Result<bool>;
81} 82}
82 83
83impl RepoActions for Repo { 84impl RepoActions for Repo {
@@ -581,8 +582,15 @@ impl RepoActions for Repo {
581 } 582 }
582 } 583 }
583 584
584 fn get_git_config_item(&self, item: &str, global: bool) -> Result<Option<String>> { 585 /// setting global to None will suppliment local config with global items
585 match if global { 586 /// not in local
587 fn get_git_config_item(&self, item: &str, global: Option<bool>) -> Result<Option<String>> {
588 let just_global = if let Some(just_global) = global {
589 just_global
590 } else {
591 false
592 };
593 match if just_global {
586 self.git_repo 594 self.git_repo
587 .config() 595 .config()
588 .context("cannot open git config")? 596 .context("cannot open git config")?
@@ -593,11 +601,22 @@ impl RepoActions for Repo {
593 } 601 }
594 .get_entry(item) 602 .get_entry(item)
595 { 603 {
596 Ok(item) => Ok(Some( 604 Ok(item) => {
597 item.value() 605 if let Some(global) = global {
598 .context("cannot find git config item")? 606 if item.level().eq(&git2::ConfigLevel::Local) {
599 .to_string(), 607 if global {
600 )), 608 bail!("only local repository login available")
609 }
610 } else if !global {
611 bail!("only global repository login available")
612 }
613 }
614 Ok(Some(
615 item.value()
616 .context("cannot find git config item")?
617 .to_string(),
618 ))
619 }
601 Err(_) => Ok(None), 620 Err(_) => Ok(None),
602 } 621 }
603 } 622 }
@@ -613,9 +632,33 @@ impl RepoActions for Repo {
613 self.git_repo.config().context("cannot open git config")? 632 self.git_repo.config().context("cannot open git config")?
614 } 633 }
615 .set_str(item, value) 634 .set_str(item, value)
616 .context("cannot set git config value")?; 635 .context(format!(
636 "cannot set {} git config item {}",
637 if global { "global" } else { "local" },
638 item
639 ))?;
617 Ok(()) 640 Ok(())
618 } 641 }
642
643 /// returns false if item doesn't exist
644 fn remove_git_config_item(&self, item: &str, global: bool) -> Result<bool> {
645 if self.get_git_config_item(item, Some(global))?.is_none() {
646 Ok(false)
647 } else {
648 if global {
649 self.git_repo
650 .config()
651 .context("cannot open git config")?
652 .open_global()
653 .context("cannot open global git config")?
654 } else {
655 self.git_repo.config().context("cannot open git config")?
656 }
657 .remove(item)
658 .context("cannot remove existing git config item")?;
659 Ok(true)
660 }
661 }
619} 662}
620 663
621fn oid_to_u8_20_bytes(oid: &Oid) -> [u8; 20] { 664fn oid_to_u8_20_bytes(oid: &Oid) -> [u8; 20] {
@@ -849,7 +892,9 @@ mod tests {
849 let git_repo = Repo::from_path(&test_repo.dir)?; 892 let git_repo = Repo::from_path(&test_repo.dir)?;
850 git_repo.save_git_config_item("test.item", "testvalue", false)?; 893 git_repo.save_git_config_item("test.item", "testvalue", false)?;
851 assert_eq!( 894 assert_eq!(
852 git_repo.get_git_config_item("test.item", false)?.unwrap(), 895 git_repo
896 .get_git_config_item("test.item", Some(false))?
897 .unwrap(),
853 "testvalue", 898 "testvalue",
854 ); 899 );
855 Ok(()) 900 Ok(())
@@ -859,7 +904,10 @@ mod tests {
859 fn get_git_config_item_returns_none_if_not_present() -> Result<()> { 904 fn get_git_config_item_returns_none_if_not_present() -> Result<()> {
860 let test_repo = GitTestRepo::default(); 905 let test_repo = GitTestRepo::default();
861 let git_repo = Repo::from_path(&test_repo.dir)?; 906 let git_repo = Repo::from_path(&test_repo.dir)?;
862 assert_eq!(git_repo.get_git_config_item("test.item", false)?, None); 907 assert_eq!(
908 git_repo.get_git_config_item("test.item", Some(false))?,
909 None
910 );
863 Ok(()) 911 Ok(())
864 } 912 }
865 913
@@ -869,11 +917,32 @@ mod tests {
869 let git_repo = Repo::from_path(&test_repo.dir)?; 917 let git_repo = Repo::from_path(&test_repo.dir)?;
870 git_repo.save_git_config_item("test.item", "", false)?; 918 git_repo.save_git_config_item("test.item", "", false)?;
871 assert_eq!( 919 assert_eq!(
872 git_repo.get_git_config_item("test.item", false)?, 920 git_repo.get_git_config_item("test.item", Some(false))?,
873 Some("".to_string()), 921 Some("".to_string()),
874 ); 922 );
875 Ok(()) 923 Ok(())
876 } 924 }
925
926 #[test]
927 fn remove_local_git_config_item() -> Result<()> {
928 let test_repo = GitTestRepo::default();
929 let git_repo = Repo::from_path(&test_repo.dir)?;
930 git_repo.save_git_config_item("test.item", "testvalue", false)?;
931 assert!(git_repo.remove_git_config_item("test.item", false)?);
932 assert_eq!(
933 git_repo.get_git_config_item("test.item", Some(false))?,
934 None,
935 );
936 Ok(())
937 }
938
939 #[test]
940 fn remove_git_config_item_returns_false_if_item_wasnt_set() -> Result<()> {
941 let test_repo = GitTestRepo::default();
942 let git_repo = Repo::from_path(&test_repo.dir)?;
943 assert!(!(git_repo.remove_git_config_item("test.item", false)?));
944 Ok(())
945 }
877 } 946 }
878 947
879 #[test] 948 #[test]