Skip to content

Commit c0a26f7

Browse files
committed
conflicts: work around rust-lang/rust#89716
1 parent b4b64eb commit c0a26f7

File tree

3 files changed

+64
-41
lines changed

3 files changed

+64
-41
lines changed

lib/src/conflicts.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,31 @@ fn write_diff_hunks(left: &[u8], right: &[u8], file: &mut dyn Write) -> std::io:
102102
for hunk in diff.hunks() {
103103
match hunk {
104104
DiffHunk::Matching(content) => {
105-
for line in content.split_inclusive(|b| *b == b'\n') {
106-
file.write_all(b" ")?;
107-
file.write_all(line)?;
105+
// TODO: Remove this check once https://github.com/rust-lang/rust/issues/89716
106+
// has been fixed and released for long enough.
107+
if !content.is_empty() {
108+
for line in content.split_inclusive(|b| *b == b'\n') {
109+
file.write_all(b" ")?;
110+
file.write_all(line)?;
111+
}
108112
}
109113
}
110114
DiffHunk::Different(content) => {
111-
for line in content[0].split_inclusive(|b| *b == b'\n') {
112-
file.write_all(b"-")?;
113-
file.write_all(line)?;
115+
// TODO: Remove this check once https://github.com/rust-lang/rust/issues/89716
116+
// has been fixed and released for long enough.
117+
if !content[0].is_empty() {
118+
for line in content[0].split_inclusive(|b| *b == b'\n') {
119+
file.write_all(b"-")?;
120+
file.write_all(line)?;
121+
}
114122
}
115-
for line in content[1].split_inclusive(|b| *b == b'\n') {
116-
file.write_all(b"+")?;
117-
file.write_all(line)?;
123+
// TODO: Remove this check once https://github.com/rust-lang/rust/issues/89716
124+
// has been fixed and released for long enough.
125+
if !content[1].is_empty() {
126+
for line in content[1].split_inclusive(|b| *b == b'\n') {
127+
file.write_all(b"+")?;
128+
file.write_all(line)?;
129+
}
118130
}
119131
}
120132
}

lib/src/files.rs

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,44 +85,56 @@ impl<'a> Iterator for DiffLineIterator<'a> {
8585
self.current_pos += 1;
8686
match hunk {
8787
diff::DiffHunk::Matching(text) => {
88-
let lines = text.split_inclusive(|b| *b == b'\n');
89-
for line in lines {
90-
self.current_line.has_left_content = true;
91-
self.current_line.has_right_content = true;
92-
self.current_line.hunks.push(DiffHunk::Matching(line));
93-
if line.ends_with(b"\n") {
94-
self.queued_lines.push_back(self.current_line.clone());
95-
self.current_line.left_line_number += 1;
96-
self.current_line.right_line_number += 1;
97-
self.current_line.reset_line();
88+
// TODO: Remove this check once https://github.com/rust-lang/rust/issues/89716
89+
// has been fixed and released for long enough.
90+
if !text.is_empty() {
91+
let lines = text.split_inclusive(|b| *b == b'\n');
92+
for line in lines {
93+
self.current_line.has_left_content = true;
94+
self.current_line.has_right_content = true;
95+
self.current_line.hunks.push(DiffHunk::Matching(line));
96+
if line.ends_with(b"\n") {
97+
self.queued_lines.push_back(self.current_line.clone());
98+
self.current_line.left_line_number += 1;
99+
self.current_line.right_line_number += 1;
100+
self.current_line.reset_line();
101+
}
98102
}
99103
}
100104
}
101105
diff::DiffHunk::Different(contents) => {
102106
let left = contents[0];
103107
let right = contents[1];
104-
let left_lines = left.split_inclusive(|b| *b == b'\n');
105-
for left_line in left_lines {
106-
self.current_line.has_left_content = true;
107-
self.current_line
108-
.hunks
109-
.push(DiffHunk::Different(vec![left_line, b""]));
110-
if left_line.ends_with(b"\n") {
111-
self.queued_lines.push_back(self.current_line.clone());
112-
self.current_line.left_line_number += 1;
113-
self.current_line.reset_line();
108+
// TODO: Remove this check once https://github.com/rust-lang/rust/issues/89716
109+
// has been fixed and released for long enough.
110+
if !left.is_empty() {
111+
let left_lines = left.split_inclusive(|b| *b == b'\n');
112+
for left_line in left_lines {
113+
self.current_line.has_left_content = true;
114+
self.current_line
115+
.hunks
116+
.push(DiffHunk::Different(vec![left_line, b""]));
117+
if left_line.ends_with(b"\n") {
118+
self.queued_lines.push_back(self.current_line.clone());
119+
self.current_line.left_line_number += 1;
120+
self.current_line.reset_line();
121+
}
114122
}
115123
}
116-
let right_lines = right.split_inclusive(|b| *b == b'\n');
117-
for right_line in right_lines {
118-
self.current_line.has_right_content = true;
119-
self.current_line
120-
.hunks
121-
.push(DiffHunk::Different(vec![b"", right_line]));
122-
if right_line.ends_with(b"\n") {
123-
self.queued_lines.push_back(self.current_line.clone());
124-
self.current_line.right_line_number += 1;
125-
self.current_line.reset_line();
124+
// TODO: Remove this check once https://github.com/rust-lang/rust/issues/89716
125+
// has been fixed and released for long enough.
126+
if !right.is_empty() {
127+
let right_lines = right.split_inclusive(|b| *b == b'\n');
128+
for right_line in right_lines {
129+
self.current_line.has_right_content = true;
130+
self.current_line
131+
.hunks
132+
.push(DiffHunk::Different(vec![b"", right_line]));
133+
if right_line.ends_with(b"\n") {
134+
self.queued_lines.push_back(self.current_line.clone());
135+
self.current_line.right_line_number += 1;
136+
self.current_line.reset_line();
137+
}
126138
}
127139
}
128140
}

lib/tests/test_conflicts.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ line 5
234234

235235
let mut result: Vec<u8> = vec![];
236236
materialize_conflict(repo.store(), &path, &conflict, &mut result);
237-
// TODO: There's an extra "+" after "-line 3".
238237
assert_eq!(
239238
String::from_utf8(result).unwrap().as_str(),
240239
"line 1
@@ -243,7 +242,7 @@ line 2
243242
-------
244243
+++++++
245244
-line 3
246-
++++++++
245+
+++++++
247246
right
248247
>>>>>>>
249248
line 4

0 commit comments

Comments
 (0)