Git.Commands.RevParse (git v0.4.0)

Copy Markdown View Source

Implements the Git.Command behaviour for git rev-parse.

Supports resolving refs, querying repository properties such as the top-level directory, git directory, and various boolean checks like whether the current directory is inside a work tree.

Summary

Functions

Returns the argument list for git rev-parse.

Parses the output of git rev-parse.

Types

t()

@type t() :: %Git.Commands.RevParse{
  abbrev_ref: boolean(),
  absolute_git_dir: boolean(),
  git_common_dir: boolean(),
  git_dir: boolean(),
  is_bare_repository: boolean(),
  is_inside_git_dir: boolean(),
  is_inside_work_tree: boolean(),
  ref: String.t() | nil,
  short: boolean() | non_neg_integer() | nil,
  show_cdup: boolean(),
  show_prefix: boolean(),
  show_toplevel: boolean(),
  symbolic_full_name: boolean(),
  verify: boolean()
}

Functions

args(command)

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

Returns the argument list for git rev-parse.

Builds the argument list from the struct fields, appending flags for each enabled boolean option and the ref (if provided) at the end.

Examples

iex> Git.Commands.RevParse.args(%Git.Commands.RevParse{ref: "HEAD"})
["rev-parse", "HEAD"]

iex> Git.Commands.RevParse.args(%Git.Commands.RevParse{show_toplevel: true})
["rev-parse", "--show-toplevel"]

iex> Git.Commands.RevParse.args(%Git.Commands.RevParse{verify: true, ref: "HEAD"})
["rev-parse", "--verify", "HEAD"]

iex> Git.Commands.RevParse.args(%Git.Commands.RevParse{short: true, ref: "HEAD"})
["rev-parse", "--short", "HEAD"]

iex> Git.Commands.RevParse.args(%Git.Commands.RevParse{short: 8, ref: "HEAD"})
["rev-parse", "--short=8", "HEAD"]

iex> Git.Commands.RevParse.args(%Git.Commands.RevParse{abbrev_ref: true, ref: "HEAD"})
["rev-parse", "--abbrev-ref", "HEAD"]

parse_output(stdout, exit_code)

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

Parses the output of git rev-parse.

On success (exit code 0), returns {:ok, trimmed_output} where the output is the trimmed stdout string. On failure, returns {:error, {stdout, exit_code}}.