1 //! Bindings to libgit2's git_libgit2_opts function.
2 
3 use std::ffi::CString;
4 
5 use crate::util::Binding;
6 use crate::{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.
set_search_path<P>(level: ConfigLevel, path: P) -> Result<(), Error> where P: IntoCString,21 pub unsafe fn set_search_path<P>(level: ConfigLevel, path: P) -> Result<(), Error>
22 where
23     P: IntoCString,
24 {
25     crate::init();
26     try_call!(raw::git_libgit2_opts(
27         raw::GIT_OPT_SET_SEARCH_PATH as libc::c_int,
28         level as libc::c_int,
29         path.into_c_string()?.as_ptr()
30     ));
31     Ok(())
32 }
33 
34 /// Reset the search path for a given level of config data to the default
35 /// (generally based on environment variables).
36 ///
37 /// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
38 /// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
39 ///
40 /// This function is unsafe as it mutates the global state but cannot guarantee
41 /// thread-safety. It needs to be externally synchronized with calls to access
42 /// the global state.
reset_search_path(level: ConfigLevel) -> Result<(), Error>43 pub unsafe fn reset_search_path(level: ConfigLevel) -> Result<(), Error> {
44     crate::init();
45     try_call!(raw::git_libgit2_opts(
46         raw::GIT_OPT_SET_SEARCH_PATH as libc::c_int,
47         level as libc::c_int,
48         core::ptr::null::<u8>()
49     ));
50     Ok(())
51 }
52 
53 /// Get the search path for a given level of config data.
54 ///
55 /// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
56 /// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
57 ///
58 /// This function is unsafe as it mutates the global state but cannot guarantee
59 /// thread-safety. It needs to be externally synchronized with calls to access
60 /// the global state.
get_search_path(level: ConfigLevel) -> Result<CString, Error>61 pub unsafe fn get_search_path(level: ConfigLevel) -> Result<CString, Error> {
62     crate::init();
63     let buf = Buf::new();
64     try_call!(raw::git_libgit2_opts(
65         raw::GIT_OPT_GET_SEARCH_PATH as libc::c_int,
66         level as libc::c_int,
67         buf.raw() as *const _
68     ));
69     buf.into_c_string()
70 }
71 
72 /// Controls whether or not libgit2 will verify when writing an object that all
73 /// objects it references are valid. Enabled by default, but disabling this can
74 /// significantly improve performance, at the cost of potentially allowing the
75 /// creation of objects that reference invalid objects (due to programming
76 /// error or repository corruption).
strict_object_creation(enabled: bool)77 pub fn strict_object_creation(enabled: bool) {
78     let error = unsafe {
79         raw::git_libgit2_opts(
80             raw::GIT_OPT_ENABLE_STRICT_OBJECT_CREATION as libc::c_int,
81             enabled as libc::c_int,
82         )
83     };
84     // This function cannot actually fail, but the function has an error return
85     // for other options that can.
86     debug_assert!(error >= 0);
87 }
88 
89 /// Controls whether or not libgit2 will verify that objects loaded have the
90 /// expected hash. Enabled by default, but disabling this can significantly
91 /// improve performance, at the cost of relying on repository integrity
92 /// without checking it.
strict_hash_verification(enabled: bool)93 pub fn strict_hash_verification(enabled: bool) {
94     let error = unsafe {
95         raw::git_libgit2_opts(
96             raw::GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION as libc::c_int,
97             enabled as libc::c_int,
98         )
99     };
100     // This function cannot actually fail, but the function has an error return
101     // for other options that can.
102     debug_assert!(error >= 0);
103 }
104 
105 #[cfg(test)]
106 mod test {
107     use super::*;
108 
109     #[test]
smoke()110     fn smoke() {
111         strict_hash_verification(false);
112     }
113 }
114