Skip to content
Merged
Show file tree
Hide file tree
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
82 changes: 77 additions & 5 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ fn make_diff(
let mut line_number_expected = 1;
let mut line_number_actual = 1;
let mut context_queue: VecDeque<&[u8]> = VecDeque::with_capacity(context_size);
let mut lines_since_mismatch = context_size + 1;
// Like GNU diff, keep changes in one hunk while at most twice the context
// size of unchanged lines separates them.
let max_gap = 2 * context_size;
let mut lines_since_mismatch = max_gap + 1;
let mut results = Vec::new();
let mut mismatch = Mismatch::new(0, 0);

Expand All @@ -80,7 +83,7 @@ fn make_diff(
for result in diff::slice(&expected_lines, &actual_lines) {
match result {
diff::Result::Left(str) => {
if lines_since_mismatch > context_size && lines_since_mismatch > 0 {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
line_number_expected - context_queue.len(),
Expand All @@ -102,7 +105,7 @@ fn make_diff(
lines_since_mismatch = 0;
}
diff::Result::Right(str) => {
if lines_since_mismatch > context_size && lines_since_mismatch > 0 {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
line_number_expected - context_queue.len(),
Expand Down Expand Up @@ -152,7 +155,7 @@ fn make_diff(
}
lines_since_mismatch = 0;
} else if line_number_actual > actual_lines_count {
if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
line_number_expected - context_queue.len(),
Expand All @@ -168,7 +171,7 @@ fn make_diff(
mismatch.actual_missing_nl = true;
lines_since_mismatch = 0;
} else if line_number_expected > expected_lines_count {
if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
line_number_expected - context_queue.len(),
Expand Down Expand Up @@ -800,4 +803,73 @@ mod tests {
);
assert!(nodiff_brief.is_empty());
}

#[test]
fn test_hunk_merging() {
use crate::assert_diff_eq;

let params = Params {
from: "foo".into(),
to: "bar".into(),
context_count: 1,
..Default::default()
};

// Changes separated by up to twice the context size share one hunk.
let from = ["a", "b", "c", "d", "e", "f", ""].join("\n");
let to = ["a", "B", "c", "d", "E", "f", ""].join("\n");
let expected = [
"*** foo\tTIMESTAMP",
"--- bar\tTIMESTAMP",
"***************",
"*** 1,6 ****",
" a",
"! b",
" c",
" d",
"! e",
" f",
"--- 1,6 ----",
" a",
"! B",
" c",
" d",
"! E",
" f",
"",
]
.join("\n");
let output = diff(from.as_bytes(), to.as_bytes(), &params);
assert_diff_eq!(output, expected);

// One more unchanged line between them splits the hunk.
let from = ["a", "b", "c", "d", "e", "f", "g", ""].join("\n");
let to = ["a", "B", "c", "d", "e", "F", "g", ""].join("\n");
let expected = [
"*** foo\tTIMESTAMP",
"--- bar\tTIMESTAMP",
"***************",
"*** 1,3 ****",
" a",
"! b",
" c",
"--- 1,3 ----",
" a",
"! B",
" c",
"***************",
"*** 5,7 ****",
" e",
"! f",
" g",
"--- 5,7 ----",
" e",
"! F",
" g",
"",
]
.join("\n");
let output = diff(from.as_bytes(), to.as_bytes(), &params);
assert_diff_eq!(output, expected);
}
}
68 changes: 63 additions & 5 deletions src/unified_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ fn make_diff(
let mut line_number_expected = 1;
let mut line_number_actual = 1;
let mut context_queue: VecDeque<&[u8]> = VecDeque::with_capacity(context_size);
let mut lines_since_mismatch = context_size + 1;
// Like GNU diff, keep changes in one hunk while at most twice the context
// size of unchanged lines separates them.
let max_gap = 2 * context_size;
let mut lines_since_mismatch = max_gap + 1;
let mut results = Vec::new();
let mut mismatch = Mismatch::new(0, 0);

Expand All @@ -68,7 +71,7 @@ fn make_diff(
for result in diff::slice(&expected_lines, &actual_lines) {
match result {
diff::Result::Left(str) => {
if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
line_number_expected - context_queue.len() as u32,
Expand Down Expand Up @@ -105,7 +108,7 @@ fn make_diff(
lines_since_mismatch = 0;
}
diff::Result::Right(str) => {
if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
line_number_expected - context_queue.len() as u32,
Expand Down Expand Up @@ -142,7 +145,7 @@ fn make_diff(
}
lines_since_mismatch = 0;
} else if line_number_actual > actual_lines_count {
if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
line_number_expected - context_queue.len() as u32,
Expand All @@ -158,7 +161,7 @@ fn make_diff(
mismatch.lines.push(DiffLine::MissingNL);
lines_since_mismatch = 0;
} else if line_number_expected > expected_lines_count {
if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
line_number_expected - context_queue.len() as u32,
Expand Down Expand Up @@ -951,4 +954,59 @@ mod tests {
);
assert!(nodiff_brief.is_empty());
}

#[test]
fn test_hunk_merging() {
use crate::assert_diff_eq;

let params = Params {
from: "foo".into(),
to: "bar".into(),
context_count: 1,
..Default::default()
};

// Changes separated by up to twice the context size share one hunk.
let from = ["a", "b", "c", "d", "e", "f", ""].join("\n");
let to = ["a", "B", "c", "d", "E", "f", ""].join("\n");
let expected = [
"--- foo\tTIMESTAMP",
"+++ bar\tTIMESTAMP",
"@@ -1,6 +1,6 @@",
" a",
"-b",
"+B",
" c",
" d",
"-e",
"+E",
" f",
"",
]
.join("\n");
let output = diff(from.as_bytes(), to.as_bytes(), &params);
assert_diff_eq!(output, expected);

// One more unchanged line between them splits the hunk.
let from = ["a", "b", "c", "d", "e", "f", "g", ""].join("\n");
let to = ["a", "B", "c", "d", "e", "F", "g", ""].join("\n");
let expected = [
"--- foo\tTIMESTAMP",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please also add the case from the issue (an insertion, so different line counts on both sides)?
in a different PR

"+++ bar\tTIMESTAMP",
"@@ -1,3 +1,3 @@",
" a",
"-b",
"+B",
" c",
"@@ -5,3 +5,3 @@",
" e",
"-f",
"+F",
" g",
"",
]
.join("\n");
let output = diff(from.as_bytes(), to.as_bytes(), &params);
assert_diff_eq!(output, expected);
}
}
Loading