feat(rit+rithub): rit (rust git core, 35 tests) + rithub (forge scaffold) under railyard/apps

This commit is contained in:
caelum0x 2026-08-24 20:43:50 +03:00
commit 8d1d981116
20 changed files with 3086 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
target/
*.db
.env

418
Cargo.lock generated Normal file
View File

@ -0,0 +1,418 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "adler2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "anstream"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
[[package]]
name = "anstyle-parse"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
[[package]]
name = "bitflags"
version = "2.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b588b76d00fde79687d7646a9b5bdf3cc0f655e0bbd080335a95d7e96f3587da"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "clap"
version = "4.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "473c7e07f409a8d772161724aa8db6a765a2532a70f9667eeb7b49d3d02fbdca"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b48fea5a88e9ae728a2dcbedbfc0e730f7d60da42e1cb049a83c9fb8b789889"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d012d2b9d65aca7f18f4d9878a045bc17899bba951561ba5ec3c2ba1eed9a061"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
[[package]]
name = "colorchoice"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
[[package]]
name = "cpufeatures"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
dependencies = [
"libc",
]
[[package]]
name = "crc32fast"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8498c871161e1742aaa9d52551b2d6ebdd4c3d45a3be423e3728f33b955be550"
dependencies = [
"cfg-if",
]
[[package]]
name = "crypto-common"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "errno"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "fastrand"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223"
[[package]]
name = "flate2"
version = "1.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099"
dependencies = [
"cfg-if",
"libc",
"r-efi",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "libc"
version = "0.2.189"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"
[[package]]
name = "linux-raw-sys"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
[[package]]
name = "miniz_oxide"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
dependencies = [
"adler2",
"simd-adler32",
]
[[package]]
name = "once_cell"
version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
[[package]]
name = "rit"
version = "0.1.0"
dependencies = [
"anyhow",
"clap",
"flate2",
"sha1",
"tempfile",
"thiserror",
]
[[package]]
name = "rustix"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190"
dependencies = [
"bitflags",
"errno",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "sha1"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a978451301f4db1d02937a4ab3ccce137717b81826e79b7d49ffe3244a13c3b8"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "simd-adler32"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea"
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "3.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6275cddf4610d1775e6d1fe9469b2e77d0f39fd98fb7450901b821e0c53649f"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tempfile"
version = "3.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
dependencies = [
"fastrand",
"getrandom",
"once_cell",
"rustix",
"windows-sys",
]
[[package]]
name = "thiserror"
version = "2.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec86235f5fcc2a73650310756d2ac5b138a5780bbbdfae3eeccec992c435ba4f"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "2.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc04cd3e1236dd4a98afca4569f2deb3f120e5422a4023be2cb683f8486292af"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "typenum"
version = "1.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20"
[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]

28
Cargo.toml Normal file
View File

@ -0,0 +1,28 @@
[package]
name = "rit"
version = "0.1.0"
edition = "2021"
license = "MIT"
description = "Library-first Rust implementation of the Git loose-object model: read, write, verify, and traverse standard Git repositories"
rust-version = "1.74"
# Standalone by design: never join a parent Cargo workspace.
[workspace]
[lib]
name = "rit"
path = "src/lib.rs"
[[bin]]
name = "rit"
path = "src/main.rs"
[dependencies]
anyhow = "1"
clap = { version = "4", features = ["derive"] }
flate2 = "1"
sha1 = "0.10"
thiserror = "2"
[dev-dependencies]
tempfile = "3"

222
PLAN.md Normal file
View File

@ -0,0 +1,222 @@
# Rust Git Forge Product and Architecture Plan
Last revised: 23 August 2026
Tracking issue: `videogen-zyo`
## Decision record
This is a production open-source systems product, not GitGram and not a
hackathon implementation. The neutral working name is **Rust Git Forge** until
brand, crate, and package-name checks are complete.
The product has two layers:
1. A library-first Rust implementation of Git repositories and the Git wire
protocol, compatible with existing Git repositories and clients.
2. A self-hostable collaboration forge built on that implementation, including
repositories, organizations, permissions, issues, pull requests, review,
protected branches, webhooks, releases, and audit logs.
GitGram remains a mostly TypeScript social product. It turns repositories into
Instagram-like accounts with posts, reels, stories, media publishing,
followers, and feeds. GitGram does not own object storage, refs, packfiles, the
Git protocol, pull-request truth, or repository permissions. It integrates with
GitHub and, later, Rust Git Forge through public authenticated APIs and webhooks.
AITimeMachine captures and restores AI coding workflows. It may attach its
checkpoints to Git commits, but it does not implement or replace Git.
## Initial customer and promise
The initial user is an engineer or small team that needs a trustworthy local
Git implementation with readable internals and strong compatibility evidence.
The first production promise is narrower than a complete GitHub replacement:
users can inspect, create, verify, exchange, and recover standard Git
repositories without corrupting data.
The forge becomes a supported product only after the core passes
interoperability, crash-recovery, parser-fuzzing, and concurrent-ref tests.
## Compatibility scope
The core implementation must cover:
- loose blobs, trees, commits, annotated tags, object verification, and object
traversal;
- SHA-1 repository compatibility with collision detection, plus an explicit
path to Git's SHA-256 repository format;
- refs, symbolic refs, packed refs, reflogs, atomic compare-and-swap updates,
and lock recovery;
- index formats, worktrees, ignore rules, status, add, restore, and commit;
- revision parsing, graph walks, merge bases, diffs, rename heuristics, and
conflict representation;
- pack indexes, packfiles, delta resolution, reachability, negotiation, and
quarantine of received objects;
- smart protocol v2 over HTTP and SSH for clone, fetch, and push;
- three-way merge, rebase, cherry-pick, revert, and deterministic conflict
handling.
Submodules, sparse checkout, partial clone, large-file storage, replace refs,
notes, and advanced maintenance enter the compatibility matrix explicitly. None
is silently claimed before its test suite passes.
## Rust workspace architecture
```text
crates/
git-object object IDs, parsing, validation, canonical serialization
git-odb loose and packed object databases, quarantine, alternates
git-refs refs, reflogs, transactions, locks
git-index index formats and extensions
git-worktree checkout, status, ignore rules, filesystem safety
git-revision revision syntax, graph traversal, reachability
git-diff text and binary diff, rename detection
git-merge merge bases, strategies, conflicts, sequencer
git-pack pack/index parsing, deltas, negotiation inputs
git-protocol protocol v2, upload-pack, receive-pack transports
git-cli compatibility-oriented command-line interface
forge-domain users, organizations, repositories, issues, reviews
forge-store metadata persistence and repository coordination
forge-http browser/API and smart-HTTP server
forge-ssh SSH command gateway and key authorization
forge-jobs durable maintenance, hooks, webhooks, notifications
```
Shared crates cannot depend on the forge UI or a specific database. Parsing and
storage APIs expose bounded streams rather than assuming objects or packs fit in
memory. Platform-specific filesystem behavior stays behind explicit adapters
and platform tests.
## Forge architecture
The first forge is a single-node, self-hosted service:
- repositories and packfiles on a durable local filesystem;
- PostgreSQL for users, organizations, permissions, issues, reviews, jobs, and
immutable audit events;
- an SSH service for Git transport and an HTTP service for API, browser, and
smart HTTP;
- a transactional job outbox for webhooks, email, repository maintenance, and
indexing;
- filesystem snapshots plus PostgreSQL backups with a tested coordinated
restore procedure.
SQLite may support desktop or single-user mode, but it does not imply safe
multi-node repository coordination. Multi-node storage, geo replication,
packages, runners, Actions-compatible CI, and large-file storage require later
designs with separate threat and cost reviews.
Cloudflare Workers are not the Git execution or storage plane. Large pack
streams, SSH, repository filesystem semantics, and long-running maintenance
need a local process or conventional server. Cloudflare may serve static project
documentation and, after measurement, proxy ordinary browser/API traffic. R2 is
only a future cold-object or backup option under the shared account budget.
## Security contract
Repository data is adversarial input. The product must defend against:
- malformed object, index, pack, bitmap, commit-graph, and protocol data;
- path traversal, case-folding collisions, NTFS/HFS hazards, unsafe symlinks,
and checkout outside the worktree;
- decompression bombs, delta chains, integer overflow, unbounded graph walks,
resource exhaustion, and archive bombs;
- SHA-1 collision attacks and object-ID confusion between hash formats;
- ref races, lock theft, non-fast-forward policy bypass, hook injection, and
partially received pushes;
- cross-tenant authorization errors, leaked deploy keys or tokens, forged
webhooks, and unsafe HTML or Markdown rendering.
Incoming pushes remain quarantined until object verification, connectivity,
authorization, policy, and size checks pass. Ref changes use atomic
transactions. The forge supports scoped tokens, SSH key fingerprints,
phishing-resistant operator MFA, protected branches, signed commit and tag
verification, secret redaction, security logs, and auditable administrative
actions.
## Verification strategy
Production claims require retained evidence from:
- differential tests that run equivalent operations through Rust Git Forge and
the reference Git CLI, then compare repository state and command semantics;
- interoperability matrices across supported Git versions, operating systems,
filesystem behaviors, HTTP servers, and SSH clients;
- property tests for serialization, graph traversal, deltas, refs, and index
round trips;
- coverage-guided fuzzing for every untrusted parser and protocol state machine;
- Git project fixtures where licensing and harness boundaries permit;
- fault injection around writes, fsync, locks, process death, partial network
reads, interrupted clone/fetch/push, and coordinated restore;
- concurrency tests for ref updates, simultaneous pushes, garbage collection,
and read/write isolation;
- independent security review before exposing public repository hosting.
Every compatibility claim is tied to a named test. A failing repository fixture
remains a release blocker or a documented unsupported capability.
## Release sequence
### Stage 0: specification and harness
Publish the compatibility matrix, threat model, repository fixtures,
differential harness, fuzz targets, performance budgets, and
corruption-recovery rules.
### Stage 1: read-only repository core
Ship object inspection, `cat-file`, `ls-tree`, log traversal, refs, pack reads,
and `fsck`. Opening hostile repositories cannot write outside the repository or
consume unbounded resources.
### Stage 2: local authoring
Ship init, status, add, diff, commit, branch, checkout/restore, tag, reflog, and
garbage-safe object writes. Pass crash tests for every state-changing command.
### Stage 3: exchange
Ship protocol v2 clone, fetch, and push over HTTP and SSH. Prove quarantine,
atomic refs, authentication, authorization, interrupted transfer recovery, and
interop with the reference Git client and server.
### Stage 4: history editing
Ship merge, rebase, cherry-pick, revert, conflict continuation/abort, and
recovery. Preserve user work during failures and match documented Git behavior.
### Stage 5: single-node forge
Ship accounts, organizations, repository permissions, issues, pull requests,
review, branch protection, webhooks, releases, operator tools, backups, restore,
export, deletion, abuse handling, and upgrade/rollback procedures.
### Stage 6: public production
Onboard outside repositories only after security review, capacity tests,
restore drills, support procedures, privacy/terms, observability, incident
response, and an upgrade from the previous release all pass. Multi-node and CI
features remain separate approvals.
## Non-goals for the first public release
- replacing every Git command at once;
- claiming GitHub parity;
- running arbitrary CI jobs;
- hosting unbounded public binary releases or package registries;
- coupling repository correctness to GitGram feeds;
- storing primary repositories in browser storage, R2, or a Worker runtime;
- inventing a new incompatible wire protocol before Git interoperability works.
## Pre-code gate
Before implementation:
1. approve the project name, license, governance, supported platforms, and MSRV;
2. freeze the Stage 1 compatibility matrix and threat model;
3. select reference Git versions and reusable fixtures after license review;
4. decide whether existing Rust Git crates are comparison subjects,
dependencies, or clean-room references;
5. define benchmark repositories and memory, CPU, disk, and network budgets;
6. approve the single-node data and backup contract without using Cloudflare as
the repository execution plane.

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# rit — rust git
A from-scratch, **library-first** Rust implementation of the Git **loose-object model** —
the `git` equivalent. Byte-for-byte compatible with real Git (interop-tested against `git` 2.50).
- Object model: loose blobs/trees/commits/tags (read + write), git-exact SHA-1, canonical framing.
- ODB (zlib, atomic writes, SHA-1 verification on read), refs (HEAD/loose/packed/symbolic).
- `rit` CLI: `hash-object`, `cat-file`, `ls-tree`, `log`.
- 35 tests incl. real-git interop (our OIDs == `git hash-object`; `git fsck` accepts what we write).
The collaboration **forge** built on this core lives in [`../rithub`](../rithub) (the GitHub equivalent).
Deferred per the design gate: packfiles/delta, index/worktree, wire protocol v2, merge/rebase, SHA-256.

31
src/blob.rs Normal file
View File

@ -0,0 +1,31 @@
//! Blob objects: opaque file contents. Git applies no interpretation, so a blob
//! is simply its bytes; the payload *is* the data.
/// A blob object.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Blob {
/// Raw file contents.
pub data: Vec<u8>,
}
impl Blob {
/// Wrap raw bytes as a blob.
pub fn new(data: Vec<u8>) -> Self {
Self { data }
}
/// The contents as a lossy UTF-8 string, for display.
pub fn as_str_lossy(&self) -> std::borrow::Cow<'_, str> {
String::from_utf8_lossy(&self.data)
}
/// Byte length of the blob.
pub fn len(&self) -> usize {
self.data.len()
}
/// Whether the blob is empty.
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
}

