History

refactor

Play with code to see what's tested, and notice that one part might be missing a test.

Sebastian Thiel committed 4 years ago

Commit 429446c1 parent 9673aae1 file · +18 −19Browse files

Unified · Split

git-discover/src/upwards.rs+18−19
@@ -86,18 +86,18 @@ pub(crate) mod function { · 85 unmodified lines
8686
8787 let max_height = if !ceiling_dirs.is_empty() {
8888 // Ceiling directory discovery requires us to canonicalize the path
89− is_canonicalized = true;
90− cursor = cursor
91− .canonicalize()
92− .map_err(|_| Error::InaccessibleDirectory { path: cursor })?;
93− Some(find_ceiling_height(&dir, ceiling_dirs))
89+ // TODO: this works nonetheless. Is a test missing? Or does 'absolutize()' do the job already?
90+ // is_canonicalized = true;
91+ // cursor = cursor
92+ // .canonicalize()
93+ // .map_err(|_| Error::InaccessibleDirectory { path: cursor })?;
94+ find_ceiling_height(&cursor, ceiling_dirs)
9495 } else {
9596 None
9697 };
9798
9899 let mut current_height = 0;
99100 'outer: loop {
100− // If we've reached the ceiling, stop looking.
101101 if max_height.map_or(false, |x| current_height > x) {
102102 return Err(Error::NoGitRepositoryWithinCeiling {
103103 path: dir.into_owned(),
@@ -156,6 +156,17 @@ pub(crate) mod function { · 52 unmodified lines
156156 }
157157
158158 fn shorten_path_with_cwd(cursor: PathBuf, cwd: Option<PathBuf>) -> PathBuf {
159+ fn comp_len(c: std::path::Component<'_>) -> usize {
160+ use std::path::Component::*;
161+ match c {
162+ Prefix(p) => p.as_os_str().len(),
163+ CurDir => 1,
164+ ParentDir => 2,
165+ Normal(p) => p.len(),
166+ RootDir => 1,
167+ }
168+ }
169+
159170 if let Some(cwd) = cwd {
160171 debug_assert_eq!(cursor.file_name().and_then(|f| f.to_str()), Some(".git"));
161172 let parent = cursor.parent().expect(".git appended");
@@ -177,21 +188,10 @@ pub(crate) mod function { · 15 unmodified lines
177188 }
178189 }
179190
180− fn comp_len(c: std::path::Component<'_>) -> usize {
181− use std::path::Component::*;
182− match c {
183− Prefix(p) => p.as_os_str().len(),
184− CurDir => 1,
185− ParentDir => 2,
186− Normal(p) => p.len(),
187− RootDir => 1,
188− }
189− }
190−
191191 /// Find the number of components parenting the `base_path` before the first directory in `ceiling_dirs`.
192192 /// `base_path` needs to be an absolute path. Non-absolute `ceiling_dirs` are discarded if `base_path` is absolute.
193193 // TODO: Handle this in a verbatim-path-prefix-neutral way on Windows (introduced by `path::canonicalize`).
194− fn find_ceiling_height(base_path: &Path, ceiling_dirs: &[PathBuf]) -> usize {
194+ fn find_ceiling_height(base_path: &Path, ceiling_dirs: &[PathBuf]) -> Option<usize> {
195195 ceiling_dirs
196196 .iter()
197197 .filter_map(|ceiling_dir| {
@@ -201,7 +201,6 @@ pub(crate) mod function { · 3 unmodified lines
201201 .map(|path_relative_to_ceiling| path_relative_to_ceiling.components().count())
202202 })
203203 .min()
204− .unwrap_or_else(|| base_path.components().count())
205204 }
206205
207206 /// Find the location of the git repository directly in `directory` or in any of its parent directories, and provide