Git.Commands.Blame (git v0.4.0)

Copy Markdown View Source

Implements the Git.Command behaviour for git blame.

Always uses --porcelain output format internally for reliable, machine-readable parsing. The output is parsed into a list of Git.BlameEntry structs.

Supports line ranges, specific revisions, email display, date formatting, reverse blame, first-parent following, encoding, and root commit handling.

Summary

Functions

Returns the argument list for git blame.

Parses the output of git blame --porcelain.

Types

t()

@type t() :: %Git.Commands.Blame{
  date: String.t() | nil,
  encoding: String.t() | nil,
  file: String.t(),
  first_parent: boolean(),
  lines: String.t() | nil,
  rev: String.t() | nil,
  reverse: boolean(),
  root: boolean(),
  show_email: boolean(),
  show_name: boolean()
}

Functions

args(command)

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

Returns the argument list for git blame.

Always includes --porcelain for reliable parsing regardless of the :porcelain struct field. The :file field is required and appended at the end of the argument list.

Examples

iex> Git.Commands.Blame.args(%Git.Commands.Blame{file: "lib/app.ex"})
["blame", "--porcelain", "lib/app.ex"]

iex> Git.Commands.Blame.args(%Git.Commands.Blame{file: "lib/app.ex", lines: "1,5"})
["blame", "--porcelain", "-L", "1,5", "lib/app.ex"]

iex> Git.Commands.Blame.args(%Git.Commands.Blame{file: "lib/app.ex", rev: "HEAD~1"})
["blame", "--porcelain", "HEAD~1", "--", "lib/app.ex"]

iex> Git.Commands.Blame.args(%Git.Commands.Blame{file: "lib/app.ex", show_email: true})
["blame", "--porcelain", "-e", "lib/app.ex"]

parse_output(stdout, exit_code)

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

Parses the output of git blame --porcelain.

On success (exit code 0), parses the porcelain output into a list of Git.BlameEntry structs. Each entry contains the commit SHA, author information, line numbers, and the actual line content.

Returns {:ok, [BlameEntry.t()]} on success or {:error, {stdout, exit_code}} on failure.