Git.Commands.Switch (git v0.4.0)

Copy Markdown View Source

Implements the Git.Command behaviour for git switch.

git switch is the modern (Git 2.23+) command for switching branches, replacing the branch-switching role of git checkout. It is more focused and less error-prone than checkout for branch operations.

Summary

Functions

Returns the argument list for git switch.

Parses the output of git switch.

Types

t()

@type t() :: %Git.Commands.Switch{
  branch: String.t() | nil,
  create: boolean(),
  detach: boolean(),
  discard_changes: boolean(),
  force: boolean(),
  force_create: boolean(),
  guess: boolean() | nil,
  merge: boolean(),
  orphan: boolean(),
  track: String.t() | nil
}

Functions

args(cmd)

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

Returns the argument list for git switch.

Examples

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

iex> Git.Commands.Switch.args(%Git.Commands.Switch{branch: "feat/new", create: true})
["switch", "-c", "feat/new"]

iex> Git.Commands.Switch.args(%Git.Commands.Switch{branch: "abc123", detach: true})
["switch", "--detach", "abc123"]

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 switch.

Reuses Git.Checkout parsing since the output format is the same (e.g. "Switched to branch 'main'").