diff --git a/crates/forge-git/src/history.rs b/crates/forge-git/src/history.rs index 6402fe2..489fb2c 100644 --- a/crates/forge-git/src/history.rs +++ b/crates/forge-git/src/history.rs @@ -631,9 +631,32 @@ pub(crate) async fn diff_between( starts.len().saturating_sub(1) }; + // Some git versions keep whitespace-only files in the name list under `-w` while the patch + // omits them. Then the streams no longer line up by position, so match by path and drop + // names that have no section in the patch. + let mut slots: Vec = (0..entries.len()).collect(); + if ignore_ws && patch_complete && starts.len() != entries.len() { + let mut j = 0usize; + let mut kept = Vec::new(); + let mut kept_entries = Vec::new(); + for entry in std::mem::take(&mut entries) { + let Some(&st) = starts.get(j) else { break }; + let header = patch[st..].lines().next().unwrap_or_default(); + let path = entry.new_path.as_deref().or(entry.old_path.as_deref()); + if path.is_some_and(|p| header.ends_with(&format!(" b/{p}"))) { + kept.push(j); + kept_entries.push(entry); + j += 1; + } + } + slots = kept; + entries = kept_entries; + } + let mut total_left = limits.max_total_lines; let mut files = Vec::with_capacity(entries.len()); for (i, entry) in entries.into_iter().enumerate() { + let i = slots[i]; let block = if i < usable { let end = starts.get(i + 1).copied().unwrap_or(patch.len()); Some(parse_block( @@ -659,6 +682,15 @@ pub(crate) async fn diff_between( truncated: missing || out.truncated, }); } + if ignore_ws { + // Whatever git listed, a text file with no changed lines left is whitespace only. + files.retain(|f| { + f.status != DiffStatus::Modified + || f.is_binary + || f.truncated + || f.additions + f.deletions > 0 + }); + } Ok(CommitDiff { files, truncated_files,