Skip to content

Commit 980bf38

Browse files
committed
feat(opts): get/set/reset search path
1 parent b797055 commit 980bf38

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

src/opts.rs

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,65 @@
11
//! Bindings to libgit2's git_libgit2_opts function.
22
3-
use crate::raw;
3+
use crate::util::Binding;
4+
use crate::{call, raw, Buf, ConfigLevel, Error, IntoCString};
5+
6+
/// Set the search path for a level of config data. The search path applied to
7+
/// shared attributes and ignore files, too.
8+
///
9+
/// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
10+
/// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
11+
///
12+
/// `path` lists directories delimited by `GIT_PATH_LIST_SEPARATOR`.
13+
/// Use magic path `$PATH` to include the old value of the path
14+
/// (if you want to prepend or append, for instance).
15+
pub fn set_search_path<P>(level: ConfigLevel, path: P) -> Result<(), Error>
16+
where
17+
P: IntoCString,
18+
{
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,
26+
))?;
27+
}
28+
Ok(())
29+
}
30+
31+
/// Reset the search path for a given level of config data to the default
32+
/// (generally based on environment variables).
33+
///
34+
/// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
35+
/// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
36+
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(())
46+
}
47+
48+
/// Get the search path for a given level of config data.
49+
///
50+
/// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
51+
/// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
52+
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())
62+
}
463

564
/// Controls whether or not libgit2 will verify when writing an object that all
665
/// objects it references are valid. Enabled by default, but disabling this can

0 commit comments

Comments
 (0)