|
1 | 1 | //! Bindings to libgit2's git_libgit2_opts function. |
2 | 2 |
|
3 | 3 | use std::ffi::CString; |
| 4 | +use std::ptr; |
4 | 5 |
|
| 6 | +use crate::string_array::StringArray; |
5 | 7 | use crate::util::Binding; |
6 | 8 | use crate::{raw, Buf, ConfigLevel, Error, IntoCString}; |
7 | 9 |
|
@@ -119,6 +121,63 @@ pub fn strict_hash_verification(enabled: bool) { |
119 | 121 | debug_assert!(error >= 0); |
120 | 122 | } |
121 | 123 |
|
| 124 | +/// Returns the list of git extensions that are supported. This is the list of |
| 125 | +/// built-in extensions supported by libgit2 and custom extensions that have |
| 126 | +/// been added with [`set_extensions`]. Extensions that have been negated will |
| 127 | +/// not be returned. |
| 128 | +/// |
| 129 | +/// # Safety |
| 130 | +/// |
| 131 | +/// libgit2 stores user extensions in a static variable. |
| 132 | +/// This function is effectively reading a `static mut` and should be treated as such |
| 133 | +pub unsafe fn get_extensions() -> Result<StringArray, Error> { |
| 134 | + crate::init(); |
| 135 | + |
| 136 | + let mut extensions = raw::git_strarray { |
| 137 | + strings: ptr::null_mut(), |
| 138 | + count: 0, |
| 139 | + }; |
| 140 | + |
| 141 | + try_call!(raw::git_libgit2_opts( |
| 142 | + raw::GIT_OPT_GET_EXTENSIONS as libc::c_int, |
| 143 | + &mut extensions |
| 144 | + )); |
| 145 | + |
| 146 | + Ok(StringArray::from_raw(extensions)) |
| 147 | +} |
| 148 | + |
| 149 | +/// Set that the given git extensions are supported by the caller. Extensions |
| 150 | +/// supported by libgit2 may be negated by prefixing them with a `!`. |
| 151 | +/// For example: setting extensions to `[ "!noop", "newext" ]` indicates that |
| 152 | +/// the caller does not want to support repositories with the `noop` extension |
| 153 | +/// but does want to support repositories with the `newext` extension. |
| 154 | +/// |
| 155 | +/// # Safety |
| 156 | +/// |
| 157 | +/// libgit2 stores user extensions in a static variable. |
| 158 | +/// This function is effectively modifying a `static mut` and should be treated as such |
| 159 | +pub unsafe fn set_extensions<E>(extensions: &[E]) -> Result<(), Error> |
| 160 | +where |
| 161 | + for<'x> &'x E: IntoCString, |
| 162 | +{ |
| 163 | + crate::init(); |
| 164 | + |
| 165 | + let extensions = extensions |
| 166 | + .iter() |
| 167 | + .map(|e| e.into_c_string()) |
| 168 | + .collect::<Result<Vec<_>, _>>()?; |
| 169 | + |
| 170 | + let extension_ptrs = extensions.iter().map(|e| e.as_ptr()).collect::<Vec<_>>(); |
| 171 | + |
| 172 | + try_call!(raw::git_libgit2_opts( |
| 173 | + raw::GIT_OPT_SET_EXTENSIONS as libc::c_int, |
| 174 | + extension_ptrs.as_ptr(), |
| 175 | + extension_ptrs.len() as libc::size_t |
| 176 | + )); |
| 177 | + |
| 178 | + Ok(()) |
| 179 | +} |
| 180 | + |
122 | 181 | #[cfg(test)] |
123 | 182 | mod test { |
124 | 183 | use super::*; |
|
0 commit comments