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.rs82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/git.rs b/src/git.rs
index fb3b353..7f44861 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -76,6 +76,8 @@ 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>>;
80 fn save_git_config_item(&self, item: &str, value: &str, global: bool) -> Result<()>;
79} 81}
80 82
81impl RepoActions for Repo { 83impl RepoActions for Repo {
@@ -578,6 +580,42 @@ impl RepoActions for Repo {
578 Ok(false) 580 Ok(false)
579 } 581 }
580 } 582 }
583
584 fn get_git_config_item(&self, item: &str, global: bool) -> Result<Option<String>> {
585 match if global {
586 self.git_repo
587 .config()
588 .context("cannot open git config")?
589 .open_global()
590 .context("cannot open global git config")?
591 } else {
592 self.git_repo.config().context("cannot open git config")?
593 }
594 .get_entry(item)
595 {
596 Ok(item) => Ok(Some(
597 item.value()
598 .context("cannot find git config item")?
599 .to_string(),
600 )),
601 Err(_) => Ok(None),
602 }
603 }
604
605 fn save_git_config_item(&self, item: &str, value: &str, global: bool) -> Result<()> {
606 if global {
607 self.git_repo
608 .config()
609 .context("cannot open git config")?
610 .open_global()
611 .context("cannot open global git config")?
612 } else {
613 self.git_repo.config().context("cannot open git config")?
614 }
615 .set_str(item, value)
616 .context("cannot set git config value")?;
617 Ok(())
618 }
581} 619}
582 620
583fn oid_to_u8_20_bytes(oid: &Oid) -> [u8; 20] { 621fn oid_to_u8_20_bytes(oid: &Oid) -> [u8; 20] {
@@ -794,6 +832,50 @@ mod tests {
794 832
795 use super::*; 833 use super::*;
796 834
835 mod git_config_item_local {
836 use super::*;
837
838 #[test]
839 fn save_git_config_item_returns_ok() -> Result<()> {
840 let test_repo = GitTestRepo::default();
841 let git_repo = Repo::from_path(&test_repo.dir)?;
842 git_repo.save_git_config_item("test.item", "testvalue", false)?;
843 Ok(())
844 }
845
846 #[test]
847 fn get_git_config_item_returns_item_just_saved() -> Result<()> {
848 let test_repo = GitTestRepo::default();
849 let git_repo = Repo::from_path(&test_repo.dir)?;
850 git_repo.save_git_config_item("test.item", "testvalue", false)?;
851 assert_eq!(
852 git_repo.get_git_config_item("test.item", false)?.unwrap(),
853 "testvalue",
854 );
855 Ok(())
856 }
857
858 #[test]
859 fn get_git_config_item_returns_none_if_not_present() -> Result<()> {
860 let test_repo = GitTestRepo::default();
861 let git_repo = Repo::from_path(&test_repo.dir)?;
862 assert_eq!(git_repo.get_git_config_item("test.item", false)?, None);
863 Ok(())
864 }
865
866 #[test]
867 fn get_git_config_item_empty_string_returns_empty_string_instead_of_none() -> Result<()> {
868 let test_repo = GitTestRepo::default();
869 let git_repo = Repo::from_path(&test_repo.dir)?;
870 git_repo.save_git_config_item("test.item", "", false)?;
871 assert_eq!(
872 git_repo.get_git_config_item("test.item", false)?,
873 Some("".to_string()),
874 );
875 Ok(())
876 }
877 }
878
797 #[test] 879 #[test]
798 fn get_commit_parent() -> Result<()> { 880 fn get_commit_parent() -> Result<()> {
799 let test_repo = GitTestRepo::default(); 881 let test_repo = GitTestRepo::default();