173
src/commit.rs Normal file
View File

@ -0,0 +1,173 @@
//! Commit objects: a snapshot (`tree`) plus ancestry (`parent`), authorship,
//! and a message.
//!
//! Payload layout:
//!
//! ```text
//! tree <40-hex>\n
//! parent <40-hex>\n (zero or more, in order)
//! author <signature>\n
//! committer <signature>\n
//! [extra headers...]\n
//! \n
//! <message>
//! ```
use crate::error::{ForgeError, Result};
use crate::headers::{self, Header};
use crate::oid::ObjectId;
use crate::signature::Signature;
/// A parsed commit object.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Commit {
/// The snapshot this commit points at.
pub tree: ObjectId,
/// Parent commits, in stored order (empty for a root commit).
pub parents: Vec<ObjectId>,
/// Who wrote the change.
pub author: Signature,
/// Who recorded it.
pub committer: Signature,
/// Any other headers (e.g. `encoding`, `gpgsig`), preserved in order so
/// re-serialization is byte-identical to Git's output.
pub extra_headers: Vec<Header>,
/// The commit message body.
pub message: Vec<u8>,
}
impl Commit {
/// Parse a commit payload, failing closed on missing required headers.
pub fn parse(payload: &[u8]) -> Result<Self> {
let block = headers::parse(payload, "commit")?;
let mut tree = None;
let mut parents = Vec::new();
let mut author = None;
let mut committer = None;
let mut extra_headers = Vec::new();
for h in block.headers {
match h.key.as_str() {
"tree" => {
let hex = std::str::from_utf8(&h.value)
.map_err(|_| ForgeError::malformed("commit", "tree oid not ASCII"))?;
tree = Some(ObjectId::from_hex(hex)?);
}
"parent" => {
let hex = std::str::from_utf8(&h.value)
.map_err(|_| ForgeError::malformed("commit", "parent oid not ASCII"))?;
parents.push(ObjectId::from_hex(hex)?);
}
"author" => {
let v = std::str::from_utf8(&h.value)
.map_err(|_| ForgeError::malformed("commit", "author not UTF-8"))?;
author = Some(Signature::parse(v, "commit")?);
}
"committer" => {
let v = std::str::from_utf8(&h.value)
.map_err(|_| ForgeError::malformed("commit", "committer not UTF-8"))?;
committer = Some(Signature::parse(v, "commit")?);
}
_ => extra_headers.push(h),
}
}
Ok(Self {
tree: tree.ok_or_else(|| ForgeError::malformed("commit", "missing tree header"))?,
parents,
author: author
.ok_or_else(|| ForgeError::malformed("commit", "missing author header"))?,
committer: committer
.ok_or_else(|| ForgeError::malformed("commit", "missing committer header"))?,
extra_headers,
message: block.message,
})
}
/// Serialize back to the exact on-disk payload.
///
/// Header order (tree, parents, author, committer, extras) matches Git, so
/// for objects Git wrote this reproduces their bytes and thus their oid.
pub fn serialize(&self) -> Vec<u8> {
let mut out = Vec::new();
push_header(&mut out, "tree", self.tree.to_hex().as_bytes());
for p in &self.parents {
push_header(&mut out, "parent", p.to_hex().as_bytes());
}
push_header(&mut out, "author", self.author.to_raw().as_bytes());
push_header(&mut out, "committer", self.committer.to_raw().as_bytes());
for h in &self.extra_headers {
push_header(&mut out, &h.key, &h.value);
}
out.push(b'\n');
out.extend_from_slice(&self.message);
out
}
/// The message as a lossy UTF-8 string.
pub fn message_str(&self) -> std::borrow::Cow<'_, str> {
String::from_utf8_lossy(&self.message)
}
/// The first line of the message (the summary).
pub fn summary(&self) -> String {
self.message_str().lines().next().unwrap_or("").to_string()
}
}
/// Append one `key value\n` header, restoring continuation-line spacing.
fn push_header(out: &mut Vec<u8>, key: &str, value: &[u8]) {
out.extend_from_slice(key.as_bytes());
out.push(b' ');
out.extend_from_slice(&headers::serialize_value(value));
out.push(b'\n');
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_serialize_roundtrips() {
let payload = concat!(
"tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n",
"parent e69de29bb2d1d6434b8b29ae775ad8c2e48c5391\n",
"author Alice <a@b.c> 1700000000 +0000\n",
"committer Alice <a@b.c> 1700000000 +0000\n",
"\n",
"initial commit\n",
)
.as_bytes()
.to_vec();
let commit = Commit::parse(&payload).unwrap();
assert_eq!(commit.parents.len(), 1);
assert_eq!(commit.author.name, "Alice");
assert_eq!(commit.summary(), "initial commit");
// byte-exact round trip is the property that keeps oids stable
assert_eq!(commit.serialize(), payload);
}
#[test]
fn root_commit_has_no_parents() {
let payload = concat!(
"tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n",
"author Alice <a@b.c> 1700000000 +0000\n",
"committer Alice <a@b.c> 1700000000 +0000\n",
"\n",
"root\n",
)
.as_bytes()
.to_vec();
let commit = Commit::parse(&payload).unwrap();
assert!(commit.parents.is_empty());
assert_eq!(commit.serialize(), payload);
}
#[test]
fn rejects_missing_tree() {
let payload = b"author Alice <a@b.c> 1 +0000\ncommitter Alice <a@b.c> 1 +0000\n\nx\n";
assert!(Commit::parse(payload).is_err());
}
}

76
src/error.rs Normal file
View File

@ -0,0 +1,76 @@
//! Stable error model for the Git core.
//!
//! Repository data is adversarial input (see PLAN "Security contract"): every
//! parser fails **closed** — it returns a typed [`ForgeError`] rather than
//! panicking on malformed objects, truncated headers, or corrupt compression.
use thiserror::Error;
/// Crate-wide result alias.
pub type Result<T> = std::result::Result<T, ForgeError>;
/// The stable error enum for the Git object core.
#[derive(Debug, Error)]
pub enum ForgeError {
/// Underlying filesystem / IO failure.
#[error("io error: {0}")]
Io(#[from] std::io::Error),
/// No `.git` directory was found while discovering a repository.
#[error("not a git repository (or any parent): {0}")]
NotARepository(String),
/// An object's on-disk bytes could not be parsed (bad header, truncated
/// payload, declared length mismatch, non-UTF-8 header, etc.).
#[error("malformed {kind} object: {reason}")]
MalformedObject {
/// The object kind being parsed (or "loose" before the type is known).
kind: &'static str,
/// Human-readable reason.
reason: String,
},
/// A requested object does not exist in the loose object database.
#[error("object not found: {0}")]
ObjectNotFound(String),
/// Content verification failed: the SHA-1 of the stored bytes did not match
/// the object id it was looked up under. Signals repository corruption.
#[error("object {looked_up} is corrupt: content hashes to {computed}")]
HashMismatch {
/// The oid the object was requested by.
looked_up: String,
/// The oid actually computed from its bytes.
computed: String,
},
/// A hex object id was the wrong length or contained non-hex characters.
#[error("invalid object id: {0}")]
InvalidOid(String),
/// A ref/revision could not be resolved to an object id.
#[error("cannot resolve reference: {0}")]
InvalidRef(String),
/// An abbreviated object id prefix matched more than one object.
#[error("ambiguous object id prefix: {0}")]
AmbiguousPrefix(String),
/// The object header named a type that is not blob/tree/commit/tag.
#[error("unknown object type: {0}")]
UnknownType(String),
/// zlib decompression of a loose object failed or exceeded the size cap.
#[error("decompression failed: {0}")]
Decompress(String),
}
impl ForgeError {
/// Convenience constructor for a malformed-object error.
pub fn malformed(kind: &'static str, reason: impl Into<String>) -> Self {
ForgeError::MalformedObject {
kind,
reason: reason.into(),
}
}
}

113
src/headers.rs Normal file
View File

@ -0,0 +1,113 @@
//! Shared header-block parsing for commit and tag objects.
//!
//! Both commit and tag payloads share a layout: a block of `key value` header
//! lines, a single blank line, then a free-form message. A header value may
//! span multiple lines — continuation lines begin with a single space (this is
//! how Git stores multi-line `gpgsig` values). We preserve the exact value
//! bytes (with embedded newlines) so re-serialization is byte-identical.
use crate::error::{ForgeError, Result};
/// One header line: an ASCII key and its raw value bytes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Header {
/// The header keyword (e.g. `tree`, `parent`, `author`, `gpgsig`).
pub key: String,
/// The value bytes; multi-line values keep embedded `\n` separators.
pub value: Vec<u8>,
}
/// The result of splitting a commit/tag payload.
pub struct HeaderBlock {
/// Ordered header lines.
pub headers: Vec<Header>,
/// The message body (everything after the blank separator line).
pub message: Vec<u8>,
}
/// Split a payload into ordered headers and the message body.
pub fn parse(payload: &[u8], kind: &'static str) -> Result<HeaderBlock> {
// Header block ends at the first blank line ("\n\n").
let (header_section, message) = match find_double_newline(payload) {
Some(idx) => (&payload[..idx], payload[idx + 2..].to_vec()),
// Some objects (a header-only tag) may have no message; then the whole
// payload up to a trailing newline is headers.
None => (payload, Vec::new()),
};
let mut headers: Vec<Header> = Vec::new();
for line in split_lines(header_section) {
if line.first() == Some(&b' ') {
// Continuation of the previous header value.
let last = headers.last_mut().ok_or_else(|| {
ForgeError::malformed(kind, "continuation line with no preceding header")
})?;
last.value.push(b'\n');
last.value.extend_from_slice(&line[1..]);
} else {
let space = line.iter().position(|&b| b == b' ').ok_or_else(|| {
ForgeError::malformed(kind, "header line missing space separator")
})?;
let key = std::str::from_utf8(&line[..space])
.map_err(|_| ForgeError::malformed(kind, "header key is not ASCII"))?
.to_string();
headers.push(Header {
key,
value: line[space + 1..].to_vec(),
});
}
}
Ok(HeaderBlock { headers, message })
}
/// Re-emit a header value, restoring the leading space on continuation lines.
pub fn serialize_value(value: &[u8]) -> Vec<u8> {
let mut out = Vec::with_capacity(value.len());
for (i, &b) in value.iter().enumerate() {
out.push(b);
if b == b'\n' && i + 1 < value.len() {
out.push(b' ');
}
}
out
}
/// Locate the first `\n\n` in a byte slice.
fn find_double_newline(data: &[u8]) -> Option<usize> {
data.windows(2).position(|w| w == b"\n\n")
}
/// Split a header section into lines, dropping the line terminators.
fn split_lines(section: &[u8]) -> Vec<&[u8]> {
if section.is_empty() {
return Vec::new();
}
section.split(|&b| b == b'\n').collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_simple_headers_and_message() {
let payload = b"tree abc\nparent def\n\nhello world\n";
let block = parse(payload, "commit").unwrap();
assert_eq!(block.headers.len(), 2);
assert_eq!(block.headers[0].key, "tree");
assert_eq!(block.headers[0].value, b"abc");
assert_eq!(block.message, b"hello world\n");
}
#[test]
fn parses_continuation_lines() {
let payload = b"gpgsig line1\n line2\n line3\n\nmsg\n";
let block = parse(payload, "commit").unwrap();
assert_eq!(block.headers.len(), 1);
assert_eq!(block.headers[0].key, "gpgsig");
assert_eq!(block.headers[0].value, b"line1\nline2\nline3");
// round-trips back to the on-disk continuation form
assert_eq!(serialize_value(&block.headers[0].value), b"line1\n line2\n line3");
}
}

