Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub struct StateMachine<'a> {
pub source: Source,
pub minus_file: String,
pub plus_file: String,
pub binary_files: bool,
pub minus_file_event: handlers::diff_header::FileEvent,
pub plus_file_event: handlers::diff_header::FileEvent,
pub diff_line: String,
Expand Down Expand Up @@ -136,6 +137,7 @@ impl<'a> StateMachine<'a> {
source: Source::Unknown,
minus_file: "".to_string(),
plus_file: "".to_string(),
binary_files: false,
minus_file_event: handlers::diff_header::FileEvent::NoEvent,
plus_file_event: handlers::diff_header::FileEvent::NoEvent,
diff_line: "".to_string(),
Expand Down
18 changes: 16 additions & 2 deletions src/handlers/diff_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ use crate::{features, utils};
// https://git-scm.com/docs/git-config#Documentation/git-config.txt-diffmnemonicPrefix
const DIFF_PREFIXES: [&str; 6] = ["a/", "b/", "c/", "i/", "o/", "w/"];

///
/// Display decoration appended in place to a binary file's `minus_file`/
/// `plus_file`; `diff_header_osc` strips it again so the metadata record
/// carries the real path.
const BINARY_FILE_SUFFIX: &str = " (binary file)";

#[derive(Debug, PartialEq, Eq)]
pub enum FileEvent {
Added,
Expand Down Expand Up @@ -220,7 +226,7 @@ impl StateMachine<'_> {
// (`handle_diff_header_misc_line`); the record carries the
// real path.
let file = file
.strip_suffix(super::diff_header_misc::BINARY_FILE_SUFFIX)
.strip_suffix(BINARY_FILE_SUFFIX)
.unwrap_or(file);
if file.is_empty() {
String::new()
Expand All @@ -237,6 +243,7 @@ impl StateMachine<'_> {
let line = get_file_change_description_from_file_paths(
&self.minus_file,
&self.plus_file,
self.binary_files,
comparing,
&self.minus_file_event,
&self.plus_file_event,
Expand Down Expand Up @@ -452,6 +459,7 @@ fn _parse_file_path(path: &str, git_diff_name: bool) -> String {
pub fn get_file_change_description_from_file_paths(
minus_file: &str,
plus_file: &str,
binary_files: bool,
comparing: bool,
minus_file_event: &FileEvent,
plus_file_event: &FileEvent,
Expand Down Expand Up @@ -489,7 +497,8 @@ pub fn get_file_change_description_from_file_paths(
_ => formatted_file,
}
};
match (minus_file, plus_file, minus_file_event, plus_file_event) {

let mut description = match (minus_file, plus_file, minus_file_event, plus_file_event) {
(minus_file, plus_file, _, _) if minus_file == plus_file => format!(
"{}{}",
format_label(&config.file_modified_label),
Expand Down Expand Up @@ -517,7 +526,12 @@ pub fn get_file_change_description_from_file_paths(
config.right_arrow,
format_file(plus_file)
),
};

if binary_files {
description.push_str(BINARY_FILE_SUFFIX);
}
description
}
}

Expand Down
1 change: 1 addition & 0 deletions src/handlers/diff_header_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl StateMachine<'_> {
let name = get_repeated_file_path_from_diff_line(&self.line).unwrap_or_default();
self.minus_file.clone_from(&name);
self.plus_file.clone_from(&name);
self.binary_files = false;
self.minus_file_event = FileEvent::Change;
self.plus_file_event = FileEvent::Change;
self.current_file_pair = Some((self.minus_file.clone(), self.plus_file.clone()));
Expand Down
8 changes: 1 addition & 7 deletions src/handlers/diff_header_misc.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use crate::delta::{DiffType, Source, State, StateMachine};
use crate::utils::path::relativize_path_maybe;

/// Display decoration appended in place to a binary file's `minus_file`/
/// `plus_file`; `diff_header_osc` strips it again so the metadata record
/// carries the real path.
pub const BINARY_FILE_SUFFIX: &str = " (binary file)";

impl StateMachine<'_> {
#[inline]
fn test_diff_file_missing(&self) -> bool {
Expand Down Expand Up @@ -34,13 +29,12 @@ impl StateMachine<'_> {
return Ok(true);
}

self.binary_files = true;
if self.minus_file != "/dev/null" {
relativize_path_maybe(&mut self.minus_file, self.config);
self.minus_file.push_str(BINARY_FILE_SUFFIX);
}
if self.plus_file != "/dev/null" {
relativize_path_maybe(&mut self.plus_file, self.config);
self.plus_file.push_str(BINARY_FILE_SUFFIX);
}
return Ok(true);
}
Expand Down
7 changes: 7 additions & 0 deletions src/tests/test_example_diffs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2104,6 +2104,13 @@ src/align.rs:71: impl<'a> Alignment<'a> { │
);
}

#[test]
fn test_hyperlinks_binary_files() {
DeltaTest::with_args(&["--hyperlinks"])
.with_input(BINARY_FILES_DIFFER)
.expect_raw_contains("]8;;\u{1b}\\ (binary file)");
}

#[test]
fn test_filenames_with_spaces() {
DeltaTest::with_args(&[])
Expand Down