Skip to content

Commit

Permalink
Make relativize_path_maybe directly update the passed path arg
Browse files Browse the repository at this point in the history
This simplifies the call sites.
  • Loading branch information
th1000s committed Jul 6, 2024
1 parent f61b60a commit f171b1c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
14 changes: 6 additions & 8 deletions src/handlers/diff_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
18 changes: 9 additions & 9 deletions src/utils/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ pub fn absolute_path(relative_path: &str, config: &Config) -> Option<PathBuf> {
.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<PathBuf> {
/// 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();
}
}

Expand Down

0 comments on commit f171b1c

Please sign in to comment.