Skip to content

Commit

Permalink
Merge pull request #1639 from cruessler/respect-env-variables
Browse files Browse the repository at this point in the history
Respect env variables when running gix
  • Loading branch information
Byron authored Oct 21, 2024
2 parents bcdce6e + e9b3db8 commit 48aa74b
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 96 deletions.
2 changes: 1 addition & 1 deletion gix-config/fuzz/fuzz_targets/fuzz_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn fuzz_mutable_section(
subsection_name,
Cow::Owned(renamed_section_name.clone()),
renamed_subsection_name.clone(),
&mut |_| false,
|_| false,
));

Ok(())
Expand Down
63 changes: 32 additions & 31 deletions gix-config/src/file/access/comfort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use std::borrow::Cow;

use bstr::BStr;

use crate::{file::MetadataFilter, value, AsKey, File};
use crate::file::Metadata;
use crate::{value, AsKey, File};

/// Comfortable API for accessing values
impl File<'_> {
/// Like [`string_by()`](File::string_by()), but suitable for statically known `key`s like `remote.origin.url`.
pub fn string(&self, key: impl AsKey) -> Option<Cow<'_, BStr>> {
self.string_filter(key, &mut |_| true)
self.string_filter(key, |_| true)
}

/// Like [`value()`](File::value()), but returning `None` if the string wasn't found.
Expand All @@ -20,13 +21,11 @@ impl File<'_> {
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
) -> Option<Cow<'_, BStr>> {
self.string_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), &mut |_| {
true
})
self.string_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), |_| true)
}

/// Like [`string_filter_by()`](File::string_filter_by()), but suitable for statically known `key`s like `remote.origin.url`.
pub fn string_filter(&self, key: impl AsKey, filter: &mut MetadataFilter) -> Option<Cow<'_, BStr>> {
pub fn string_filter(&self, key: impl AsKey, filter: impl FnMut(&Metadata) -> bool) -> Option<Cow<'_, BStr>> {
let key = key.try_as_key()?;
self.raw_value_filter_by(key.section_name, key.subsection_name, key.value_name, filter)
.ok()
Expand All @@ -38,15 +37,15 @@ impl File<'_> {
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
filter: &mut MetadataFilter,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Cow<'_, BStr>> {
self.raw_value_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), filter)
.ok()
}

/// Like [`path_by()`](File::path_by()), but suitable for statically known `key`s like `remote.origin.url`.
pub fn path(&self, key: impl AsKey) -> Option<crate::Path<'_>> {
self.path_filter(key, &mut |_| true)
self.path_filter(key, |_| true)
}

/// Like [`value()`](File::value()), but returning `None` if the path wasn't found.
Expand All @@ -61,13 +60,11 @@ impl File<'_> {
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
) -> Option<crate::Path<'_>> {
self.path_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), &mut |_| {
true
})
self.path_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), |_| true)
}

/// Like [`path_filter_by()`](File::path_filter_by()), but suitable for statically known `key`s like `remote.origin.url`.
pub fn path_filter(&self, key: impl AsKey, filter: &mut MetadataFilter) -> Option<crate::Path<'_>> {
pub fn path_filter(&self, key: impl AsKey, filter: impl FnMut(&Metadata) -> bool) -> Option<crate::Path<'_>> {
let key = key.try_as_key()?;
self.path_filter_by(key.section_name, key.subsection_name, key.value_name, filter)
}
Expand All @@ -83,7 +80,7 @@ impl File<'_> {
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
filter: &mut MetadataFilter,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<crate::Path<'_>> {
self.raw_value_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), filter)
.ok()
Expand All @@ -92,7 +89,7 @@ impl File<'_> {

/// Like [`boolean_by()`](File::boolean_by()), but suitable for statically known `key`s like `remote.origin.url`.
pub fn boolean(&self, key: impl AsKey) -> Option<Result<bool, value::Error>> {
self.boolean_filter(key, &mut |_| true)
self.boolean_filter(key, |_| true)
}

/// Like [`value()`](File::value()), but returning `None` if the boolean value wasn't found.
Expand All @@ -102,13 +99,15 @@ impl File<'_> {
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
) -> Option<Result<bool, value::Error>> {
self.boolean_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), &mut |_| {
true
})
self.boolean_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), |_| true)
}

