use std::ffi::CString;
use std::ptr;
use crate::string_array::StringArray;
use crate::util::Binding;
use crate::{raw, Buf, ConfigLevel, Error, IntoCString, ObjectType};
pub unsafe fn set_search_path<P>(level: ConfigLevel, path: P) -> Result<(), Error>
where
P: IntoCString,
{
crate::init();
try_call!(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(())
}
pub unsafe fn reset_search_path(level: ConfigLevel) -> Result<(), Error> {
crate::init();
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_SET_SEARCH_PATH as libc::c_int,
level as libc::c_int,
core::ptr::null::<u8>()
));
Ok(())
}
pub unsafe fn get_search_path(level: ConfigLevel) -> Result<CString, Error> {
crate::init();
let buf = Buf::new();
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_GET_SEARCH_PATH as libc::c_int,
level as libc::c_int,
buf.raw() as *const _
));
buf.into_c_string()
}
pub fn enable_caching(enabled: bool) {
crate::init();
let error = unsafe {
raw::git_libgit2_opts(
raw::GIT_OPT_ENABLE_CACHING as libc::c_int,
enabled as libc::c_int,
)
};
debug_assert!(error >= 0);
}
pub unsafe fn set_cache_object_limit(kind: ObjectType, size: libc::size_t) -> Result<(), Error> {
crate::init();
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_SET_CACHE_OBJECT_LIMIT as libc::c_int,
kind as libc::c_int,
size
));
Ok(())
}
pub fn strict_object_creation(enabled: bool) {
crate::init();
let error = unsafe {
raw::git_libgit2_opts(
raw::GIT_OPT_ENABLE_STRICT_OBJECT_CREATION as libc::c_int,
enabled as libc::c_int,
)
};
debug_assert!(error >= 0);
}
pub fn strict_hash_verification(enabled: bool) {
crate::init();
let error = unsafe {
raw::git_libgit2_opts(
raw::GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION as libc::c_int,
enabled as libc::c_int,
)
};
debug_assert!(error >= 0);
}
pub unsafe fn get_extensions() -> Result<StringArray, Error> {
crate::init();
let mut extensions = raw::git_strarray {
strings: ptr::null_mut(),
count: 0,
};
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_GET_EXTENSIONS as libc::c_int,
&mut extensions
));
Ok(StringArray::from_raw(extensions))
}
pub unsafe fn set_extensions<E>(extensions: &[E]) -> Result<(), Error>
where
for<'x> &'x E: IntoCString,
{
crate::init();
let extensions = extensions
.iter()
.map(|e| e.into_c_string())
.collect::<Result<Vec<_>, _>>()?;
let extension_ptrs = extensions.iter().map(|e| e.as_ptr()).collect::<Vec<_>>();
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_SET_EXTENSIONS as libc::c_int,
extension_ptrs.as_ptr(),
extension_ptrs.len() as libc::size_t
));
Ok(())
}
pub unsafe fn set_verify_owner_validation(enabled: bool) -> Result<(), Error> {
crate::init();
let error = raw::git_libgit2_opts(
raw::GIT_OPT_SET_OWNER_VALIDATION as libc::c_int,
enabled as libc::c_int,
);
debug_assert!(error >= 0);
Ok(())
}
pub unsafe fn set_ssl_cert_file<P>(file: P) -> Result<(), Error>
where
P: IntoCString,
{
crate::init();
unsafe {
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_SET_SSL_CERT_LOCATIONS as libc::c_int,
file.into_c_string()?.as_ptr(),
core::ptr::null::<libc::c_char>()
));
}
Ok(())
}
pub unsafe fn set_ssl_cert_dir<P>(path: P) -> Result<(), Error>
where
P: IntoCString,
{
crate::init();
unsafe {
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_SET_SSL_CERT_LOCATIONS as libc::c_int,
core::ptr::null::<libc::c_char>(),
path.into_c_string()?.as_ptr()
));
}
Ok(())
}
pub unsafe fn get_mwindow_size() -> Result<libc::size_t, Error> {
crate::init();
let mut size = 0;
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_GET_MWINDOW_SIZE as libc::c_int,
&mut size
));
Ok(size)
}
pub unsafe fn set_mwindow_size(size: libc::size_t) -> Result<(), Error> {
crate::init();
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_SET_MWINDOW_SIZE as libc::c_int,
size
));
Ok(())
}
pub unsafe fn get_mwindow_mapped_limit() -> Result<libc::size_t, Error> {
crate::init();
let mut limit = 0;
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_GET_MWINDOW_MAPPED_LIMIT as libc::c_int,
&mut limit
));
Ok(limit)
}
pub unsafe fn set_mwindow_mapped_limit(limit: libc::size_t) -> Result<(), Error> {
crate::init();
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_SET_MWINDOW_MAPPED_LIMIT as libc::c_int,
limit
));
Ok(())
}
pub unsafe fn get_mwindow_file_limit() -> Result<libc::size_t, Error> {
crate::init();
let mut limit = 0;
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_GET_MWINDOW_FILE_LIMIT as libc::c_int,
&mut limit
));
Ok(limit)
}
pub unsafe fn set_mwindow_file_limit(limit: libc::size_t) -> Result<(), Error> {
crate::init();
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_SET_MWINDOW_FILE_LIMIT as libc::c_int,
limit
));
Ok(())
}
pub unsafe fn get_server_connect_timeout_in_milliseconds() -> Result<libc::c_int, Error> {
crate::init();
let mut server_connect_timeout = 0;
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_GET_SERVER_CONNECT_TIMEOUT as libc::c_int,
&mut server_connect_timeout
));
Ok(server_connect_timeout)
}
pub unsafe fn set_server_connect_timeout_in_milliseconds(
timeout: libc::c_int,
) -> Result<(), Error> {
crate::init();
let error = raw::git_libgit2_opts(
raw::GIT_OPT_SET_SERVER_CONNECT_TIMEOUT as libc::c_int,
timeout,
);
debug_assert!(error >= 0);
Ok(())
}
pub unsafe fn get_server_timeout_in_milliseconds() -> Result<libc::c_int, Error> {
crate::init();
let mut server_timeout = 0;
try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_GET_SERVER_TIMEOUT as libc::c_int,
&mut server_timeout
));
Ok(server_timeout)
}
pub unsafe fn set_server_timeout_in_milliseconds(timeout: libc::c_int) -> Result<(), Error> {
crate::init();
let error = raw::git_libgit2_opts(
raw::GIT_OPT_SET_SERVER_TIMEOUT as libc::c_int,
timeout as libc::c_int,
);
debug_assert!(error >= 0);
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn smoke() {
strict_hash_verification(false);
}
#[test]
fn mwindow_size() {
unsafe {
assert!(set_mwindow_size(1024).is_ok());
assert!(get_mwindow_size().unwrap() == 1024);
}
}
#[test]
fn mwindow_mapped_limit() {
unsafe {
assert!(set_mwindow_mapped_limit(1024).is_ok());
assert!(get_mwindow_mapped_limit().unwrap() == 1024);
}
}
#[test]
fn mwindow_file_limit() {
unsafe {
assert!(set_mwindow_file_limit(1024).is_ok());
assert!(get_mwindow_file_limit().unwrap() == 1024);
}
}
#[test]
fn server_connect_timeout() {
unsafe {
assert!(set_server_connect_timeout_in_milliseconds(5000).is_ok());
assert!(get_server_connect_timeout_in_milliseconds().unwrap() == 5000);
}
}
#[test]
fn server_timeout() {
unsafe {
assert!(set_server_timeout_in_milliseconds(10_000).is_ok());
assert!(get_server_timeout_in_milliseconds().unwrap() == 10_000);
}
}
}