From baa67fe1dbfc1e838b1aa675ad289598f4cb50be Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Mon, 6 Feb 2023 19:21:21 +0900 Subject: [PATCH] cli: make interpolate_mergetool_filename_patterns() simply work with strings The assumption here is temp_dir wouldn't contain invalid utf-8 bytes. If it can contain invalid bytes, maybe we can remove temp_dir from arguments, and chdir(temp_dir) instead. This unblocks the use of Regex. We could use regex::bytes, but it's way more complex as we would have to go back and forth between str/OsStr and bytes. --- src/merge_tools.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/merge_tools.rs b/src/merge_tools.rs index 9f263e735..abfbc6b40 100644 --- a/src/merge_tools.rs +++ b/src/merge_tools.rs @@ -227,7 +227,12 @@ pub fn run_mergetool( // TODO: Should actually ignore the error here, or have a warning. set_readonly_recursively(&path).map_err(ExternalToolError::SetUpDirError)?; } - Ok((*role, path)) + Ok(( + *role, + path.into_os_string() + .into_string() + .expect("temp_dir would be valid utf-8"), + )) }) .try_collect()?; @@ -277,23 +282,21 @@ pub fn run_mergetool( Ok(tree_builder.write_tree()) } -fn interpolate_mergetool_filename_patterns>( +fn interpolate_mergetool_filename_patterns>( merge_args: &[String], - paths: &HashMap<&str, PathBuf>, -) -> Vec -where - Vec: FromIterator, -{ + paths: &HashMap<&str, V>, +) -> Vec { merge_args .iter() .map(|arg| { // TODO: Match all instances of `\$\w+` pattern and replace them // so that portions of args can be replaced, and so that file paths // that include the '$' character are processed correctly. - arg.strip_prefix('$') - .and_then(|p| paths.get(p)) - .and_then(|p| From::from(p.clone())) - .unwrap_or_else(|| From::from(arg.clone())) + if let Some(subst) = arg.strip_prefix('$').and_then(|p| paths.get(p)) { + subst.as_ref().to_owned() + } else { + arg.clone() + } }) .collect() }