diff: Decode colored Git pager output
eb5a470e8367895ee54d881b72b0cb50379c144c
Running git show through rustik-diff could pass the colored stream through unchanged because ANSI decoration hid the diff prefixes. Remove Git's line decoration before decoding while preserving escape sequences in uncolored source content. Assisted-by: Codex:gpt-5.6-sol
1 parent
a6cb1f95
crates/diff/src/decode.rs
+33 -2
| 8 | 8 | //! rustik-diff has no libgit2 layer to do it for us. |
|
| 9 | 9 | //! |
|
| 10 | 10 | //! [`Decode::decode_str`] is a thin string entry point — it just wraps the |
|
| 11 | 11 | //! input in [`io::Cursor`] and runs the streaming decoder. |
|
| 12 | 12 | ||
| 13 | + | use std::borrow::Cow; |
|
| 13 | 14 | use std::io::{self, BufRead}; |
|
| 14 | 15 | use std::path::PathBuf; |
|
| 15 | 16 | use std::str::FromStr; |
|
| 16 | 17 | ||
| 17 | 18 | use crate::document::{ |
| 98 | 99 | Stream::new(input).decode_document() |
|
| 99 | 100 | } |
|
| 100 | 101 | ||
| 101 | 102 | /// Decodes a diff document from raw bytes. |
|
| 102 | 103 | /// |
|
| 103 | - | /// Non-UTF-8 bytes are replaced with U+FFFD. All valid UTF-8 content is |
|
| 104 | - | /// preserved exactly. |
|
| 104 | + | /// Non-UTF-8 bytes are replaced with U+FFFD. ANSI control sequences on |
|
| 105 | + | /// lines colored by Git for pager output are removed before decoding. |
|
| 105 | 106 | fn decode_bytes(input: &[u8]) -> Result<Self, DecodeError> { |
|
| 106 | 107 | let text = String::from_utf8_lossy(input); |
|
| 108 | + | let text = without_git_color(&text); |
|
| 107 | 109 | let mut reader = io::BufReader::new(text.as_bytes()); |
|
| 108 | 110 | Self::decode(&mut reader) |
|
| 109 | 111 | } |
|
| 110 | 112 | } |
|
| 111 | 113 | ||
| 114 | + | /// Removes control sequences from Git-colored diff input. |
|
| 115 | + | /// |
|
| 116 | + | /// Git starts decorated lines with an escape sequence and appends a reset to |
|
| 117 | + | /// undecorated context lines. The initial escape identifies a colored document, |
|
| 118 | + | /// so uncolored source content keeps all escape bytes. |
|
| 119 | + | fn without_git_color(input: &str) -> Cow<'_, str> { |
|
| 120 | + | if !input.lines().any(|line| line.starts_with('\x1b')) { |
|
| 121 | + | return Cow::Borrowed(input); |
|
| 122 | + | } |
|
| 123 | + | ||
| 124 | + | let mut output = String::with_capacity(input.len()); |
|
| 125 | + | for line in input.split_inclusive('\n') { |
|
| 126 | + | if line.starts_with('\x1b') { |
|
| 127 | + | output.push_str(&rustik_tui::ansi::strip(line)); |
|
| 128 | + | } else if let Some(line) = line.strip_suffix("\x1b[m\r\n") { |
|
| 129 | + | output.push_str(line); |
|
| 130 | + | output.push_str("\r\n"); |
|
| 131 | + | } else if let Some(line) = line.strip_suffix("\x1b[m\n") { |
|
| 132 | + | output.push_str(line); |
|
| 133 | + | output.push('\n'); |
|
| 134 | + | } else if let Some(line) = line.strip_suffix("\x1b[m") { |
|
| 135 | + | output.push_str(line); |
|
| 136 | + | } else { |
|
| 137 | + | output.push_str(line); |
|
| 138 | + | } |
|
| 139 | + | } |
|
| 140 | + | Cow::Owned(output) |
|
| 141 | + | } |
|
| 142 | + | ||
| 112 | 143 | /// Streaming decoder over a buffered input stream. |
|
| 113 | 144 | /// |
|
| 114 | 145 | /// Stores the reader, a line counter, and per-document ID counters used while |
|
| 115 | 146 | /// building a [`DiffDocument`]. Each decode call (`Hunk` or `FileDiff`) starts |
|
| 116 | 147 | /// from a fresh stream, so IDs begin at zero and callers that combine pieces |
{
+0 -0
+0 -0
=> break,
+0 -0
a.rs
+0 -0
a.rs\x1b[m\n\
+0 -0
new.rs
+0 -0
old.rs
+0 -0
to.rs
+0 -0
copy2.rs
+0 -0
mode.sh
+0 -0
image.png
+0 -0
caf\303\251\t\"slash\\.rs" "b/modified caf\303\251\t\"slash\\.rs"
+0 -0
ordinary.rs
+0 -0
from caf\303\251\t\"slash\\.rs" "b/rename to caf\303\251\t\"slash\\.rs"
+0 -0
"b/non-utf8-\377.rs"
+0 -0
a
+0 -0
a
+0 -0
a
+0 -0
a
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0