57
src/lib.rs Normal file
View File

@ -0,0 +1,57 @@
//! # rithub (core slice)
//!
//! A library-first Rust implementation of the **Git loose-object model**: read,
//! write, verify, and traverse standard Git repositories without corrupting
//! data. This crate is the narrow first promise from `PLAN.md` — the
//! collaboration *forge* (issues, pull requests, permissions, webhooks, wire
//! protocol) is deliberately **deferred** until this core passes interop,
//! crash-recovery, and fuzz testing.
//!
//! ## What is implemented
//!
//! - [`object`] — object types, canonical `"<type> <len>\0<payload>"` framing,
//! SHA-1 object ids (byte-identical to real Git), and loose parsing.
//! - [`blob`], [`tree`], [`commit`], [`tag`] — read **and** write the four
//! loose object kinds with byte-exact serialization.
//! - [`odb`] — the loose object database under `.git/objects/xx/...` with zlib
//! compression and read-time SHA-1 verification.
//! - [`refs`] — `HEAD`, loose refs, symbolic refs, and `packed-refs`.
//! - [`repo`] — the porcelain-ish [`Repository`] API: `hash-object`, `cat-file`,
//! `rev-parse`, tree/commit traversal, and `log`.
//!
//! ## Compatibility
//!
//! Real-git interop is proven in `tests/interop.rs`: object ids computed here
//! are asserted identical to `git hash-object`, and repositories created by real
//! `git` are read back with this crate.
//!
//! ## Deferred (per PLAN)
//!
//! Packfiles/delta resolution, the index/worktree, the smart wire protocol,
//! merge/rebase, SHA-256 repositories, SHA-1 collision detection (sha1dc), and
//! the entire forge layer are out of scope for this slice.
pub mod blob;
pub mod commit;
pub mod error;
pub mod headers;
pub mod object;
pub mod odb;
pub mod oid;
pub mod refs;
pub mod repo;
pub mod signature;
pub mod tag;
pub mod tree;
pub use blob::Blob;
pub use commit::Commit;
pub use error::{ForgeError, Result};
pub use object::{hash_content, parse_loose, Object, ObjectType};
pub use odb::Odb;
pub use oid::ObjectId;
pub use refs::{RefTarget, Refs};
pub use repo::Repository;
pub use signature::Signature;
pub use tag::Tag;
pub use tree::{Tree, TreeEntry};

225
src/main.rs Normal file
View File

@ -0,0 +1,225 @@
//! `rgf` — a small, compatibility-oriented CLI over the rithub core.
//!
//! Runnable proof of the library: `hash-object`, `cat-file`, `ls-tree`, and
//! `log`, operating on real `.git` repositories.
use std::io::{Read, Write};
use std::path::PathBuf;
use anyhow::{bail, Context, Result};
use clap::{Args, Parser, Subcommand};
use rit::object::Object;
use rit::{ObjectType, Repository};
#[derive(Parser)]
#[command(
name = "rgf",
version,
about = "rithub: inspect and create Git objects (loose-object core slice)"
)]
struct Cli {
/// Repository path to operate on (its `.git` is discovered by walking up).
#[arg(short = 'C', long, default_value = ".", global = true)]
repo: PathBuf,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Compute an object id (and optionally write the object). Like `git hash-object`.
HashObject(HashObjectArgs),
/// Print an object's type or pretty content. Like `git cat-file`.
CatFile(CatFileArgs),
/// List the entries of a tree. Like `git ls-tree`.
LsTree(LsTreeArgs),
/// Show commit history from a revision (default HEAD). Like `git log`.
Log(LogArgs),
}
#[derive(Args)]
struct HashObjectArgs {
/// Object type to hash as.
#[arg(short = 't', long = "type", default_value = "blob")]
object_type: String,
/// Write the object into the object database.
#[arg(short = 'w')]
write: bool,
/// Read the content from stdin instead of a file.
#[arg(long)]
stdin: bool,
/// Path to the file to hash (unless `--stdin`).
file: Option<PathBuf>,
}
#[derive(Args)]
struct CatFileArgs {
/// Show the object type.
#[arg(short = 't', group = "mode")]
show_type: bool,
/// Pretty-print the object content.
#[arg(short = 'p', group = "mode")]
pretty: bool,
/// The revision / object to read (oid, ref, or HEAD).
object: String,
}
#[derive(Args)]
struct LsTreeArgs {
/// A revision resolving to a tree or a commit (whose tree is listed).
#[arg(default_value = "HEAD")]
rev: String,
}
#[derive(Args)]
struct LogArgs {
/// Revision to start from.
#[arg(default_value = "HEAD")]
rev: String,
/// Maximum number of commits to show.
#[arg(short = 'n', long)]
max: Option<usize>,
}
fn main() {
if let Err(err) = run() {
eprintln!("rgf: {err:#}");
std::process::exit(1);
}
}
fn run() -> Result<()> {
let cli = Cli::parse();
match &cli.command {
Command::HashObject(args) => cmd_hash_object(&cli, args),
Command::CatFile(args) => cmd_cat_file(&cli, args),
Command::LsTree(args) => cmd_ls_tree(&cli, args),
Command::Log(args) => cmd_log(&cli, args),
}
}
fn cmd_hash_object(cli: &Cli, args: &HashObjectArgs) -> Result<()> {
let obj_type = ObjectType::parse(&args.object_type)
.with_context(|| format!("invalid object type {:?}", args.object_type))?;
let data = if args.stdin {
let mut buf = Vec::new();
std::io::stdin()
.read_to_end(&mut buf)
.context("reading stdin")?;
buf
} else {
let file = args
.file
.as_ref()
.context("provide a file path or use --stdin")?;
std::fs::read(file).with_context(|| format!("reading {}", file.display()))?
};
// Writing requires a real repo; hashing-only does not.
let oid = if args.write {
let repo = Repository::discover(&cli.repo)
.with_context(|| format!("opening repo at {}", cli.repo.display()))?;
repo.hash_object(obj_type, &data, true)?
} else {
rit::hash_content(obj_type, &data).0
};
println!("{oid}");
Ok(())
}
fn cmd_cat_file(cli: &Cli, args: &CatFileArgs) -> Result<()> {
if !args.show_type && !args.pretty {
bail!("specify -t (type) or -p (pretty-print)");
}
let repo = Repository::discover(&cli.repo)
.with_context(|| format!("opening repo at {}", cli.repo.display()))?;
let oid = repo.resolve(&args.object)?;
if args.show_type {
println!("{}", repo.object_type(&oid)?);
return Ok(());
}
// pretty-print
match repo.read_object(&oid)? {
Object::Blob(b) => {
std::io::stdout().write_all(&b.data)?;
}
Object::Tree(t) => {
let mut out = std::io::stdout().lock();
for e in &t.entries {
writeln!(
out,
"{} {} {}\t{}",
e.display_mode(),
e.kind(),
e.oid,
e.name_str()
)?;
}
}
Object::Commit(c) => {
std::io::stdout().write_all(&c.serialize())?;
}
Object::Tag(t) => {
std::io::stdout().write_all(&t.serialize())?;
}
}
Ok(())
}
fn cmd_ls_tree(cli: &Cli, args: &LsTreeArgs) -> Result<()> {
let repo = Repository::discover(&cli.repo)
.with_context(|| format!("opening repo at {}", cli.repo.display()))?;
let oid = repo.resolve(&args.rev)?;
// Accept either a tree oid or a commit (list its tree).
let tree = match repo.read_object(&oid)? {
Object::Tree(t) => t,
Object::Commit(c) => repo.tree(&c.tree)?,
Object::Tag(_) => {
let commit = repo.peel_to_commit(oid)?;
repo.tree(&commit.tree)?
}
Object::Blob(_) => bail!("{oid} is a blob, not a tree"),
};
let mut out = std::io::stdout().lock();
for e in &tree.entries {
writeln!(
out,
"{} {} {}\t{}",
e.display_mode(),
e.kind(),
e.oid,
e.name_str()
)?;
}
Ok(())
}
fn cmd_log(cli: &Cli, args: &LogArgs) -> Result<()> {
let repo = Repository::discover(&cli.repo)
.with_context(|| format!("opening repo at {}", cli.repo.display()))?;
let start = repo.resolve(&args.rev)?;
let history = repo.log(start)?;
let mut out = std::io::stdout().lock();
for (oid, commit) in history.iter().take(args.max.unwrap_or(usize::MAX)) {
writeln!(out, "commit {oid}")?;
writeln!(
out,
"Author: {} <{}>",
commit.author.name, commit.author.email
)?;
writeln!(out, "Date: {} {}", commit.author.time, commit.author.tz)?;
writeln!(out)?;
for line in commit.message_str().lines() {
writeln!(out, " {line}")?;
}
writeln!(out)?;
}
Ok(())
}

