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 cache loaded objects.  Enabled by
73 /// default, but disabling this can improve performance and memory usage if
74 /// loading a large number of objects that will not be referenced again.
75 /// Disabling this will cause repository objects to clear their caches when next
76 /// accessed.
enable_caching(enabled: bool)77 pub fn enable_caching(enabled: bool) {
78     let error = unsafe {
79         raw::git_libgit2_opts(
80             raw::GIT_OPT_ENABLE_CACHING 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 when writing an object that all
90 /// objects it references are valid. Enabled by default, but disabling this can
91 /// significantly improve performance, at the cost of potentially allowing the
92 /// creation of objects that reference invalid objects (due to programming
93 /// error or repository corruption).
strict_object_creation(enabled: bool)94 pub fn strict_object_creation(enabled: bool) {
95     let error = unsafe {
96         raw::git_libgit2_opts(
97             raw::GIT_OPT_ENABLE_STRICT_OBJECT_CREATION as libc::c_int,
98             enabled as libc::c_int,
99         )
100     };
101     // This function cannot actually fail, but the function has an error return
102     // for other options that can.
103     debug_assert!(error >= 0);
104 }
105 
106 /// Controls whether or not libgit2 will verify that objects loaded have the
107 /// expected hash. Enabled by default, but disabling this can significantly
108 /// improve performance, at the cost of relying on repository integrity
109 /// without checking it.
strict_hash_verification(enabled: bool)110 pub fn strict_hash_verification(enabled: bool) {
111     let error = unsafe {
112         raw::git_libgit2_opts(
113             raw::GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION as libc::c_int,
114             enabled as libc::c_int,
115         )
116     };
117     // This function cannot actually fail, but the function has an error return
118     // for other options that can.
119     debug_assert!(error >= 0);
120 }
121 
122 #[cfg(test)]
123 mod test {
124     use super::*;
125 
126     #[test]
smoke()127     fn smoke() {
128         strict_hash_verification(false);
129     }
130 }
131