Git.Commands.Worktree (git v0.4.0)

Copy Markdown View Source

Implements the Git.Command behaviour for git worktree.

Supports listing worktrees (default), adding a new worktree, removing a worktree, and pruning stale worktree information. List output is always parsed from --porcelain format for reliable structured data.

Summary

Functions

Returns the argument list for git worktree.

Parses the output of git worktree.

Types

t()

@type t() :: %Git.Commands.Worktree{
  add_branch: String.t() | nil,
  add_new_branch: String.t() | nil,
  add_path: String.t() | nil,
  detach: boolean(),
  force: boolean(),
  list: boolean(),
  lock: boolean(),
  porcelain: boolean(),
  prune: boolean(),
  remove_path: String.t() | nil
}

Functions

args(command)

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

Returns the argument list for git worktree.

  • If :add_path is set, builds git worktree add [options] <path> [<branch>].
  • If :remove_path is set, builds git worktree remove [--force] <path>.
  • If :prune is true, builds git worktree prune.
  • Otherwise, lists worktrees with git worktree list --porcelain.

Examples

iex> Git.Commands.Worktree.args(%Git.Commands.Worktree{})
["worktree", "list", "--porcelain"]

iex> Git.Commands.Worktree.args(%Git.Commands.Worktree{add_path: "/tmp/wt", add_branch: "main"})
["worktree", "add", "/tmp/wt", "main"]

iex> Git.Commands.Worktree.args(%Git.Commands.Worktree{remove_path: "/tmp/wt", force: true})
["worktree", "remove", "--force", "/tmp/wt"]

iex> Git.Commands.Worktree.args(%Git.Commands.Worktree{prune: true})
["worktree", "prune"]

parse_output(stdout, exit_code)

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

Parses the output of git worktree.

For list operations (exit 0), parses porcelain output into a list of Git.Worktree structs. For add/remove/prune operations (exit 0), returns {:ok, :done}. On failure, returns {:error, {stdout, exit_code}}.