Giter Site home page Giter Site logo

difflib's Introduction

This project contains code that will do Diff generation, comparing two collections of any type and outputting the differences between the two, including an attempt to align up differences, specifically in text files.

The library also comes with 3-way merge functionality, allowing you to take two collections that both started as a 3rd common base but has been modified, and then merge the modifications into a final output. This is the type of 3-way merge that version control systems do.

Details:

  • Language: C# 7.0
  • Runtime: .NET Standard 1.0

Repository and project location: GitHub
Maintainer: Lasse V. Karlsen


GPG Signing

If you want to import my GPG key to verify my commits you can do it using one of these commands:

git cat-file blob pubkey | gpg --import
git cat-file blob pubkey | gpg2 --import

difflib's People

Contributors

ha07z7xe avatar lassevk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

difflib's Issues

NuGet package should receive an overhaul

Must reverify that the NuGet package is still correct. A lot has happened with NuGet over the past years so need to ensure there's no mold stuck in the file.

PR/Merge back from SharpDiff?

Hi @lassevk
Two years ago, I did a fork of your codeplex project to github to add support for diff3 available at SharpDiff.
I thought that I contacted you at that time, but I haven't found any emails... maybe I completely forgot! Sorry for that.

I was a bit in a hurry to require diff3 lib, so I tried to follow the "A formal investigation of diff3" and quickly made a first attempt. I'm not particularly satisfied with the current implem, we are using it to perform some merge of complex graph objects and it may still have bugs (edge cases)
Anyway, if you are interested, I could make a PR, or even just discuss about it.
Let me know.

Support .NET Standard

The assembly should support .NET Standard instead of the .NET portable versions that it now supports, to make it easier to handle cross platform support going forward.

Since the class requires no platform support for anything the initial goal is to support .NET Standard 1.0 and up.

Screenshot and demo

This samples show the basic power of this DiffLib and is provided as VB.NET:

image

Option Explicit On
Option Strict On

Imports DiffLib

Module Module1

    Sub Main()
        Const text1 As String = "This is a test of the diff implementation, with some text that is deleted."
        Const text2 As String = "This is another test of the same implementation, with some more text."
        Diffs.DumpDiffToConsole(text1, text2)
        Console.WriteLine()
    End Sub
End Module

Public Class Diffs

    Public Shared Function DumpDiffAsHtml(text1 As String, text2 As String) As String
        Dim sections As IEnumerable(Of DiffSection) = Diff.CalculateSections(Of Char)(text1.ToCharArray, text2.ToCharArray)
        Return DumpDiffAsHtml(text1, text2, sections)
    End Function

    Private Shared Function DumpDiffAsHtml(text1 As String, text2 As String, sections As IEnumerable(Of DiffSection)) As String
        Dim html = New System.Text.StringBuilder()
        Dim i1 = 0
        Dim i2 = 0
        For Each section In sections
            If section.IsMatch Then
                html.Append(text1.Substring(i1, section.LengthInCollection1))
            Else
                html.Append("<span style='background-color: #ffcccc; text-decoration: line-through;'>" + text1.Substring(i1, section.LengthInCollection1) + "</span>")
                html.Append("<span style='background-color: #ccffcc;'>" + text2.Substring(i2, section.LengthInCollection2) + "</span>")
            End If

            i1 += section.LengthInCollection1
            i2 += section.LengthInCollection2
        Next
        Return html.ToString()
    End Function

    Public Shared Sub DumpDiffToConsole(text1 As String, text2 As String)
        Dim sections As IEnumerable(Of DiffSection) = Diff.CalculateSections(Of Char)(text1.ToCharArray, text2.ToCharArray)
        DumpDiffToConsole(text1, text2, sections)
    End Sub

    Private Shared Sub DumpDiffToConsole(text1 As String, text2 As String, sections As IEnumerable(Of DiffSection))
        Dim DefaultColor As ConsoleColor = Console.ForegroundColor
        Dim html = New System.Text.StringBuilder()
        Dim i1 = 0
        Dim i2 = 0
        For Each section In sections
            If section.IsMatch Then
                Console.ForegroundColor = DefaultColor
                Console.Write(text1.Substring(i1, section.LengthInCollection1))
            Else
                Console.ForegroundColor = ConsoleColor.Red
                Console.Write(text1.Substring(i1, section.LengthInCollection1))
                Console.ForegroundColor = ConsoleColor.Green
                Console.Write(text2.Substring(i2, section.LengthInCollection2))
            End If

            i1 += section.LengthInCollection1
            i2 += section.LengthInCollection2
        Next
        Console.ForegroundColor = DefaultColor
    End Sub

