Skip to content

ldiff does not behave well with empty files #100

Description

@Annih

Impacted version

bundle exec ldiff --version
ldiff 1.5.1

Context

When performing a diff between a non-empty file and an empty file (could it be /dev/null), the ldiff output is very different than gnu diff and I would say ... wrong!

Repro

Let's take a file with 4 lines name four_lines:

cat -A four_lines
first$
second$
third$
last$

And now let's compare it with an empty file using unified format: bundle exec ldiff -u /dev/null four_lines

Current result

Here is what we get

--- /dev/null   2025-01-27 23:29:19.000000000 +0100
+++ four_lines  2025-01-28 08:43:54.000000000 +0100
@@ -1,3 +1,7 @@
\ No newline at end of file
+first
+second
+third
+fourth

Expected result

I would expect the same result as GNU diff:

--- /dev/null   2025-01-27 23:29:19.936649933 +0100
+++ four_lines  2025-01-28 08:43:54.620170046 +0100
@@ -0,0 +1,4 @@
+first
+second
+third
+fourth

The main issues I see ordered by ascending criticality:

  • there should be no No newline at end of file notice
  • there seems to be an off by 1 thing in the range, but I think the patch is still valid (need to confirm)
  • the number of "new lines" is definitely not 7!!!

More

The example I used here is 0 vs 4 lines in unified format, but:

  • actually all format seems impacted
  • any non-zero value would work 0 vs 1, 0 vs 2, 0 vs 1000
  • it could also be inverested 1 vs 0, 2 vs 0 & 1000 vs 0 (the result is less scary though)

I'll submit soon a "fake PR" with tests to demonstrate some failures.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions