mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-21 10:30:41 +08:00
cli: Avoid overflow in xdiff on Windows
We would like the diff output of changes to the identity document to not collapse into separate hunks, but show all of the identity document in the diff. The way to do this is to set the number of context lines sufficiently high. It turns out that `u32::MAX` is too high for `xdiff`, which is commonly used on Windows. `xdiff` stores the context lines in a signed integer of 32 bits. Also, it appears to double line numbers when it merges hunks. Thus, a safe value seems to be half of `i32::MAX`.
This commit is contained in:
@@ -606,7 +606,21 @@ fn print_diff(
|
||||
};
|
||||
let current = radicle::git::write_tree(&doc::PATH, current.as_bytes(), &repo)?;
|
||||
let mut opts = radicle::git::raw::DiffOptions::new();
|
||||
opts.context_lines(u32::MAX);
|
||||
|
||||
{
|
||||
#[cfg(not(windows))]
|
||||
const CONTEXT_LINES: u32 = u32::MAX;
|
||||
|
||||
// `xdiff`, which is commonly used on Windows, stores the number of
|
||||
// context lines as a signed integer and doubles the value when
|
||||
// merging hunks. Avoid overflow. See also:
|
||||
// - <https://github.com/git-for-windows/git/tree/main/xdiff>
|
||||
// - <https://github.com/libgit2/xdiff>
|
||||
#[cfg(windows)]
|
||||
const CONTEXT_LINES: u32 = (i32::MAX / 2) as u32;
|
||||
|
||||
opts.context_lines(CONTEXT_LINES);
|
||||
}
|
||||
|
||||
let diff = repo.diff_tree_to_tree(previous.as_ref(), Some(¤t), Some(&mut opts))?;
|
||||
let diff = Diff::try_from(diff)?;
|
||||
|
||||
Reference in New Issue
Block a user