From 59a907117bb9aeb467b392c4fdc36f2f931330c0 Mon Sep 17 00:00:00 2001 From: Thomas Otto Date: Fri, 1 Aug 2025 11:02:55 +0200 Subject: Fix diff output when a diff ends with a mode change Output can be generated directly via Painter::write, or Painter::output_buffer. The latter must be emit()'ed before the former method can be used again, otherwise the order is incorrect. Tests added. Fixes #1504 --- src/handlers/diff_header.rs | 68 ++++++++++++++++++++++++++++++++++++++++ src/handlers/diff_header_diff.rs | 1 + 2 files changed, 69 insertions(+) diff --git a/src/handlers/diff_header.rs b/src/handlers/diff_header.rs index 78157cb..b5fe114 100644 --- a/src/handlers/diff_header.rs +++ b/src/handlers/diff_header.rs @@ -646,6 +646,74 @@ mod tests { ); } + #[test] + fn test_diff_header_with_mode_change_in_last_hunk() { + let input = "\ +diff --git a/a.txt b/a.txt +index 44371ed..e69de29 100644 +--- a/a.txt ++++ b/a.txt +@@ -1 +0,0 @@ +-A-content +diff --git a/b.txt b/b.txt +old mode 100644 +new mode 100755 +"; + + let result = DeltaTest::with_args(&[]).with_input(input); + + assert_snapshot!(result.output, @r" + a.txt + ─────────────────────────────────────────── + + ───┐ + 0: │ + ───┘ + A-content + + b.txt (mode +x) + ─────────────────────────────────────────── + "); + } + + #[test] + fn test_diff_header_with_2x_mode_change() { + let input = "\ +diff --git a/a.txt b/a.txt +index 44371ed..e69de29 100644 +--- a/a.txt ++++ b/a.txt +@@ -1 +0,0 @@ +-A-content +diff --git a/b.txt b/b.txt +old mode 100644 +new mode 100755 +--- a/b.txt ++++ b/b.txt +diff --git a/c.txt b/c.txt +old mode 100644 +new mode 100755 +"; + + let result = DeltaTest::with_args(&[]).with_input(input); + + assert_snapshot!(result.output, @r" + a.txt + ─────────────────────────────────────────── + + ───┐ + 0: │ + ───┘ + A-content + + b.txt (mode +x) + ─────────────────────────────────────────── + + c.txt (mode +x) + ─────────────────────────────────────────── + "); + } + pub const BIN_AND_TXT_FILE_ADDED: &str = "\ diff --git a/BIN b/BIN new file mode 100644 diff --git a/src/handlers/diff_header_diff.rs b/src/handlers/diff_header_diff.rs index e88f89d..70aca72 100644 --- a/src/handlers/diff_header_diff.rs +++ b/src/handlers/diff_header_diff.rs @@ -13,6 +13,7 @@ impl StateMachine<'_> { return Ok(false); } self.painter.paint_buffered_minus_and_plus_lines(); + self.painter.emit()?; self.state = if self.line.starts_with("diff --cc ") || self.line.starts_with("diff --combined ") { // We will determine the number of parents when we see the hunk header. -- cgit v1.2.3