Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

normalize crlf if line breaks differ #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,21 @@ impl VersionDiff {
let mut files = BTreeMap::new();
let mut summary: BTreeMap<Utf8PathBuf, (usize, usize)> = BTreeMap::new();

let normalize_crlf = |bytes: &Bytes| {
let mut normalized = Vec::with_capacity(bytes.len());
let mut iter = bytes.iter().peekable();

while let Some(&byte) = iter.next() {
if byte == b'\r' {
if matches!(iter.peek(), Some(&b'\n')) {
continue;
}
}
normalized.push(byte);
}
Bytes::from(normalized)
};

// intersection of file paths in both left and right crate sources
let file_paths: BTreeSet<&Utf8Path> = left
.files
Expand All @@ -441,9 +456,23 @@ impl VersionDiff {
let left = left.files.get(path).cloned().unwrap_or_default();
let right = right.files.get(path).cloned().unwrap_or_default();

// Normalize line endings to - nut only if they differ
let left_crlf = left.windows(2).any(|w| w == b"\r\n");
let right_crlf = right.windows(2).any(|w| w == b"\r\n");

let left = if left_crlf && !right_crlf {
normalize_crlf(&left)
} else {
left
};
let right = if right_crlf && !left_crlf {
normalize_crlf(&right)
} else {
right
};

// generate text diff
let diff = TextDiff::from_lines(&left[..], &right[..]);

// collect changes
let changes: Vec<_> = diff
.iter_all_changes()
Expand Down