Skip to content

Commit

Permalink
Fix crash on empty conflict sections
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Feb 4, 2024
1 parent 3fa9e68 commit 7722f80
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/conflicts_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ impl ConflictsHighlighter {
/// vs C1 or vs C2.
/// * In section C2, we highlight additions compared to base
fn render_diff3(&self, thread_pool: &ThreadPool) -> StringFuture {
assert!(self.base.is_some());
let c1_header = self.c1_header.clone();
let c1 = self.c1.clone();
let base_header = self.base_header.clone();
Expand All @@ -183,12 +184,16 @@ impl ConflictsHighlighter {

return StringFuture::from_function(
move || {
let base_or_newline = if base.is_empty() { "\n" } else { &base };

let c1_or_newline = if c1.is_empty() { "\n" } else { &c1 };
let (base_vs_c1_tokens, c1_tokens, _, _) =
refiner::to_highlighted_tokens(&base, &c1);
refiner::to_highlighted_tokens(base_or_newline, c1_or_newline);
let highlighted_c1 = token_collector::render(&LINE_STYLE_NEW, "", &c1_tokens);

let c2_or_newline = if c2.is_empty() { "\n" } else { &c2 };
let (base_vs_c2_tokens, c2_tokens, _, _) =
refiner::to_highlighted_tokens(&base, &c2);
refiner::to_highlighted_tokens(base_or_newline, c2_or_newline);
let highlighted_c2 = token_collector::render(&LINE_STYLE_NEW, "", &c2_tokens);

assert_eq!(base_vs_c1_tokens.len(), base_vs_c2_tokens.len());
Expand All @@ -208,15 +213,21 @@ impl ConflictsHighlighter {
let mut rendered = String::new();
rendered.push_str(&c1_header);
rendered.push('\n');
rendered.push_str(&highlighted_c1);
if !c1.is_empty() {
rendered.push_str(&highlighted_c1);
}

rendered.push_str(&base_header);
rendered.push('\n');
rendered.push_str(&highlighted_base);
if !base.is_empty() {
rendered.push_str(&highlighted_base);
}

rendered.push_str(&c2_header);
rendered.push('\n');
rendered.push_str(&highlighted_c2);
if !c2.is_empty() {
rendered.push_str(&highlighted_c2);
}

rendered.push_str(&footer);
rendered.push('\n');
Expand Down
8 changes: 8 additions & 0 deletions testdata/conflict-markers-diff3-removed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This is an example of git conflict markers.

<<<<<<< HEAD
This line is changed on the main branch.
||||||| 07ffb9b
This line is from the initial commit on the main branch.
=======
>>>>>>> branch
8 changes: 8 additions & 0 deletions testdata/conflict-markers-diff3-removed.txt.riff-output
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This is an example of git conflict markers.

<<<<<<< HEAD
This line is changed on the main branch.
||||||| 07ffb9b
This line is from the initial commit on the main branch.
=======
>>>>>>> branch

0 comments on commit 7722f80

Please sign in to comment.