Git.Commands.CatFile (git v0.4.0)

Copy Markdown View Source

Implements the Git.Command behaviour for git cat-file.

Provides content or type/size information for repository objects. Supports pretty-printing object contents, querying object type or size, and checking whether an object exists.

Interactive and batch modes are intentionally not supported because they require stdin interaction which cannot be driven programmatically.

Summary

Functions

Returns the argument list for git cat-file.

Parses the output of git cat-file.

Types

t()

@type t() :: %Git.Commands.CatFile{
  exists: boolean(),
  filters: boolean(),
  object: String.t() | nil,
  print: boolean(),
  size: boolean(),
  textconv: boolean(),
  type: boolean()
}

Functions

args(command)

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

Returns the argument list for git cat-file.

Builds the argument list from the struct fields. Exactly one of the mode flags (:type, :size, :print, :exists) should be set, or none for the default pretty-print behaviour.

Examples

iex> Git.Commands.CatFile.args(%Git.Commands.CatFile{object: "HEAD", type: true})
["cat-file", "-t", "HEAD"]

iex> Git.Commands.CatFile.args(%Git.Commands.CatFile{object: "HEAD", size: true})
["cat-file", "-s", "HEAD"]

iex> Git.Commands.CatFile.args(%Git.Commands.CatFile{object: "HEAD", print: true})
["cat-file", "-p", "HEAD"]

iex> Git.Commands.CatFile.args(%Git.Commands.CatFile{object: "HEAD", exists: true})
["cat-file", "-e", "HEAD"]

iex> Git.Commands.CatFile.args(%Git.Commands.CatFile{object: "HEAD", textconv: true})
["cat-file", "--textconv", "HEAD"]

iex> Git.Commands.CatFile.args(%Git.Commands.CatFile{object: "HEAD", filters: true})
["cat-file", "--filters", "HEAD"]

parse_output(stdout, exit_code)

@spec parse_output(String.t(), non_neg_integer()) ::
  {:ok, atom()}
  | {:ok, integer()}
  | {:ok, String.t()}
  | {:ok, boolean()}
  | {:error, {String.t(), non_neg_integer()}}

Parses the output of git cat-file.

  • For :type mode, returns {:ok, atom} where atom is one of :blob, :tree, :commit, or :tag.
  • For :size mode, returns {:ok, integer}.
  • For :print mode (including textconv and filters), returns {:ok, String.t()}.
  • For :exists mode, exit code 0 returns {:ok, true} and exit code 1 returns {:ok, false} (not an error).