Git.Commands.Stash (git v0.6.1)

Copy Markdown View Source

Implements the Git.Command behaviour for git stash.

Supports listing stash entries (default), saving (pushing) changes to the stash, popping the top stash entry, applying a stash without dropping it, dropping a stash entry, clearing all entries, creating a branch from a stash, and showing the diff for a stash entry.

Summary

Functions

Returns the argument list for git stash.

Parses the output of git stash.

Types

t()

@type t() :: %Git.Commands.Stash{
  apply: boolean(),
  branch: String.t() | nil,
  clear: boolean(),
  drop: boolean(),
  include_untracked: boolean(),
  index: non_neg_integer() | nil,
  keep_index: boolean(),
  list: boolean(),
  message: String.t() | nil,
  pop: boolean(),
  save: boolean(),
  show: boolean()
}

Functions

args(command)

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

Returns the argument list for git stash.

  • If :save is true, builds git stash push [-m <message>] [-u] [--keep-index].
  • If :pop is true, builds git stash pop [stash@{index}].
  • If :apply is true, builds git stash apply [stash@{index}].
  • If :drop is true, builds git stash drop [stash@{index}].
  • If :clear is true, builds git stash clear.
  • If :branch is a string, builds git stash branch <name> [stash@{index}].
  • If :show is true, builds git stash show [stash@{index}].
  • Otherwise, lists stash entries with git stash list.

Examples

iex> Git.Commands.Stash.args(%Git.Commands.Stash{})
["stash", "list"]

iex> Git.Commands.Stash.args(%Git.Commands.Stash{save: true})
["stash", "push"]

iex> Git.Commands.Stash.args(%Git.Commands.Stash{save: true, message: "wip"})
["stash", "push", "-m", "wip"]

iex> Git.Commands.Stash.args(%Git.Commands.Stash{pop: true})
["stash", "pop"]

iex> Git.Commands.Stash.args(%Git.Commands.Stash{drop: true, index: 1})
["stash", "drop", "stash@{1}"]

iex> Git.Commands.Stash.args(%Git.Commands.Stash{apply: true, index: 1})
["stash", "apply", "stash@{1}"]

iex> Git.Commands.Stash.args(%Git.Commands.Stash{clear: true})
["stash", "clear"]

iex> Git.Commands.Stash.args(%Git.Commands.Stash{branch: "recovered"})
["stash", "branch", "recovered"]

iex> Git.Commands.Stash.args(%Git.Commands.Stash{show: true})
["stash", "show"]

parse_output(stdout, exit_code)

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

Parses the output of git stash.

For list operations (exit 0), parses each line into a Git.StashEntry struct. For show operations (exit 0), returns the raw diff as {:ok, stdout}. For save/pop/apply/drop/clear/branch operations (exit 0), returns {:ok, :done}. On failure, returns {:error, {stdout, exit_code}}.