Git.Commands.LsTree (git v0.4.0)

Copy Markdown View Source

Implements the Git.Command behaviour for git ls-tree.

Lists the contents of a tree object, showing the mode, type, name, and optionally size of each object. Parses output into a list of Git.TreeEntry structs or plain path strings when --name-only is used.

Summary

Functions

Returns the argument list for git ls-tree.

Parses the output of git ls-tree.

Types

t()

@type t() :: %Git.Commands.LsTree{
  abbrev: non_neg_integer() | nil,
  full_name: boolean(),
  full_tree: boolean(),
  long: boolean(),
  name_only: boolean(),
  path: String.t() | nil,
  recursive: boolean(),
  ref: String.t(),
  tree_only: boolean()
}

Functions

args(command)

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

Returns the argument list for git ls-tree.

The tree-ish ref is placed after flags. An optional path filter is appended after --.

Examples

iex> Git.Commands.LsTree.args(%Git.Commands.LsTree{})
["ls-tree", "HEAD"]

iex> Git.Commands.LsTree.args(%Git.Commands.LsTree{recursive: true, long: true})
["ls-tree", "-r", "-l", "HEAD"]

iex> Git.Commands.LsTree.args(%Git.Commands.LsTree{name_only: true, ref: "main"})
["ls-tree", "--name-only", "main"]

iex> Git.Commands.LsTree.args(%Git.Commands.LsTree{path: "lib/"})
["ls-tree", "HEAD", "--", "lib/"]

iex> Git.Commands.LsTree.args(%Git.Commands.LsTree{abbrev: 8})
["ls-tree", "--abbrev=8", "HEAD"]

parse_output(stdout, exit_code)

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

Parses the output of git ls-tree.

On success (exit code 0), parses each line into a Git.TreeEntry struct. When name_only mode was used (detected by the absence of tabs in output), returns {:ok, [String.t()]} with just the path names.

The default format is mode type sha\tpath. With --long, the format is mode type sha size\tpath where size is right-justified with spaces.

Returns {:error, {stdout, exit_code}} on failure.