Skip to content

Diffs containing target code equal to source code are not represented as in Git #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ArturMarcinkowski opened this issue Mar 10, 2025 · 7 comments

Comments

@ArturMarcinkowski
Copy link

ArturMarcinkowski commented Mar 10, 2025

Hi,
I recently encountered a bug where the way diffs are calculated differs from GitLab’s approach.

I've created an example to illustrate the issue. There are two lists to simulate lines before and after the changes.

There are two identical blocks of lines, so the changes can be interpreted in two different ways:

Git's approach: The first six lines are replaced with two new lines, while the last two lines remain unchanged.
DiffUtils' approach: The first two lines are replaced with two new lines, the next two lines remain unchanged, and the last four lines are removed.
While both approaches lead to the same final result, DiffUtils returns CHANGE, EQUAL, and DELETE blocks, whereas Git only uses CHANGE and EQUAL.

List<String> beforeChanges = List.of(
                "differentLine1",
                "differentLine2",
                "sameLine1",
                "sameLine2",
                "differentLine3",
                "differentLine4",
                "sameLine1",
                "sameLine2"
);
List<String> afterChanges = List.of(
                "newDifferentLine1",
                "newDifferentLine2",
                "sameLine1",
                "sameLine2"
);
var deltas = DiffUtils.diff(beforeChanges, afterChanges, true).getDeltas();

I'm attaching screens how this operation looks in github and how looks output from DiffUtils

Image

Image

Can you please fix the way the Diffs are calculated to match Git?

@ArturMarcinkowski ArturMarcinkowski changed the title Diffs not generate properly for code with multiple same parts Diffs do not generate properly for code with equal parts Mar 11, 2025
@ArturMarcinkowski ArturMarcinkowski changed the title Diffs do not generate properly for code with equal parts Diffs containing target code equal to source code are not represented as in Git Mar 11, 2025
@MadnessIRL
Copy link

bump

@MadnessIRL
Copy link

bumping once again because this is behaving differently from how git displays changes and will drive me insane

@PanOscar
Copy link

PanOscar commented May 14, 2025

@wumpz Please fix

@Sliderro
Copy link

Can confirm this issue as well.

@PatrykLeczycki
Copy link

Also confirm

@dpel94
Copy link

dpel94 commented May 14, 2025

It would be very useful to fix this.

@wumpz
Copy link
Collaborator

wumpz commented May 24, 2025

Is there anything wrong with the way diff utils calculate this? diff utils and git using different algorithms to calculate it.

We using Myers and git uses some histogram algorithm.

There is a subproject java-diff-utils-jgit which uses JGit to prerform all operations and provider java-diff-utils API. Look into the tests to see how to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants