Skip to content

Commit

Permalink
Make relativize_path_maybe directly update the path argument
Browse files Browse the repository at this point in the history
This simplifies the call sites.
Also skip updating if the path is already absolute. As a side effect of
this '/dev/null' is no longer converted to '\dev\null' on Windows.
  • Loading branch information
th1000s committed Jul 7, 2024
1 parent db56217 commit 2d848d9
Show file tree
Hide file tree
Showing 2 changed files with 17 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(&mut path_or_mode, self.config);
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(&mut path_or_mode, self.config);
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
20 changes: 11 additions & 9 deletions src/utils/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ 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> {
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
/// Relativize `path` if delta `config` demands that and paths are not already relativized by git.
pub fn relativize_path_maybe(path: &mut String, config: &Config) {
let mut inner_relativize = || -> Option<()> {
let base = config.cwd_relative_to_repo_root.as_deref()?;
let relative_path = pathdiff::diff_paths(&path, base)?;
if relative_path.is_relative() {
*path = relative_path.to_string_lossy().into_owned();
}
} else {
None
Some(())
};
if config.relative_paths && !calling_process().paths_in_input_are_relative_to_cwd() {
let _ = inner_relativize();
}
}

Expand Down

0 comments on commit 2d848d9

Please sign in to comment.