From f171b1c9d50b6f3cfea421b29fa496a0681f25cc Mon Sep 17 00:00:00 2001 From: Thomas Otto Date: Sat, 6 Jul 2024 08:48:52 +0200 Subject: [PATCH] Make relativize_path_maybe directly update the passed path arg This simplifies the call sites. --- src/handlers/diff_header.rs | 14 ++++++-------- src/utils/path.rs | 18 +++++++++--------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/handlers/diff_header.rs b/src/handlers/diff_header.rs index 24ecc9d50..2a098175a 100644 --- a/src/handlers/diff_header.rs +++ b/src/handlers/diff_header.rs @@ -84,12 +84,11 @@ impl<'a> StateMachine<'a> { return Ok(false); } - let (path_or_mode, file_event) = + let (mut path_or_mode, file_event) = parse_diff_header_line(&self.line, self.source == Source::GitDiff); - self.minus_file = utils::path::relativize_path_maybe(&path_or_mode, self.config) - .map(|p| p.to_string_lossy().into_owned()) - .unwrap_or(path_or_mode); + utils::path::relativize_path_maybe(self.config, &mut path_or_mode); + self.minus_file = path_or_mode; self.minus_file_event = file_event; if self.source == Source::DiffUnified { @@ -121,12 +120,11 @@ impl<'a> StateMachine<'a> { return Ok(false); } let mut handled_line = false; - let (path_or_mode, file_event) = + let (mut path_or_mode, file_event) = parse_diff_header_line(&self.line, self.source == Source::GitDiff); - self.plus_file = utils::path::relativize_path_maybe(&path_or_mode, self.config) - .map(|p| p.to_string_lossy().into_owned()) - .unwrap_or(path_or_mode); + utils::path::relativize_path_maybe(self.config, &mut path_or_mode); + self.plus_file = path_or_mode; self.plus_file_event = file_event; self.painter .set_syntax(get_filename_from_diff_header_line_file_path( diff --git a/src/utils/path.rs b/src/utils/path.rs index 524ef910b..375cf4058 100644 --- a/src/utils/path.rs +++ b/src/utils/path.rs @@ -25,16 +25,16 @@ pub fn absolute_path(relative_path: &str, config: &Config) -> Option { .map(normalize_path) } -/// Relativize path if delta config demands that and paths are not already relativized by git. -pub fn relativize_path_maybe(path: &str, config: &Config) -> Option { +/// Relativize `path` if delta `config` demands that and paths are not already relativized by git. +pub fn relativize_path_maybe(config: &Config, path: &mut String) { + let mut inner_relativize = || -> Option<()> { + let base = config.cwd_relative_to_repo_root.as_deref()?; + let relative_path = pathdiff::diff_paths(&path, base)?; + *path = relative_path.to_string_lossy().into_owned(); + Some(()) + }; if config.relative_paths && !calling_process().paths_in_input_are_relative_to_cwd() { - if let Some(base) = config.cwd_relative_to_repo_root.as_deref() { - pathdiff::diff_paths(path, base) - } else { - None - } - } else { - None + let _ = inner_relativize(); } }