grit
CLILibrary
HEAD → docs/library/refs
Library guide / RefsMarkdown

Refs

Resolve HEAD, list branches and tags, update refs with reflog, and how grit-lib picks loose, packed, or reftable storage.

References name commits (and other objects). grit-lib exposes them through refs and reftable, with reflog for update history. The references module groups these for navigation in rustdoc.

Resolving HEAD and symbolic refs

resolve_head reads HEAD and returns a HeadState: on a branch (symbolic ref plus commit, if any), detached at a commit, or invalid. To resolve any ref name to an object id, use resolve_ref, which follows symbolic refs with cycle detection.

read_ref_file returns a Ref (Direct or Symbolic) without resolving the whole chain.

Listing branches and tags

list_refs takes a prefix such as refs/heads/ or refs/tags/ and returns sorted (name, ObjectId) pairs. Loose refs under refs/ override stale lines in packed-refs, matching Git. list_refs_glob applies pattern matching when you need DWIM-style filtering.

Creating and updating refs with reflog

write_ref points a ref at a commit (or other object). write_symbolic_ref updates symbolic refs such as HEAD.

Record history with append_reflog, then read it back with read_reflog. Each ReflogEntry carries old and new ids, identity, and message. Batch updates can use update_refs when you need compare-and-swap semantics across many refs.

Loose, packed, and reftable backends

By default, grit uses the files backend: one file per ref under refs/, plus an optional packed-refs file. list_refs and resolve_ref merge packed and loose sources so callers see a single namespace.

When extensions.refStorage = reftable is set in config, the same functions use the reftable backend. Backend selection is centralized: open_ref_store detects the on-disk format from repository-local config and opens FilesRefStore or ReftableRefStore. An open Repository caches that store on RepoCaches; call refs() on the handle to borrow the store, or with_ref_store to inject a custom backend (for example MemoryRefStore).

Pluggable ref storage (RefStore)

refs::store defines RefStore: raw reads, sorted prefix iteration, compare-and-swap transactions (RefTransaction → prepare/commit/abort), and reflog helpers. MemoryRefStore is for tests and embedders; FilesRefStore and ReftableRefStore match on-disk layouts. Path-based helpers such as resolve_ref delegate to open_ref_store; rev-parse on a Repository resolves ref names through the handle's cached store.