From 8b8a5c43c101718f319cfbb8904d962e7335160b Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 13 Jun 2024 13:59:03 +0100 Subject: feat(git): add get and save git config item local or global but tests only added for local --- src/git.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'src/git.rs') 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 { ) -> Result>; fn parse_starting_commits(&self, starting_commits: &str) -> Result>; fn ancestor_of(&self, decendant: &Sha1Hash, ancestor: &Sha1Hash) -> Result; + fn get_git_config_item(&self, item: &str, global: bool) -> Result>; + fn save_git_config_item(&self, item: &str, value: &str, global: bool) -> Result<()>; } impl RepoActions for Repo { @@ -578,6 +580,42 @@ impl RepoActions for Repo { Ok(false) } } + + fn get_git_config_item(&self, item: &str, global: bool) -> Result> { + match if global { + self.git_repo + .config() + .context("cannot open git config")? + .open_global() + .context("cannot open global git config")? + } else { + self.git_repo.config().context("cannot open git config")? + } + .get_entry(item) + { + Ok(item) => Ok(Some( + item.value() + .context("cannot find git config item")? + .to_string(), + )), + Err(_) => Ok(None), + } + } + + fn save_git_config_item(&self, item: &str, value: &str, global: bool) -> Result<()> { + if global { + self.git_repo + .config() + .context("cannot open git config")? + .open_global() + .context("cannot open global git config")? + } else { + self.git_repo.config().context("cannot open git config")? + } + .set_str(item, value) + .context("cannot set git config value")?; + Ok(()) + } } fn oid_to_u8_20_bytes(oid: &Oid) -> [u8; 20] { @@ -794,6 +832,50 @@ mod tests { use super::*; + mod git_config_item_local { + use super::*; + + #[test] + fn save_git_config_item_returns_ok() -> Result<()> { + let test_repo = GitTestRepo::default(); + let git_repo = Repo::from_path(&test_repo.dir)?; + git_repo.save_git_config_item("test.item", "testvalue", false)?; + Ok(()) + } + + #[test] + fn get_git_config_item_returns_item_just_saved() -> Result<()> { + let test_repo = GitTestRepo::default(); + let git_repo = Repo::from_path(&test_repo.dir)?; + git_repo.save_git_config_item("test.item", "testvalue", false)?; + assert_eq!( + git_repo.get_git_config_item("test.item", false)?.unwrap(), + "testvalue", + ); + Ok(()) + } + + #[test] + fn get_git_config_item_returns_none_if_not_present() -> Result<()> { + let test_repo = GitTestRepo::default(); + let git_repo = Repo::from_path(&test_repo.dir)?; + assert_eq!(git_repo.get_git_config_item("test.item", false)?, None); + Ok(()) + } + + #[test] + fn get_git_config_item_empty_string_returns_empty_string_instead_of_none() -> Result<()> { + let test_repo = GitTestRepo::default(); + let git_repo = Repo::from_path(&test_repo.dir)?; + git_repo.save_git_config_item("test.item", "", false)?; + assert_eq!( + git_repo.get_git_config_item("test.item", false)?, + Some("".to_string()), + ); + Ok(()) + } + } + #[test] fn get_commit_parent() -> Result<()> { let test_repo = GitTestRepo::default(); -- cgit v1.2.3