pub struct Config { /* private fields */ }
Expand description
A structure representing a git configuration key/value store
Implementations§
source§impl Config
impl Config
sourcepub fn new() -> Result<Config, Error>
pub fn new() -> Result<Config, Error>
Allocate a new configuration object
This object is empty, so you have to add a file to it before you can do anything with it.
sourcepub fn open(path: &Path) -> Result<Config, Error>
pub fn open(path: &Path) -> Result<Config, Error>
Create a new config instance containing a single on-disk file
sourcepub fn open_default() -> Result<Config, Error>
pub fn open_default() -> Result<Config, Error>
Open the global, XDG and system configuration files
Utility wrapper that finds the global, XDG and system configuration files and opens them into a single prioritized config object that can be used when accessing default config data outside a repository.
sourcepub fn find_global() -> Result<PathBuf, Error>
pub fn find_global() -> Result<PathBuf, Error>
Locate the path to the global configuration file
The user or global configuration file is usually located in
$HOME/.gitconfig
.
This method will try to guess the full path to that file, if the file exists. The returned path may be used on any method call to load the global configuration file.
This method will not guess the path to the XDG compatible config file
(.config/git/config
).
sourcepub fn find_system() -> Result<PathBuf, Error>
pub fn find_system() -> Result<PathBuf, Error>
Locate the path to the system configuration file
If /etc/gitconfig doesn’t exist, it will look for %PROGRAMFILES%
sourcepub fn find_xdg() -> Result<PathBuf, Error>
pub fn find_xdg() -> Result<PathBuf, Error>
Locate the path to the global XDG compatible configuration file
The XDG compatible configuration file is usually located in
$HOME/.config/git/config
.
sourcepub fn add_file(
&mut self,
path: &Path,
level: ConfigLevel,
force: bool,
) -> Result<(), Error>
pub fn add_file( &mut self, path: &Path, level: ConfigLevel, force: bool, ) -> Result<(), Error>
Add an on-disk config file instance to an existing config
The on-disk file pointed at by path will be opened and parsed; it’s expected to be a native Git config file following the default Git config syntax (see man git-config).
Further queries on this config object will access each of the config file instances in order (instances with a higher priority level will be accessed first).
sourcepub fn remove(&mut self, name: &str) -> Result<(), Error>
pub fn remove(&mut self, name: &str) -> Result<(), Error>
Delete a config variable from the config file with the highest level (usually the local one).
sourcepub fn remove_multivar(&mut self, name: &str, regexp: &str) -> Result<(), Error>
pub fn remove_multivar(&mut self, name: &str, regexp: &str) -> Result<(), Error>
Remove multivar config variables in the config file with the highest level (usually the local one).
The regular expression is applied case-sensitively on the value.
sourcepub fn get_bool(&self, name: &str) -> Result<bool, Error>
pub fn get_bool(&self, name: &str) -> Result<bool, Error>
Get the value of a boolean config variable.
All config files will be looked into, in the order of their defined level. A higher level means a higher priority. The first occurrence of the variable will be returned here.
sourcepub fn get_i32(&self, name: &str) -> Result<i32, Error>
pub fn get_i32(&self, name: &str) -> Result<i32, Error>
Get the value of an integer config variable.
All config files will be looked into, in the order of their defined level. A higher level means a higher priority. The first occurrence of the variable will be returned here.
sourcepub fn get_i64(&self, name: &str) -> Result<i64, Error>
pub fn get_i64(&self, name: &str) -> Result<i64, Error>
Get the value of an integer config variable.
All config files will be looked into, in the order of their defined level. A higher level means a higher priority. The first occurrence of the variable will be returned here.
sourcepub fn get_str(&self, name: &str) -> Result<&str, Error>
pub fn get_str(&self, name: &str) -> Result<&str, Error>
Get the value of a string config variable.
This is the same as get_bytes
except that it may return Err
if
the bytes are not valid utf-8.
This method will return an error if this Config
is not a snapshot.
sourcepub fn get_bytes(&self, name: &str) -> Result<&[u8], Error>
pub fn get_bytes(&self, name: &str) -> Result<&[u8], Error>
Get the value of a string config variable as a byte slice.
This method will return an error if this Config
is not a snapshot.
sourcepub fn get_string(&self, name: &str) -> Result<String, Error>
pub fn get_string(&self, name: &str) -> Result<String, Error>
Get the value of a string config variable as an owned string.
All config files will be looked into, in the order of their defined level. A higher level means a higher priority. The first occurrence of the variable will be returned here.
An error will be returned if the config value is not valid utf-8.
sourcepub fn get_path(&self, name: &str) -> Result<PathBuf, Error>
pub fn get_path(&self, name: &str) -> Result<PathBuf, Error>
Get the value of a path config variable as an owned PathBuf
.
A leading ‘~’ will be expanded to the global search path (which
defaults to the user’s home directory but can be overridden via
raw::git_libgit2_opts
.
All config files will be looked into, in the order of their defined level. A higher level means a higher priority. The first occurrence of the variable will be returned here.
sourcepub fn get_entry(&self, name: &str) -> Result<ConfigEntry<'_>, Error>
pub fn get_entry(&self, name: &str) -> Result<ConfigEntry<'_>, Error>
Get the ConfigEntry for a config variable.
sourcepub fn entries(&self, glob: Option<&str>) -> Result<ConfigEntries<'_>, Error>
pub fn entries(&self, glob: Option<&str>) -> Result<ConfigEntries<'_>, Error>
Iterate over all the config variables
If glob
is Some
, then the iterator will only iterate over all
variables whose name matches the pattern.
The regular expression is applied case-sensitively on the normalized form of the variable name: the section and variable parts are lower-cased. The subsection is left unchanged.
Due to lifetime restrictions, the returned value does not implement
the standard Iterator
trait. See ConfigEntries
for more.
§Example
use git2::Config;
let cfg = Config::new().unwrap();
let mut entries = cfg.entries(None).unwrap();
while let Some(entry) = entries.next() {
let entry = entry.unwrap();
println!("{} => {}", entry.name().unwrap(), entry.value().unwrap());
}
sourcepub fn multivar(
&self,
name: &str,
regexp: Option<&str>,
) -> Result<ConfigEntries<'_>, Error>
pub fn multivar( &self, name: &str, regexp: Option<&str>, ) -> Result<ConfigEntries<'_>, Error>
Iterate over the values of a multivar
If regexp
is Some
, then the iterator will only iterate over all
values which match the pattern.
The regular expression is applied case-sensitively on the normalized form of the variable name: the section and variable parts are lower-cased. The subsection is left unchanged.
Due to lifetime restrictions, the returned value does not implement
the standard Iterator
trait. See ConfigEntries
for more.
sourcepub fn open_global(&mut self) -> Result<Config, Error>
pub fn open_global(&mut self) -> Result<Config, Error>
Open the global/XDG configuration file according to git’s rules
Git allows you to store your global configuration at $HOME/.config
or
$XDG_CONFIG_HOME/git/config
. For backwards compatibility, the XDG file
shouldn’t be used unless the use has created it explicitly. With this
function you’ll open the correct one to write to.
sourcepub fn open_level(&self, level: ConfigLevel) -> Result<Config, Error>
pub fn open_level(&self, level: ConfigLevel) -> Result<Config, Error>
Build a single-level focused config object from a multi-level one.
The returned config object can be used to perform get/set/delete operations on a single specific level.
sourcepub fn set_bool(&mut self, name: &str, value: bool) -> Result<(), Error>
pub fn set_bool(&mut self, name: &str, value: bool) -> Result<(), Error>
Set the value of a boolean config variable in the config file with the highest level (usually the local one).
sourcepub fn set_i32(&mut self, name: &str, value: i32) -> Result<(), Error>
pub fn set_i32(&mut self, name: &str, value: i32) -> Result<(), Error>
Set the value of an integer config variable in the config file with the highest level (usually the local one).
sourcepub fn set_i64(&mut self, name: &str, value: i64) -> Result<(), Error>
pub fn set_i64(&mut self, name: &str, value: i64) -> Result<(), Error>
Set the value of an integer config variable in the config file with the highest level (usually the local one).
sourcepub fn set_multivar(
&mut self,
name: &str,
regexp: &str,
value: &str,
) -> Result<(), Error>
pub fn set_multivar( &mut self, name: &str, regexp: &str, value: &str, ) -> Result<(), Error>
Set the value of an multivar config variable in the config file with the highest level (usually the local one).
The regular expression is applied case-sensitively on the value.
sourcepub fn set_str(&mut self, name: &str, value: &str) -> Result<(), Error>
pub fn set_str(&mut self, name: &str, value: &str) -> Result<(), Error>
Set the value of a string config variable in the config file with the highest level (usually the local one).
sourcepub fn snapshot(&mut self) -> Result<Config, Error>
pub fn snapshot(&mut self) -> Result<Config, Error>
Create a snapshot of the configuration
Create a snapshot of the current state of a configuration, which allows you to look into a consistent view of the configuration for looking up complex values (e.g. a remote, submodule).
sourcepub fn parse_bool<S: IntoCString>(s: S) -> Result<bool, Error>
pub fn parse_bool<S: IntoCString>(s: S) -> Result<bool, Error>
Parse a string as a bool.
Interprets “true”, “yes”, “on”, 1, or any non-zero number as true. Interprets “false”, “no”, “off”, 0, or an empty string as false.