pub struct Snapshot<'repo> { /* private fields */ }
Expand description

A platform to access configuration values as read from disk.

Note that these values won’t update even if the underlying file(s) change.

Implementations§

Access configuration values, frozen in time, using a key which is a . separated string of up to three tokens, namely section_name.[subsection_name.]value_name, like core.bare or remote.origin.url.

Note that single-value methods always return the last value found, which is the one set most recently in the hierarchy of configuration files, aka ‘last one wins’.

Return the boolean at key, or None if there is no such value or if the value can’t be interpreted as boolean.

For a non-degenerating version, use try_boolean(…).

Note that this method takes the most recent value at key even if it is from a file with reduced trust.

Like boolean(), but it will report an error if the value couldn’t be interpreted as boolean.

Return the resolved integer at key, or None if there is no such value or if the value can’t be interpreted as integer or exceeded the value range.

For a non-degenerating version, use try_integer(…).

Note that this method takes the most recent value at key even if it is from a file with reduced trust.

Like integer(), but it will report an error if the value couldn’t be interpreted as boolean.

Return the string at key, or None if there is no such value.

Note that this method takes the most recent value at key even if it is from a file with reduced trust.

Return the trusted and fully interpolated path at key, or None if there is no such value or if no value was found in a trusted file. An error occurs if the path could not be interpolated to its final value.

Utilities and additional access

Returns the underlying configuration implementation for a complete API, despite being a little less convenient.

It’s expected that more functionality will move up depending on demand.

Returns the configuration for all git-credential helpers from trusted configuration that apply to the given url along with an action preconfigured to invoke the cascade with. This includes url which may be altered to contain a user-name as configured.

These can be invoked to obtain credentials. Note that the url is expected to be the one used to connect to a remote, and thus should already have passed the url-rewrite engine.

Deviation
  • Invalid urls can’t be used to obtain credential helpers as they are rejected early when creating a valid url here.
  • Parsed urls will automatically drop the port if it’s the default, i.e. http://host:80 becomes http://host when parsed. This affects the prompt provided to the user, so that git will use the verbatim url, whereas we use http://host.
  • Upper-case scheme and host will be lower-cased automatically when parsing into a url, so prompts differ compared to git.
  • A difference in prompt might affect the matching of getting existing stored credentials, and it’s a question of this being a feature or a bug.

Methods from Deref<Target = File<'static>>§

Like value(), but returning None if the string wasn’t found.

As strings perform no conversions, this will never fail.

Like string(), but suitable for statically known keys like remote.origin.url.

Like string(), but the section containing the returned value must pass filter as well.

Like string_filter(), but suitable for statically known keys like remote.origin.url.

Like value(), but returning None if the path wasn’t found.

Note that this path is not vetted and should only point to resources which can’t be used to pose a security risk. Prefer using path_filter() instead.

As paths perform no conversions, this will never fail.

Like path(), but suitable for statically known keys like remote.origin.url.

Like path(), but the section containing the returned value must pass filter as well.

This should be the preferred way of accessing paths as those from untrusted locations can be

As paths perform no conversions, this will never fail.

Like path_filter(), but suitable for statically known keys like remote.origin.url.

Like value(), but returning None if the boolean value wasn’t found.

Like boolean(), but suitable for statically known keys like remote.origin.url.

Like boolean(), but the section containing the returned value must pass filter as well.

Like boolean_filter(), but suitable for statically known keys like remote.origin.url.

Like value(), but returning an Option if the integer wasn’t found.

Like integer(), but suitable for statically known keys like remote.origin.url.

Like integer(), but the section containing the returned value must pass filter as well.

Like integer_filter(), but suitable for statically known keys like remote.origin.url.

Similar to values(…) but returning strings if at least one of them was found.

Like strings(), but suitable for statically known keys like remote.origin.url.

Similar to strings(…), but all values are in sections that passed filter.

Like strings_filter(), but suitable for statically known keys like remote.origin.url.

Similar to values(…) but returning integers if at least one of them was found and if none of them overflows.

Like integers(), but suitable for statically known keys like remote.origin.url.

Similar to integers(…) but all integers are in sections that passed filter and that are not overflowing.

Like integers_filter(), but suitable for statically known keys like remote.origin.url.

Returns an uninterpreted value given a section, an optional subsection and key.

Consider Self::raw_values() if you want to get all values of a multivar instead.

Returns an uninterpreted value given a section, an optional subsection and key, if it passes the filter.

Consider Self::raw_values() if you want to get all values of a multivar instead.

Returns all uninterpreted values given a section, an optional subsection ain order of occurrence.

