GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — diff: stop the trailing context at -U/-C lines when both files lack a… · uutils/diffutils@bb44ff5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • context_diff.rs
    • unified_diff.rs
  • tests/by-util
    • test_diff.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/context_diff.rs‎

Copy file name to clipboardExpand all lines: src/context_diff.rs
+7-12Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,14 @@ fn make_diff(
181181
&& (line_number_expected > expected_lines_count)
182182
{
183183
ChangeBlock::close(&mut block, &mut mismatch);
184-
if context_queue.len() < context_size {
185-
while let Some(line) = context_queue.pop_front() {
186-
mismatch.expected.push(DiffLine::Context(line.to_vec()));
187-
mismatch.actual.push(DiffLine::Context(line.to_vec()));
188-
}
189-
if lines_since_mismatch < context_size {
190-
mismatch.expected.push(DiffLine::Context(str.to_vec()));
191-
mismatch.actual.push(DiffLine::Context(str.to_vec()));
192-
mismatch.expected_missing_nl = true;
193-
mismatch.actual_missing_nl = true;
194-
}
184+
// this is the last line: lines still in the queue are past
185+
// the trailing context and there is no next hunk to lead into
186+
if lines_since_mismatch < context_size {
187+
mismatch.expected.push(DiffLine::Context(str.to_vec()));
188+
mismatch.actual.push(DiffLine::Context(str.to_vec()));
189+
mismatch.expected_missing_nl = true;
190+
mismatch.actual_missing_nl = true;
195191
}
196-
lines_since_mismatch = 0;
197192
} else if line_number_actual > actual_lines_count {
198193
if lines_since_mismatch > max_gap {
199194
results.push(mismatch);
Collapse file

‎src/unified_diff.rs‎

Copy file name to clipboardExpand all lines: src/unified_diff.rs
+5-10Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,12 @@ fn make_diff(
133133
if (line_number_actual > actual_lines_count)
134134
&& (line_number_expected > expected_lines_count)
135135
{
136-
if context_queue.len() < context_size {
137-
while let Some(line) = context_queue.pop_front() {
138-
debug_assert!(mismatch.lines.last() != Some(&DiffLine::MissingNL));
139-
mismatch.lines.push(DiffLine::Context(line.to_vec()));
140-
}
141-
if lines_since_mismatch < context_size {
142-
mismatch.lines.push(DiffLine::Context(str.to_vec()));
143-
mismatch.lines.push(DiffLine::MissingNL);
144-
}
136+
// this is the last line: lines still in the queue are past
137+
// the trailing context and there is no next hunk to lead into
138+
if lines_since_mismatch < context_size {
139+
mismatch.lines.push(DiffLine::Context(str.to_vec()));
140+
mismatch.lines.push(DiffLine::MissingNL);
145141
}
146-
lines_since_mismatch = 0;
147142
} else if line_number_actual > actual_lines_count {
148143
if lines_since_mismatch > max_gap {
149144
results.push(mismatch);
Collapse file

‎tests/by-util/test_diff.rs‎

Copy file name to clipboardExpand all lines: tests/by-util/test_diff.rs
+48Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,3 +1249,51 @@ fn extra_operand_without_double_dash() -> Result<(), Box<dyn std::error::Error>>
12491249
.stderr("diff: extra operand 'extra'\ndiff: Try 'diff --help' for more information.\n");
12501250
Ok(())
12511251
}
1252+
1253+
#[test]
1254+
fn trailing_context_without_final_newlines() -> Result<(), Box<dyn std::error::Error>> {
1255+
let mut file1 = NamedTempFile::new()?;
1256+
file1.write_all(b"1\n2\n3\n4\n5\n6\n7\n8")?;
1257+
let mut file2 = NamedTempFile::new()?;
1258+
file2.write_all(b"1\nX\n3\n4\n5\n6\n7\n8")?;
1259+
1260+
for (format, hunk) in [
1261+
("-u", "@@ -1,5 +1,5 @@\n 1\n-2\n+X\n 3\n 4\n 5\n"),
1262+
(
1263+
"-c",
1264+
"*** 1,5 ****\n 1\n! 2\n 3\n 4\n 5\n--- 1,5 ----\n 1\n! X\n 3\n 4\n 5\n",
1265+
),
1266+
] {
1267+
let mut cmd = cargo_bin_cmd!("diffutils");
1268+
cmd.arg("diff")
1269+
.arg(format)
1270+
.arg(file1.path())
1271+
.arg(file2.path());
1272+
cmd.assert()
1273+
.code(predicate::eq(1))
1274+
.stdout(predicate::str::ends_with(hunk));
1275+
}
1276+
1277+
// the last line is still shown when it is within the trailing context
1278+
let mut cmd = cargo_bin_cmd!("diffutils");
1279+
cmd.arg("diff")
1280+
.arg("-U5")
1281+
.arg(file1.path())
1282+
.arg(file2.path());
1283+
cmd.assert()
1284+
.code(predicate::eq(1))
1285+
.stdout(predicate::str::ends_with(
1286+
"@@ -1,7 +1,7 @@\n 1\n-2\n+X\n 3\n 4\n 5\n 6\n 7\n",
1287+
));
1288+
let mut cmd = cargo_bin_cmd!("diffutils");
1289+
cmd.arg("diff")
1290+
.arg("-U6")
1291+
.arg(file1.path())
1292+
.arg(file2.path());
1293+
cmd.assert()
1294+
.code(predicate::eq(1))
1295+
.stdout(predicate::str::ends_with(
1296+
"@@ -1,8 +1,8 @@\n 1\n-2\n+X\n 3\n 4\n 5\n 6\n 7\n 8\n\\ No newline at end of file\n",
1297+
));
1298+
Ok(())
1299+
}

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.