imara-diff icon indicating copy to clipboard operation
imara-diff copied to clipboard

Unexpected new line in a unidiff hunk

Open krlvi opened this issue 9 months ago • 2 comments

Hello! Firstly, let me say thank you for creating this library - it is something that I get to enjoy using indirectly through gitoxide

I think we've run into a condition which might generate hunks incorrectly, or at the very least, differently from how git diff produces them.

The problematic change takes the following form:

Before

a

foo

b

After

a

b

The unidiff I'd expect is

@@ -1,5 +1,3 @@
 a

-foo
-
 b

However the unidiff I get from imara-diff looks like

@@ -1,5 +1,3 @@
 a
-
-foo

 b

I need to clarify that this only happens when there are other changes preceding it - if there are no other changes, the unidiff looks as expected (aligned with git's diff output).

In order to make debugging easier, I created a test with the scenario that allowed me to reproduce this - https://github.com/krlvi/imara-diff/blob/f259243ba3873189744b81b7a8d7e0dde58b0700/src/tests.rs#L159

Attaching below a screenshot from the test output (I allowed myself to add pretty_assertions to my branch for better visibility) Image

Thanks

krlvi avatar Mar 05 '25 17:03 krlvi

HI @krlvi, I think this may be an example for the slider problem, i.e. there are multiple correct solutions and it's a matter of tuning which one is chosen.

imara-diff is very raw in that it has received no (or very little) of such tuning and is expected to have different results.

I'd hope that as imara-diff rises in popularity that there will be time to tune it (a process I have no idea about just yet 😅).

Byron avatar Mar 11 '25 08:03 Byron

I actually stumbled upon this issue today while using the gix crate. The diff computed by the imara-diff crate sometimes differs from the diff computed by libgit2 and the git CLI.

blinxen avatar Mar 21 '25 14:03 blinxen