diff --git a/src/lib.rs b/src/lib.rs index 2a3444855b..76e5e748ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -363,7 +363,7 @@ pub enum BranchType { #[derive(PartialEq, Eq, Debug, Copy, Clone)] pub enum ConfigLevel { /// System-wide on Windows, for compatibility with portable git - ProgramData, + ProgramData = 1, /// System-wide configuration file, e.g. /etc/gitconfig System, /// XDG-compatible configuration file, e.g. ~/.config/git/config @@ -375,7 +375,7 @@ pub enum ConfigLevel { /// Application specific configuration file App, /// Highest level available - Highest, + Highest = -1, } /// Merge file favor options for `MergeOptions` instruct the file-level diff --git a/src/opts.rs b/src/opts.rs index 71fa4eadf4..ca36804e92 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -1,6 +1,70 @@ //! Bindings to libgit2's git_libgit2_opts function. -use crate::raw; +use std::ffi::CString; + +use crate::util::Binding; +use crate::{call, raw, Buf, ConfigLevel, Error, IntoCString}; + +/// Set the search path for a level of config data. The search path applied to +/// shared attributes and ignore files, too. +/// +/// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`], +/// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`]. +/// +/// `path` lists directories delimited by `GIT_PATH_LIST_SEPARATOR`. +/// Use magic path `$PATH` to include the old value of the path +/// (if you want to prepend or append, for instance). +/// +/// This function is unsafe as it mutates the global state but cannot guarantee +/// thread-safety. It needs to be externally synchronized with calls to access +/// the global state. +pub unsafe fn set_search_path
(level: ConfigLevel, path: P) -> Result<(), Error>
+where
+ P: IntoCString,
+{
+ call::c_try(raw::git_libgit2_opts(
+ raw::GIT_OPT_SET_SEARCH_PATH as libc::c_int,
+ level as libc::c_int,
+ path.into_c_string()?.as_ptr(),
+ ))?;
+ Ok(())
+}
+
+/// Reset the search path for a given level of config data to the default
+/// (generally based on environment variables).
+///
+/// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
+/// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
+///
+/// This function is unsafe as it mutates the global state but cannot guarantee
+/// thread-safety. It needs to be externally synchronized with calls to access
+/// the global state.
+pub unsafe fn reset_search_path(level: ConfigLevel) -> Result<(), Error> {
+ call::c_try(raw::git_libgit2_opts(
+ raw::GIT_OPT_SET_SEARCH_PATH as libc::c_int,
+ level as libc::c_int,
+ core::ptr::null::