Skip to content

Commit

Permalink
conflicts: work around rust-lang/rust#89716
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvonz committed Oct 13, 2021
1 parent b4b64eb commit c0a26f7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 41 deletions.
30 changes: 21 additions & 9 deletions lib/src/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,31 @@ fn write_diff_hunks(left: &[u8], right: &[u8], file: &mut dyn Write) -> std::io:
for hunk in diff.hunks() {
match hunk {
DiffHunk::Matching(content) => {
for line in content.split_inclusive(|b| *b == b'\n') {
file.write_all(b" ")?;
file.write_all(line)?;
// TODO: Remove this check once https://github.com/rust-lang/rust/issues/89716
// has been fixed and released for long enough.
if !content.is_empty() {
for line in content.split_inclusive(|b| *b == b'\n') {
file.write_all(b" ")?;
file.write_all(line)?;
}
}
}
DiffHunk::Different(content) => {
for line in content[0].split_inclusive(|b| *b == b'\n') {
file.write_all(b"-")?;
file.write_all(line)?;
// TODO: Remove this check once https://github.com/rust-lang/rust/issues/89716
// has been fixed and released for long enough.
if !content[0].is_empty() {
for line in content[0].split_inclusive(|b| *b == b'\n') {
file.write_all(b"-")?;
file.write_all(line)?;
}
}
for line in content[1].split_inclusive(|b| *b == b'\n') {
file.write_all(b"+")?;
file.write_all(line)?;
// TODO: Remove this check once https://github.com/rust-lang/rust/issues/89716
// has been fixed and released for long enough.
if !content[1].is_empty() {
for line in content[1].split_inclusive(|b| *b == b'\n') {
file.write_all(b"+")?;
file.write_all(line)?;
}
}
}
}
Expand Down
72 changes: 42 additions & 30 deletions lib/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,44 +85,56 @@ impl<'a> Iterator for DiffLineIterator<'a> {
self.current_pos += 1;
match hunk {
diff::DiffHunk::Matching(text) => {
let lines = text.split_inclusive(|b| *b == b'\n');
for line in lines {
self.current_line.has_left_content = true;
self.current_line.has_right_content = true;
self.current_line.hunks.push(DiffHunk::Matching(line));
if line.ends_with(b"\n") {
self.queued_lines.push_back(self.current_line.clone());
self.current_line.left_line_number += 1;
self.current_line.right_line_number += 1;
self.current_line.reset_line();
// TODO: Remove this check once https://github.com/rust-lang/rust/issues/89716
// has been fixed and released for long enough.
if !text.is_empty() {
let lines = text.split_inclusive(|b| *b == b'\n');
for line in lines {
self.current_line.has_left_content = true;
self.current_line.has_right_content = true;
self.current_line.hunks.push(DiffHunk::Matching(line));
if line.ends_with(b"\n") {
self.queued_lines.push_back(self.current_line.clone());
self.current_line.left_line_number += 1;
self.current_line.right_line_number += 1;
self.current_line.reset_line();
}
}
}
}
diff::DiffHunk::Different(contents) => {
let left = contents[0];
let right = contents[1];
let left_lines = left.split_inclusive(|b| *b == b'\n');
for left_line in left_lines {
self.current_line.has_left_content = true;
self.current_line
.hunks
.push(DiffHunk::Different(vec![left_line, b""]));
if left_line.ends_with(b"\n") {
self.queued_lines.push_back(self.current_line.clone());
self.current_line.left_line_number += 1;
self.current_line.reset_line();
// TODO: Remove this check once https://github.com/rust-lang/rust/issues/89716
// has been fixed and released for long enough.
if !left.is_empty() {
let left_lines = left.split_inclusive(|b| *b == b'\n');
for left_line in left_lines {
self.current_line.has_left_content = true;
self.current_line
.hunks
.push(DiffHunk::Different(vec![left_line, b""]));
if left_line.ends_with(b"\n") {
self.queued_lines.push_back(self.current_line.clone());
self.current_line.left_line_number += 1;
self.current_line.reset_line();
}
}
}
let right_lines = right.split_inclusive(|b| *b == b'\n');
for right_line in right_lines {
self.current_line.has_right_content = true;
self.current_line
.hunks
.push(DiffHunk::Different(vec![b"", right_line]));
if right_line.ends_with(b"\n") {
self.queued_lines.push_back(self.current_line.clone());
self.current_line.right_line_number += 1;
self.current_line.reset_line();
// TODO: Remove this check once https://github.com/rust-lang/rust/issues/89716
// has been fixed and released for long enough.
if !right.is_empty() {
let right_lines = right.split_inclusive(|b| *b == b'\n');
for right_line in right_lines {
self.current_line.has_right_content = true;
self.current_line
.hunks
.push(DiffHunk::Different(vec![b"", right_line]));
if right_line.ends_with(b"\n") {
self.queued_lines.push_back(self.current_line.clone());
self.current_line.right_line_number += 1;
self.current_line.reset_line();
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions lib/tests/test_conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ line 5

let mut result: Vec<u8> = vec![];
materialize_conflict(repo.store(), &path, &conflict, &mut result);
// TODO: There's an extra "+" after "-line 3".
assert_eq!(
String::from_utf8(result).unwrap().as_str(),
"line 1
Expand All @@ -243,7 +242,7 @@ line 2
-------
+++++++
-line 3
++++++++
+++++++
right
>>>>>>>
line 4
Expand Down

0 comments on commit c0a26f7

Please sign in to comment.