|
1 | 1 | //! Bindings to libgit2's git_libgit2_opts function.
|
2 | 2 |
|
3 |
| -use crate::raw; |
| 3 | +use std::ffi::CString; |
| 4 | + |
| 5 | +use crate::util::Binding; |
| 6 | +use crate::{call, raw, Buf, ConfigLevel, Error, IntoCString}; |
| 7 | + |
| 8 | +/// Set the search path for a level of config data. The search path applied to |
| 9 | +/// shared attributes and ignore files, too. |
| 10 | +/// |
| 11 | +/// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`], |
| 12 | +/// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`]. |
| 13 | +/// |
| 14 | +/// `path` lists directories delimited by `GIT_PATH_LIST_SEPARATOR`. |
| 15 | +/// Use magic path `$PATH` to include the old value of the path |
| 16 | +/// (if you want to prepend or append, for instance). |
| 17 | +/// |
| 18 | +/// This function is unsafe as it mutates the global state but cannot guarantee |
| 19 | +/// thread-safety. It needs to be externally synchronized with calls to access |
| 20 | +/// the global state. |
| 21 | +pub unsafe fn set_search_path<P>(level: ConfigLevel, path: P) -> Result<(), Error> |
| 22 | +where |
| 23 | + P: IntoCString, |
| 24 | +{ |
| 25 | + call::c_try(raw::git_libgit2_opts( |
| 26 | + raw::GIT_OPT_SET_SEARCH_PATH as libc::c_int, |
| 27 | + level as libc::c_int, |
| 28 | + path.into_c_string()?.as_ptr(), |
| 29 | + ))?; |
| 30 | + Ok(()) |
| 31 | +} |
| 32 | + |
| 33 | +/// Reset the search path for a given level of config data to the default |
| 34 | +/// (generally based on environment variables). |
| 35 | +/// |
| 36 | +/// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`], |
| 37 | +/// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`]. |
| 38 | +/// |
| 39 | +/// This function is unsafe as it mutates the global state but cannot guarantee |
| 40 | +/// thread-safety. It needs to be externally synchronized with calls to access |
| 41 | +/// the global state. |
| 42 | +pub unsafe fn reset_search_path(level: ConfigLevel) -> Result<(), Error> { |
| 43 | + call::c_try(raw::git_libgit2_opts( |
| 44 | + raw::GIT_OPT_SET_SEARCH_PATH as libc::c_int, |
| 45 | + level as libc::c_int, |
| 46 | + core::ptr::null::<u8>(), |
| 47 | + ))?; |
| 48 | + Ok(()) |
| 49 | +} |
| 50 | + |
| 51 | +/// Get the search path for a given level of config data. |
| 52 | +/// |
| 53 | +/// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`], |
| 54 | +/// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`]. |
| 55 | +/// |
| 56 | +/// This function is unsafe as it mutates the global state but cannot guarantee |
| 57 | +/// thread-safety. It needs to be externally synchronized with calls to access |
| 58 | +/// the global state. |
| 59 | +pub unsafe fn get_search_path(level: ConfigLevel) -> Result<CString, Error> { |
| 60 | + let buf = Buf::new(); |
| 61 | + call::c_try(raw::git_libgit2_opts( |
| 62 | + raw::GIT_OPT_GET_SEARCH_PATH as libc::c_int, |
| 63 | + level as libc::c_int, |
| 64 | + buf.raw(), |
| 65 | + ))?; |
| 66 | + buf.into_c_string() |
| 67 | +} |
4 | 68 |
|
5 | 69 | /// Controls whether or not libgit2 will verify when writing an object that all
|
6 | 70 | /// objects it references are valid. Enabled by default, but disabling this can
|
@@ -37,8 +101,10 @@ pub fn strict_hash_verification(enabled: bool) {
|
37 | 101 |
|
38 | 102 | #[cfg(test)]
|
39 | 103 | mod test {
|
| 104 | + use super::*; |
| 105 | + |
40 | 106 | #[test]
|
41 | 107 | fn smoke() {
|
42 |
| - super::strict_hash_verification(false); |
| 108 | + strict_hash_verification(false); |
43 | 109 | }
|
44 | 110 | }
|
0 commit comments