git

index

Overview

Pure-Lua git repository reader.

Parses .git directory structures directly, avoiding the need to spawn git processes: loose and packed objects (pack .idx v2, OFS/REF delta resolution), refs (loose, packed, symbolic), HEAD, the staging index, commit and tree objects.

Works on bare repositories and worktrees alike, every function takes an explicit git_dir.

Read-only. Higher layers build on this core: git.prompt (shell prompt status) and git.pack (packfile generation for serving).

The pack reader keeps state for the life of the process. A pack stays open once read, and each entry is read whole in one pread: the idx's offsets, sorted, give its extent. At most 16 packs stay open across every repository, the least recently read closed first. Resolved objects are cached by pack and offset, at most 32 MB of content, so a delta chain costs one read per link not yet seen. A lookup that misses every pack lists the pack dir again before it answers nil.

object_info gives an object's type and size from headers, and inflates at most 32 bytes of it. It keeps its packed answers with the pack, a type and a size per entry asked, for as long as the pack is listed.

packed_entry hands out a packed entry's zlib stream as it lies in the pack, with a delta's base named by its id. git.pack copies it into the pack it writes.

list_refs keeps a snapshot of the refs per repository. A call reuses it while packed-refs and every directory the walk listed stat the same. git writes a ref through a lock file and a rename, which moves its directory's mtime. The stat gives whole seconds, so a walk is kept only when it started at least 2 seconds after the newest mtime it saw: a later write then lands in a later second. packed-refs alone takes the same rule, with its inode and size besides.

Submodules

ModuleDescription
git.packPackfile generation on top of the git core reader: walk the object graph from a set of wanted refs and serialize a version 2 packfile.
git.promptGit repository status for shell prompt integration, built on the git core reader.

Functions

NameSignature
find_git_dirfind_git_dir(start_path) -> git_dir, worktree
resolve_refresolve_ref(git_dir, ref_path) -> sha
read_headread_head(git_dir) -> head
locate_objectlocate_object(git_dir, sha_hex) -> where
packed_entrypacked_entry(git_dir, sha_hex) -> entry
reader_statsreader_stats() -> stats
expand_shaexpand_sha(git_dir, prefix) -> sha, err
read_objectread_object(git_dir, sha_hex) -> obj
object_infoobject_info(git_dir, sha_hex) -> type, size
parse_commitparse_commit(content) -> commit
parse_treeparse_tree(content) -> entries
list_refslist_refs(git_dir) -> refs

find_git_dir(start_path) -> git_dir, worktree

Discover the .git directory by walking up from a starting path

Walks the directory tree upward from start_path (defaults to cwd) looking for a .git directory or gitdir file (worktrees/submodules). Returns the resolved git directory path and the worktree root, or nil if not inside a git repository.

resolve_ref(git_dir, ref_path) -> sha

Resolve a ref path (e.g. refs/heads/main) to a SHA, following symbolic refs and packed-refs

read_head(git_dir) -> head

Read HEAD of a repository

Returns { type = "branch", name = short_name, ref = full_ref } for a symbolic HEAD, or { type = "detached", sha = sha } for a detached one.

locate_object(git_dir, sha_hex) -> where

Say where an object lives, without reading it

Returns { kind = "loose", path = path } for a loose object, or { kind = "pack", pack = pack_path, offset = n } for a packed one, with the offset of its entry in the pack. Returns nil when no object has the id. A loose copy wins over a packed one, as in read_object.

packed_entry(git_dir, sha_hex) -> entry

Read a packed object's entry as its pack stores it

Returns { type = t, size = n, stream = bytes, pack = pack_path }, with the entry's zlib stream as it lies in the pack, inflated nothing. type is "commit", "tree", "blob", "tag" or "delta". size is the header's: the object's size, or the delta's own inflated size.

A delta adds base, its base's id. An OFS_DELTA's base offset is turned into its id through the idx.

Returns nil when no pack holds the object, even when a loose copy exists. A packed copy wins here, where a loose copy wins in read_object: both copies hold the same object.

reader_stats() -> stats

Report the pack reader's state: its cache and its open packs

Returns { cache_bytes = n, cache_entries = n, open_packs = n }. The cache holds resolved packed objects, at most 32 MB of content. At most 16 packs stay open, across every repository the process reads.

expand_sha(git_dir, prefix) -> sha, err

Expand an abbreviated object id to its full 40-hex form

Resolves a short object id the way git does: loose objects and every pack index are searched for ids starting with prefix. Returns nil and "ambiguous object id" when more than one object matches, so a caller never acts on a guess. A full 40-character id is returned unchanged without touching the object database.

read_object(git_dir, sha_hex) -> obj

Read an object from the object database (loose or packed, deltas resolved)

Returns { type = "commit"|"tree"|"blob"|"tag", size = n, content = raw } or nil when the object is not found.

object_info(git_dir, sha_hex) -> type, size

Report an object's type and size, without its content

Returns the type, "commit", "tree", "blob" or "tag", and the size of the content, as read_object would give them. Returns nil when the object is not found. Headers answer it: a packed plain entry inflates nothing, a packed delta and a loose object inflate their first 32 bytes. A delta's type takes one short read per link down its chain. A packed object's answer stays with its pack, so a second ask reads nothing; so does the type of each link a walk passed.

parse_commit(content) -> commit

Parse a raw commit object's content

Returns { tree = sha, parents = {sha, ...}, author = str, committer = str, message = str }.

parse_tree(content) -> entries

Parse a raw tree object's content

Returns an array of { mode = str, name = str, sha = hex_str } entries.

list_refs(git_dir) -> refs

List all refs of a repository: loose and packed, with peeled ^{} entries for annotated tags

Returns a map of ref path to sha. The table is shared between calls and must not be changed: copy it first.

A call reuses the last walk while packed-refs and every directory under refs/ stat as they did. A walk is kept only when it began at least 2 seconds after the newest mtime it saw, so a ref git writes, even within the same second, shows on the next call. A ref edited in place moves no directory and can go unseen; git never writes one that way. A tag's peel is kept by the tag object's sha across walks.