Skip to content

Commit fd1fb54

Browse files
committed
add from_paths::Options::default(); minor refactor (GitoxideLabs#331)
1 parent 9239bb2 commit fd1fb54

File tree

1 file changed

+34
-46
lines changed

1 file changed

+34
-46
lines changed

git-config/src/file/git_config.rs

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,11 @@ pub mod from_paths {
123123
use crate::parser;
124124
use crate::values::path::interpolate;
125125
use quick_error::quick_error;
126+
use std::borrow::Cow;
126127

127128
quick_error! {
128129
#[derive(Debug)]
129-
/// The error returned by [`GitConfig::from_paths()` and `GitConfig::from_env_paths()`].
130+
/// The error returned by [`GitConfig::from_paths()`][super::GitConfig::from_paths()] and [`GitConfig::from_env_paths()`][super::GitConfig::from_env_paths()].
130131
#[allow(missing_docs)]
131132
pub enum Error {
132133
ParserOrIoError(err: parser::ParserOrIoError<'static>) {
@@ -142,13 +143,23 @@ pub mod from_paths {
142143
}
143144
}
144145

145-
/// Options when loading git config
146+
/// Options when loading git config using [`GitConfig::from_paths()`][super::GitConfig::from_paths()].
147+
#[derive(Clone)]
146148
pub struct Options<'a> {
147-
/// the location where gitoxide is installed
148-
pub git_install_dir: Option<&'a std::path::Path>,
149-
/// max_depth of nested includes to follow
149+
/// the location where gitoxide or git is installed
150+
pub git_install_dir: Option<Cow<'a, std::path::Path>>,
151+
/// The maximum length of the file include chain built by following nested includes.
150152
pub max_depth: u8,
151153
}
154+
155+
impl<'a> Default for Options<'a> {
156+
fn default() -> Self {
157+
Options {
158+
git_install_dir: None,
159+
max_depth: 10,
160+
}
161+
}
162+
}
152163
}
153164

154165
impl<'event> GitConfig<'event> {
@@ -205,7 +216,7 @@ impl<'event> GitConfig<'event> {
205216
let mut paths = vec![];
206217
for path_value in path_values {
207218
let interpolated_path =
208-
values::Path::from(path_value).interpolate(options.git_install_dir)?;
219+
values::Path::from(path_value).interpolate(options.git_install_dir.as_deref())?;
209220
let path = if interpolated_path.is_relative() {
210221
config_file_path
211222
.parent()
@@ -1736,11 +1747,7 @@ mod from_paths_tests {
17361747
let config_path = dir.path().join("config");
17371748

17381749
let paths = vec![config_path];
1739-
let options = from_paths::Options {
1740-
git_install_dir: None,
1741-
max_depth: 10,
1742-
};
1743-
let error = GitConfig::from_paths(paths, &options).unwrap_err();
1750+
let error = GitConfig::from_paths(paths, &Default::default()).unwrap_err();
17441751
assert!(
17451752
matches!(error, Error::ParserOrIoError(ParserOrIoError::Io(io_error)) if io_error.kind() == io::ErrorKind::NotFound)
17461753
);
@@ -1753,11 +1760,7 @@ mod from_paths_tests {
17531760
fs::write(config_path.as_path(), b"[core]\nboolean = true").expect("Unable to write config file");
17541761

17551762
let paths = vec![config_path];
1756-
let options = from_paths::Options {
1757-
git_install_dir: None,
1758-
max_depth: 10,
1759-
};
1760-
let config = GitConfig::from_paths(paths, &options).unwrap();
1763+
let config = GitConfig::from_paths(paths, &Default::default()).unwrap();
17611764

17621765
assert_eq!(
17631766
config.get_raw_value("core", None, "boolean"),
@@ -1816,11 +1819,7 @@ mod from_paths_tests {
18161819
)
18171820
.unwrap();
18181821

1819-
let options = from_paths::Options {
1820-
git_install_dir: None,
1821-
max_depth: 10,
1822-
};
1823-
let config = GitConfig::from_paths(vec![c_path], &options).unwrap();
1822+
let config = GitConfig::from_paths(vec![c_path], &Default::default()).unwrap();
18241823

18251824
assert_eq!(
18261825
config.get_raw_value("core", None, "c"),
@@ -1847,8 +1846,13 @@ mod from_paths_tests {
18471846
}
18481847

18491848
#[test]
1850-
fn nested_include_has_max_depth_10() {
1849+
fn nested_include_respects_max_depth() {
18511850
let dir = tempdir().unwrap();
1851+
let max_depth = 3;
1852+
let options = from_paths::Options {
1853+
git_install_dir: None,
1854+
max_depth,
1855+
};
18521856
let path = dir.path().join("0");
18531857
fs::write(
18541858
path.as_path(),
@@ -1863,9 +1867,9 @@ mod from_paths_tests {
18631867
)
18641868
.unwrap();
18651869

1866-
for i in 1..11 {
1867-
let path = dir.path().join(&i.to_string());
1868-
let prev_path = dir.path().join(&(i - 1).to_string());
1870+
for (i, prev_i) in (1..=max_depth).zip(0..max_depth) {
1871+
let path = dir.path().join(i.to_string());
1872+
let prev_path = dir.path().join(prev_i.to_string());
18691873
fs::write(
18701874
path.as_path(),
18711875
format!(
@@ -1880,17 +1884,13 @@ mod from_paths_tests {
18801884
.unwrap();
18811885
}
18821886

1883-
let options = from_paths::Options {
1884-
git_install_dir: None,
1885-
max_depth: 10,
1886-
};
1887-
let config = GitConfig::from_paths(vec![dir.path().join("9")], &options).unwrap();
1887+
let config = GitConfig::from_paths(vec![dir.path().join((max_depth - 1).to_string())], &options).unwrap();
18881888
assert_eq!(
18891889
config.get_raw_value("core", None, "i"),
18901890
Ok(Cow::<[u8]>::Borrowed(b"true"))
18911891
);
18921892

1893-
let config = GitConfig::from_paths(vec![dir.path().join("10")], &options).unwrap();
1893+
let config = GitConfig::from_paths(vec![dir.path().join(max_depth.to_string())], &options).unwrap();
18941894
assert_eq!(
18951895
config.get_raw_value("core", None, "i"),
18961896
Ok(Cow::<[u8]>::Borrowed(b"false"))
@@ -1939,11 +1939,7 @@ mod from_paths_tests {
19391939
)
19401940
.unwrap();
19411941

1942-
let options = from_paths::Options {
1943-
git_install_dir: None,
1944-
max_depth: 10,
1945-
};
1946-
let config = GitConfig::from_paths(vec![c_path], &options).unwrap();
1942+
let config = GitConfig::from_paths(vec![c_path], &Default::default()).unwrap();
19471943

19481944
assert_eq!(config.get_raw_value("core", None, "c"), Ok(Cow::<[u8]>::Borrowed(b"1")));
19491945

@@ -1975,11 +1971,7 @@ mod from_paths_tests {
19751971
fs::write(d_path.as_path(), b"[core]\na = false").expect("Unable to write config file");
19761972

19771973
let paths = vec![a_path, b_path, c_path, d_path];
1978-
let options = from_paths::Options {
1979-
git_install_dir: None,
1980-
max_depth: 10,
1981-
};
1982-
let config = GitConfig::from_paths(paths, &options).unwrap();
1974+
let config = GitConfig::from_paths(paths, &Default::default()).unwrap();
19831975

19841976
assert_eq!(
19851977
config.get_raw_value("core", None, "a"),
@@ -2013,11 +2005,7 @@ mod from_paths_tests {
20132005
fs::write(c_path.as_path(), b"[core]\nkey = c").expect("Unable to write config file");
20142006

20152007
let paths = vec![a_path, b_path, c_path];
2016-
let options = from_paths::Options {
2017-
git_install_dir: None,
2018-
max_depth: 10,
2019-
};
2020-
let config = GitConfig::from_paths(paths, &options).unwrap();
2008+
let config = GitConfig::from_paths(paths, &Default::default()).unwrap();
20212009

20222010
assert_eq!(
20232011
config.get_raw_multi_value("core", None, "key").unwrap(),

0 commit comments

Comments
 (0)