From c0a070ecc93b4e7e2f3358d54440ad8caa770e7e Mon Sep 17 00:00:00 2001 From: dawn Date: Sat, 24 Oct 2026 01:33:36 +0300 Subject: [PATCH] gitmirror/xrpc: diff each file of a patch once Signed-off-by: dawn --- .../crates/gitmirror-xrpc/src/format_patch.rs | 171 ++++++++++++------ 1 file changed, 112 insertions(+), 59 deletions(-) diff --git a/gitmirror/crates/gitmirror-xrpc/src/format_patch.rs b/gitmirror/crates/gitmirror-xrpc/src/format_patch.rs index 7902c1603..a99e394da 100644 --- a/gitmirror/crates/gitmirror-xrpc/src/format_patch.rs +++ b/gitmirror/crates/gitmirror-xrpc/src/format_patch.rs @@ -1,9 +1,11 @@ use std::io::{self, Write}; use anyhow::Context as _; +use fixedbitset::FixedBitSet; use gix::ObjectId; use gix::bstr::{BStr, ByteSlice as _}; -use gix::diff::blob::{Algorithm, InternedInput, diff_with_slider_heuristics}; +use gix::diff::blob::sources::byte_lines; +use gix::diff::blob::{Algorithm, Hunk, InternedInput, diff_with_slider_heuristics}; use gix::object::tree::diff::ChangeDetached; use gix::objs::tree::{EntryKind, EntryMode}; use gix::prelude::ObjectIdExt as _; @@ -105,17 +107,19 @@ fn write_patch( changes.sort_by(|a, b| a.location().cmp(b.location())); out.write_all(b"---\n")?; - let stats = changes + let (stats, lines): (Vec<_>, Vec<_>) = changes .iter() .map(|change| file_stat(repo, change, max_blob_bytes)) - .collect::>>()?; + .collect::>>()? + .into_iter() + .unzip(); write_stat(out, &stats)?; for change in &changes { write_summary(out, change)?; } out.write_all(b"\n")?; - for change in &changes { - write_file_patch(repo, change, max_blob_bytes, out)?; + for (change, lines) in changes.iter().zip(&lines) { + write_file_patch(repo, change, lines.as_ref(), max_blob_bytes, out)?; } Ok(()) } @@ -554,11 +558,60 @@ struct FileStat { binary: bool, } +/// one bit per line, so the diff can wait for the stat without keeping the text +struct ChangedLines { + removed: FixedBitSet, + added: FixedBitSet, +} + +impl ChangedLines { + fn diff(old: &[u8], new: &[u8]) -> Self { + let input = InternedInput::new(old, new); + let diff = diff_with_slider_heuristics(Algorithm::Myers, &input); + let mut lines = ChangedLines { + removed: FixedBitSet::with_capacity(input.before.len()), + added: FixedBitSet::with_capacity(input.after.len()), + }; + let range = |range: std::ops::Range| range.start as usize..range.end as usize; + for hunk in diff.hunks() { + lines.removed.insert_range(range(hunk.before)); + lines.added.insert_range(range(hunk.after)); + } + lines + } + + fn hunks(&self) -> Vec { + let (n1, n2) = (self.removed.len(), self.added.len()); + let (mut i, mut j) = (0, 0); + let mut hunks = Vec::new(); + loop { + let start = (i, j); + while i < n1 && self.removed[i] { + i += 1; + } + while j < n2 && self.added[j] { + j += 1; + } + if (i, j) != start { + hunks.push(Hunk { + before: start.0 as u32..i as u32, + after: start.1 as u32..j as u32, + }); + } + if i >= n1 && j >= n2 { + return hunks; + } + i += 1; + j += 1; + } + } +} + fn file_stat( repo: &gix::Repository, change: &ChangeDetached, max_blob_bytes: u64, -) -> anyhow::Result { +) -> anyhow::Result<(FileStat, Option)> { let (old, new) = sides(change); let name = match (old, new, rewrite(change)) { (Some(old), Some(new), Some(_)) => rename_name(old.path, new.path), @@ -570,26 +623,27 @@ fn file_stat( load(repo, new, max_blob_bytes)?, ); let binary = old_content.is_binary() || new_content.is_binary(); - let (added, deleted) = if same { - (0, 0) + let (added, deleted, lines) = if same { + (0, 0, None) } else if binary { - (new_content.size(), old_content.size()) + (new_content.size(), old_content.size(), None) } else if let (Content::Text(old), Content::Text(new)) = (&old_content, &new_content) { - let input = InternedInput::new(old.as_slice(), new.as_slice()); - let diff = diff_with_slider_heuristics(Algorithm::Myers, &input); + let lines = ChangedLines::diff(old, new); ( - u64::from(diff.count_additions()), - u64::from(diff.count_removals()), + lines.added.count_ones(..) as u64, + lines.removed.count_ones(..) as u64, + Some(lines), ) } else { unreachable!("both sides are text when neither is binary") }; - Ok(FileStat { + let stat = FileStat { name, added, deleted, binary, - }) + }; + Ok((stat, lines)) } fn decimal_width(n: u64) -> usize { @@ -746,6 +800,7 @@ fn write_summary(out: &mut impl Write, change: &ChangeDetached) -> io::Result<() fn write_file_patch( repo: &gix::Repository, change: &ChangeDetached, + lines: Option<&ChangedLines>, max_blob_bytes: u64, out: &mut impl Write, ) -> anyhow::Result<()> { @@ -754,10 +809,10 @@ fn write_file_patch( && old.file_type() != new.file_type() { // git shows a type change as a delete plus an add - write_pair(repo, Some(old), None, None, max_blob_bytes, out)?; - return write_pair(repo, None, Some(new), None, max_blob_bytes, out); + write_pair(repo, Some(old), None, None, None, max_blob_bytes, out)?; + return write_pair(repo, None, Some(new), None, None, max_blob_bytes, out); } - write_pair(repo, old, new, rewrite(change), max_blob_bytes, out) + write_pair(repo, old, new, rewrite(change), lines, max_blob_bytes, out) } fn abbrev(repo: &gix::Repository, id: ObjectId, full: bool) -> String { @@ -775,6 +830,7 @@ fn write_pair( old: Option>, new: Option>, rewrite: Option<(u32, bool)>, + lines: Option<&ChangedLines>, max_blob_bytes: u64, out: &mut impl Write, ) -> anyhow::Result<()> { @@ -782,9 +838,6 @@ fn write_pair( let (name_a, name_b) = (old.unwrap_or(either).path, new.unwrap_or(either).path); let a = quote_prefixed(b"a/", name_a); let b = quote_prefixed(b"b/", name_b); - let old_content = load(repo, old, max_blob_bytes)?; - let new_content = load(repo, new, max_blob_bytes)?; - let binary = old_content.is_binary() || new_content.is_binary(); let mut header = Vec::new(); header.extend_from_slice(b"diff --git "); @@ -819,24 +872,31 @@ fn write_pair( header.push(b'\n'); } let ids = (old.map(|side| side.id), new.map(|side| side.id)); - if ids.0 != ids.1 { - let null = ObjectId::null(repo.object_hash()); - let short = |id: Option| id.map(|id| abbrev(repo, id, binary)); - let (short_a, short_b) = (short(ids.0), short(ids.1)); - let len = short_a.as_ref().or(short_b.as_ref()).map_or(0, String::len); - let null_short = || null.to_string()[..len].to_owned(); - write!( - header, - "index {}..{}", - short_a.unwrap_or_else(null_short), - short_b.unwrap_or_else(null_short) - )?; - match (old, new) { - (Some(old), Some(new)) if old.octal() == new.octal() => { - writeln!(header, " {:06o}", old.octal())? - } - _ => header.push(b'\n'), + if ids.0 == ids.1 { + if must_show_header { + out.write_all(&header)?; + } + return Ok(()); + } + let old_content = load(repo, old, max_blob_bytes)?; + let new_content = load(repo, new, max_blob_bytes)?; + let binary = old_content.is_binary() || new_content.is_binary(); + let null = ObjectId::null(repo.object_hash()); + let short = |id: Option| id.map(|id| abbrev(repo, id, binary)); + let (short_a, short_b) = (short(ids.0), short(ids.1)); + let len = short_a.as_ref().or(short_b.as_ref()).map_or(0, String::len); + let null_short = || null.to_string()[..len].to_owned(); + write!( + header, + "index {}..{}", + short_a.unwrap_or_else(null_short), + short_b.unwrap_or_else(null_short) + )?; + match (old, new) { + (Some(old), Some(new)) if old.octal() == new.octal() => { + writeln!(header, " {:06o}", old.octal())? } + _ => header.push(b'\n'), } let label = |side: Option>, quoted: &[u8]| -> Vec { @@ -848,12 +908,6 @@ fn write_pair( let (label_a, label_b) = (label(old, &a), label(new, &b)); if binary { - if ids.0 == ids.1 { - if must_show_header { - out.write_all(&header)?; - } - return Ok(()); - } out.write_all(&header)?; match (&old_content, &new_content) { (old_content, new_content) @@ -881,8 +935,17 @@ fn write_pair( let (Content::Text(old_text), Content::Text(new_text)) = (&old_content, &new_content) else { unreachable!("both sides are text when neither is binary") }; + let diffed; + let lines = match lines { + Some(lines) => lines, + // split halves get no precomputed lines + None => { + diffed = ChangedLines::diff(old_text, new_text); + &diffed + } + }; let mut hunks = Vec::new(); - write_hunks(&mut hunks, old_text, new_text)?; + write_hunks(&mut hunks, old_text, new_text, lines)?; if hunks.is_empty() { if must_show_header { out.write_all(&header)?; @@ -955,20 +1018,10 @@ fn funcname(line: &[u8]) -> Option<&[u8]> { Some(trim_end(&line[..line.len().min(FUNCNAME_BYTES)])) } -fn write_hunks(out: &mut Vec, old: &[u8], new: &[u8]) -> io::Result<()> { - let input = InternedInput::new(old, new); - let diff = diff_with_slider_heuristics(Algorithm::Myers, &input); - let before: Vec<&[u8]> = input - .before - .iter() - .map(|&token| input.interner[token]) - .collect(); - let after: Vec<&[u8]> = input - .after - .iter() - .map(|&token| input.interner[token]) - .collect(); - let hunks: Vec<_> = diff.hunks().collect(); +fn write_hunks(out: &mut Vec, old: &[u8], new: &[u8], lines: &ChangedLines) -> io::Result<()> { + let before: Vec<&[u8]> = byte_lines(old).collect(); + let after: Vec<&[u8]> = byte_lines(new).collect(); + let hunks = lines.hunks(); let (n1, n2) = (before.len() as u32, after.len() as u32); let mut func: &[u8] = b""; -- knotmirror 0.0.1