/// Like [`boolean_filter_by()`](File::boolean_filter_by()), but suitable for statically known `key`s like `remote.origin.url`.
pub fn boolean_filter(&self, key: impl AsKey, filter: &mut MetadataFilter) -> Option<Result<bool, value::Error>> {
pub fn boolean_filter(
&self,
key: impl AsKey,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Result<bool, value::Error>> {
let key = key.try_as_key()?;
self.boolean_filter_by(key.section_name, key.subsection_name, key.value_name, filter)
}
Expand All @@ -119,7 +118,7 @@ impl File<'_> {
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
filter: &mut MetadataFilter,
mut filter: impl FnMut(&Metadata) -> bool,
) -> Option<Result<bool, value::Error>> {
let section_name = section_name.as_ref();
let section_ids = self
Expand All @@ -142,7 +141,7 @@ impl File<'_> {

/// Like [`integer_by()`](File::integer_by()), but suitable for statically known `key`s like `remote.origin.url`.
pub fn integer(&self, key: impl AsKey) -> Option<Result<i64, value::Error>> {
self.integer_filter(key, &mut |_| true)
self.integer_filter(key, |_| true)
}

/// Like [`value()`](File::value()), but returning an `Option` if the integer wasn't found.
Expand All @@ -152,11 +151,15 @@ impl File<'_> {
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
) -> Option<Result<i64, value::Error>> {
self.integer_filter_by(section_name, subsection_name, value_name, &mut |_| true)
self.integer_filter_by(section_name, subsection_name, value_name, |_| true)
}

/// Like [`integer_filter_by()`](File::integer_filter_by()), but suitable for statically known `key`s like `remote.origin.url`.
pub fn integer_filter(&self, key: impl AsKey, filter: &mut MetadataFilter) -> Option<Result<i64, value::Error>> {
pub fn integer_filter(
&self,
key: impl AsKey,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Result<i64, value::Error>> {
let key = key.try_as_key()?;
self.integer_filter_by(key.section_name, key.subsection_name, key.value_name, filter)
}
Expand All @@ -167,7 +170,7 @@ impl File<'_> {
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
filter: &mut MetadataFilter,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Result<i64, value::Error>> {
let int = self
.raw_value_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), filter)
Expand Down Expand Up @@ -196,7 +199,7 @@ impl File<'_> {
}

/// Like [`strings_filter_by()`](File::strings_filter_by()), but suitable for statically known `key`s like `remote.origin.url`.
pub fn strings_filter(&self, key: impl AsKey, filter: &mut MetadataFilter) -> Option<Vec<Cow<'_, BStr>>> {
pub fn strings_filter(&self, key: impl AsKey, filter: impl FnMut(&Metadata) -> bool) -> Option<Vec<Cow<'_, BStr>>> {
let key = key.try_as_key()?;
self.strings_filter_by(key.section_name, key.subsection_name, key.value_name, filter)
}
Expand All @@ -207,15 +210,15 @@ impl File<'_> {
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
filter: &mut MetadataFilter,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Vec<Cow<'_, BStr>>> {
self.raw_values_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), filter)
.ok()
}

/// Like [`integers()`](File::integers()), but suitable for statically known `key`s like `remote.origin.url`.
pub fn integers(&self, key: impl AsKey) -> Option<Result<Vec<i64>, value::Error>> {
self.integers_filter(key, &mut |_| true)
self.integers_filter(key, |_| true)
}

/// Similar to [`values_by(…)`](File::values_by()) but returning integers if at least one of them was found
Expand All @@ -226,16 +229,14 @@ impl File<'_> {
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
) -> Option<Result<Vec<i64>, value::Error>> {
self.integers_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), &mut |_| {
true
})
self.integers_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), |_| true)
}

/// Like [`integers_filter_by()`](File::integers_filter_by()), but suitable for statically known `key`s like `remote.origin.url`.
pub fn integers_filter(
&self,
key: impl AsKey,
filter: &mut MetadataFilter,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Result<Vec<i64>, value::Error>> {
let key = key.try_as_key()?;
self.integers_filter_by(key.section_name, key.subsection_name, key.value_name, filter)
Expand All @@ -248,7 +249,7 @@ impl File<'_> {
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
filter: &mut MetadataFilter,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Result<Vec<i64>, value::Error>> {
self.raw_values_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), filter)
.ok()
Expand Down
21 changes: 11 additions & 10 deletions gix-config/src/file/access/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::borrow::Cow;
use bstr::BStr;
use gix_features::threading::OwnShared;

use crate::file::Metadata;
use crate::{
file::{self, rename_section, write::ends_with_newline, MetadataFilter, SectionBodyIdsLut, SectionId, SectionMut},
file::{self, rename_section, write::ends_with_newline, SectionBodyIdsLut, SectionId, SectionMut},
lookup,
parse::{section, Event, FrontMatterEvents},
File,
Expand Down Expand Up @@ -61,7 +62,7 @@ impl<'event> File<'event> {
name: impl AsRef<str>,
subsection_name: Option<&BStr>,
) -> Result<SectionMut<'a, 'event>, section::header::Error> {
self.section_mut_or_create_new_filter(name, subsection_name, &mut |_| true)
self.section_mut_or_create_new_filter(name, subsection_name, |_| true)
}

/// Returns an mutable section with a given `name` and optional `subsection_name`, _if it exists_ **and** passes `filter`, or create
Expand All @@ -70,7 +71,7 @@ impl<'event> File<'event> {
&'a mut self,
name: impl AsRef<str>,
subsection_name: Option<&BStr>,
filter: &mut MetadataFilter,
filter: impl FnMut(&Metadata) -> bool,
) -> Result<SectionMut<'a, 'event>, section::header::Error> {
self.section_mut_or_create_new_filter_inner(name.as_ref(), subsection_name, filter)
}
Expand All @@ -79,7 +80,7 @@ impl<'event> File<'event> {
&'a mut self,
name: &str,
subsection_name: Option<&BStr>,
filter: &mut MetadataFilter,
mut filter: impl FnMut(&Metadata) -> bool,
) -> Result<SectionMut<'a, 'event>, section::header::Error> {
match self
.section_ids_by_name_and_subname(name.as_ref(), subsection_name)
Expand Down Expand Up @@ -110,7 +111,7 @@ impl<'event> File<'event> {
&'a mut self,
name: impl AsRef<str>,
subsection_name: Option<&BStr>,
filter: &mut MetadataFilter,
filter: impl FnMut(&Metadata) -> bool,
) -> Result<Option<file::SectionMut<'a, 'event>>, lookup::existing::Error> {
self.section_mut_filter_inner(name.as_ref(), subsection_name, filter)
}
Expand All @@ -119,7 +120,7 @@ impl<'event> File<'event> {
&'a mut self,
name: &str,
subsection_name: Option<&BStr>,
filter: &mut MetadataFilter,
mut filter: impl FnMut(&Metadata) -> bool,
) -> Result<Option<file::SectionMut<'a, 'event>>, lookup::existing::Error> {
let id = self
.section_ids_by_name_and_subname(name, subsection_name)?
Expand All @@ -137,7 +138,7 @@ impl<'event> File<'event> {
pub fn section_mut_filter_by_key<'a, 'b>(
&'a mut self,
key: impl Into<&'b BStr>,
filter: &mut MetadataFilter,
filter: impl FnMut(&Metadata) -> bool,
) -> Result<Option<file::SectionMut<'a, 'event>>, lookup::existing::Error> {
let key = section::unvalidated::Key::parse(key).ok_or(lookup::existing::Error::KeyMissing)?;
self.section_mut_filter(key.section_name, key.subsection_name, filter)
Expand Down Expand Up @@ -289,7 +290,7 @@ impl<'event> File<'event> {
&mut self,
name: impl AsRef<str>,
subsection_name: impl Into<Option<&'a BStr>>,
filter: &mut MetadataFilter,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<file::Section<'event>> {
self.remove_section_filter_inner(name.as_ref(), subsection_name.into(), filter)
}
Expand All @@ -298,7 +299,7 @@ impl<'event> File<'event> {
&mut self,
name: &str,
subsection_name: Option<&BStr>,
filter: &mut MetadataFilter,
mut filter: impl FnMut(&Metadata) -> bool,
) -> Option<file::Section<'event>> {
let id = self
.section_ids_by_name_and_subname(name, subsection_name)
Expand Down Expand Up @@ -352,7 +353,7 @@ impl<'event> File<'event> {
subsection_name: impl Into<Option<&'a BStr>>,
new_name: impl Into<Cow<'event, str>>,
new_subsection_name: impl Into<Option<Cow<'event, BStr>>>,
filter: &mut MetadataFilter,
mut filter: impl FnMut(&Metadata) -> bool,
) -> Result<(), rename_section::Error> {
let id = self
.section_ids_by_name_and_subname(name.as_ref(), subsection_name.into())?
Expand Down
Loading

0 comments on commit 48aa74b

Please sign in to comment.