Presubmit.Tree (presubmit v0.2.2)

Copy Markdown View Source

A snapshot of every file in the repository at one point in history.

Trees are backed either by a git tree object or by an in-memory map of paths to contents. The rest of the library only ever calls paths/1, exists?/2, and read/2, so both backends behave identically.

Summary

Functions

The blob object id of path, or nil for in-memory trees and missing paths.

Builds a tree from a git tree object.

Builds an in-memory tree from a map of paths to file contents.

Every path in the tree, sorted.

Returns a tree whose reads of paths are served from memory, fetched in one git archive call. Other paths still go through git. In-memory trees are returned unchanged.

Reads the file at path, or :error if it does not exist in this tree.

Reads the file at path, raising if it does not exist.

Types

t()

@type t() :: %Presubmit.Tree{
  blobs: %{optional(String.t()) => String.t()},
  oid: String.t() | nil,
  paths: MapSet.t(String.t()),
  reader: (String.t() -> {:ok, binary()} | :error),
  repo: Path.t() | nil
}

Functions

blob(tree, path)

@spec blob(t(), String.t()) :: String.t() | nil

The blob object id of path, or nil for in-memory trees and missing paths.

exists?(tree, path)

@spec exists?(t(), String.t()) :: boolean()

from_git(repo, oid)

@spec from_git(Path.t(), Presubmit.Git.oid()) :: t()

Builds a tree from a git tree object.

from_map(files)

@spec from_map(%{optional(String.t()) => binary()}) :: t()

Builds an in-memory tree from a map of paths to file contents.

paths(tree)

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

Every path in the tree, sorted.

prefetch(tree, paths)

@spec prefetch(t(), [String.t()]) :: t()

Returns a tree whose reads of paths are served from memory, fetched in one git archive call. Other paths still go through git. In-memory trees are returned unchanged.

Reading many files one cat-file at a time is what dominates the cost of tree-wide discovery, so anything about to read a batch should prefetch it.

read(tree, path)

@spec read(t(), String.t()) :: {:ok, binary()} | :error

Reads the file at path, or :error if it does not exist in this tree.

read!(tree, path)

@spec read!(t(), String.t()) :: binary()

Reads the file at path, raising if it does not exist.