208
src/object.rs Normal file
View File

@ -0,0 +1,208 @@
//! Core object model: types, canonical serialization, SHA-1 hashing, and the
//! loose-object framing shared by every Git object.
//!
//! Every Git object is `"<type> <len>\0<payload>"`, and its identity is the
//! SHA-1 of *those exact bytes*. Getting this framing byte-for-byte right is
//! what makes our object ids identical to real Git's.
use crate::blob::Blob;
use crate::commit::Commit;
use crate::error::{ForgeError, Result};
use crate::oid::{ObjectId, OID_LEN};
use crate::tag::Tag;
use crate::tree::Tree;
use sha1::{Digest, Sha1};
/// The four object kinds in the loose-object model.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ObjectType {
/// File contents.
Blob,
/// Directory listing.
Tree,
/// A history node.
Commit,
/// An annotated tag.
Tag,
}
impl ObjectType {
/// The keyword used in the object header and by `cat-file -t`.
pub const fn as_str(&self) -> &'static str {
match self {
ObjectType::Blob => "blob",
ObjectType::Tree => "tree",
ObjectType::Commit => "commit",
ObjectType::Tag => "tag",
}
}
/// Parse a header keyword.
pub fn parse(s: &str) -> Result<Self> {
match s {
"blob" => Ok(ObjectType::Blob),
"tree" => Ok(ObjectType::Tree),
"commit" => Ok(ObjectType::Commit),
"tag" => Ok(ObjectType::Tag),
other => Err(ForgeError::UnknownType(other.to_string())),
}
}
}
impl std::fmt::Display for ObjectType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
/// Maximum size we will decompress for a single loose object (a fail-closed
/// guard against decompression bombs — see PLAN "Security contract"). 512 MiB
/// is far above any realistic loose object.
pub const MAX_OBJECT_SIZE: u64 = 512 * 1024 * 1024;
/// Build the loose-object bytes for a payload and return `(oid, full_bytes)`.
///
/// `full_bytes` is the *uncompressed* `"<type> <len>\0<payload>"` framing whose
/// SHA-1 is the object id. This is the single source of truth for hashing.
pub fn hash_content(obj_type: ObjectType, payload: &[u8]) -> (ObjectId, Vec<u8>) {
let header = format!("{} {}\0", obj_type.as_str(), payload.len());
let mut full = Vec::with_capacity(header.len() + payload.len());
full.extend_from_slice(header.as_bytes());
full.extend_from_slice(payload);
let mut hasher = Sha1::new();
hasher.update(&full);
let digest = hasher.finalize();
let mut bytes = [0u8; OID_LEN];
bytes.copy_from_slice(&digest);
(ObjectId::from_bytes(bytes), full)
}
/// Split an uncompressed loose object into its type and payload.
///
/// Fails closed if the header is missing, non-numeric, or if the declared
/// length disagrees with the actual payload length (a corruption signal).
pub fn parse_loose(full: &[u8]) -> Result<(ObjectType, &[u8])> {
let nul = full
.iter()
.position(|&b| b == 0)
.ok_or_else(|| ForgeError::malformed("loose", "no NUL terminating the header"))?;
let header = &full[..nul];
let space = header
.iter()
.position(|&b| b == b' ')
.ok_or_else(|| ForgeError::malformed("loose", "no space in header"))?;
let type_str = std::str::from_utf8(&header[..space])
.map_err(|_| ForgeError::malformed("loose", "type is not ASCII"))?;
let len_str = std::str::from_utf8(&header[space + 1..])
.map_err(|_| ForgeError::malformed("loose", "length is not ASCII"))?;
let obj_type = ObjectType::parse(type_str)?;
let declared: usize = len_str
.parse()
.map_err(|_| ForgeError::malformed("loose", "length is not a number"))?;
let payload = &full[nul + 1..];
if payload.len() != declared {
return Err(ForgeError::malformed(
"loose",
format!(
"declared length {declared} but payload is {} bytes",
payload.len()
),
));
}
Ok((obj_type, payload))
}
/// A fully parsed Git object.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Object {
/// A blob.
Blob(Blob),
/// A tree.
Tree(Tree),
/// A commit.
Commit(Commit),
/// An annotated tag.
Tag(Tag),
}
impl Object {
/// Parse a typed object from its payload.
pub fn parse(obj_type: ObjectType, payload: &[u8]) -> Result<Self> {
Ok(match obj_type {
ObjectType::Blob => Object::Blob(Blob::new(payload.to_vec())),
ObjectType::Tree => Object::Tree(Tree::parse(payload)?),
ObjectType::Commit => Object::Commit(Commit::parse(payload)?),
ObjectType::Tag => Object::Tag(Tag::parse(payload)?),
})
}
/// This object's type.
pub fn object_type(&self) -> ObjectType {
match self {
Object::Blob(_) => ObjectType::Blob,
Object::Tree(_) => ObjectType::Tree,
Object::Commit(_) => ObjectType::Commit,
Object::Tag(_) => ObjectType::Tag,
}
}
/// The canonical payload bytes (round-trips Git's serialization).
pub fn serialize_payload(&self) -> Vec<u8> {
match self {
Object::Blob(b) => b.data.clone(),
Object::Tree(t) => t.serialize(),
Object::Commit(c) => c.serialize(),
Object::Tag(t) => t.serialize(),
}
}
/// The object id this object hashes to.
pub fn id(&self) -> ObjectId {
hash_content(self.object_type(), &self.serialize_payload()).0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_blob_matches_git_constant() {
// Every Git repo agrees on the empty blob oid.
let (id, _) = hash_content(ObjectType::Blob, b"");
assert_eq!(id.to_hex(), "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
}
#[test]
fn hello_blob_matches_known_git_oid() {
// `printf 'hello\n' | git hash-object --stdin` -> this oid.
let (id, _) = hash_content(ObjectType::Blob, b"hello\n");
assert_eq!(id.to_hex(), "ce013625030ba8dba906f756967f9e9ca394464a");
}
#[test]
fn loose_framing_roundtrips() {
let (_, full) = hash_content(ObjectType::Blob, b"payload");
let (ty, payload) = parse_loose(&full).unwrap();
assert_eq!(ty, ObjectType::Blob);
assert_eq!(payload, b"payload");
}
#[test]
fn rejects_length_mismatch() {
let mut full = b"blob 99\0short".to_vec();
assert!(parse_loose(&full).is_err());
full = b"blob\0nolen".to_vec();
assert!(parse_loose(&full).is_err());
}
#[test]
fn rejects_unknown_type() {
assert!(parse_loose(b"widget 3\0abc").is_err());
}
}

232
src/odb.rs Normal file
View File

@ -0,0 +1,232 @@
//! The loose object database: read, write, and verify objects stored under
//! `.git/objects/xx/<38-hex>` in zlib-compressed loose format.
//!
//! Reads **always verify** that the decompressed bytes hash back to the oid
//! they were requested under — corruption is surfaced as an error, never
//! silently returned (PLAN: "inspect ... without corrupting data").
use crate::error::{ForgeError, Result};
use crate::object::{self, Object, ObjectType, MAX_OBJECT_SIZE};
use crate::oid::ObjectId;
use flate2::read::ZlibDecoder;
use flate2::write::ZlibEncoder;
use flate2::Compression;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
/// A loose object database rooted at a `.git/objects` directory.
pub struct Odb {
objects_dir: PathBuf,
}
impl Odb {
/// Open the object database inside a `.git` directory.
pub fn new(git_dir: &Path) -> Self {
Self {
objects_dir: git_dir.join("objects"),
}
}
/// The on-disk path for an object (`objects/xx/<rest>`).
pub fn loose_path(&self, oid: &ObjectId) -> PathBuf {
self.objects_dir
.join(oid.dir_prefix())
.join(oid.file_suffix())
}
/// Whether an object exists on disk.
pub fn contains(&self, oid: &ObjectId) -> bool {
self.loose_path(oid).exists()
}
/// Write a payload as a loose object and return its oid.
///
/// Idempotent: if the object already exists it is left untouched (its oid is
/// content-addressed, so identical content is identical bytes). The write is
/// atomic — bytes land in a temp file, are fsync'd, then renamed into place.
pub fn write(&self, obj_type: ObjectType, payload: &[u8]) -> Result<ObjectId> {
let (oid, full) = object::hash_content(obj_type, payload);
let final_path = self.loose_path(&oid);
if final_path.exists() {
return Ok(oid);
}
let dir = final_path
.parent()
.expect("loose path always has a parent directory");
std::fs::create_dir_all(dir)?;
let compressed = zlib_compress(&full)?;
// Atomic publish: write to a unique temp file, flush + fsync, rename.
let tmp_path = self.temp_path(dir);
{
let mut file = std::fs::File::create(&tmp_path)?;
file.write_all(&compressed)?;
file.sync_all()?;
}
// rename is atomic within a filesystem; if it fails, clean up the temp.
if let Err(e) = std::fs::rename(&tmp_path, &final_path) {
let _ = std::fs::remove_file(&tmp_path);
return Err(e.into());
}
Ok(oid)
}
/// Read an object's raw type and payload, verifying its SHA-1.
pub fn read_raw(&self, oid: &ObjectId) -> Result<(ObjectType, Vec<u8>)> {
let path = self.loose_path(oid);
let compressed = std::fs::read(&path)
.map_err(|_| ForgeError::ObjectNotFound(oid.to_hex()))?;
let full = zlib_decompress(&compressed)?;
// Verify integrity before trusting the bytes.
let computed = {
use sha1::{Digest, Sha1};
let mut h = Sha1::new();
h.update(&full);
let d = h.finalize();
let mut b = [0u8; 20];
b.copy_from_slice(&d);
ObjectId::from_bytes(b)
};
if &computed != oid {
return Err(ForgeError::HashMismatch {
looked_up: oid.to_hex(),
computed: computed.to_hex(),
});
}
let (obj_type, payload) = object::parse_loose(&full)?;
Ok((obj_type, payload.to_vec()))
}
/// Read and fully parse an object.
pub fn read(&self, oid: &ObjectId) -> Result<Object> {
let (obj_type, payload) = self.read_raw(oid)?;
Object::parse(obj_type, &payload)
}
/// Read only an object's type (`cat-file -t`).
pub fn object_type(&self, oid: &ObjectId) -> Result<ObjectType> {
Ok(self.read_raw(oid)?.0)
}
/// Resolve an abbreviated hex prefix (>= 4 chars) to a unique full oid.
pub fn resolve_prefix(&self, prefix: &str) -> Result<ObjectId> {
if prefix.len() < 4 || prefix.len() > 40 {
return Err(ForgeError::InvalidOid(format!(
"prefix must be 4-40 hex chars: {prefix:?}"
)));
}
let (dir_part, file_part) = prefix.split_at(2.min(prefix.len()));
let subdir = self.objects_dir.join(dir_part);
let mut matches: Vec<ObjectId> = Vec::new();
if let Ok(read_dir) = std::fs::read_dir(&subdir) {
for entry in read_dir.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
let full_hex = format!("{dir_part}{name}");
if full_hex.starts_with(prefix) {
if let Ok(oid) = ObjectId::from_hex(&full_hex) {
matches.push(oid);
}
}
}
}
let _ = file_part;
match matches.len() {
0 => Err(ForgeError::ObjectNotFound(prefix.to_string())),
1 => Ok(matches[0]),
_ => Err(ForgeError::AmbiguousPrefix(prefix.to_string())),
}
}
/// Build a unique temporary file path inside `dir` (no extra deps).
fn temp_path(&self, dir: &Path) -> PathBuf {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let pid = std::process::id();
dir.join(format!(".rgf-tmp-{pid}-{nanos}"))
}
}
/// Compress bytes with zlib (the format Git uses for loose objects).
fn zlib_compress(data: &[u8]) -> Result<Vec<u8>> {
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
encoder.write_all(data)?;
encoder
.finish()
.map_err(|e| ForgeError::Decompress(format!("compression failed: {e}")))
}
/// Decompress a zlib stream, guarding against decompression bombs.
fn zlib_decompress(data: &[u8]) -> Result<Vec<u8>> {
let decoder = ZlibDecoder::new(data);
let mut limited = decoder.take(MAX_OBJECT_SIZE + 1);
let mut out = Vec::new();
limited
.read_to_end(&mut out)
.map_err(|e| ForgeError::Decompress(e.to_string()))?;
if out.len() as u64 > MAX_OBJECT_SIZE {
return Err(ForgeError::Decompress(format!(
"object exceeds {MAX_OBJECT_SIZE} byte cap"
)));
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::object::Object;
fn temp_git_dir() -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir_all(dir.path().join("objects")).unwrap();
dir
}
#[test]
fn write_then_read_roundtrips_blob() {
let dir = temp_git_dir();
let odb = Odb::new(dir.path());
let oid = odb.write(ObjectType::Blob, b"hello odb\n").unwrap();
assert!(odb.contains(&oid));
match odb.read(&oid).unwrap() {
Object::Blob(b) => assert_eq!(b.data, b"hello odb\n"),
other => panic!("expected blob, got {other:?}"),
}
}
#[test]
fn write_is_idempotent() {
let dir = temp_git_dir();
let odb = Odb::new(dir.path());
let a = odb.write(ObjectType::Blob, b"same").unwrap();
let b = odb.write(ObjectType::Blob, b"same").unwrap();
assert_eq!(a, b);
}
#[test]
fn detects_corruption() {
let dir = temp_git_dir();
let odb = Odb::new(dir.path());
let oid = odb.write(ObjectType::Blob, b"trust me").unwrap();
// Corrupt the stored bytes.
let path = odb.loose_path(&oid);
std::fs::write(&path, b"not even zlib").unwrap();
assert!(odb.read_raw(&oid).is_err());
}
#[test]
fn resolve_prefix_finds_unique() {
let dir = temp_git_dir();
let odb = Odb::new(dir.path());
let oid = odb.write(ObjectType::Blob, b"prefix me\n").unwrap();
let short = &oid.to_hex()[..8];
assert_eq!(odb.resolve_prefix(short).unwrap(), oid);
}
}

118
src/oid.rs Normal file
View File

@ -0,0 +1,118 @@
//! Git object identifiers.
//!
//! An [`ObjectId`] is a 20-byte SHA-1 digest — the same identity Git assigns to
//! every object. The hex form is the familiar 40-character string printed by
//! `git rev-parse`.
use crate::error::{ForgeError, Result};
use std::fmt;
/// Number of raw bytes in a SHA-1 object id.
pub const OID_LEN: usize = 20;
/// Number of hex characters in a SHA-1 object id.
pub const OID_HEX_LEN: usize = OID_LEN * 2;
/// A 20-byte SHA-1 Git object identifier.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ObjectId([u8; OID_LEN]);
impl ObjectId {
/// Wrap 20 raw digest bytes.
pub const fn from_bytes(bytes: [u8; OID_LEN]) -> Self {
Self(bytes)
}
/// Borrow the raw digest bytes (used when serializing tree entries).
pub const fn as_bytes(&self) -> &[u8; OID_LEN] {
&self.0
}
/// Parse a full 40-character lowercase/uppercase hex string.
pub fn from_hex(s: &str) -> Result<Self> {
let s = s.trim();
if s.len() != OID_HEX_LEN {
return Err(ForgeError::InvalidOid(format!(
"expected {OID_HEX_LEN} hex chars, got {}",
s.len()
)));
}
let mut bytes = [0u8; OID_LEN];
let raw = s.as_bytes();
for (i, byte) in bytes.iter_mut().enumerate() {
let hi = hex_val(raw[i * 2])
.ok_or_else(|| ForgeError::InvalidOid(format!("non-hex character in {s:?}")))?;
let lo = hex_val(raw[i * 2 + 1])
.ok_or_else(|| ForgeError::InvalidOid(format!("non-hex character in {s:?}")))?;
*byte = (hi << 4) | lo;
}
Ok(Self(bytes))
}
/// Render the lowercase 40-character hex form.
pub fn to_hex(&self) -> String {
const HEX: &[u8; 16] = b"0123456789abcdef";
let mut out = String::with_capacity(OID_HEX_LEN);
for &b in &self.0 {
out.push(HEX[(b >> 4) as usize] as char);
out.push(HEX[(b & 0x0f) as usize] as char);
}
out
}
/// The two-character sharded subdirectory (`objects/xx/...`).
pub fn dir_prefix(&self) -> String {
self.to_hex()[..2].to_string()
}
/// The remaining 38 hex characters (`objects/xx/<rest>`).
pub fn file_suffix(&self) -> String {
self.to_hex()[2..].to_string()
}
}
impl fmt::Display for ObjectId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.to_hex())
}
}
impl fmt::Debug for ObjectId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ObjectId({})", self.to_hex())
}
}
/// Map an ASCII hex digit to its numeric value, or `None` if not hex.
fn hex_val(c: u8) -> Option<u8> {
match c {
b'0'..=b'9' => Some(c - b'0'),
b'a'..=b'f' => Some(c - b'a' + 10),
b'A'..=b'F' => Some(c - b'A' + 10),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hex_roundtrips() {
let hex = "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391";
let oid = ObjectId::from_hex(hex).unwrap();
assert_eq!(oid.to_hex(), hex);
assert_eq!(oid.dir_prefix(), "e6");
assert_eq!(oid.file_suffix(), "9de29bb2d1d6434b8b29ae775ad8c2e48c5391");
}
#[test]
fn rejects_bad_length() {
assert!(ObjectId::from_hex("abc").is_err());
}
#[test]
fn rejects_non_hex() {
let bad = "z69de29bb2d1d6434b8b29ae775ad8c2e48c5391";
assert!(ObjectId::from_hex(bad).is_err());
}
}

198
src/refs.rs Normal file
View File

@ -0,0 +1,198 @@
//! References: `HEAD`, loose refs under `.git/refs/`, and `.git/packed-refs`.
//!
//! Supports symbolic refs (`ref: refs/heads/main`), direct oids, and the
//! standard shorthand resolution order Git uses (`main` ->
//! `refs/heads/main`, etc.). Enough to answer "what commit is HEAD?".
use crate::error::{ForgeError, Result};
use crate::oid::ObjectId;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
/// A resolved-one-step reference value.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RefTarget {
/// A symbolic ref pointing at another ref name.
Symbolic(String),
/// A direct object id.
Direct(ObjectId),
}
/// Ref storage for one repository.
pub struct Refs {
git_dir: PathBuf,
}
/// Guard against symbolic-ref cycles.
const MAX_REF_DEPTH: usize = 10;
impl Refs {
/// Create ref access rooted at a `.git` directory.
pub fn new(git_dir: &Path) -> Self {
Self {
git_dir: git_dir.to_path_buf(),
}
}
/// Read and interpret `.git/HEAD`.
pub fn read_head(&self) -> Result<RefTarget> {
let raw = std::fs::read_to_string(self.git_dir.join("HEAD"))
.map_err(|_| ForgeError::InvalidRef("HEAD is unreadable".into()))?;
parse_ref_content(raw.trim())
}
/// Resolve `HEAD` all the way to an object id.
pub fn resolve_head(&self) -> Result<ObjectId> {
match self.read_head()? {
RefTarget::Direct(oid) => Ok(oid),
RefTarget::Symbolic(name) => self.resolve(&name),
}
}
/// Resolve a ref name (full or shorthand) to an object id.
pub fn resolve(&self, name: &str) -> Result<ObjectId> {
self.resolve_depth(name, 0)
}
fn resolve_depth(&self, name: &str, depth: usize) -> Result<ObjectId> {
if depth > MAX_REF_DEPTH {
return Err(ForgeError::InvalidRef(format!(
"symbolic ref chain too deep at {name}"
)));
}
if name == "HEAD" {
return self.resolve_head();
}
for candidate in self.candidate_paths(name) {
if let Some(target) = self.read_loose_ref(&candidate)? {
return match target {
RefTarget::Direct(oid) => Ok(oid),
RefTarget::Symbolic(next) => self.resolve_depth(&next, depth + 1),
};
}
}
// Fall back to packed-refs, keyed by full ref name.
let packed = self.read_packed_refs()?;
for candidate in self.candidate_names(name) {
if let Some(oid) = packed.get(&candidate) {
return Ok(*oid);
}
}
Err(ForgeError::InvalidRef(name.to_string()))
}
/// List all loose ref names and their resolved oids (used by `log` and
/// for listing). Packed refs are merged in, loose winning on conflict.
pub fn list(&self) -> Result<Vec<(String, ObjectId)>> {
let mut out: HashMap<String, ObjectId> = self.read_packed_refs()?;
let refs_root = self.git_dir.join("refs");
collect_loose(&refs_root, &refs_root, &mut out)?;
let mut v: Vec<(String, ObjectId)> = out.into_iter().collect();
v.sort_by(|a, b| a.0.cmp(&b.0));
Ok(v)
}
/// Read one loose ref file, if present, one step (no recursion).
fn read_loose_ref(&self, refname: &str) -> Result<Option<RefTarget>> {
let path = self.git_dir.join(refname);
match std::fs::read_to_string(&path) {
Ok(raw) => Ok(Some(parse_ref_content(raw.trim())?)),
Err(_) => Ok(None),
}
}
/// Parse `.git/packed-refs` into a name -> oid map (peel lines ignored).
fn read_packed_refs(&self) -> Result<HashMap<String, ObjectId>> {
let path = self.git_dir.join("packed-refs");
let mut map = HashMap::new();
let raw = match std::fs::read_to_string(&path) {
Ok(r) => r,
Err(_) => return Ok(map), // absent packed-refs is normal
};
for line in raw.lines() {
if line.starts_with('#') || line.starts_with('^') || line.trim().is_empty() {
continue;
}
if let Some((oid_hex, name)) = line.split_once(' ') {
if let Ok(oid) = ObjectId::from_hex(oid_hex) {
map.insert(name.trim().to_string(), oid);
}
}
}
Ok(map)
}
/// Candidate loose-ref *paths* (relative to `.git`) for a shorthand name.
fn candidate_paths(&self, name: &str) -> Vec<String> {
self.candidate_names(name)
}
/// Standard Git ref resolution order for a shorthand.
fn candidate_names(&self, name: &str) -> Vec<String> {
if name.starts_with("refs/") {
return vec![name.to_string()];
}
vec![
name.to_string(),
format!("refs/{name}"),
format!("refs/tags/{name}"),
format!("refs/heads/{name}"),
format!("refs/remotes/{name}"),
format!("refs/remotes/{name}/HEAD"),
]
}
}
/// Interpret raw ref file / HEAD content.
fn parse_ref_content(content: &str) -> Result<RefTarget> {
if let Some(target) = content.strip_prefix("ref:") {
Ok(RefTarget::Symbolic(target.trim().to_string()))
} else {
Ok(RefTarget::Direct(ObjectId::from_hex(content)?))
}
}
/// Recursively collect loose refs under `dir`, keyed by full ref name.
fn collect_loose(
root: &Path,
dir: &Path,
out: &mut HashMap<String, ObjectId>,
) -> Result<()> {
let read_dir = match std::fs::read_dir(dir) {
Ok(rd) => rd,
Err(_) => return Ok(()),
};
for entry in read_dir.flatten() {
let path = entry.path();
if path.is_dir() {
collect_loose(root, &path, out)?;
} else if let Ok(raw) = std::fs::read_to_string(&path) {
// Build "refs/..." name relative to the .git dir.
if let Ok(rel) = path.strip_prefix(root.parent().unwrap_or(root)) {
let name = rel.to_string_lossy().replace('\\', "/");
if let Ok(RefTarget::Direct(oid)) = parse_ref_content(raw.trim()) {
out.insert(name, oid);
}
}
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_symbolic_head() {
let t = parse_ref_content("ref: refs/heads/main").unwrap();
assert_eq!(t, RefTarget::Symbolic("refs/heads/main".into()));
}
#[test]
fn parses_detached_head() {
let hex = "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391";
let t = parse_ref_content(hex).unwrap();
assert_eq!(t, RefTarget::Direct(ObjectId::from_hex(hex).unwrap()));
}
}

222
src/repo.rs Normal file
View File

@ -0,0 +1,222 @@
//! The porcelain-ish repository API — the layer that proves the PLAN's first
//! promise: "inspect, create, verify ... standard Git repositories without
//! corrupting data."
//!
//! [`Repository`] ties the object database and refs together and exposes the
//! equivalents of `hash-object -w`, `cat-file -p/-t`, `rev-parse`, `ls-tree`,
//! and `log`.
use crate::blob::Blob;
use crate::commit::Commit;
use crate::error::{ForgeError, Result};
use crate::object::{Object, ObjectType};
use crate::odb::Odb;
use crate::oid::ObjectId;
use crate::refs::Refs;
use crate::tree::Tree;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
/// An opened Git repository (loose-object scope).
pub struct Repository {
git_dir: PathBuf,
odb: Odb,
refs: Refs,
}
impl Repository {
/// Open a repository given its `.git` directory directly.
pub fn open_git_dir(git_dir: impl AsRef<Path>) -> Result<Self> {
let git_dir = git_dir.as_ref().to_path_buf();
if !git_dir.join("objects").is_dir() {
return Err(ForgeError::NotARepository(git_dir.display().to_string()));
}
let odb = Odb::new(&git_dir);
let refs = Refs::new(&git_dir);
Ok(Self {
git_dir,
odb,
refs,
})
}
/// Discover a repository by walking up from `start` looking for `.git`.
pub fn discover(start: impl AsRef<Path>) -> Result<Self> {
let start = start.as_ref();
let mut cur: Option<&Path> = Some(start);
while let Some(dir) = cur {
let candidate = dir.join(".git");
if candidate.is_dir() {
return Self::open_git_dir(candidate);
}
// Also accept being handed a `.git` dir itself.
if dir.file_name().map(|n| n == ".git").unwrap_or(false) && dir.join("objects").is_dir()
{
return Self::open_git_dir(dir);
}
cur = dir.parent();
}
Err(ForgeError::NotARepository(start.display().to_string()))
}
/// The `.git` directory path.
pub fn git_dir(&self) -> &Path {
&self.git_dir
}
/// Borrow the object database.
pub fn odb(&self) -> &Odb {
&self.odb
}
/// Borrow ref access.
pub fn refs(&self) -> &Refs {
&self.refs
}
// --- object plumbing --------------------------------------------------
/// `git hash-object [-w]`: hash `data` as `obj_type`, optionally writing it.
pub fn hash_object(&self, obj_type: ObjectType, data: &[u8], write: bool) -> Result<ObjectId> {
if write {
self.odb.write(obj_type, data)
} else {
Ok(crate::object::hash_content(obj_type, data).0)
}
}
/// `git cat-file <type> <oid>`: read and parse an object.
pub fn read_object(&self, oid: &ObjectId) -> Result<Object> {
self.odb.read(oid)
}
/// `git cat-file -t <oid>`: an object's type.
pub fn object_type(&self, oid: &ObjectId) -> Result<ObjectType> {
self.odb.object_type(oid)
}
/// Read an object expected to be a blob.
pub fn blob(&self, oid: &ObjectId) -> Result<Blob> {
match self.odb.read(oid)? {
Object::Blob(b) => Ok(b),
other => Err(type_error(oid, "blob", other)),
}
}
/// Read an object expected to be a tree.
pub fn tree(&self, oid: &ObjectId) -> Result<Tree> {
match self.odb.read(oid)? {
Object::Tree(t) => Ok(t),
other => Err(type_error(oid, "tree", other)),
}
}
/// Read an object expected to be a commit.
pub fn commit(&self, oid: &ObjectId) -> Result<Commit> {
match self.odb.read(oid)? {
Object::Commit(c) => Ok(c),
other => Err(type_error(oid, "commit", other)),
}
}
/// Verify that a stored object is not corrupt (its bytes hash to its oid).
/// `read_raw` performs the check; this exposes it as a boolean.
pub fn verify(&self, oid: &ObjectId) -> Result<bool> {
match self.odb.read_raw(oid) {
Ok(_) => Ok(true),
Err(ForgeError::HashMismatch { .. }) => Ok(false),
Err(e) => Err(e),
}
}
// --- refs / revisions -------------------------------------------------
/// The commit `HEAD` points at.
pub fn head(&self) -> Result<ObjectId> {
self.refs.resolve_head()
}
/// Read the commit `HEAD` currently points at.
pub fn head_commit(&self) -> Result<Commit> {
let oid = self.head()?;
self.peel_to_commit(oid)
}
/// Read the tree of the `HEAD` commit.
pub fn head_tree(&self) -> Result<Tree> {
let commit = self.head_commit()?;
self.tree(&commit.tree)
}
/// Resolve a revision string: `HEAD`, a ref name, a full 40-hex oid, or an
/// unambiguous abbreviated oid prefix.
pub fn resolve(&self, rev: &str) -> Result<ObjectId> {
let rev = rev.trim();
// Full oid?
if rev.len() == 40 {
if let Ok(oid) = ObjectId::from_hex(rev) {
return Ok(oid);
}
}
// Ref (HEAD, branch, tag, remote)?
if let Ok(oid) = self.refs.resolve(rev) {
return Ok(oid);
}
// Abbreviated oid?
if rev.len() >= 4 && rev.len() < 40 && rev.chars().all(|c| c.is_ascii_hexdigit()) {
return self.odb.resolve_prefix(rev);
}
Err(ForgeError::InvalidRef(rev.to_string()))
}
/// If `oid` is an annotated tag, follow it to the commit it references;
/// otherwise require `oid` to already be a commit.
pub fn peel_to_commit(&self, oid: ObjectId) -> Result<Commit> {
let mut current = oid;
for _ in 0..10 {
match self.odb.read(&current)? {
Object::Commit(c) => return Ok(c),
Object::Tag(t) => current = t.object,
other => return Err(type_error(&current, "commit", other)),
}
}
Err(ForgeError::InvalidRef(format!(
"{oid} does not peel to a commit"
)))
}
/// `git log`: walk history from `start`, newest first by committer time.
///
/// Bounded by a visited set so cycles or malicious graphs cannot loop
/// forever (PLAN "Security contract": no unbounded graph walks).
pub fn log(&self, start: ObjectId) -> Result<Vec<(ObjectId, Commit)>> {
let mut seen: HashSet<ObjectId> = HashSet::new();
let mut stack: Vec<ObjectId> = vec![start];
let mut out: Vec<(ObjectId, Commit)> = Vec::new();
while let Some(oid) = stack.pop() {
if !seen.insert(oid) {
continue;
}
let commit = self.peel_to_commit(oid)?;
for parent in &commit.parents {
if !seen.contains(parent) {
stack.push(*parent);
}
}
out.push((oid, commit));
}
// Newest first; committer time is a stable, Git-visible ordering.
out.sort_by(|a, b| b.1.committer.time.cmp(&a.1.committer.time));
Ok(out)
}
}
/// Build a helpful "wrong type" error.
fn type_error(oid: &ObjectId, expected: &'static str, got: Object) -> ForgeError {
ForgeError::malformed(
"object",
format!("{oid} is a {}, expected {expected}", got.object_type()),
)
}

102
src/signature.rs Normal file
View File

@ -0,0 +1,102 @@
//! Author / committer / tagger signatures.
//!
//! A Git signature line looks like:
//!
//! ```text
//! Alice Example <alice@example.com> 1700000000 +0100
//! ```
//!
//! We keep the exact `raw` value so re-serialization is byte-identical to what
//! Git wrote, while also exposing parsed `name` / `email` / `time` for the API.
use crate::error::{ForgeError, Result};
/// A parsed Git identity + timestamp.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Signature {
/// The exact bytes after the `author `/`committer `/`tagger ` prefix.
/// Preserved verbatim so serialization round-trips Git output exactly.
pub raw: String,
/// Display name (everything before the ` <`).
pub name: String,
/// Email address (between `<` and `>`).
pub email: String,
/// Unix timestamp in seconds.
pub time: i64,
/// Timezone offset token, e.g. `+0100`.
pub tz: String,
}
impl Signature {
/// Parse a signature value (the text after the header keyword).
///
/// Fails closed on a missing email or timestamp rather than guessing.
pub fn parse(value: &str, kind: &'static str) -> Result<Self> {
let lt = value
.find('<')
.ok_or_else(|| ForgeError::malformed(kind, "signature missing '<' in email"))?;
let gt = value
.find('>')
.ok_or_else(|| ForgeError::malformed(kind, "signature missing '>' in email"))?;
if gt < lt {
return Err(ForgeError::malformed(kind, "signature '>' precedes '<'"));
}
let name = value[..lt].trim().to_string();
let email = value[lt + 1..gt].to_string();
let rest = value[gt + 1..].trim();
// rest is "<unixtime> <tz>". Both are optional in some hand-written
// objects, but Git always writes them, so we require them.
let mut parts = rest.split_whitespace();
let time = parts
.next()
.ok_or_else(|| ForgeError::malformed(kind, "signature missing timestamp"))?
.parse::<i64>()
.map_err(|_| ForgeError::malformed(kind, "signature timestamp is not an integer"))?;
let tz = parts
.next()
.ok_or_else(|| ForgeError::malformed(kind, "signature missing timezone"))?
.to_string();
Ok(Self {
raw: value.to_string(),
name,
email,
time,
tz,
})
}
/// The exact on-disk value (round-trips Git output).
pub fn to_raw(&self) -> &str {
&self.raw
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_standard_signature() {
let sig = Signature::parse("Alice Example <alice@example.com> 1700000000 +0100", "commit")
.unwrap();
assert_eq!(sig.name, "Alice Example");
assert_eq!(sig.email, "alice@example.com");
assert_eq!(sig.time, 1700000000);
assert_eq!(sig.tz, "+0100");
// raw preserved for byte-exact re-serialization
assert_eq!(sig.to_raw(), "Alice Example <alice@example.com> 1700000000 +0100");
}
#[test]
fn rejects_missing_email() {
assert!(Signature::parse("Alice 1700000000 +0100", "commit").is_err());
}
#[test]
fn rejects_missing_timestamp() {
assert!(Signature::parse("Alice <a@b.c>", "commit").is_err());
}
}

147
src/tag.rs Normal file
View File

@ -0,0 +1,147 @@
//! Annotated tag objects.
//!
//! Payload layout:
//!
//! ```text
//! object <40-hex>\n
//! type <object-type>\n
//! tag <name>\n
//! tagger <signature>\n
//! \n
//! <message>
//! ```
use crate::error::{ForgeError, Result};
use crate::headers::{self, Header};
use crate::object::ObjectType;
use crate::oid::ObjectId;
use crate::signature::Signature;
/// A parsed annotated tag.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Tag {
/// The tagged object's id.
pub object: ObjectId,
/// The tagged object's type.
pub object_type: ObjectType,
/// The tag name (e.g. `v1.0.0`).
pub tag: String,
/// The tagger identity (Git always writes one; lightweight tags are refs,
/// not tag objects, so this is expected to be present).
pub tagger: Option<Signature>,
/// Any other headers, preserved in order for byte-exact serialization.
pub extra_headers: Vec<Header>,
/// The tag message body.
pub message: Vec<u8>,
}
impl Tag {
/// Parse a tag payload, failing closed on missing required headers.
pub fn parse(payload: &[u8]) -> Result<Self> {
let block = headers::parse(payload, "tag")?;
let mut object = None;
let mut object_type = None;
let mut tag = None;
let mut tagger = None;
let mut extra_headers = Vec::new();
for h in block.headers {
match h.key.as_str() {
"object" => {
let hex = std::str::from_utf8(&h.value)
.map_err(|_| ForgeError::malformed("tag", "object oid not ASCII"))?;
object = Some(ObjectId::from_hex(hex)?);
}
"type" => {
let s = std::str::from_utf8(&h.value)
.map_err(|_| ForgeError::malformed("tag", "type not ASCII"))?;
object_type = Some(ObjectType::parse(s)?);
}
"tag" => {
tag = Some(
std::str::from_utf8(&h.value)
.map_err(|_| ForgeError::malformed("tag", "tag name not UTF-8"))?
.to_string(),
);
}
"tagger" => {
let v = std::str::from_utf8(&h.value)
.map_err(|_| ForgeError::malformed("tag", "tagger not UTF-8"))?;
tagger = Some(Signature::parse(v, "tag")?);
}
_ => extra_headers.push(h),
}
}
Ok(Self {
object: object.ok_or_else(|| ForgeError::malformed("tag", "missing object header"))?,
object_type: object_type
.ok_or_else(|| ForgeError::malformed("tag", "missing type header"))?,
tag: tag.ok_or_else(|| ForgeError::malformed("tag", "missing tag header"))?,
tagger,
extra_headers,
message: block.message,
})
}
/// Serialize back to the exact on-disk payload.
pub fn serialize(&self) -> Vec<u8> {
let mut out = Vec::new();
push_header(&mut out, "object", self.object.to_hex().as_bytes());
push_header(&mut out, "type", self.object_type.as_str().as_bytes());
push_header(&mut out, "tag", self.tag.as_bytes());
if let Some(tagger) = &self.tagger {
push_header(&mut out, "tagger", tagger.to_raw().as_bytes());
}
for h in &self.extra_headers {
push_header(&mut out, &h.key, &h.value);
}
out.push(b'\n');
out.extend_from_slice(&self.message);
out
}
/// The message as a lossy UTF-8 string.
pub fn message_str(&self) -> std::borrow::Cow<'_, str> {
String::from_utf8_lossy(&self.message)
}
}
fn push_header(out: &mut Vec<u8>, key: &str, value: &[u8]) {
out.extend_from_slice(key.as_bytes());
out.push(b' ');
out.extend_from_slice(&headers::serialize_value(value));
out.push(b'\n');
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_serialize_roundtrips() {
let payload = concat!(
"object e69de29bb2d1d6434b8b29ae775ad8c2e48c5391\n",
"type commit\n",
"tag v1.0.0\n",
"tagger Alice <a@b.c> 1700000000 +0000\n",
"\n",
"release one\n",
)
.as_bytes()
.to_vec();
let tag = Tag::parse(&payload).unwrap();
assert_eq!(tag.tag, "v1.0.0");
assert_eq!(tag.object_type, ObjectType::Commit);
assert!(tag.tagger.is_some());
assert_eq!(tag.serialize(), payload);
}
#[test]
fn rejects_missing_object() {
let payload = b"type commit\ntag v1\ntagger A <a@b.c> 1 +0000\n\nmsg\n";
assert!(Tag::parse(payload).is_err());
}
}

