terezka / elm-diff / Diff

This is a library to assist the parsing of git diff output and subsequent display.

Parser

fromString : String -> Result Error (List Diff)

Parse the full output of git diff.

Data model


type Diff
    = New NewFile
    | Deleted DeletedFile
    | Edited EditedFile
    | Renamed RenamedFile

A diff for a single file. There may be several of these in a single git diff output.

Note: This does not include all possible modes, like changing permissions of file, symlinks etc. If you need that though, you're welcome to add it.


type alias NewFile =
{ path : Path
, commit : Hash
, chunks : List Chunk 
}


type alias DeletedFile =
{ path : Path
, commit : Hash
, chunks : List Chunk 
}


type alias EditedFile =
{ path : Path
, commit1 : Hash
, commit2 : Hash
, chunks : List Chunk 
}


type alias RenamedFile =
{ from : Path
, to : Path
, similarity : Basics.Int
, commit1 : Hash
, commit2 : Hash
, chunks : List Chunk 
}


type alias Chunk =
{ lineNum1 : Basics.Int
, lineCount1 : Basics.Int
, lineNum2 : Basics.Int
, lineCount2 : Basics.Int
, changes : List ( Action
, List Word ) 
}


type Word
    = Keyword String
    | Constant String
    | CustomType String
    | Definiton String
    | BuildIn String
    | Comment String
    | Other String

The diff is parsed as Elm code. This incidentally also works fairly well for JSON. For any other file extensions other than .elm and .json, the line of code is just left in its entirety in the Other type.

Since we're working with diffs, this is meant to parse incomplete code. There are thus certain limits for highlighting as we don't have access to the full code. E.g. it is difficult (sometimes impossible) to highlight multiline comments / quotes correctly. This has thus been omitted.


type Action
    = None
    | Added
    | Removed


type alias Hash =
String


type alias Path =
String


type alias Error =
{ subject : String
, message : String 
}

The parser error. The subject is what has been parsed, and the message is what went wrong.

You shouldn't run into this if you're parsing correct diffs, but if there are any bugs in the parser, this output is helpful.

You can display it using Ui.Diff.viewError.

Helpers

toPath : Diff -> Path

Helper to get the path for the diff. In the case of renames, this is the latest path.

toChunks : Diff -> List Chunk

Helper to get the diff chunks.