@@ -123,10 +123,11 @@ pub mod from_paths {
123
123
use crate :: parser;
124
124
use crate :: values:: path:: interpolate;
125
125
use quick_error:: quick_error;
126
+ use std:: borrow:: Cow ;
126
127
127
128
quick_error ! {
128
129
#[ 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() ].
130
131
#[ allow( missing_docs) ]
131
132
pub enum Error {
132
133
ParserOrIoError ( err: parser:: ParserOrIoError <' static >) {
@@ -142,13 +143,23 @@ pub mod from_paths {
142
143
}
143
144
}
144
145
145
- /// Options when loading git config
146
+ /// Options when loading git config using [`GitConfig::from_paths()`][super::GitConfig::from_paths()].
147
+ #[ derive( Clone ) ]
146
148
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.
150
152
pub max_depth : u8 ,
151
153
}
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
+ }
152
163
}
153
164
154
165
impl < ' event > GitConfig < ' event > {
@@ -205,7 +216,7 @@ impl<'event> GitConfig<'event> {
205
216
let mut paths = vec ! [ ] ;
206
217
for path_value in path_values {
207
218
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 ( ) ) ?;
209
220
let path = if interpolated_path. is_relative ( ) {
210
221
config_file_path
211
222
. parent ( )
@@ -1736,11 +1747,7 @@ mod from_paths_tests {
1736
1747
let config_path = dir. path ( ) . join ( "config" ) ;
1737
1748
1738
1749
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 ( ) ;
1744
1751
assert ! (
1745
1752
matches!( error, Error :: ParserOrIoError ( ParserOrIoError :: Io ( io_error) ) if io_error. kind( ) == io:: ErrorKind :: NotFound )
1746
1753
) ;
@@ -1753,11 +1760,7 @@ mod from_paths_tests {
1753
1760
fs:: write ( config_path. as_path ( ) , b"[core]\n boolean = true" ) . expect ( "Unable to write config file" ) ;
1754
1761
1755
1762
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 ( ) ;
1761
1764
1762
1765
assert_eq ! (
1763
1766
config. get_raw_value( "core" , None , "boolean" ) ,
@@ -1816,11 +1819,7 @@ mod from_paths_tests {
1816
1819
)
1817
1820
. unwrap ( ) ;
1818
1821
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 ( ) ;
1824
1823
1825
1824
assert_eq ! (
1826
1825
config. get_raw_value( "core" , None , "c" ) ,
@@ -1847,8 +1846,13 @@ mod from_paths_tests {
1847
1846
}
1848
1847
1849
1848
#[ test]
1850
- fn nested_include_has_max_depth_10 ( ) {
1849
+ fn nested_include_respects_max_depth ( ) {
1851
1850
let dir = tempdir ( ) . unwrap ( ) ;
1851
+ let max_depth = 3 ;
1852
+ let options = from_paths:: Options {
1853
+ git_install_dir : None ,
1854
+ max_depth,
1855
+ } ;
1852
1856
let path = dir. path ( ) . join ( "0" ) ;
1853
1857
fs:: write (
1854
1858
path. as_path ( ) ,
@@ -1863,9 +1867,9 @@ mod from_paths_tests {
1863
1867
)
1864
1868
. unwrap ( ) ;
1865
1869
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 ( ) ) ;
1869
1873
fs:: write (
1870
1874
path. as_path ( ) ,
1871
1875
format ! (
@@ -1880,17 +1884,13 @@ mod from_paths_tests {
1880
1884
. unwrap ( ) ;
1881
1885
}
1882
1886
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 ( ) ;
1888
1888
assert_eq ! (
1889
1889
config. get_raw_value( "core" , None , "i" ) ,
1890
1890
Ok ( Cow :: <[ u8 ] >:: Borrowed ( b"true" ) )
1891
1891
) ;
1892
1892
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 ( ) ;
1894
1894
assert_eq ! (
1895
1895
config. get_raw_value( "core" , None , "i" ) ,
1896
1896
Ok ( Cow :: <[ u8 ] >:: Borrowed ( b"false" ) )
@@ -1939,11 +1939,7 @@ mod from_paths_tests {
1939
1939
)
1940
1940
. unwrap ( ) ;
1941
1941
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 ( ) ;
1947
1943
1948
1944
assert_eq ! ( config. get_raw_value( "core" , None , "c" ) , Ok ( Cow :: <[ u8 ] >:: Borrowed ( b"1" ) ) ) ;
1949
1945
@@ -1975,11 +1971,7 @@ mod from_paths_tests {
1975
1971
fs:: write ( d_path. as_path ( ) , b"[core]\n a = false" ) . expect ( "Unable to write config file" ) ;
1976
1972
1977
1973
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 ( ) ;
1983
1975
1984
1976
assert_eq ! (
1985
1977
config. get_raw_value( "core" , None , "a" ) ,
@@ -2013,11 +2005,7 @@ mod from_paths_tests {
2013
2005
fs:: write ( c_path. as_path ( ) , b"[core]\n key = c" ) . expect ( "Unable to write config file" ) ;
2014
2006
2015
2007
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 ( ) ;
2021
2009
2022
2010
assert_eq ! (
2023
2011
config. get_raw_multi_value( "core" , None , "key" ) . unwrap( ) ,
0 commit comments