Skip to content

Commit e0fdad2

Browse files
committed
WIP
1 parent ed1c957 commit e0fdad2

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

git-config/src/file/git_config.rs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,29 +153,38 @@ impl<'event> GitConfig<'event> {
153153
.iter()
154154
.map(|p| PathBuf::from(p.to_str().expect("invalid utf-8 path")))
155155
.collect();
156+
const MAX_DEPTH: u8 = 10;
156157

157158
fn from_paths_req(
158159
paths: Vec<PathBuf>,
159160
seen: &mut HashSet<PathBuf>,
160161
config: &mut GitConfig,
162+
depth: u8,
161163
) -> Result<(), ParserOrIoError<'static>> {
164+
if depth >= MAX_DEPTH {
165+
return Ok(());
166+
}
162167
for path in paths.iter() {
163168
let other = GitConfig::open(path)?;
164-
for (section_id, section_header) in other.section_headers {
169+
let mut section_ids: Vec<_> = other.section_headers.keys().collect();
170+
section_ids.sort();
171+
for section_id in section_ids {
172+
let section_header = &other.section_headers[&section_id];
165173
if section_header.name.0 == "include" {
166174
let path_values = other.sections[&section_id].values(&Key::from("path"));
167175
let mut paths = vec![];
168-
// TODO handle relative and interpolated path
169-
for path in path_values {
170-
let path = PathBuf::from(
171-
std::string::String::from_utf8(path.to_vec()).expect("invalid utf-8 path"),
172-
);
173-
if !seen.contains(&path) {
174-
seen.insert(path.clone());
175-
paths.push(path.clone());
176+
for path_value in path_values {
177+
let interpolated_path = crate::values::Path::from(path_value).interpolate(None).unwrap();
178+
let path = if interpolated_path.is_relative() {
179+
path.parent().unwrap().join(interpolated_path)
180+
} else {
181+
interpolated_path.into()
182+
};
183+
if seen.insert(path.clone()) {
184+
paths.push(path);
176185
}
177186
}
178-
from_paths_req(paths, seen, config)?;
187+
from_paths_req(paths, seen, config, depth + 1)?;
179188
} else {
180189
config.push_section(
181190
section_header.name.0.to_owned(),
@@ -188,7 +197,7 @@ impl<'event> GitConfig<'event> {
188197
Ok(())
189198
}
190199

191-
from_paths_req(paths, &mut seen, &mut config)?;
200+
from_paths_req(paths, &mut seen, &mut config, 0)?;
192201

193202
Ok(config)
194203
}
@@ -1714,11 +1723,14 @@ mod from_paths {
17141723
r"
17151724
[core]
17161725
a = false
1726+
sslVerify = true
1727+
d = 41
17171728
",
17181729
)
17191730
.expect("Unable to write config file");
17201731

17211732
let b_path = dir.path().join("b");
1733+
let relative_b_path = std::path::PathBuf::from("b");
17221734
fs::write(
17231735
b_path.as_path(),
17241736
r"
@@ -1735,12 +1747,15 @@ mod from_paths {
17351747
r"
17361748
[core]
17371749
c = 12
1750+
d = 42
17381751
[include]
17391752
path = {}
17401753
path = {}
1754+
[http]
1755+
sslVerify = false
17411756
",
17421757
a_path.as_path().to_str().unwrap(),
1743-
b_path.as_path().to_str().unwrap()
1758+
relative_b_path.as_path().to_str().unwrap()
17441759
),
17451760
)
17461761
.expect("Unable to write config file");
@@ -1751,6 +1766,14 @@ mod from_paths {
17511766
config.get_raw_value("core", None, "c"),
17521767
Ok(Cow::<[u8]>::Borrowed(b"12"))
17531768
);
1769+
assert_eq!(
1770+
config.get_raw_value("core", None, "d"),
1771+
Ok(Cow::<[u8]>::Borrowed(b"41"))
1772+
);
1773+
assert_eq!(
1774+
config.get_raw_value("http", None, "sslVerify"),
1775+
Ok(Cow::<[u8]>::Borrowed(b"false"))
1776+
);
17541777

17551778
assert_eq!(
17561779
config.get_raw_value("diff", None, "renames"),
@@ -1770,11 +1793,11 @@ mod from_paths {
17701793
let a_path = dir.path().join("a");
17711794
fs::write(
17721795
a_path.as_path(),
1773-
r"
1796+
r#"
17741797
[core]
17751798
a = false
17761799
c = 1
1777-
",
1800+
"#,
17781801
)
17791802
.expect("Unable to write config file");
17801803

0 commit comments

Comments
 (0)