Git.Commands.Diff (git v0.4.0)

Copy Markdown View Source

Implements the Git.Command behaviour for git diff.

Supports working-tree diffs, staged (cached) diffs, stat-only output, comparing against a specific ref, and limiting to a path.

Summary

Functions

Returns the argument list for git diff.

Parses the output of git diff.

Types

t()

@type t() :: %Git.Commands.Diff{
  name_only: boolean(),
  name_status: boolean(),
  path: String.t() | nil,
  ref: String.t() | nil,
  ref_end: String.t() | nil,
  staged: boolean(),
  stat: boolean()
}

Functions

args(command)

@spec args(t()) :: [String.t()]

Returns the argument list for git diff.

Options:

  • :staged — adds --cached to show staged changes
  • :stat — adds --stat for file-level summary instead of full patch
  • :name_only — adds --name-only for listing just file paths
  • :name_status — adds --name-status for file paths with status letters
  • :ref — adds a ref to compare against (e.g., "HEAD~1")
  • :ref_end — when set with :ref, compares ref ref_end (two-ref diff)
  • :path — adds -- <path> to limit the diff

Examples

iex> Git.Commands.Diff.args(%Git.Commands.Diff{})
["diff"]

iex> Git.Commands.Diff.args(%Git.Commands.Diff{staged: true, stat: true})
["diff", "--cached", "--stat"]

iex> Git.Commands.Diff.args(%Git.Commands.Diff{ref: "HEAD~1", path: "lib/"})
["diff", "HEAD~1", "--", "lib/"]

parse_output(stdout, exit_code)

@spec parse_output(String.t(), non_neg_integer()) ::
  {:ok, Git.Diff.t()} | {:error, {String.t(), non_neg_integer()}}

Parses the output of git diff.

On success (exit code 0), returns {:ok, %Git.Diff{}}. On failure, returns {:error, {stdout, exit_code}}.