208
src/tree.rs Normal file
View File

@ -0,0 +1,208 @@
//! Tree objects: the directory listings of a Git repository.
//!
//! On-disk payload format is a concatenation of entries, each:
//!
//! ```text
//! <mode> <name>\0<20-byte-oid>
//! ```
//!
//! `mode` is octal ASCII with no fixed width (Git stores `40000` for a
//! sub-tree, not `040000`), `name` is raw path-component bytes, and the oid is
//! 20 raw bytes (not hex).
use crate::error::{ForgeError, Result};
use crate::oid::{ObjectId, OID_LEN};
/// A single entry (child) of a tree.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TreeEntry {
/// Mode string exactly as stored (e.g. `100644`, `100755`, `40000`).
pub mode: String,
/// Entry name (a single path component), preserved as raw bytes because
/// Git does not require paths to be valid UTF-8.
pub name: Vec<u8>,
/// The child object id (blob for files, tree for directories).
pub oid: ObjectId,
}
impl TreeEntry {
/// The entry name as a lossy UTF-8 string, for display.
pub fn name_str(&self) -> std::borrow::Cow<'_, str> {
String::from_utf8_lossy(&self.name)
}
/// The human `git cat-file`-style kind derived from the mode.
pub fn kind(&self) -> &'static str {
match self.mode.as_str() {
"40000" | "040000" => "tree",
"160000" => "commit", // gitlink / submodule
_ => "blob",
}
}
/// The mode left-padded to 6 digits, matching `git cat-file -p` output
/// (Git prints `040000` for trees even though it stores `40000`).
pub fn display_mode(&self) -> String {
format!("{:0>6}", self.mode)
}
}
/// A parsed tree object.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct Tree {
/// Entries in stored order (Git stores them sorted).
pub entries: Vec<TreeEntry>,
}
impl Tree {
/// Parse a tree payload into entries, failing closed on truncation.
pub fn parse(payload: &[u8]) -> Result<Self> {
let mut entries = Vec::new();
let mut pos = 0;
while pos < payload.len() {
// mode: bytes up to the space
let space = payload[pos..]
.iter()
.position(|&b| b == b' ')
.ok_or_else(|| ForgeError::malformed("tree", "entry missing space after mode"))?;
let mode = std::str::from_utf8(&payload[pos..pos + space])
.map_err(|_| ForgeError::malformed("tree", "mode is not ASCII"))?
.to_string();
pos += space + 1;
// name: bytes up to the NUL
let nul = payload[pos..]
.iter()
.position(|&b| b == 0)
.ok_or_else(|| ForgeError::malformed("tree", "entry missing NUL after name"))?;
let name = payload[pos..pos + nul].to_vec();
if name.is_empty() {
return Err(ForgeError::malformed("tree", "entry has empty name"));
}
pos += nul + 1;
// oid: exactly 20 raw bytes
if pos + OID_LEN > payload.len() {
return Err(ForgeError::malformed("tree", "truncated entry oid"));
}
let mut raw = [0u8; OID_LEN];
raw.copy_from_slice(&payload[pos..pos + OID_LEN]);
pos += OID_LEN;
entries.push(TreeEntry {
mode,
name,
oid: ObjectId::from_bytes(raw),
});
}
Ok(Self { entries })
}
/// Serialize back to the exact on-disk payload (round-trips Git output).
pub fn serialize(&self) -> Vec<u8> {
let mut out = Vec::new();
for e in &self.entries {
out.extend_from_slice(e.mode.as_bytes());
out.push(b' ');
out.extend_from_slice(&e.name);
out.push(0);
out.extend_from_slice(e.oid.as_bytes());
}
out
}
/// Find an entry by exact name.
pub fn entry(&self, name: &[u8]) -> Option<&TreeEntry> {
self.entries.iter().find(|e| e.name == name)
}
/// Sort entries into Git's canonical order.
///
/// Git orders tree entries by name, but compares as though every tree
/// entry's name had a trailing `/`. This is what makes `write-tree`
/// deterministic and hash-stable.
pub fn canonicalize(&mut self) {
self.entries.sort_by(git_name_cmp);
}
}
/// Compare two entries using Git's tree-sort rule (trees sort as `name/`).
fn git_name_cmp(a: &TreeEntry, b: &TreeEntry) -> std::cmp::Ordering {
let a_key = sort_key(a);
let b_key = sort_key(b);
a_key.cmp(&b_key)
}
fn sort_key(e: &TreeEntry) -> Vec<u8> {
let mut key = e.name.clone();
if e.kind() == "tree" {
key.push(b'/');
}
key
}
#[cfg(test)]
mod tests {
use super::*;
use crate::object;
fn oid(hex: &str) -> ObjectId {
ObjectId::from_hex(hex).unwrap()
}
#[test]
fn parse_serialize_roundtrips() {
let entries = vec![
TreeEntry {
mode: "100644".into(),
name: b"a.txt".to_vec(),
oid: oid("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"),
},
TreeEntry {
mode: "40000".into(),
name: b"sub".to_vec(),
oid: oid("4b825dc642cb6eb9a060e54bf8d69288fbee4904"),
},
];
let tree = Tree { entries };
let payload = tree.serialize();
let reparsed = Tree::parse(&payload).unwrap();
assert_eq!(tree, reparsed);
}
#[test]
fn empty_tree_matches_known_oid() {
// The canonical empty tree oid every Git repo shares.
let payload = Tree::default().serialize();
let (id, _) = object::hash_content(object::ObjectType::Tree, &payload);
assert_eq!(id.to_hex(), "4b825dc642cb6eb9a060e54bf8d69288fbee4904");
}
#[test]
fn rejects_truncated_oid() {
let bad = b"100644 a.txt\0short".to_vec();
assert!(Tree::parse(&bad).is_err());
}
#[test]
fn canonicalize_sorts_trees_as_slashed() {
let mut tree = Tree {
entries: vec![
TreeEntry {
mode: "100644".into(),
name: b"foo".to_vec(),
oid: oid("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"),
},
TreeEntry {
mode: "40000".into(),
name: b"foo".to_vec(),
oid: oid("4b825dc642cb6eb9a060e54bf8d69288fbee4904"),
},
],
};
tree.canonicalize();
// "foo" (blob) sorts before "foo/" (tree)
assert_eq!(tree.entries[0].kind(), "blob");
assert_eq!(tree.entries[1].kind(), "tree");
}
}

