Skip to content

ldiff/hunk ranges are still incorrect #107

Description

@Annih

Impacted version

bundle exec ldiff --version
ldiff 1.5.1

Context

While some attempts to fix the ranges already occured around versions 1.4.
The ranges are still incorrect, and the tests created for #60 are actually incorrect (not helping to fix the behavior).
It seems the fix mostly relies on the last parameter that is used to correct the indexes of the last hunk but this means user has to pass this to get proper ranges. Also it would theoretically means (not verified) that only the last hunk is properly fixed.
As a user of the lib, I would like the range to be properly computed in most cases, without need of the last parameter.

IMHO: the last parameter is mostly (only?) useful to determine whether we should or not output No newline at end of file.

Repro

Let's create 2 files old & new as specified in the non-regression test of #60.

cat old
{
  "name": "x",
  "description": "hi"
}

and

cat new
{
  "name": "x",
  "description": "lo"
}

Now let's compare them as described in the test:

old_data = ::File.read('old').lines.map(&:chomp)
new_data = ::File.read('new').lines.map(&:chomp)
diffs = ::Diff::LCS.diff(old_data, new_data)
hunk = ::Diff::LCS::Hunk.new(old_data, new_data, diff.first, 3, 0)
puts hunk.diff(:unified)

Current result

Here is what we get:

@@ -1,5 +1,5 @@
 {
   "name": "x",
-  "description": "hi"
+  "description": "lo"
 }

Expected result

Here the old & new lengths are 5 while it should be 4:

@@ -1,4 +1,4 @@
 {
   "name": "x",
-  "description": "hi"
+  "description": "lo"
 }

More

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