check all sections for lookup before failing

This commit is contained in:
Edward Shen 2021-03-01 23:59:38 -05:00
parent 0905642feb
commit 925a3b4afa
Signed by: edward
GPG key ID: 19182661E818369F
3 changed files with 98 additions and 72 deletions

View file

@ -245,8 +245,10 @@ impl<'event> GitConfig<'event> {
// Note: cannot wrap around the raw_multi_value method because we need
// to guarantee that the highest section id is used (so that we follow
// the "last one wins" resolution strategy by `git-config`).
let section_id = self.get_section_id_by_name_and_subname(section_name, subsection_name)?;
let section_ids =
self.get_section_ids_by_name_and_subname(section_name, subsection_name)?;
for section_id in section_ids.iter().rev() {
let mut found_key = false;
let mut latest_value = None;
let mut partial_value = None;
@ -273,9 +275,13 @@ impl<'event> GitConfig<'event> {
}
}
latest_value
.or_else(|| partial_value.map(normalize_vec))
.ok_or(GitConfigError::KeyDoesNotExist(key))
match latest_value.or_else(|| partial_value.map(normalize_vec)) {
Some(v) => return Ok(v),
None => (),
}
}
Err(GitConfigError::KeyDoesNotExist(key))
}
/// Returns a mutable reference to an uninterpreted value given a section,
@ -297,8 +303,10 @@ impl<'event> GitConfig<'event> {
// Note: cannot wrap around the raw_multi_value method because we need
// to guarantee that the highest section id is used (so that we follow
// the "last one wins" resolution strategy by `git-config`).
let section_id = self.get_section_id_by_name_and_subname(section_name, subsection_name)?;
let section_ids =
self.get_section_ids_by_name_and_subname(section_name, subsection_name)?;
for section_id in section_ids.iter().rev() {
let mut size = 0;
let mut index = 0;
let mut found_key = false;
@ -323,15 +331,18 @@ impl<'event> GitConfig<'event> {
}
if size == 0 {
return Err(GitConfigError::KeyDoesNotExist(key));
continue;
}
Ok(MutableValue {
return Ok(MutableValue {
section: self.sections.get_mut(&section_id).unwrap(),
key,
size,
index,
})
});
}
Err(GitConfigError::KeyDoesNotExist(key))
}
/// Returns all uninterpreted values given a section, an optional subsection
@ -387,7 +398,7 @@ impl<'event> GitConfig<'event> {
let mut partial_value = None;
// section_id is guaranteed to exist in self.sections, else we
// have a violated invariant.
for event in self.sections.get(section_id).unwrap() {
for event in self.sections.get(&section_id).unwrap() {
match event {
Event::Key(event_key) if *event_key == key => found_key = true,
Event::Value(v) if found_key => {
@ -708,24 +719,11 @@ impl<'event> GitConfig<'event> {
}
}
fn get_section_id_by_name_and_subname<'lookup>(
&'event self,
section_name: &'lookup str,
subsection_name: Option<&'lookup str>,
) -> Result<SectionId, GitConfigError<'lookup>> {
self.get_section_ids_by_name_and_subname(section_name, subsection_name)
.map(|vec| {
// get_section_ids_by_name_and_subname is guaranteed to return
// a non-empty vec, so max can never return empty.
*vec.iter().max().unwrap()
})
}
fn get_section_ids_by_name_and_subname<'lookup>(
&self,
section_name: &'lookup str,
subsection_name: Option<&'lookup str>,
) -> Result<&[SectionId], GitConfigError<'lookup>> {
) -> Result<Vec<SectionId>, GitConfigError<'lookup>> {
let section_ids = self
.section_lookup_tree
.get(section_name)
@ -750,7 +748,7 @@ impl<'event> GitConfig<'event> {
}
}
maybe_ids
.map(Vec::as_slice)
.map(Vec::to_owned)
.ok_or(GitConfigError::SubSectionDoesNotExist(subsection_name))
}
}

View file

@ -69,3 +69,31 @@ fn get_value_for_all_provided_values() -> Result<(), Box<dyn std::error::Error>>
Ok(())
}
/// There was a regression where lookup would fail because we only checked the
/// last section entry for any given section and subsection
#[test]
fn get_value_looks_up_all_sections_before_failing() -> Result<(), Box<dyn std::error::Error>> {
let config = r#"
[core]
bool-explicit = false
bool-implicit = false
[core]
bool-implicit
"#;
let file = GitConfig::try_from(config)?;
// Checks that we check the last entry first still
assert_eq!(
file.get_value::<Boolean>("core", None, "bool-implicit")?,
Boolean::True(TrueVariant::Implicit)
);
assert_eq!(
file.get_value::<Boolean>("core", None, "bool-explicit")?,
Boolean::False(Cow::Borrowed("false"))
);
Ok(())
}