292
tests/interop.rs Normal file
View File

@ -0,0 +1,292 @@
//! Real-git interoperability tests — the whole point of this slice.
//!
//! These shell out to the installed `git` binary and assert that:
//! 1. object ids we compute are byte-identical to `git hash-object`;
//! 2. a repository authored by real `git` is read back correctly by us;
//! 3. objects we write are readable by real `git` (`cat-file`), and
//! 4. our serialization reproduces git's exact bytes (round-trip via oid).
//!
//! Requires `git` on PATH (the task environment has it).
use std::path::Path;
use std::process::Command;
use rit::object::{hash_content, Object};
use rit::{ObjectType, Repository};
use tempfile::TempDir;
/// Run `git` with args in `cwd`, returning trimmed stdout; panics on failure.
fn git(cwd: &Path, args: &[&str]) -> String {
let output = Command::new("git")
.args(args)
.current_dir(cwd)
.output()
.expect("git must be installed and on PATH");
assert!(
output.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&output.stderr)
);
String::from_utf8(output.stdout).unwrap().trim().to_string()
}
/// Run `git` returning raw (untrimmed) stdout bytes.
fn git_bytes(cwd: &Path, args: &[&str]) -> Vec<u8> {
let output = Command::new("git")
.args(args)
.current_dir(cwd)
.output()
.expect("git must be installed and on PATH");
assert!(
output.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&output.stderr)
);
output.stdout
}
/// Create an initialized repo with deterministic identity + a first commit.
fn init_repo_with_commit() -> TempDir {
let dir = tempfile::tempdir().unwrap();
let p = dir.path();
git(p, &["init", "-q", "-b", "main"]);
git(p, &["config", "user.name", "Forge Tester"]);
git(p, &["config", "user.email", "forge@example.com"]);
git(p, &["config", "commit.gpgsign", "false"]);
std::fs::write(p.join("hello.txt"), b"Hello, Forge!\n").unwrap();
std::fs::create_dir(p.join("sub")).unwrap();
std::fs::write(p.join("sub").join("nested.txt"), b"nested content\n").unwrap();
git(p, &["add", "."]);
// Fixed dates so commits are reproducible.
let env_commit = [
"-c",
"user.name=Forge Tester",
"-c",
"user.email=forge@example.com",
];
let mut args = vec!["commit", "-q", "-m", "initial commit"];
// prepend the -c overrides
let mut full: Vec<&str> = env_commit.to_vec();
full.append(&mut args);
git(p, &full);
dir
}
#[test]
fn blob_oid_matches_git_hash_object() {
let dir = tempfile::tempdir().unwrap();
let contents: &[&[u8]] = &[b"hello world\n", b"", b"no trailing newline", b"\x00\x01\x02binary"];
for (i, content) in contents.iter().enumerate() {
let file = dir.path().join(format!("f{i}.bin"));
std::fs::write(&file, content).unwrap();
let git_oid = git(dir.path(), &["hash-object", file.to_str().unwrap()]);
let ours = hash_content(ObjectType::Blob, content).0.to_hex();
assert_eq!(
ours, git_oid,
"blob oid mismatch for content #{i} ({content:?})"
);
}
}
#[test]
fn reads_repo_authored_by_real_git() {
let dir = init_repo_with_commit();
let p = dir.path();
let repo = Repository::discover(p).unwrap();
// HEAD oid matches `git rev-parse HEAD`.
let head = repo.head().unwrap();
assert_eq!(head.to_hex(), git(p, &["rev-parse", "HEAD"]));
// The HEAD commit's tree matches `git rev-parse HEAD^{tree}`.
let commit = repo.head_commit().unwrap();
assert_eq!(commit.tree.to_hex(), git(p, &["rev-parse", "HEAD^{tree}"]));
// Parsed metadata is correct.
assert_eq!(commit.author.name, "Forge Tester");
assert_eq!(commit.author.email, "forge@example.com");
assert_eq!(commit.summary(), "initial commit");
assert!(commit.parents.is_empty(), "first commit has no parents");
// The tree lists hello.txt with the same blob oid git reports.
let tree = repo.tree(&commit.tree).unwrap();
let entry = tree.entry(b"hello.txt").expect("hello.txt in tree");
assert_eq!(entry.oid.to_hex(), git(p, &["rev-parse", "HEAD:hello.txt"]));
assert_eq!(entry.display_mode(), "100644");
// And the blob content round-trips.
let blob = repo.blob(&entry.oid).unwrap();
assert_eq!(blob.data, b"Hello, Forge!\n");
// Sub-tree is reachable and its nested blob matches git.
let sub = tree.entry(b"sub").expect("sub dir in tree");
assert_eq!(sub.kind(), "tree");
let subtree = repo.tree(&sub.oid).unwrap();
let nested = subtree.entry(b"nested.txt").expect("nested.txt");
assert_eq!(
nested.oid.to_hex(),
git(p, &["rev-parse", "HEAD:sub/nested.txt"])
);
}
#[test]
fn objects_we_write_are_readable_by_real_git() {
let dir = init_repo_with_commit();
let p = dir.path();
let repo = Repository::discover(p).unwrap();
// Write a blob with OUR code.
let content = b"round trip through our writer\n";
let oid = repo.hash_object(ObjectType::Blob, content, true).unwrap();
// Real git can read its type and content.
assert_eq!(git(p, &["cat-file", "-t", &oid.to_hex()]), "blob");
assert_eq!(git_bytes(p, &["cat-file", "-p", &oid.to_hex()]), content);
// And git's own oid for the same content agrees with ours.
let git_oid = {
let f = p.join("rt.txt");
std::fs::write(&f, content).unwrap();
git(p, &["hash-object", f.to_str().unwrap()])
};
assert_eq!(oid.to_hex(), git_oid);
// fsck is clean: nothing we wrote corrupts the repo.
let fsck = git(p, &["fsck", "--strict"]);
assert!(
!fsck.to_lowercase().contains("error"),
"git fsck reported problems: {fsck}"
);
}
#[test]
fn our_serialization_reproduces_git_bytes_exactly() {
// If our parse+serialize is byte-exact, re-hashing git's objects with our
// code must reproduce git's oids.
let dir = init_repo_with_commit();
let p = dir.path();
let repo = Repository::discover(p).unwrap();
let head = repo.head().unwrap();
// Commit: parse then re-serialize then re-hash -> same oid.
match repo.read_object(&head).unwrap() {
Object::Commit(c) => {
let (id, _) = hash_content(ObjectType::Commit, &c.serialize());
assert_eq!(id, head, "commit re-serialization is not byte-exact");
}
other => panic!("HEAD should be a commit, got {other:?}"),
}
// Tree: same property.
let tree_oid = repo.head_commit().unwrap().tree;
let tree = repo.tree(&tree_oid).unwrap();
let (id, _) = hash_content(ObjectType::Tree, &tree.serialize());
assert_eq!(id, tree_oid, "tree re-serialization is not byte-exact");
}
#[test]
fn reads_annotated_tag_from_real_git() {
let dir = init_repo_with_commit();
let p = dir.path();
git(
p,
&[
"-c",
"user.name=Forge Tester",
"-c",
"user.email=forge@example.com",
"tag",
"-a",
"v1.0.0",
"-m",
"first release",
],
);
let repo = Repository::discover(p).unwrap();
let tag_oid = repo.resolve("v1.0.0").unwrap();
match repo.read_object(&tag_oid).unwrap() {
Object::Tag(t) => {
assert_eq!(t.tag, "v1.0.0");
assert_eq!(t.object_type, ObjectType::Commit);
assert_eq!(t.object, repo.head().unwrap());
assert!(t.message_str().contains("first release"));
// byte-exact: re-hash reproduces git's tag oid
let (id, _) = hash_content(ObjectType::Tag, &t.serialize());
assert_eq!(id, tag_oid, "tag re-serialization is not byte-exact");
}
other => panic!("expected annotated tag, got {other:?}"),
}
// Peeling the tag reaches the commit.
let commit = repo.peel_to_commit(tag_oid).unwrap();
assert_eq!(commit.summary(), "initial commit");
}
#[test]
fn log_walks_multi_commit_history() {
let dir = init_repo_with_commit();
let p = dir.path();
let overrides = [
"-c",
"user.name=Forge Tester",
"-c",
"user.email=forge@example.com",
];
// second commit
std::fs::write(p.join("second.txt"), b"second\n").unwrap();
git(p, &["add", "."]);
let mut a: Vec<&str> = overrides.to_vec();
a.extend_from_slice(&["commit", "-q", "-m", "second commit"]);
git(p, &a);
// third commit
std::fs::write(p.join("third.txt"), b"third\n").unwrap();
git(p, &["add", "."]);
let mut b: Vec<&str> = overrides.to_vec();
b.extend_from_slice(&["commit", "-q", "-m", "third commit"]);
git(p, &b);
let repo = Repository::discover(p).unwrap();
let head = repo.head().unwrap();
let history = repo.log(head).unwrap();
assert_eq!(history.len(), 3, "expected 3 commits");
// Compare our ordered oids against `git rev-list HEAD`.
let git_list: Vec<String> = git(p, &["rev-list", "HEAD"])
.lines()
.map(str::to_string)
.collect();
let ours: Vec<String> = history.iter().map(|(oid, _)| oid.to_hex()).collect();
assert_eq!(ours, git_list, "log order/content differs from git rev-list");
assert_eq!(history[0].1.summary(), "third commit");
}
#[test]
fn corrupt_object_is_rejected_not_panicked() {
let dir = init_repo_with_commit();
let p = dir.path();
let repo = Repository::discover(p).unwrap();
let head = repo.head().unwrap();
// Overwrite the loose object with garbage and confirm we error, not panic.
// Git stores loose objects read-only (0444), so make it writable first.
let path = repo.odb().loose_path(&head);
if path.exists() {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap();
}
std::fs::write(&path, b"totally not a git object").unwrap();
let result = repo.read_object(&head);
assert!(result.is_err(), "corrupt object must be rejected");
}
}