Drop whitespace-only files from the diff by path when git keeps them under -w
Browse files at this commitcbf0cb1a00e7574b5da39de0610bb555fdad2545Parent: 5b805d7- fix
- drop whitespace-only files from the diff by path when git keeps them under -w
1 changed file +32 -0
crates/forge-git/src/history.rsmodified+32 -0
| @@ -631,9 +631,32 @@ pub(crate) async fn diff_between( | |||
| 631 | starts.len().saturating_sub(1) | 631 | starts.len().saturating_sub(1) |
| 632 | }; | 632 | }; |
| 633 | 633 | ||
| 634 | + // Some git versions keep whitespace-only files in the name list under `-w` while the patch | ||
| 635 | + // omits them. Then the streams no longer line up by position, so match by path and drop | ||
| 636 | + // names that have no section in the patch. | ||
| 637 | + let mut slots: Vec<usize> = (0..entries.len()).collect(); | ||
| 638 | + if ignore_ws && patch_complete && starts.len() != entries.len() { | ||
| 639 | + let mut j = 0usize; | ||
| 640 | + let mut kept = Vec::new(); | ||
| 641 | + let mut kept_entries = Vec::new(); | ||
| 642 | + for entry in std::mem::take(&mut entries) { | ||
| 643 | + let Some(&st) = starts.get(j) else { break }; | ||
| 644 | + let header = patch[st..].lines().next().unwrap_or_default(); | ||
| 645 | + let path = entry.new_path.as_deref().or(entry.old_path.as_deref()); | ||
| 646 | + if path.is_some_and(|p| header.ends_with(&format!(" b/{p}"))) { | ||
| 647 | + kept.push(j); | ||
| 648 | + kept_entries.push(entry); | ||
| 649 | + j += 1; | ||
| 650 | + } | ||
| 651 | + } | ||
| 652 | + slots = kept; | ||
| 653 | + entries = kept_entries; | ||
| 654 | + } | ||
| 655 | + | ||
| 634 | let mut total_left = limits.max_total_lines; | 656 | let mut total_left = limits.max_total_lines; |
| 635 | let mut files = Vec::with_capacity(entries.len()); | 657 | let mut files = Vec::with_capacity(entries.len()); |
| 636 | for (i, entry) in entries.into_iter().enumerate() { | 658 | for (i, entry) in entries.into_iter().enumerate() { |
| 659 | + let i = slots[i]; | ||
| 637 | let block = if i < usable { | 660 | let block = if i < usable { |
| 638 | let end = starts.get(i + 1).copied().unwrap_or(patch.len()); | 661 | let end = starts.get(i + 1).copied().unwrap_or(patch.len()); |
| 639 | Some(parse_block( | 662 | Some(parse_block( |
| @@ -659,6 +682,15 @@ pub(crate) async fn diff_between( | |||
| 659 | truncated: missing || out.truncated, | 682 | truncated: missing || out.truncated, |
| 660 | }); | 683 | }); |
| 661 | } | 684 | } |
| 685 | + if ignore_ws { | ||
| 686 | + // Whatever git listed, a text file with no changed lines left is whitespace only. | ||
| 687 | + files.retain(|f| { | ||
| 688 | + f.status != DiffStatus::Modified | ||
| 689 | + || f.is_binary | ||
| 690 | + || f.truncated | ||
| 691 | + || f.additions + f.deletions > 0 | ||
| 692 | + }); | ||
| 693 | + } | ||
| 662 | Ok(CommitDiff { | 694 | Ok(CommitDiff { |
| 663 | files, | 695 | files, |
| 664 | truncated_files, | 696 | truncated_files, |