grit-lib
Core library for Grit, a from-scratch reimplementation of Git in Rust.
grit-lib provides the low-level building blocks for working with Git
repositories: object storage, index manipulation, refs, diffing, merge,
revision walking, configuration, and more. It is the engine behind the grit
CLI but is designed to be usable as a standalone library for anyone who wants
to read, write, or inspect Git repositories from Rust without shelling out to
git.
Design philosophy
- Correctness first. Behavior is validated against Git's own test suite. Edge cases (empty trees, encoding quirks, unusual ref names) are handled the way Git handles them.
- No unsafe code. The workspace forbids
unsafe— all I/O goes through safe Rust and standard-library abstractions. - Minimal dependencies. The library leans on a small set of widely-used
crates (
sha1,flate2,hex,similar,regex,time,tempfile,thiserror) and nothing else. - Granular errors. Every module returns typed errors via
thiserrorso callers can match on exactly what went wrong.
Quick start
Add the dependency:
[]
= "0.1"
Open a repository and read an object
use Repository;
let repo = discover?;
let head = resolve_head?;
if let Some = head.oid
Walk the commit graph
use ;
let mut opts = default;
opts.output = OidOnly;
let result = rev_list?;
for oid in &result.oids
Diff two trees
use ;
let entries: = diff_trees?;
for e in &entries
Read and write the index
use Index;
let mut index = load?;
index.add_or_replace;
index.sort;
index.write?;
Module overview
| Module | Purpose |
|---|---|
repo |
Discover and open repositories |
objects |
Object model — ObjectId, ObjectKind, CommitData, TagData, TreeEntry |
odb |
Loose-object database: read, write, hash, existence checks |
pack |
Pack file and pack index reading, verification, delta application |
index |
Index (staging area) parsing, writing, and entry manipulation |
refs |
Loose reference reading, writing, deletion, listing |
reflog |
Reflog entry parsing, appending, expiry |
state |
HEAD resolution, in-progress operation detection |
diff |
Tree-to-tree, index-to-tree, and worktree diffing with rename/copy detection |
merge_base |
Merge-base computation, ancestry checks |
merge_file |
Three-way line-level merge with conflict styles |
rev_parse |
Revision string parsing (HEAD~3, v1.0^{commit}, etc.) |
rev_list |
Commit graph traversal with filtering and ordering |
name_rev |
Map commit OIDs to human-readable ref-based names |
config |
Multi-scope configuration loading and typed value parsing |
ignore |
.gitignore pattern matching |
hooks |
Hook resolution and execution |
write_tree |
Build tree objects from index entries |
patch_ids |
Patch ID computation for cherry-pick detection |
check_ref_format |
Ref name validation per Git rules |
stripspace |
Whitespace and comment normalization |
fmt_merge_msg |
Merge commit message formatting |
ls_remote |
List references from a local repository |
unpack_objects |
Unpack a pack stream into loose objects |
error |
Typed error enum covering all failure modes |
License
MIT