End Class

An alternative for HTML output is following method:

        Const text1 As String = "This is a test of the diff implementation, with some text that is deleted."
        Const text2 As String = "This is another test of the same implementation, with some more text."
        Dim Html As String = Diffs.DumpDiffAsHtml(text1, text2)

which generated following output

This is a<span style="background-color: #ffcccc; text-decoration: line-through;"></span><span style="background-color: #ccffcc;">nother</span> test of the <span style="background-color: #ffcccc; text-decoration: line-through;">diff</span><span style="background-color: #ccffcc;">same</span> implementation, with some <span style="background-color: #ffcccc; text-decoration: line-through;"></span><span style="background-color: #ccffcc;">more </span>text<span style="background-color: #ffcccc; text-decoration: line-through;"> that is deleted</span><span style="background-color: #ccffcc;"></span>.

image

Build script depends on Mercurial

The repository used to be a Mercurial repository, the finalbuilder build script depends on Mercurial commands to work out some things, need to fix this.

No TakeRight*MergeConflictResolver types

For the two left-centric merge conflict resolvers:

  • TakeLeftThenRightMergeConflictResolver
  • TakeLeftThenRightIfRightDiffersFromLeft

should have counterparts that point the other way.

Potentially "incorrect" merge (diff?) behavior related to similar code

I tried to merge the following scenario:

base:   {}
left:   {a}
right:  {} {b}

This is what I want to happen:

base:   {}
left:   {a}
right:  {} {b}
merged: {a} {b}
         + ++++

Instead a merge conflict happens because the following two blocks ends up being connected and compared:

 +
{a}
{} {b}
 ++++

The expected outcome would merge cleanly, whereas the second will generate a merge conflict.

I need to see if I can make the diff/merge algorithm less greedy in this case.

Need to raise code coverage to near 100% to ensure no odd cornercases

The new implementation has exposed a few weaknesses in the old implementation, in addition to the inconsistent API.

To ensure the new implementation has left no cornercases behind a more extensive test suite must be written.

The goal is to come up with scenarios with differences between two collections and verify that the produced diff and alignments are according to expectations, as well as raise the code coverage of all the code up to close to 100%.

Possible to specifically list .NET Standard/Core assemblies needed?

Before 2017.7.26.1241), adding a reference to the .NET Standard 1.0 version of DiffLib also added a ton of references to other standard shim assemblies.

Almost none of those provide code that DiffLib uses.

I need to investigate if:

  • This is still an issue that needs to be solved (if the new version, which includes .NET Framework versions, now only uses the .NET Standard version for .NET Standard/Core projects, where these assemblies are already available, then this is no longer an issue)
  • If it is, what can I do about it? Can I find out which specific .NET Standard assemblies/packages I require and take a dependency only on those?

Verify how diff code handles null-elements in the collections

The diff implementation should handle null-elements as though they were real objects, having a hash code of 0 and only comparing equal to either other null-elements in the other collection, or to objects in that other collection that says they are equal to null.

Unify API

The API is divided into two distinct sections now, the aligned diffs, and the unaligned diffs. These have different method signatures and different result types.

For v2, a complete overhaul of the entire API is planned, unifying the API into one common API that handles both cases, depending on behaviors or parameterization to signal whether an aligned diff is requested or not.

Nuget package contains incorrect link to license file

The link in the Nuget package points here:

https://github.com/lassevk/DiffLib/blob/master/LICENSE

The correct link is this:

https://github.com/lassevk/DiffLib/blob/master/LICENSE.md

The link was placed into the Nuget file before the license file was renamed to have a .md extension, but the Nuget package was not subsequently changed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.