Git.Commands.GitConfig (git v0.4.0)

Copy Markdown View Source

Implements the Git.Command behaviour for git config.

Named GitConfig to avoid confusion with Git.Config which holds wrapper configuration. Supports getting, setting, unsetting, and listing git configuration values at local, global, or system scope.

Summary

Functions

Returns the argument list for git config.

Parses the output of git config.

Types

t()

@type t() :: %Git.Commands.GitConfig{
  add: boolean(),
  default: String.t() | nil,
  get: String.t() | nil,
  get_regexp: String.t() | nil,
  global: boolean(),
  list: boolean(),
  local: boolean(),
  name_only: boolean(),
  null_terminated: boolean(),
  set_key: String.t() | nil,
  set_value: String.t() | nil,
  system: boolean(),
  type: String.t() | nil,
  unset: String.t() | nil
}

Functions

args(command)

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

Returns the argument list for git config.

  • If :get is set, builds git config [--global|--local|--system] [--type] [--default] <key>.
  • If :set_key is set, builds git config [--global|--local|--system] [--add] [--type] <key> <value>.
  • If :unset is set, builds git config [--global|--local|--system] --unset <key>.
  • If :get_regexp is set, builds git config [--global|--local|--system] --get-regexp <pattern>.
  • If :list is true, builds git config [--global|--local|--system] --list.

Examples

iex> Git.Commands.GitConfig.args(%Git.Commands.GitConfig{get: "user.name"})
["config", "user.name"]

iex> Git.Commands.GitConfig.args(%Git.Commands.GitConfig{set_key: "user.name", set_value: "Test"})
["config", "user.name", "Test"]

iex> Git.Commands.GitConfig.args(%Git.Commands.GitConfig{list: true, local: true})
["config", "--local", "--list"]

iex> Git.Commands.GitConfig.args(%Git.Commands.GitConfig{unset: "user.name"})
["config", "--unset", "user.name"]

parse_output(stdout, exit_code)

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

Parses the output of git config.

For get operations (exit 0), returns {:ok, trimmed_value}. For list operations (exit 0), returns {:ok, [{key, value}]}. For set/unset operations (exit 0), returns {:ok, :done}. On failure, returns {:error, {stdout, exit_code}}.