The ordering means that the last of the returned values is the one that would be the value used in the single-value case.nd key.

Examples

If you have the following config:

[core]
    a = b
[core]
    a = c
    a = d

Attempting to get all values of a yields the following:

assert_eq!(
    git_config.raw_values("core", None, "a").unwrap(),
    vec![
        Cow::<BStr>::Borrowed("b".into()),
        Cow::<BStr>::Borrowed("c".into()),
        Cow::<BStr>::Borrowed("d".into()),
    ],
);

Consider Self::raw_value if you want to get the resolved single value for a given key, if your key does not support multi-valued values.

Returns all uninterpreted values given a section, an optional subsection and key, if the value passes filter, in order of occurrence.

The ordering means that the last of the returned values is the one that would be the value used in the single-value case.

Returns an interpreted value given a section, an optional subsection and key.

It’s recommended to use one of the value types provide dby this crate as they implement the conversion, but this function is flexible and will accept any type that implements TryFrom<&BStr>.

Consider Self::values if you want to get all values of a multivar instead.

If a string is desired, use the string() method instead.

Examples
let config = r#"
    [core]
        a = 10k
        c = false
"#;
let git_config = git_config::File::try_from(config)?;
// You can either use the turbofish to determine the type...
let a_value = git_config.value::<Integer>("core", None, "a")?;
// ... or explicitly declare the type to avoid the turbofish
let c_value: Boolean = git_config.value("core", None, "c")?;

Like value(), but returning an None if the value wasn’t found at section[.subsection].key

Returns all interpreted values given a section, an optional subsection and key.

It’s recommended to use one of the value types provide dby this crate as they implement the conversion, but this function is flexible and will accept any type that implements TryFrom<&BStr>.

Consider Self::value if you want to get a single value (following last-one-wins resolution) instead.

To access plain strings, use the strings() method instead.

Examples
let config = r#"
    [core]
        a = true
        c
    [core]
        a
        a = false
"#;
let git_config = git_config::File::try_from(config).unwrap();
// You can either use the turbofish to determine the type...
let a_value = git_config.values::<Boolean>("core", None, "a")?;
assert_eq!(
    a_value,
    vec![
        Boolean(true),
        Boolean(false),
        Boolean(false),
    ]
);
// ... or explicitly declare the type to avoid the turbofish
let c_value: Vec<Boolean> = git_config.values("core", None, "c").unwrap();
assert_eq!(c_value, vec![Boolean(false)]);

Returns the last found immutable section with a given name and optional subsection_name.

Returns the last found immutable section with a given key, identifying the name and subsection name like core or remote.origin.

Returns the last found immutable section with a given name and optional subsection_name, that matches filter.

If there are sections matching section_name and subsection_name but the filter rejects all of them, Ok(None) is returned.

Like section_filter(), but identifies the section with key like core or remote.origin.

Gets all sections that match the provided name, ignoring any subsections.

Examples

Provided the following config:

[core]
    a = b
[core ""]
    c = d
[core "apple"]
    e = f

Calling this method will yield all sections:

let config = r#"
    [core]
        a = b
    [core ""]
        c = d
    [core "apple"]
        e = f
"#;
let git_config = git_config::File::try_from(config)?;
assert_eq!(git_config.sections_by_name("core").map_or(0, |s|s.count()), 3);

Similar to sections_by_name(), but returns an identifier for this section as well to allow referring to it unambiguously even in the light of deletions.

Gets all sections that match the provided name, ignoring any subsections, and pass the filter.

Returns the number of values in the config, no matter in which section.

For example, a config with multiple empty sections will return 0. This ignores any comments.

Returns if there are no entries in the config. This will return true if there are only empty sections, with whitespace and comments not being considered void.

Return the file’s metadata to guide filtering of all values upon retrieval.

This is the metadata the file was instantiated with for use in all newly created sections.

Similar to meta(), but with shared ownership.

Return an iterator over all sections, in order of occurrence in the file itself.

Return an iterator over all sections and their ids, in order of occurrence in the file itself.

Return an iterator over all sections along with non-section events that are placed right after them, in order of occurrence in the file itself.

This allows to reproduce the look of sections perfectly when serializing them with write_to().

Return all events which are in front of the first of our sections, or None if there are none.

Return the newline characters that have been detected in this config file or the default ones for the current platform.

Note that the first found newline is the one we use in the assumption of consistency.

Serialize this type into a BString for convenience.

Note that to_string() can also be used, but might not be lossless.

Stream ourselves to the given out, in order to reproduce this file mostly losslessly as it was parsed.

Trait Implementations§

Formats the value using the given formatter. Read more
The resulting type after dereferencing.
Dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.