From patchwork Fri Feb 5 09:26:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: D9960: rust: Remove unnecessary check for absolute path before joining From: phabricator X-Patchwork-Id: 48274 Message-Id: To: Phabricator Cc: mercurial-devel@mercurial-scm.org Date: Fri, 5 Feb 2021 09:26:20 +0000 SimonSapin created this revision. Herald added a reviewer: hg-reviewers. Herald added a subscriber: mercurial-patches. REVISION SUMMARY `Path::join` does the right thing if its argument is absolute. REPOSITORY rHG Mercurial BRANCH default REVISION DETAIL https://phab.mercurial-scm.org/D9960 AFFECTED FILES rust/hg-core/src/config/layer.rs CHANGE DETAILS To: SimonSapin, #hg-reviewers Cc: mercurial-patches, mercurial-devel diff --git a/rust/hg-core/src/config/layer.rs b/rust/hg-core/src/config/layer.rs --- a/rust/hg-core/src/config/layer.rs +++ b/rust/hg-core/src/config/layer.rs @@ -13,7 +13,6 @@ use lazy_static::lazy_static; use regex::bytes::Regex; use std::collections::HashMap; -use std::io; use std::path::{Path, PathBuf}; lazy_static! { @@ -97,12 +96,12 @@ while let Some((index, bytes)) = lines_iter.next() { if let Some(m) = INCLUDE_RE.captures(&bytes) { let filename_bytes = &m[1]; - let filename_to_include = get_path_from_bytes(&filename_bytes); - let (include_src, result) = - read_include(&src, &filename_to_include); - let data = result.for_file(filename_to_include)?; + // `Path::join` with an absolute argument does the right thing + let dir = src.parent().unwrap(); + let filename = dir.join(&get_path_from_bytes(&filename_bytes)); + let data = std::fs::read(&filename).for_file(&filename)?; layers.push(current_layer); - layers.extend(Self::parse(&include_src, &data)?); + layers.extend(Self::parse(&filename, &data)?); current_layer = Self::new(ConfigOrigin::File(src.to_owned())); } else if let Some(_) = EMPTY_RE.captures(&bytes) { } else if let Some(m) = SECTION_RE.captures(&bytes) { @@ -234,18 +233,3 @@ fn make_regex(pattern: &'static str) -> Regex { Regex::new(pattern).expect("expected a valid regex") } - -/// Includes are relative to the file they're defined in, unless they're -/// absolute. -fn read_include( - old_src: &Path, - new_src: &Path, -) -> (PathBuf, io::Result>) { - if new_src.is_absolute() { - (new_src.to_path_buf(), std::fs::read(&new_src)) - } else { - let dir = old_src.parent().unwrap(); - let new_src = dir.join(&new_src); - (new_src.to_owned(), std::fs::read(&new_src)) - } -}