diff options
Diffstat (limited to 'src/lib/git/mod.rs')
| -rw-r--r-- | src/lib/git/mod.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/lib/git/mod.rs b/src/lib/git/mod.rs index 5d14ce3..45ac58c 100644 --- a/src/lib/git/mod.rs +++ b/src/lib/git/mod.rs | |||
| @@ -860,6 +860,44 @@ fn extract_sig_from_patch_tags<'a>(tags: &'a Tags, tag_name: &str) -> Result<git | |||
| 860 | .context("failed to create git signature") | 860 | .context("failed to create git signature") |
| 861 | } | 861 | } |
| 862 | 862 | ||
| 863 | pub fn get_git_config_item(git_repo: &Option<&Repo>, item: &str) -> Result<Option<String>> { | ||
| 864 | if let Some(git_repo) = git_repo { | ||
| 865 | git_repo.get_git_config_item(item, Some(false)) | ||
| 866 | } else { | ||
| 867 | Ok( | ||
| 868 | match git2::Config::open_default()?.open_global()?.get_entry(item) { | ||
| 869 | Ok(item) => item.value().map(|v| v.to_string()), | ||
| 870 | Err(_) => None, | ||
| 871 | }, | ||
| 872 | ) | ||
| 873 | } | ||
| 874 | } | ||
| 875 | |||
| 876 | pub fn save_git_config_item(git_repo: &Option<&Repo>, item: &str, value: &str) -> Result<()> { | ||
| 877 | if let Some(git_repo) = git_repo { | ||
| 878 | git_repo.save_git_config_item(item, value, false) | ||
| 879 | } else { | ||
| 880 | git2::Config::open_default()? | ||
| 881 | .open_global()? | ||
| 882 | .set_str(item, value) | ||
| 883 | .context(format!("cannot set global git config item {}", item)) | ||
| 884 | } | ||
| 885 | } | ||
| 886 | |||
| 887 | pub fn remove_git_config_item(git_repo: &Option<&Repo>, item: &str) -> Result<bool> { | ||
| 888 | if let Some(git_repo) = git_repo { | ||
| 889 | git_repo.remove_git_config_item(item, false) | ||
| 890 | } else if get_git_config_item(&None, item)?.is_none() { | ||
| 891 | Ok(false) | ||
| 892 | } else { | ||
| 893 | git2::Config::open_default()? | ||
| 894 | .open_global()? | ||
| 895 | .remove(item) | ||
| 896 | .context(format!("cannot remove existing git config item {}", item))?; | ||
| 897 | Ok(true) | ||
| 898 | } | ||
| 899 | } | ||
| 900 | |||
| 863 | #[cfg(test)] | 901 | #[cfg(test)] |
| 864 | mod tests { | 902 | mod tests { |
| 865 | use std::fs; | 903 | use std::fs; |