Git.Commands.Checkout (git v0.4.0)

Copy Markdown View Source

Implements the Git.Command behaviour for git checkout.

Supports switching to an existing branch, creating and switching to a new branch (-b), and restoring files from the index.

Summary

Functions

Returns the argument list for git checkout.

Parses the output of git checkout.

Types

t()

@type t() :: %Git.Commands.Checkout{
  branch: String.t() | nil,
  create: boolean(),
  files: [String.t()]
}

Functions

args(checkout)

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

Returns the argument list for git checkout.

  • If :files is non-empty, builds git checkout -- <files...>.
  • If :branch is set and :create is true, builds git checkout -b <branch>.
  • If :branch is set, builds git checkout <branch>.

Examples

iex> Git.Commands.Checkout.args(%Git.Commands.Checkout{branch: "main"})
["checkout", "main"]

iex> Git.Commands.Checkout.args(%Git.Commands.Checkout{branch: "feat/new", create: true})
["checkout", "-b", "feat/new"]

iex> Git.Commands.Checkout.args(%Git.Commands.Checkout{files: ["README.md", "lib/foo.ex"]})
["checkout", "--", "README.md", "lib/foo.ex"]

parse_output(stdout, exit_code)

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

Parses the output of git checkout.

For file restore operations (exit 0), returns {:ok, :done}. For branch operations (exit 0), parses into a Git.Checkout struct. On failure, returns {:error, {stdout, exit_code}}.