Mercurial > hg
comparison rust/hg-core/src/config/layer.rs @ 46482:39128182f04e
rust: Remove unnecessary check for absolute path before joining
`Path::join` does the right thing if its argument is absolute.
Differential Revision: https://phab.mercurial-scm.org/D9960
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Mon, 01 Feb 2021 13:32:00 +0100 |
parents | 0d734c0ae1cf |
children | 2845892dd489 |
comparison
equal
deleted
inserted
replaced
46481:0d734c0ae1cf | 46482:39128182f04e |
---|---|
11 use crate::utils::files::{get_bytes_from_path, get_path_from_bytes}; | 11 use crate::utils::files::{get_bytes_from_path, get_path_from_bytes}; |
12 use format_bytes::format_bytes; | 12 use format_bytes::format_bytes; |
13 use lazy_static::lazy_static; | 13 use lazy_static::lazy_static; |
14 use regex::bytes::Regex; | 14 use regex::bytes::Regex; |
15 use std::collections::HashMap; | 15 use std::collections::HashMap; |
16 use std::io; | |
17 use std::path::{Path, PathBuf}; | 16 use std::path::{Path, PathBuf}; |
18 | 17 |
19 lazy_static! { | 18 lazy_static! { |
20 static ref SECTION_RE: Regex = make_regex(r"^\[([^\[]+)\]"); | 19 static ref SECTION_RE: Regex = make_regex(r"^\[([^\[]+)\]"); |
21 static ref ITEM_RE: Regex = make_regex(r"^([^=\s][^=]*?)\s*=\s*((.*\S)?)"); | 20 static ref ITEM_RE: Regex = make_regex(r"^([^=\s][^=]*?)\s*=\s*((.*\S)?)"); |
95 let mut section = b"".to_vec(); | 94 let mut section = b"".to_vec(); |
96 | 95 |
97 while let Some((index, bytes)) = lines_iter.next() { | 96 while let Some((index, bytes)) = lines_iter.next() { |
98 if let Some(m) = INCLUDE_RE.captures(&bytes) { | 97 if let Some(m) = INCLUDE_RE.captures(&bytes) { |
99 let filename_bytes = &m[1]; | 98 let filename_bytes = &m[1]; |
100 let filename_to_include = get_path_from_bytes(&filename_bytes); | 99 // `Path::parent` only fails for the root directory, |
101 let (include_src, result) = | 100 // which `src` can’t be since we’ve managed to open it as a file. |
102 read_include(&src, &filename_to_include); | 101 let dir = src |
103 let data = result.for_file(filename_to_include)?; | 102 .parent() |
103 .expect("Path::parent fail on a file we’ve read"); | |
104 // `Path::join` with an absolute argument correctly ignores the base path | |
105 let filename = dir.join(&get_path_from_bytes(&filename_bytes)); | |
106 let data = std::fs::read(&filename).for_file(&filename)?; | |
104 layers.push(current_layer); | 107 layers.push(current_layer); |
105 layers.extend(Self::parse(&include_src, &data)?); | 108 layers.extend(Self::parse(&filename, &data)?); |
106 current_layer = Self::new(ConfigOrigin::File(src.to_owned())); | 109 current_layer = Self::new(ConfigOrigin::File(src.to_owned())); |
107 } else if let Some(_) = EMPTY_RE.captures(&bytes) { | 110 } else if let Some(_) = EMPTY_RE.captures(&bytes) { |
108 } else if let Some(m) = SECTION_RE.captures(&bytes) { | 111 } else if let Some(m) = SECTION_RE.captures(&bytes) { |
109 section = m[1].to_vec(); | 112 section = m[1].to_vec(); |
110 } else if let Some(m) = ITEM_RE.captures(&bytes) { | 113 } else if let Some(m) = ITEM_RE.captures(&bytes) { |
232 } | 235 } |
233 | 236 |
234 fn make_regex(pattern: &'static str) -> Regex { | 237 fn make_regex(pattern: &'static str) -> Regex { |
235 Regex::new(pattern).expect("expected a valid regex") | 238 Regex::new(pattern).expect("expected a valid regex") |
236 } | 239 } |
237 | |
238 /// Includes are relative to the file they're defined in, unless they're | |
239 /// absolute. | |
240 fn read_include( | |
241 old_src: &Path, | |
242 new_src: &Path, | |
243 ) -> (PathBuf, io::Result<Vec<u8>>) { | |
244 if new_src.is_absolute() { | |
245 (new_src.to_path_buf(), std::fs::read(&new_src)) | |
246 } else { | |
247 let dir = old_src.parent().unwrap(); | |
248 let new_src = dir.join(&new_src); | |
249 (new_src.to_owned(), std::fs::read(&new_src)) | |
250 } | |
251 } |