1
1
//! Bindings to libgit2's git_libgit2_opts function.
2
2
3
+ use std:: sync:: Mutex ;
4
+
5
+ use once_cell:: sync:: Lazy ;
6
+
3
7
use crate :: util:: Binding ;
4
8
use crate :: { call, raw, Buf , ConfigLevel , Error , IntoCString } ;
5
9
10
+ static SEARCH_PATH : Lazy < Mutex < SearchPath > > = Lazy :: new ( || {
11
+ crate :: init ( ) ;
12
+ Mutex :: new ( SearchPath )
13
+ } ) ;
14
+
15
+ struct SearchPath ;
16
+
17
+ impl SearchPath {
18
+ pub fn set < P > ( & self , level : ConfigLevel , path : P ) -> Result < ( ) , Error >
19
+ where
20
+ P : IntoCString ,
21
+ {
22
+ let path = path. into_c_string ( ) ?;
23
+ unsafe {
24
+ call:: c_try ( raw:: git_libgit2_opts (
25
+ raw:: GIT_OPT_SET_SEARCH_PATH as libc:: c_int ,
26
+ level as libc:: c_int ,
27
+ path. as_ptr ( ) ,
28
+ ) ) ?;
29
+ }
30
+ Ok ( ( ) )
31
+ }
32
+
33
+ pub fn reset ( & self , level : ConfigLevel ) -> Result < ( ) , Error > {
34
+ unsafe {
35
+ call:: c_try ( raw:: git_libgit2_opts (
36
+ raw:: GIT_OPT_SET_SEARCH_PATH as libc:: c_int ,
37
+ level as libc:: c_int ,
38
+ core:: ptr:: null :: < u8 > ( ) ,
39
+ ) ) ?;
40
+ }
41
+ Ok ( ( ) )
42
+ }
43
+
44
+ pub fn get ( & self , level : ConfigLevel ) -> Result < String , Error > {
45
+ let buf = Buf :: new ( ) ;
46
+ unsafe {
47
+ call:: c_try ( raw:: git_libgit2_opts (
48
+ raw:: GIT_OPT_GET_SEARCH_PATH as libc:: c_int ,
49
+ level as libc:: c_int ,
50
+ buf. raw ( ) ,
51
+ ) ) ?;
52
+ }
53
+ Ok ( buf. as_str ( ) . unwrap ( ) . to_string ( ) )
54
+ }
55
+ }
56
+
6
57
/// Set the search path for a level of config data. The search path applied to
7
58
/// shared attributes and ignore files, too.
8
59
///
@@ -16,16 +67,7 @@ pub fn set_search_path<P>(level: ConfigLevel, path: P) -> Result<(), Error>
16
67
where
17
68
P : IntoCString ,
18
69
{
19
- crate :: init ( ) ;
20
- let path = path. into_c_string ( ) ?;
21
- unsafe {
22
- call:: c_try ( raw:: git_libgit2_opts (
23
- raw:: GIT_OPT_SET_SEARCH_PATH as libc:: c_int ,
24
- level as libc:: c_int ,
25
- path. as_ptr ( ) ,
26
- ) ) ?;
27
- }
28
- Ok ( ( ) )
70
+ SEARCH_PATH . lock ( ) . unwrap ( ) . set ( level, path)
29
71
}
30
72
31
73
/// Reset the search path for a given level of config data to the default
@@ -34,31 +76,15 @@ where
34
76
/// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
35
77
/// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
36
78
pub fn reset_search_path ( level : ConfigLevel ) -> Result < ( ) , Error > {
37
- crate :: init ( ) ;
38
- unsafe {
39
- call:: c_try ( raw:: git_libgit2_opts (
40
- raw:: GIT_OPT_SET_SEARCH_PATH as libc:: c_int ,
41
- level as libc:: c_int ,
42
- core:: ptr:: null :: < u8 > ( ) ,
43
- ) ) ?;
44
- }
45
- Ok ( ( ) )
79
+ SEARCH_PATH . lock ( ) . unwrap ( ) . reset ( level)
46
80
}
47
81
48
82
/// Get the search path for a given level of config data.
49
83
///
50
84
/// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
51
85
/// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
52
86
pub fn get_search_path ( level : ConfigLevel ) -> Result < String , Error > {
53
- let buf = Buf :: new ( ) ;
54
- unsafe {
55
- call:: c_try ( raw:: git_libgit2_opts (
56
- raw:: GIT_OPT_GET_SEARCH_PATH as libc:: c_int ,
57
- level as libc:: c_int ,
58
- buf. raw ( ) ,
59
- ) ) ?;
60
- }
61
- Ok ( buf. as_str ( ) . unwrap ( ) . to_string ( ) )
87
+ SEARCH_PATH . lock ( ) . unwrap ( ) . get ( level)
62
88
}
63
89
64
90
/// Controls whether or not libgit2 will verify when writing an object that all
0 commit comments