GitDiff v0.6.3 GitDiff View Source

A simple implementation for taking the output from 'git diff' and transforming it into Elixir structs.


The package can be installed by adding git_diff to your list of dependencies in mix.exs:

def deps do
    {:git_diff, "~> 0.6.1"}



    chunks: [
        from_num_lines: "42",
        from_start_line: "42",
        header: "@@ -481,23 +483,24 @@ class Cursor extends Model {"
        context: "class Cursor extends Model {", # will be "" if there is no context
        lines: [
            from_line_number: 481,
            text: "   {",
            to_line_number: 483,
            type: :context # will be one of :context, :add, :remove
        to_num_lines: "42",
        to_start_line: "42"
  from: "src/cursor.js",
  headers: %{"index" => {"10bdef8", "181eeb9", "100644"}},
  to: "src/cursor.js"},

The above output is heavily truncated for illustration, but it should give enough of an idea of what to expect. The code, while naive, is less than 100 lines of actual code and all takes place in the GitDiff module. Emulate the tests in a an interactive shell for quick viewing of the output.


Haven't done much benchmarking, but up to around a 5k (I just stopped trying there) line diff the performance was linear and took a whopping 35ms per call on the test VM. For a more reasonably sized ~150 line diff it clocked in at around 340 microseconds.

Link to this section Summary


Parse the output from a 'git diff' command.

Parse the output from a 'git diff' command.

Link to this section Functions

Link to this function

parse_patch(git_diff, opts \\ [])

View Source
parse_patch(String.t(), Keyword.t()) ::
   [%GitDiff.Patch{chunks: term(), from: term(), headers: term(), to: term()}]}
  | {:error, :unrecognized_format}

Parse the output from a 'git diff' command.

Returns {:ok, [%GitDiff.Patch{}]} for success, {:error, :unrecognized_format} otherwise. See GitDiff.Patch.

Link to this function

stream_patch(stream, opts \\ [])

View Source
stream_patch(Enum.t(), Keyword.t()) :: Enum.t()

Parse the output from a 'git diff' command.

Like parse_patch/1 but takes an Enumerable of lines and returns a stream of {:ok, %GitDiff.Patch{}} for successfully parsed patches or {:error, _} if the patch failed to parse.