diff --git a/Cargo.lock b/Cargo.lock index 5e5eb28..a41b6a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,15 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "1.0.0" @@ -222,6 +231,15 @@ dependencies = [ "thiserror", ] +[[package]] +name = "diffy" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e3dc2f773b6aaa63b1a7684b8589f670a8a0146a510b74d23a401c882364b49" +dependencies = [ + "hashbrown 0.17.1", +] + [[package]] name = "digest" version = "0.10.7" @@ -269,6 +287,12 @@ dependencies = [ "zlib-rs", ] +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + [[package]] name = "generic-array" version = "0.14.7" @@ -313,6 +337,19 @@ dependencies = [ "thiserror", ] +[[package]] +name = "gix-commitgraph" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bb23121e952f43a5b07e3e80890336cb847297467a410475036242732980d06" +dependencies = [ + "bstr", + "gix-chunk", + "gix-hash", + "memmap2", + "thiserror", +] + [[package]] name = "gix-date" version = "0.10.7" @@ -326,6 +363,18 @@ dependencies = [ "thiserror", ] +[[package]] +name = "gix-diff" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de854852010d44a317f30c92d67a983e691c9478c8a3fb4117c1f48626bcdea8" +dependencies = [ + "bstr", + "gix-hash", + "gix-object", + "thiserror", +] + [[package]] name = "gix-features" version = "0.43.1" @@ -376,7 +425,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c35300b54896153e55d53f4180460931ccd69b7e8d2f6b9d6401122cdedc4f07" dependencies = [ "gix-hash", - "hashbrown", + "hashbrown 0.15.5", "parking_lot", ] @@ -429,11 +478,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8571df89bfca5abb49c3e3372393f7af7e6f8b8dbe2b96303593cef5b263019" dependencies = [ "gix-chunk", + "gix-diff", "gix-features", "gix-hash", + "gix-hashtable", "gix-object", "gix-path", + "gix-tempfile", + "gix-traverse", "memmap2", + "parking_lot", "smallvec", "thiserror", ] @@ -461,12 +515,57 @@ dependencies = [ "thiserror", ] +[[package]] +name = "gix-revwalk" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06e74f91709729e099af6721bd0fa7d62f243f2005085152301ca5cdd86ec02c" +dependencies = [ + "gix-commitgraph", + "gix-date", + "gix-hash", + "gix-hashtable", + "gix-object", + "smallvec", + "thiserror", +] + +[[package]] +name = "gix-tempfile" +version = "18.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "666c0041bcdedf5fa05e9bef663c897debab24b7dc1741605742412d1d47da57" +dependencies = [ + "gix-fs", + "libc", + "once_cell", + "parking_lot", + "tempfile", +] + [[package]] name = "gix-trace" version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be3eb81d9dc914335923e50d52829c551feefd6a72d176c4130c546b67a60814" +[[package]] +name = "gix-traverse" +version = "0.47.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7cdc82509d792ba0ad815f86f6b469c7afe10f94362e96c4494525a6601bdd5" +dependencies = [ + "bitflags 2.13.1", + "gix-commitgraph", + "gix-date", + "gix-hash", + "gix-hashtable", + "gix-object", + "gix-revwalk", + "smallvec", + "thiserror", +] + [[package]] name = "gix-utils" version = "0.3.6" @@ -488,6 +587,19 @@ dependencies = [ "thiserror", ] +[[package]] +name = "globset" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07c34a9410465b45bd9787443bc7370f37735bad04b0f0cd57ff1a3186c98988" +dependencies = [ + "aho-corasick", + "bstr", + "log", + "regex-automata", + "regex-syntax", +] + [[package]] name = "hash32" version = "0.3.1" @@ -503,6 +615,15 @@ version = "0.15.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +dependencies = [ + "foldhash", +] + [[package]] name = "heapless" version = "0.8.0" @@ -733,6 +854,17 @@ name = "regex-automata" version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" [[package]] name = "rit" @@ -740,10 +872,14 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "diffy", "flate2", + "gix-features", "gix-hash", "gix-object", "gix-odb", + "gix-pack", + "globset", "sha1", "tempfile", "thiserror", diff --git a/Cargo.toml b/Cargo.toml index 98aa2e9..2952c05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,10 +20,14 @@ path = "src/main.rs" [dependencies] anyhow = "1" clap = { version = "4", features = ["derive"] } +diffy = "0.5" flate2 = "1" gix-hash = "0.19.0" +gix-features = { version = "0.43.1", features = ["progress", "zlib"] } gix-object = "0.50.2" gix-odb = "0.70.0" +gix-pack = "0.60.0" +globset = "=0.4.20" sha1 = "0.10" thiserror = "2" diff --git a/README.md b/README.md index f936b42..22988e2 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,65 @@ # 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). +A **library-first** Rust implementation of Git's local object/authoring model. +rit owns its safety-critical loose-object, index, tree, commit, and ref-transaction +paths while adopting audited open-source crates for expensive commodity layers. +It is byte-for-byte compatible with real Git where claimed (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). +- ODB (zlib, atomic no-clobber writes, SHA-1 verification on read), refs + (HEAD/loose/packed/symbolic plus atomic compare-and-swap updates). +- Checksum-verifying Git index v2/v3 reader, atomic `index.lock` mutation, + recursive ignore-aware `add`, tracked deletion staging, and symlink/path-safe status. +- Canonical recursive `write-tree`, Git commit objects, and atomic branch + compare-and-swap updates. +- Transactional `checkout-tree` materialization with whole-operation collision + preflight, symlink/path confinement, atomic file replacement, and matching + index synchronization. `restore` explicitly replaces selected tracked paths + from the current index without moving `HEAD` or rewriting the index. +- Strict binary pkt-line framing (including protocol-v2 delimiter/response-end + controls), receive-pack command/capability parsing, and report-status encoding. +- Raw and thin Git pack import through gitoxide with checksum/index/delta + verification, target-ODB base lookup, and a two-phase scratch/quarantine + boundary. Decoded objects then pass declared-OID, byte/object/graph bounds, + strict connectivity/fsck, tree/type validation, fast-forward enforcement, + immutable object publication, and one atomic ref CAS. Expected-old deletion + is supported for loose refs and refuses packed backing values. Failed receives + never move a ref; disconnected objects are not published. +- `rit` CLI: `hash-object`, `cat-file`, `ls-tree`, `log`, `status`, `add`, + `write-tree`, `commit`, `checkout-tree`, `restore`, plus a stateful + stdin/stdout `receive-pack` process server for local/SSH adapters and an + argument-safe `upload-pack` bridge to the installed open-source Git backend. +- Real-git interop proves our OIDs match `git hash-object`, `git fsck` accepts + objects we write, and real Git can read/write-tree from an index authored by + rit. Checkout/restore interop also covers modified/untracked collision refusal + and symlink-parent escape attempts. +- Full and genuinely thin packs generated by stock Git can be staged through + receive quarantine into a bare target and pass `git fsck --strict` after + promotion. Corrupt/truncated packs and thin packs with missing bases publish + neither objects nor refs. +- Stock `git push --receive-pack=' receive-pack'` creates and fast-forwards + a bare repository through the process server and deletes loose refs. A client + `--force` does not bypass the server's non-fast-forward policy; deleting a + packed ref fails without exposing or changing its old value. +- Stock `git clone/fetch --upload-pack=' upload-pack'` works through the + explicit compatibility bridge. This fetch path requires Git at runtime and is + not misrepresented as Rit-native protocol implementation. 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. +Open-source foundations: packed-object reads and received pack/index/delta +verification use gitoxide; ignore patterns use globset. rit does not pretend +these costly primitives were written from scratch. + +The protocol boundary follows Git's official +[`gitprotocol-common`](https://git-scm.com/docs/gitprotocol-common), +[`gitprotocol-v2`](https://git-scm.com/docs/gitprotocol-v2), and +[`gitprotocol-pack`](https://git-scm.com/docs/gitprotocol-pack) specifications. +Pkt-line, raw/thin pack ingestion, decoded receive safety, and the stateful +receive-pack process contract are Rit-native. Clone/fetch is functional through +the tested stock-Git upload-pack bridge. Smart-HTTP/SSH listener integration, +native Rust upload-pack/fetch, multi-ref atomic updates (including mixed +deletions), packed-refs rewrite/deletion, merge/rebase, interrupted-ingest +recovery, and SHA-256 remain deferred. `rithub` +stays blocked on those gates—it must not turn this safe receive core into a +claim of a complete forge transport. diff --git a/src/diff.rs b/src/diff.rs new file mode 100644 index 0000000..b5d5c24 --- /dev/null +++ b/src/diff.rs @@ -0,0 +1,201 @@ +//! Tree diff: compare two tree snapshots and report the per-path changes. +//! +//! This is the git-domain half of a forge "pull request diff": given two tree +//! object ids (typically the trees of a base and head commit), it returns the +//! set of file paths that were **added**, **deleted**, or **modified**, with the +//! old/new blob ids and modes. Rendering those blobs into a textual patch is a +//! presentation concern left to the caller (the forge). Rename detection is out +//! of scope (a delete + add is reported), matching a conservative first cut. + +use crate::error::{ForgeError, Result}; +use crate::fsck::ObjectReader; +use crate::object::Object; +use crate::oid::ObjectId; +use std::collections::BTreeMap; + +/// Upper bound on files considered in a single diff (a fail-closed guard against +/// pathological trees). +pub const MAX_DIFF_ENTRIES: usize = 1_000_000; +/// Maximum tree recursion depth. +pub const MAX_DIFF_DEPTH: usize = 256; + +/// The kind of change to a path between two trees. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ChangeKind { + /// Present only in the new tree. + Added, + /// Present only in the old tree. + Deleted, + /// Present in both with a different blob id or mode. + Modified, +} + +impl ChangeKind { + /// A short status token (`A`/`D`/`M`), matching `git diff --name-status`. + pub fn code(&self) -> char { + match self { + ChangeKind::Added => 'A', + ChangeKind::Deleted => 'D', + ChangeKind::Modified => 'M', + } + } +} + +/// One changed path between two trees. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct TreeChange { + /// The full path (raw bytes, `/`-separated) relative to the tree root. + pub path: Vec, + /// What happened to the path. + pub kind: ChangeKind, + /// The old mode/oid (absent for an addition). + pub old_mode: Option, + pub old_oid: Option, + /// The new mode/oid (absent for a deletion). + pub new_mode: Option, + pub new_oid: Option, +} + +/// Compute the changes between two (optional) trees. `None` means "empty tree", +/// so a brand-new repository's first commit diffs cleanly as all-additions. +pub fn diff_trees( + reader: &impl ObjectReader, + old: Option, + new: Option, +) -> Result> { + let mut old_map = BTreeMap::new(); + if let Some(tree) = old { + flatten(reader, tree, Vec::new(), 0, &mut old_map)?; + } + let mut new_map = BTreeMap::new(); + if let Some(tree) = new { + flatten(reader, tree, Vec::new(), 0, &mut new_map)?; + } + + let mut changes = Vec::new(); + for (path, (new_mode, new_oid)) in &new_map { + match old_map.get(path) { + None => changes.push(TreeChange { + path: path.clone(), + kind: ChangeKind::Added, + old_mode: None, + old_oid: None, + new_mode: Some(new_mode.clone()), + new_oid: Some(*new_oid), + }), + Some((old_mode, old_oid)) if old_oid != new_oid || old_mode != new_mode => { + changes.push(TreeChange { + path: path.clone(), + kind: ChangeKind::Modified, + old_mode: Some(old_mode.clone()), + old_oid: Some(*old_oid), + new_mode: Some(new_mode.clone()), + new_oid: Some(*new_oid), + }) + } + Some(_) => {} // identical — not a change + } + } + for (path, (old_mode, old_oid)) in &old_map { + if !new_map.contains_key(path) { + changes.push(TreeChange { + path: path.clone(), + kind: ChangeKind::Deleted, + old_mode: Some(old_mode.clone()), + old_oid: Some(*old_oid), + new_mode: None, + new_oid: None, + }); + } + } + changes.sort_by(|a, b| a.path.cmp(&b.path)); + Ok(changes) +} + +/// Recursively flatten a tree into `path -> (mode, blob oid)` leaf entries. +fn flatten( + reader: &impl ObjectReader, + tree_oid: ObjectId, + prefix: Vec, + depth: usize, + out: &mut BTreeMap, (String, ObjectId)>, +) -> Result<()> { + if depth > MAX_DIFF_DEPTH { + return Err(ForgeError::ResourceLimit(format!( + "tree depth exceeds {MAX_DIFF_DEPTH}" + ))); + } + let Object::Tree(tree) = reader.read_object(&tree_oid)? else { + return Err(ForgeError::malformed("tree", "diff input is not a tree")); + }; + for entry in tree.entries { + let mut path = prefix.clone(); + if !path.is_empty() { + path.push(b'/'); + } + path.extend_from_slice(&entry.name); + if entry.kind() == "tree" { + flatten(reader, entry.oid, path, depth + 1, out)?; + } else { + out.insert(path, (entry.mode.clone(), entry.oid)); + if out.len() > MAX_DIFF_ENTRIES { + return Err(ForgeError::ResourceLimit(format!( + "diff exceeds {MAX_DIFF_ENTRIES} paths" + ))); + } + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::object::ObjectType; + use crate::{Repository, Tree, TreeEntry}; + + fn blob(repo: &Repository, data: &[u8]) -> ObjectId { + repo.hash_object(ObjectType::Blob, data, true).unwrap() + } + + fn tree(repo: &Repository, entries: Vec) -> ObjectId { + let mut t = Tree { entries }; + t.canonicalize(); + repo.hash_object(ObjectType::Tree, &t.serialize(), true).unwrap() + } + + #[test] + fn reports_add_modify_delete() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path(), true, Some("main")).unwrap(); + let a1 = blob(&repo, b"one\n"); + let b1 = blob(&repo, b"keep\n"); + let old = tree( + &repo, + vec![ + TreeEntry { mode: "100644".into(), name: b"a.txt".to_vec(), oid: a1 }, + TreeEntry { mode: "100644".into(), name: b"b.txt".to_vec(), oid: b1 }, + ], + ); + let a2 = blob(&repo, b"two\n"); + let c1 = blob(&repo, b"new\n"); + let new = tree( + &repo, + vec![ + TreeEntry { mode: "100644".into(), name: b"a.txt".to_vec(), oid: a2 }, + TreeEntry { mode: "100644".into(), name: b"b.txt".to_vec(), oid: b1 }, + TreeEntry { mode: "100644".into(), name: b"c.txt".to_vec(), oid: c1 }, + ], + ); + let changes = diff_trees(repo.odb(), Some(old), Some(new)).unwrap(); + assert_eq!(changes.len(), 2); + assert_eq!(changes[0].path, b"a.txt"); + assert_eq!(changes[0].kind, ChangeKind::Modified); + assert_eq!(changes[1].path, b"c.txt"); + assert_eq!(changes[1].kind, ChangeKind::Added); + + // Reverse diff sees the deletion. + let rev = diff_trees(repo.odb(), Some(new), Some(old)).unwrap(); + assert!(rev.iter().any(|c| c.path == b"c.txt" && c.kind == ChangeKind::Deleted)); + } +} diff --git a/src/error.rs b/src/error.rs index ef138bc..ed61ea4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -20,6 +20,10 @@ pub enum ForgeError { #[error("not a git repository (or any parent): {0}")] NotARepository(String), + /// The operation requires a non-bare worktree, but only a Git directory is available. + #[error("repository has no supported worktree: {0}")] + NoWorktree(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}")] @@ -52,6 +56,24 @@ pub enum ForgeError { #[error("cannot resolve reference: {0}")] InvalidRef(String), + /// A ref compare-and-swap observed a different value than the caller's + /// expected old object id. + #[error("reference changed concurrently: {name} (expected {expected}, found {actual})")] + RefConflict { + name: String, + expected: String, + actual: String, + }, + + /// Another writer (or a crashed writer awaiting explicit recovery) owns the + /// ref lock. + #[error("reference is locked: {0}")] + RefLocked(String), + + /// Repository-controlled input attempted to escape a repository-owned path. + #[error("unsafe repository path: {0}")] + UnsafePath(String), + /// An abbreviated object id prefix matched more than one object. #[error("ambiguous object id prefix: {0}")] AmbiguousPrefix(String), @@ -70,6 +92,47 @@ pub enum ForgeError { /// pack decode errors fail **closed** as a typed error rather than a panic. #[error("packfile error: {0}")] Pack(String), + + /// The Git index is corrupt, unsupported, or exceeds a defensive bound. + #[error("invalid git index: {0}")] + InvalidIndex(String), + + /// Another Git-compatible writer owns `.git/index.lock`. + #[error("git index is locked: {0}")] + IndexLocked(String), + + /// A snapshot/restore would overwrite untracked or locally modified work. + #[error("worktree collision: {0}")] + WorktreeCollision(String), + + /// An adversarial repository graph exceeded a declared traversal bound. + #[error("repository resource limit exceeded: {0}")] + ResourceLimit(String), + + /// A received object graph is malformed, disconnected, type-confused, or + /// contains repository structures that strict fsck cannot accept. + #[error("received object graph rejected: {0}")] + Connectivity(String), + + /// A branch update would discard commits without an explicit force policy. + #[error("non-fast-forward update rejected for {name}: {old} is not an ancestor of {new}")] + NonFastForward { + name: String, + old: String, + new: String, + }, + + /// A quarantine transaction or receive command violated its bounded input + /// contract before repository state was published. + #[error("receive transaction rejected: {0}")] + ReceiveRejected(String), + + /// A three-way merge encountered a criss-cross history with more than one + /// merge base. The single-base strategy fails closed here rather than + /// silently choosing an arbitrary base (which risks mis-resolving a hunk); + /// a recursive/virtual-base merge is out of scope for this slice. + #[error("cannot merge: {0} merge bases (criss-cross history); recursive merge is unsupported")] + MergeMultipleBases(usize), } impl ForgeError { diff --git a/src/fsck.rs b/src/fsck.rs new file mode 100644 index 0000000..88a632a --- /dev/null +++ b/src/fsck.rs @@ -0,0 +1,305 @@ +//! Strict, bounded connectivity verification for received Git object graphs. +//! +//! The verifier deliberately operates over an [`ObjectReader`] rather than a +//! concrete repository. Receive quarantine can therefore overlay isolated +//! incoming objects on the existing object database without publishing either +//! source before the complete graph has passed validation. + +use std::collections::{BTreeMap, BTreeSet, HashSet}; + +use crate::error::{ForgeError, Result}; +use crate::object::{Object, ObjectType}; +use crate::odb::Odb; +use crate::oid::ObjectId; +use crate::tree::TreeEntry; + +/// Read-only object source used by fsck and ancestry walks. +pub trait ObjectReader { + fn read_object(&self, oid: &ObjectId) -> Result; +} + +impl ObjectReader for Odb { + fn read_object(&self, oid: &ObjectId) -> Result { + self.read(oid) + } +} + +/// Defensive graph limits applied before any received ref can move. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ConnectivityLimits { + pub max_objects: usize, + pub max_depth: usize, +} + +impl Default for ConnectivityLimits { + fn default() -> Self { + Self { + max_objects: 1_000_000, + max_depth: 4096, + } + } +} + +/// Verified reachability result. Receive uses this to reject disconnected +/// objects rather than allowing an adversary to fill the object database with +/// data unrelated to the proposed ref update. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ConnectivityReport { + pub reachable: BTreeSet, +} + +/// Verify every object reachable from `roots`, including object-kind edges, +/// strict tree structure, annotated-tag targets, and commit ancestry. +pub fn check_connectivity( + reader: &impl ObjectReader, + roots: &[ObjectId], + limits: ConnectivityLimits, +) -> Result { + if roots.is_empty() { + return Err(ForgeError::Connectivity( + "at least one graph root is required".into(), + )); + } + let mut reachable = BTreeSet::new(); + let mut object_types = BTreeMap::new(); + let mut pending = roots + .iter() + .copied() + .map(|oid| (oid, None, 0usize)) + .collect::>(); + + while let Some((oid, expected, depth)) = pending.pop() { + if depth > limits.max_depth { + return Err(ForgeError::ResourceLimit(format!( + "object graph exceeds depth {}", + limits.max_depth + ))); + } + if let Some(actual) = object_types.get(&oid) { + if expected.is_some_and(|expected| expected != *actual) { + return Err(ForgeError::Connectivity(format!( + "object {oid} is referenced as incompatible object types" + ))); + } + continue; + } + if reachable.len() == limits.max_objects { + return Err(ForgeError::ResourceLimit(format!( + "object graph exceeds {} objects", + limits.max_objects + ))); + } + + let object = reader.read_object(&oid).map_err(|error| { + ForgeError::Connectivity(format!("cannot read reachable object {oid}: {error}")) + })?; + if let Some(expected) = expected { + if object.object_type() != expected { + return Err(ForgeError::Connectivity(format!( + "object {oid} has type {}, expected {expected}", + object.object_type() + ))); + } + } + object_types.insert(oid, object.object_type()); + reachable.insert(oid); + + match object { + Object::Blob(_) => {} + Object::Commit(commit) => { + pending.push((commit.tree, Some(ObjectType::Tree), depth + 1)); + pending.extend( + commit + .parents + .into_iter() + .map(|parent| (parent, Some(ObjectType::Commit), depth + 1)), + ); + } + Object::Tree(tree) => { + validate_tree(&tree.entries, oid)?; + for entry in tree.entries { + match entry.mode.as_str() { + "40000" | "040000" => { + pending.push((entry.oid, Some(ObjectType::Tree), depth + 1)); + } + "160000" => { + // A gitlink names a commit in another repository; + // its object is intentionally not required locally. + } + _ => pending.push((entry.oid, Some(ObjectType::Blob), depth + 1)), + } + } + } + Object::Tag(tag) => { + pending.push((tag.object, Some(tag.object_type), depth + 1)); + } + } + } + + Ok(ConnectivityReport { reachable }) +} + +/// Return whether `ancestor` is reachable through commit-parent edges from +/// `descendant`. Non-commit inputs fail closed instead of being treated as a +/// force update. +pub fn is_ancestor( + reader: &impl ObjectReader, + ancestor: ObjectId, + descendant: ObjectId, + limits: ConnectivityLimits, +) -> Result { + let mut visited = HashSet::new(); + let mut pending = vec![(descendant, 0usize)]; + while let Some((oid, depth)) = pending.pop() { + if oid == ancestor { + return Ok(true); + } + if !visited.insert(oid) { + continue; + } + if visited.len() > limits.max_objects || depth > limits.max_depth { + return Err(ForgeError::ResourceLimit( + "commit ancestry exceeds receive limits".into(), + )); + } + let Object::Commit(commit) = reader.read_object(&oid).map_err(|error| { + ForgeError::Connectivity(format!("cannot read commit {oid}: {error}")) + })? + else { + return Err(ForgeError::Connectivity(format!( + "branch tip {oid} is not a commit" + ))); + }; + pending.extend(commit.parents.into_iter().map(|parent| (parent, depth + 1))); + } + Ok(false) +} + +fn validate_tree(entries: &[TreeEntry], tree_oid: ObjectId) -> Result<()> { + let mut previous: Option> = None; + for entry in entries { + if !matches!( + entry.mode.as_str(), + "100644" | "100755" | "120000" | "160000" | "40000" | "040000" + ) { + return Err(ForgeError::Connectivity(format!( + "tree {tree_oid} contains invalid mode {}", + entry.mode + ))); + } + if entry.name.is_empty() + || entry.name == b"." + || entry.name == b".." + || entry.name.contains(&b'/') + || entry.name.contains(&0) + { + return Err(ForgeError::Connectivity(format!( + "tree {tree_oid} contains an unsafe entry name" + ))); + } + let key = tree_sort_key(entry); + if previous.as_ref().is_some_and(|previous| previous >= &key) { + return Err(ForgeError::Connectivity(format!( + "tree {tree_oid} entries are duplicated or not canonically ordered" + ))); + } + previous = Some(key); + } + Ok(()) +} + +fn tree_sort_key(entry: &TreeEntry) -> Vec { + let mut key = entry.name.clone(); + if matches!(entry.mode.as_str(), "40000" | "040000") { + key.push(b'/'); + } + key +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::object::ObjectType; + use crate::tree::{Tree, TreeEntry}; + + #[test] + fn rejects_tree_type_confusion_and_unsafe_modes() { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(dir.path().join("objects")).unwrap(); + let odb = Odb::new(dir.path()); + let blob = odb.write(ObjectType::Blob, b"not a tree").unwrap(); + let tree = Tree { + entries: vec![TreeEntry { + mode: "40000".into(), + name: b"sub".to_vec(), + oid: blob, + }], + }; + let root = odb.write(ObjectType::Tree, &tree.serialize()).unwrap(); + assert!(matches!( + check_connectivity(&odb, &[root], ConnectivityLimits::default()), + Err(ForgeError::Connectivity(_)) + )); + + let invalid = Tree { + entries: vec![TreeEntry { + mode: "100777".into(), + name: b"file".to_vec(), + oid: blob, + }], + }; + let root = odb.write(ObjectType::Tree, &invalid.serialize()).unwrap(); + assert!(check_connectivity(&odb, &[root], ConnectivityLimits::default()).is_err()); + } + + #[test] + fn rejects_noncanonical_duplicate_tree_entries() { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(dir.path().join("objects")).unwrap(); + let odb = Odb::new(dir.path()); + let blob = odb.write(ObjectType::Blob, b"x").unwrap(); + let tree = Tree { + entries: vec![ + TreeEntry { + mode: "100644".into(), + name: b"same".to_vec(), + oid: blob, + }, + TreeEntry { + mode: "100644".into(), + name: b"same".to_vec(), + oid: blob, + }, + ], + }; + let root = odb.write(ObjectType::Tree, &tree.serialize()).unwrap(); + assert!(check_connectivity(&odb, &[root], ConnectivityLimits::default()).is_err()); + } + + #[test] + fn rejects_one_object_referenced_as_both_blob_and_tree() { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(dir.path().join("objects")).unwrap(); + let odb = Odb::new(dir.path()); + let blob = odb.write(ObjectType::Blob, b"same oid").unwrap(); + let tree = Tree { + entries: vec![ + TreeEntry { + mode: "40000".into(), + name: b"a-tree".to_vec(), + oid: blob, + }, + TreeEntry { + mode: "100644".into(), + name: b"b-blob".to_vec(), + oid: blob, + }, + ], + }; + let root = odb.write(ObjectType::Tree, &tree.serialize()).unwrap(); + assert!(matches!( + check_connectivity(&odb, &[root], ConnectivityLimits::default()), + Err(ForgeError::Connectivity(_)) + )); + } +} diff --git a/src/index.rs b/src/index.rs new file mode 100644 index 0000000..2db8f49 --- /dev/null +++ b/src/index.rs @@ -0,0 +1,367 @@ +//! Bounded reader for the on-disk Git index (`DIRC`) v2/v3. +//! +//! The checksum and every entry boundary are verified before paths are exposed. +//! Version 4 path compression is rejected explicitly until implemented rather +//! than being misparsed as v2. + +use std::fs::OpenOptions; +use std::io::Write; +use std::path::{Path, PathBuf}; + +use sha1::{Digest, Sha1}; + +use crate::{ForgeError, ObjectId, Result}; + +const HEADER_LEN: usize = 12; +const CHECKSUM_LEN: usize = 20; +const ENTRY_FIXED_LEN: usize = 62; +const MAX_INDEX_ENTRIES: u32 = 1_000_000; +const MAX_INDEX_BYTES: u64 = 256 * 1024 * 1024; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct IndexEntry { + pub mode: u32, + pub file_size: u32, + pub oid: ObjectId, + pub flags: u16, + pub path: Vec, +} + +impl IndexEntry { + pub fn stage(&self) -> u8 { + ((self.flags >> 12) & 0x3) as u8 + } + + pub fn path_str(&self) -> String { + String::from_utf8_lossy(&self.path).into_owned() + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Index { + pub version: u32, + pub entries: Vec, +} + +impl Index { + pub fn read(git_dir: &Path) -> Result { + let path = git_dir.join("index"); + let metadata = std::fs::metadata(&path)?; + if metadata.len() > MAX_INDEX_BYTES { + return Err(ForgeError::InvalidIndex(format!( + "index exceeds {MAX_INDEX_BYTES} byte cap" + ))); + } + Self::parse(&std::fs::read(path)?) + } + + pub fn parse(bytes: &[u8]) -> Result { + Self::parse_inner(bytes).map(|(index, _)| index) + } + + fn parse_inner(bytes: &[u8]) -> Result<(Self, usize)> { + if bytes.len() < HEADER_LEN + CHECKSUM_LEN || &bytes[..4] != b"DIRC" { + return Err(ForgeError::InvalidIndex("missing DIRC header".into())); + } + let content_len = bytes.len() - CHECKSUM_LEN; + let expected = &bytes[content_len..]; + let actual = Sha1::digest(&bytes[..content_len]); + if actual.as_slice() != expected { + return Err(ForgeError::InvalidIndex("checksum mismatch".into())); + } + + let version = read_u32(bytes, 4)?; + if !matches!(version, 2 | 3) { + return Err(ForgeError::InvalidIndex(format!( + "unsupported index version {version}; supported: 2, 3" + ))); + } + let count = read_u32(bytes, 8)?; + if count > MAX_INDEX_ENTRIES { + return Err(ForgeError::InvalidIndex(format!( + "entry count {count} exceeds {MAX_INDEX_ENTRIES}" + ))); + } + + let mut entries = Vec::with_capacity(count as usize); + let mut cursor = HEADER_LEN; + for _ in 0..count { + let start = cursor; + let fixed_end = cursor + .checked_add(ENTRY_FIXED_LEN) + .filter(|end| *end <= content_len) + .ok_or_else(|| ForgeError::InvalidIndex("truncated entry".into()))?; + let mode = read_u32(bytes, cursor + 24)?; + let file_size = read_u32(bytes, cursor + 36)?; + let mut oid_bytes = [0u8; 20]; + oid_bytes.copy_from_slice(&bytes[cursor + 40..cursor + 60]); + let oid = ObjectId::from_bytes(oid_bytes); + let flags = read_u16(bytes, cursor + 60)?; + cursor = fixed_end; + if version >= 3 && flags & 0x4000 != 0 { + cursor = cursor + .checked_add(2) + .filter(|end| *end <= content_len) + .ok_or_else(|| ForgeError::InvalidIndex("truncated extended flags".into()))?; + } + let nul = bytes[cursor..content_len] + .iter() + .position(|byte| *byte == 0) + .map(|offset| cursor + offset) + .ok_or_else(|| ForgeError::InvalidIndex("unterminated entry path".into()))?; + let path = bytes[cursor..nul].to_vec(); + validate_index_path(&path)?; + cursor = nul + 1; + let consumed = cursor - start; + let padded = consumed + .checked_add(7) + .map(|value| value & !7) + .ok_or_else(|| ForgeError::InvalidIndex("entry size overflow".into()))?; + cursor = start + .checked_add(padded) + .filter(|end| *end <= content_len) + .ok_or_else(|| ForgeError::InvalidIndex("truncated entry padding".into()))?; + entries.push(IndexEntry { + mode, + file_size, + oid, + flags, + path, + }); + } + + Ok((Self { version, entries }, cursor)) + } + + /// Read an index for mutation. Extensions are rejected for now because + /// silently dropping split-index/conflict metadata would corrupt semantics. + pub fn read_updateable(git_dir: &Path) -> Result { + let path = git_dir.join("index"); + let bytes = match std::fs::read(&path) { + Ok(bytes) => bytes, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + return Ok(Self { + version: 2, + entries: Vec::new(), + }) + } + Err(error) => return Err(error.into()), + }; + if bytes.len() as u64 > MAX_INDEX_BYTES { + return Err(ForgeError::InvalidIndex(format!( + "index exceeds {MAX_INDEX_BYTES} byte cap" + ))); + } + let (index, entries_end) = Self::parse_inner(&bytes)?; + let content_len = bytes.len() - CHECKSUM_LEN; + validate_droppable_extensions(&bytes, entries_end, content_len)?; + Ok(index) + } + + /// Replace all stages of `entry.path` with one stage-0 entry. + pub fn upsert(&mut self, entry: IndexEntry) -> Result<()> { + validate_index_path(&entry.path)?; + self.entries.retain(|existing| existing.path != entry.path); + self.entries.push(entry); + self.entries.sort_by(|a, b| a.path.cmp(&b.path)); + Ok(()) + } + + /// Remove every stage for one path. Returns whether anything changed. + pub fn remove_path(&mut self, path: &[u8]) -> bool { + let before = self.entries.len(); + self.entries.retain(|entry| entry.path != path); + before != self.entries.len() + } + + /// Serialize a canonical Git index v2 with checksum. + pub fn serialize(&self) -> Result> { + if self.entries.len() > MAX_INDEX_ENTRIES as usize { + return Err(ForgeError::InvalidIndex("too many index entries".into())); + } + let mut out = Vec::new(); + out.extend_from_slice(b"DIRC"); + out.extend_from_slice(&2u32.to_be_bytes()); + out.extend_from_slice(&(self.entries.len() as u32).to_be_bytes()); + for entry in &self.entries { + validate_index_path(&entry.path)?; + if entry.flags & 0x4000 != 0 { + return Err(ForgeError::InvalidIndex( + "extended index flags are not mutation-safe yet".into(), + )); + } + let start = out.len(); + // ctime, mtime, dev, ino: zero means Git will refresh stat data. + for _ in 0..6 { + out.extend_from_slice(&0u32.to_be_bytes()); + } + out.extend_from_slice(&entry.mode.to_be_bytes()); + out.extend_from_slice(&0u32.to_be_bytes()); // uid + out.extend_from_slice(&0u32.to_be_bytes()); // gid + out.extend_from_slice(&entry.file_size.to_be_bytes()); + out.extend_from_slice(entry.oid.as_bytes()); + let path_len = entry.path.len().min(0x0fff) as u16; + let flags = (entry.flags & 0x3000) | path_len; + out.extend_from_slice(&flags.to_be_bytes()); + out.extend_from_slice(&entry.path); + out.push(0); + while (out.len() - start) % 8 != 0 { + out.push(0); + } + } + let checksum = Sha1::digest(&out); + out.extend_from_slice(&checksum); + Ok(out) + } + + /// Mutate `.git/index` under the standard create-new `index.lock`, then + /// fsync + rename it atomically. A competing or crashed writer is never + /// overwritten. + pub fn mutate(git_dir: &Path, edit: impl FnOnce(&mut Index) -> Result) -> Result { + let lock_path = git_dir.join("index.lock"); + let mut lock = match OpenOptions::new() + .create_new(true) + .write(true) + .open(&lock_path) + { + Ok(file) => file, + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => { + return Err(ForgeError::IndexLocked(lock_path.display().to_string())) + } + Err(error) => return Err(error.into()), + }; + let mut cleanup = IndexLockCleanup::new(lock_path.clone()); + let mut index = Self::read_updateable(git_dir)?; + let result = edit(&mut index)?; + let encoded = index.serialize()?; + lock.write_all(&encoded)?; + lock.sync_all()?; + std::fs::rename(&lock_path, git_dir.join("index"))?; + cleanup.published = true; + sync_directory(git_dir)?; + Ok(result) + } + + /// Hold the standard index lock while performing a read-only operation. + /// The index is not rewritten; this is used by worktree restore so a + /// concurrent staging writer cannot change the source halfway through. + pub fn with_locked(git_dir: &Path, read: impl FnOnce(&Index) -> Result) -> Result { + let lock_path = git_dir.join("index.lock"); + let lock = match OpenOptions::new() + .create_new(true) + .write(true) + .open(&lock_path) + { + Ok(file) => file, + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => { + return Err(ForgeError::IndexLocked(lock_path.display().to_string())) + } + Err(error) => return Err(error.into()), + }; + let cleanup = IndexLockCleanup::new(lock_path); + let index = Self::read_updateable(git_dir)?; + let result = read(&index); + drop(lock); + drop(cleanup); + result + } +} + +struct IndexLockCleanup { + path: PathBuf, + published: bool, +} + +impl IndexLockCleanup { + fn new(path: PathBuf) -> Self { + Self { + path, + published: false, + } + } +} + +impl Drop for IndexLockCleanup { + fn drop(&mut self) { + if !self.published { + let _ = std::fs::remove_file(&self.path); + } + } +} + +fn sync_directory(path: &Path) -> Result<()> { + let directory = std::fs::File::open(path)?; + directory.sync_all()?; + Ok(()) +} + +pub fn validate_index_path(path: &[u8]) -> Result<()> { + if path.is_empty() + || path[0] == b'/' + || path.contains(&0) + || path.split(|byte| *byte == b'/').any(|part| { + part.is_empty() || part == b"." || part == b".." || part.eq_ignore_ascii_case(b".git") + }) + { + return Err(ForgeError::UnsafePath( + String::from_utf8_lossy(path).into_owned(), + )); + } + Ok(()) +} + +fn read_u32(bytes: &[u8], offset: usize) -> Result { + let raw: [u8; 4] = bytes + .get(offset..offset + 4) + .ok_or_else(|| ForgeError::InvalidIndex("truncated integer".into()))? + .try_into() + .expect("checked four-byte slice"); + Ok(u32::from_be_bytes(raw)) +} + +fn read_u16(bytes: &[u8], offset: usize) -> Result { + let raw: [u8; 2] = bytes + .get(offset..offset + 2) + .ok_or_else(|| ForgeError::InvalidIndex("truncated flags".into()))? + .try_into() + .expect("checked two-byte slice"); + Ok(u16::from_be_bytes(raw)) +} + +/// Uppercase-signature extensions are optional caches and may be discarded by +/// a writer. Lowercase signatures are mandatory (notably split-index `link`) +/// and must fail closed until their semantics are implemented. +fn validate_droppable_extensions(bytes: &[u8], mut cursor: usize, end: usize) -> Result<()> { + while cursor < end { + let header = bytes + .get(cursor..cursor + 8) + .ok_or_else(|| ForgeError::InvalidIndex("truncated index extension".into()))?; + let signature = &header[..4]; + if signature[0].is_ascii_lowercase() { + return Err(ForgeError::InvalidIndex(format!( + "mandatory index extension {:?} is not mutation-safe yet", + String::from_utf8_lossy(signature) + ))); + } + let length = u32::from_be_bytes(header[4..8].try_into().expect("four-byte length")); + cursor = cursor + .checked_add(8) + .and_then(|value| value.checked_add(length as usize)) + .filter(|value| *value <= end) + .ok_or_else(|| ForgeError::InvalidIndex("truncated index extension data".into()))?; + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn rejects_bad_checksum_and_unsafe_paths() { + assert!(Index::parse(b"DIRC").is_err()); + assert!(validate_index_path(b"../escape").is_err()); + assert!(validate_index_path(b"src/.git/config").is_err()); + assert!(validate_index_path(b"src/lib.rs").is_ok()); + } +} diff --git a/src/lib.rs b/src/lib.rs index b059240..a385d3c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,9 +21,20 @@ //! callers get one interface over loose **or** packed objects. This is the //! OSS-fork strategy — the highest-cost from-scratch subsystem is reused, not //! rewritten, while `rit` keeps ownership of the loose path. -//! - [`refs`] — `HEAD`, loose refs, symbolic refs, and `packed-refs`. +//! - [`refs`] — `HEAD`, loose refs, symbolic refs, `packed-refs`, and atomic CAS updates. +//! - [`wire`] — binary-safe pkt-line framing, receive-pack command/capability +//! parsing, pack-boundary detection, and report-status encoding. +//! - [`fsck`] / [`receive`] — gitoxide-backed full/thin pack validation, +//! bounded overlay-graph verification, and isolated decoded-object quarantine +//! before immutable publication + one ref CAS. +//! - [`receive_pack`] — ref advertisement plus a complete one-ref receive-pack +//! application boundary used by the CLI's stateful stdin/stdout server. +//! - [`index`] / [`worktree`] — verified index v2/v3 reads, atomic Git-compatible +//! v2 mutation, recursive ignore-aware `add`, `write-tree`, and safe tracked status. //! - [`repo`] — the porcelain-ish [`Repository`] API: `hash-object`, `cat-file`, -//! `rev-parse`, tree/commit traversal, and `log`. +//! `rev-parse`, tree/commit traversal, `log`, transactional `checkout-tree`, +//! and explicit index-to-worktree `restore`. Materialization preflights all +//! collisions before mutation and never follows a symlink outside the worktree. //! //! ## Compatibility //! @@ -33,34 +44,72 @@ //! //! ## Deferred (per PLAN) //! -//! The index/worktree, the smart wire protocol, merge/rebase, SHA-256 +//! File/directory type-transition checkout, native Rust upload-pack/fetch, +//! Smart-HTTP/SSH listeners, multi-ref atomic receive, SHA-256 //! repositories, SHA-1 collision detection (sha1dc), and the entire forge layer -//! are out of scope for this slice. Packfile **reads** are now covered by -//! adopting gitoxide (see [`pack`]); pack **writing** remains deferred. +//! remain out of scope for this slice. A single-base three-way [`merge`] +//! (merge-base, tree merge, and content merge) is implemented and exported as a +//! library API but is intentionally not wired into the CLI; recursive +//! (multi-base/criss-cross) merge and rebase remain deferred — [`merge_commits`] +//! fails closed on a criss-cross history rather than guessing a base. Packfile reads and received full/thin pack +//! decoding adopt gitoxide (see [`pack`] and [`receive`]); clone/fetch currently +//! uses an explicit installed-Git upload-pack bridge. Rit retains limits, +//! quarantine, connectivity, publication, and ref-policy ownership. pub mod blob; pub mod commit; +pub mod diff; pub mod error; +pub mod fsck; pub mod headers; +pub mod index; +pub mod merge; pub mod object; pub mod odb; pub mod oid; pub mod pack; +pub mod receive; +pub mod receive_pack; pub mod refs; pub mod repo; +pub mod server; pub mod signature; pub mod tag; pub mod tree; +pub mod upload_pack; +pub mod wire; +pub mod worktree; pub use blob::Blob; pub use commit::Commit; +pub use diff::{diff_trees, ChangeKind, TreeChange}; pub use error::{ForgeError, Result}; +pub use fsck::{ + check_connectivity, is_ancestor, ConnectivityLimits, ConnectivityReport, ObjectReader, +}; +pub use index::{Index, IndexEntry}; +pub use merge::{merge_base, merge_bases, merge_commits, merge_trees, MergeOutcome}; pub use object::{hash_content, parse_loose, Object, ObjectType}; pub use odb::Odb; pub use oid::ObjectId; pub use pack::PackStore; -pub use refs::{RefTarget, Refs}; +pub use receive::{ReceiveLimits, ReceiveObject, ReceiveOutcome, ReceiveTransaction, RefUpdate}; +pub use receive_pack::{ + advertise_receive_pack, execute_receive_pack, DeletedRef, ReceivePackExecution, + ReceivePackPolicy, RECEIVE_PACK_CAPABILITIES, +}; +pub use refs::{validate_refname, RefTarget, Refs}; pub use repo::Repository; +pub use server::{dumb_info_refs, objects_info_packs, update_server_info}; pub use signature::Signature; pub use tag::Tag; pub use tree::{Tree, TreeEntry}; +pub use upload_pack::{ + advertise_upload_pack, generate_pack, parse_fetch_request, reachable_objects, upload_pack_v0, + FetchRequest, UPLOAD_PACK_CAPABILITIES, +}; +pub use wire::{ + decode_packet_lines, encode_packet_line, encode_receive_status, parse_receive_pack_request, + PacketLine, PacketLineLimits, ReceivePackCommand, ReceivePackRequest, ReceiveStatus, +}; +pub use worktree::{StatusEntry, WorktreeState}; diff --git a/src/main.rs b/src/main.rs index 712f4e4..73d6d00 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -//! `rgf` — a small, compatibility-oriented CLI over the rithub core. +//! `rit` — a compatibility-oriented CLI and receive-pack process server. //! //! Runnable proof of the library: `hash-object`, `cat-file`, `ls-tree`, and //! `log`, operating on real `.git` repositories. @@ -9,11 +9,15 @@ use std::path::PathBuf; use anyhow::{bail, Context, Result}; use clap::{Args, Parser, Subcommand}; use rit::object::Object; -use rit::{ObjectType, Repository}; +use rit::{ + advertise_receive_pack, execute_receive_pack, parse_receive_pack_request, ObjectType, + PacketLineLimits, ReceiveLimits, ReceivePackCommand, ReceivePackPolicy, Repository, Signature, + WorktreeState, RECEIVE_PACK_CAPABILITIES, +}; #[derive(Parser)] #[command( - name = "rgf", + name = "rit", version, about = "rithub: inspect and create Git objects (loose-object core slice)" )] @@ -28,6 +32,10 @@ struct Cli { #[derive(Subcommand)] enum Command { + /// Create an empty Git repository or reinitialize an existing one. Like `git init`. + Init(InitArgs), + /// List references and the object ids they resolve to. Like `git show-ref`. + ShowRef, /// 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`. @@ -36,6 +44,35 @@ enum Command { LsTree(LsTreeArgs), /// Show commit history from a revision (default HEAD). Like `git log`. Log(LogArgs), + /// Show tracked worktree changes from the checksum-verified Git index. + Status, + /// Stage explicit files or tracked deletions in the Git index. + Add(AddArgs), + /// Write the staged index as a recursive tree object. + WriteTree, + /// Commit the staged index and atomically advance the current branch. + Commit(CommitArgs), + /// Safely materialize a commit/tree snapshot and synchronize the index without moving HEAD. + CheckoutTree(CheckoutTreeArgs), + /// Restore tracked paths from the current index. + Restore(RestoreArgs), + /// Serve one stateful Git receive-pack session over stdin/stdout. + ReceivePack(ReceivePackArgs), + /// Serve Git upload-pack through the installed open-source Git backend. + UploadPack(UploadPackArgs), +} + +#[derive(Args)] +struct InitArgs { + /// Directory to initialize (created if needed). Defaults to the current directory. + #[arg(default_value = ".")] + directory: PathBuf, + /// Create a bare repository (no worktree; skeleton lives directly in `directory`). + #[arg(long)] + bare: bool, + /// Name of the initial branch HEAD points at (default: main). + #[arg(short = 'b', long = "initial-branch")] + branch: Option, } #[derive(Args)] @@ -82,9 +119,53 @@ struct LogArgs { max: Option, } +#[derive(Args)] +struct AddArgs { + /// Worktree-relative files or directories to stage recursively. + #[arg(required = true)] + paths: Vec, +} + +#[derive(Args)] +struct CommitArgs { + /// Commit message. + #[arg(short = 'm', long, required = true)] + message: String, +} + +#[derive(Args)] +struct CheckoutTreeArgs { + /// Commit or tree revision to materialize. HEAD is accepted. + rev: String, +} + +#[derive(Args)] +struct RestoreArgs { + /// Worktree-relative tracked files or directories to restore from the index. + #[arg(required = true)] + paths: Vec, +} + +#[derive(Args)] +struct ReceivePackArgs { + /// Exact bare repository/git directory supplied by Git's transport. + git_dir: PathBuf, + /// Branch (short name, e.g. `main`) whose direct push is blocked. Repeatable. + /// A transport (SSH/HTTP) passes the repo's protected branches here for + /// non-admin pushers so the same protection applies as over smart-HTTP. + #[arg(long = "block-branch")] + block_branch: Vec, +} + +#[derive(Args)] +struct UploadPackArgs { + /// Exact bare repository/git directory supplied by Git's transport. + git_dir: PathBuf, +} + fn main() { if let Err(err) = run() { - eprintln!("rgf: {err:#}"); + eprintln!("rit: {err:#}"); std::process::exit(1); } } @@ -92,13 +173,459 @@ fn main() { fn run() -> Result<()> { let cli = Cli::parse(); match &cli.command { + Command::Init(args) => cmd_init(args), + Command::ShowRef => cmd_show_ref(&cli), 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), + Command::Status => cmd_status(&cli), + Command::Add(args) => cmd_add(&cli, args), + Command::WriteTree => cmd_write_tree(&cli), + Command::Commit(args) => cmd_commit(&cli, args), + Command::CheckoutTree(args) => cmd_checkout_tree(&cli, args), + Command::Restore(args) => cmd_restore(&cli, args), + Command::ReceivePack(args) => cmd_receive_pack(args), + Command::UploadPack(args) => cmd_upload_pack(args), } } +/// Serve a **native** stateful (SSH/process) upload-pack session over +/// stdin/stdout — no `git upload-pack` backend. Writes the v0 ref advertisement, +/// reads the client's `want`/`have`/`done` negotiation (answering `NAK` after +/// each have batch, since we advertise no multi-ack), then streams `NAK` + a +/// `rit`-generated packfile. +fn cmd_upload_pack(args: &UploadPackArgs) -> Result<()> { + let repo = Repository::open_git_dir(&args.git_dir) + .with_context(|| format!("opening bare repo at {}", args.git_dir.display()))?; + + // 1. Advertise refs. + { + let advertisement = rit::advertise_upload_pack(&repo)?; + let mut stdout = std::io::stdout().lock(); + stdout.write_all(&advertisement)?; + stdout.flush()?; + } + + // 2. Read the negotiation. The `want` list ends with a flush that needs NO + // acknowledgement; only subsequent `have` batches are answered with NAK + // (we advertise no multi-ack) until the client sends `done`. + let stdin = std::io::stdin(); + let mut input = stdin.lock(); + let mut wants: Vec = Vec::new(); + let mut haves: Vec = Vec::new(); + let mut done = false; + + // Phase 1: the want list, terminated by a flush (or `done`/EOF directly). + loop { + match read_pkt_line(&mut input)? { + PktLine::Want(oid) => wants.push(oid), + PktLine::Have(oid) => haves.push(oid), + PktLine::Flush => break, + PktLine::Done => { + done = true; + break; + } + PktLine::Eof => { + done = true; + break; + } + PktLine::Other => {} + } + } + + // Phase 2: have-negotiation rounds (only if the client isn't already done). + while !done { + let mut batch_had_have = false; + loop { + match read_pkt_line(&mut input)? { + PktLine::Have(oid) => { + haves.push(oid); + batch_had_have = true; + } + PktLine::Done => { + done = true; + break; + } + PktLine::Flush => break, + PktLine::Eof => { + done = true; + break; + } + PktLine::Want(oid) => wants.push(oid), + PktLine::Other => {} + } + } + if done { + break; + } + // Acknowledge this have-batch's flush; no common commit → NAK. + { + let mut stdout = std::io::stdout().lock(); + stdout.write_all(b"0008NAK\n")?; + stdout.flush()?; + } + if !batch_had_have { + break; // an empty batch means the client stopped negotiating + } + } + + if wants.is_empty() { + return Ok(()); + } + + // 3. Compute the object set and stream NAK + pack. + let send = rit::reachable_objects(&repo, &wants, &haves)?; + let pack = rit::generate_pack(&repo, &send)?; + let mut stdout = std::io::stdout().lock(); + stdout.write_all(b"0008NAK\n")?; + stdout.write_all(&pack)?; + stdout.flush()?; + Ok(()) +} + +/// A parsed line from an upload-pack negotiation stream. +enum PktLine { + Want(rit::ObjectId), + Have(rit::ObjectId), + Done, + Flush, + Other, + Eof, +} + +/// Read one pkt-line from `input` and classify it for upload-pack negotiation. +fn read_pkt_line(input: &mut impl Read) -> Result { + let mut header = [0u8; 4]; + match input.read_exact(&mut header) { + Ok(()) => {} + Err(error) if error.kind() == std::io::ErrorKind::UnexpectedEof => return Ok(PktLine::Eof), + Err(error) => return Err(error.into()), + } + let text = std::str::from_utf8(&header).context("pkt-line length not ASCII")?; + if !text.bytes().all(|b| b.is_ascii_hexdigit()) { + bail!("pkt-line length is not hexadecimal"); + } + let length = usize::from_str_radix(text, 16).context("parsing pkt-line length")?; + match length { + 0 => Ok(PktLine::Flush), + 1..=3 => Ok(PktLine::Other), // delimiter / response-end control packets + 4 => Ok(PktLine::Other), // empty payload + _ => { + let mut payload = vec![0u8; length - 4]; + input.read_exact(&mut payload).context("reading pkt-line payload")?; + let line = String::from_utf8_lossy(&payload); + let line = line.trim_end(); + if let Some(rest) = line.strip_prefix("want ") { + if let Some(hex) = rest.split_whitespace().next() { + return Ok(PktLine::Want(rit::ObjectId::from_hex(hex)?)); + } + } else if let Some(rest) = line.strip_prefix("have ") { + if let Some(hex) = rest.split_whitespace().next() { + if let Ok(oid) = rit::ObjectId::from_hex(hex) { + return Ok(PktLine::Have(oid)); + } + } + } else if line == "done" { + return Ok(PktLine::Done); + } + Ok(PktLine::Other) + } + } +} + +fn cmd_receive_pack(args: &ReceivePackArgs) -> Result<()> { + let repo = Repository::open_git_dir(&args.git_dir) + .with_context(|| format!("opening bare repo at {}", args.git_dir.display()))?; + let advertisement = advertise_receive_pack(&repo, RECEIVE_PACK_CAPABILITIES)?; + { + let mut stdout = std::io::stdout().lock(); + stdout.write_all(&advertisement)?; + stdout.flush()?; + } + + let packet_limits = PacketLineLimits::default(); + let receive_limits = ReceiveLimits::default(); + let request = read_receive_pack_request(packet_limits, receive_limits)?; + + // Protected-branch enforcement (same policy as smart-HTTP): if any command + // targets a blocked branch, reject the whole push with a per-ref `ng` status + // and never touch objects or refs. + if !args.block_branch.is_empty() { + let parsed = parse_receive_pack_request(&request, RECEIVE_PACK_CAPABILITIES, packet_limits)?; + let blocked = parsed.commands.iter().any(|cmd| { + cmd.name() + .strip_prefix("refs/heads/") + .map(|b| args.block_branch.iter().any(|x| x == b)) + .unwrap_or(false) + }); + if blocked { + let report_status = parsed.capabilities.contains("report-status") + || parsed.capabilities.contains("report-status-v2"); + if report_status { + let statuses: Vec = parsed + .commands + .iter() + .map(|cmd| rit::ReceiveStatus { + ref_name: cmd.name().to_string(), + error: Some( + "protected branch: direct push blocked, open a pull request".into(), + ), + }) + .collect(); + let response = rit::encode_receive_status(None, &statuses)?; + let mut stdout = std::io::stdout().lock(); + stdout.write_all(&response)?; + stdout.flush()?; + } + eprintln!("rit receive-pack rejected: protected branch"); + return Ok(()); + } + } + + let execution = execute_receive_pack( + &repo, + &request, + RECEIVE_PACK_CAPABILITIES, + packet_limits, + receive_limits, + ReceivePackPolicy::default(), + )?; + let mut stdout = std::io::stdout().lock(); + stdout.write_all(&execution.response)?; + stdout.flush()?; + if let Some(error) = execution.rejection { + eprintln!("rit receive-pack rejected request: {error}"); + } + Ok(()) +} + +/// Read the command pkt-lines exactly through their flush boundary. A pure +/// deletion has no pack and Git keeps the write side open while awaiting the +/// status response, so waiting for EOF would deadlock. Create/update requests +/// continue through the bounded raw-pack body; stock Git closes that request +/// side after its pack trailer. +fn read_receive_pack_request( + packet_limits: PacketLineLimits, + receive_limits: ReceiveLimits, +) -> Result> { + let stdin = std::io::stdin(); + let mut input = stdin.lock(); + let mut request = Vec::new(); + let mut packets = 0usize; + let mut payload_bytes = 0usize; + loop { + if packets == packet_limits.max_packets { + bail!( + "receive-pack exceeds {} command packets", + packet_limits.max_packets + ); + } + let mut header = [0u8; 4]; + input + .read_exact(&mut header) + .context("reading receive-pack packet header")?; + let header_text = + std::str::from_utf8(&header).context("receive-pack packet header is not ASCII")?; + if !header_text.bytes().all(|byte| byte.is_ascii_hexdigit()) { + bail!("receive-pack packet length is not hexadecimal"); + } + let length = + usize::from_str_radix(header_text, 16).context("parsing receive-pack packet length")?; + request.extend_from_slice(&header); + packets += 1; + match length { + 0 => break, + 1..=3 => bail!("receive-pack command stream used reserved control {header_text}"), + 4..=rit::wire::MAX_PACKET_LENGTH => { + let payload_len = length - 4; + payload_bytes = payload_bytes + .checked_add(payload_len) + .context("receive command byte count overflow")?; + if payload_bytes > packet_limits.max_total_payload { + bail!( + "receive commands exceed {} payload bytes", + packet_limits.max_total_payload + ); + } + let start = request.len(); + request.resize(start + payload_len, 0); + input + .read_exact(&mut request[start..]) + .context("reading receive-pack packet payload")?; + } + _ => bail!( + "receive-pack packet length exceeds {} bytes", + rit::wire::MAX_PACKET_LENGTH + ), + } + } + + let parsed = parse_receive_pack_request(&request, RECEIVE_PACK_CAPABILITIES, packet_limits)?; + let needs_pack = parsed.commands.len() == 1 + && !matches!(parsed.commands[0], ReceivePackCommand::Delete { .. }); + if needs_pack { + let max_pack = usize::try_from(receive_limits.max_pack_bytes) + .context("receive pack limit does not fit this platform")?; + let read_limit = receive_limits + .max_pack_bytes + .checked_add(1) + .context("receive pack limit overflow")?; + input + .take(read_limit) + .read_to_end(&mut request) + .context("reading receive-pack pack body")?; + if request.len().saturating_sub(parsed.pack_offset) > max_pack { + bail!("receive-pack body exceeds {max_pack} bytes"); + } + } + Ok(request) +} + +fn cmd_checkout_tree(cli: &Cli, args: &CheckoutTreeArgs) -> Result<()> { + let repo = Repository::discover(&cli.repo) + .with_context(|| format!("opening repo at {}", cli.repo.display()))?; + let oid = repo.resolve(&args.rev)?; + let tree = match repo.read_object(&oid)? { + Object::Tree(_) => oid, + Object::Commit(commit) => commit.tree, + Object::Tag(_) => repo.peel_to_commit(oid)?.tree, + Object::Blob(_) => bail!("{} is a blob, not a checkout snapshot", args.rev), + }; + repo.checkout_tree(tree)?; + Ok(()) +} + +fn cmd_restore(cli: &Cli, args: &RestoreArgs) -> Result<()> { + let repo = Repository::discover(&cli.repo) + .with_context(|| format!("opening repo at {}", cli.repo.display()))?; + repo.restore_index(&args.paths)?; + Ok(()) +} + +fn cmd_write_tree(cli: &Cli) -> Result<()> { + let repo = Repository::discover(&cli.repo) + .with_context(|| format!("opening repo at {}", cli.repo.display()))?; + println!("{}", repo.write_tree()?); + Ok(()) +} + +fn cmd_commit(cli: &Cli, args: &CommitArgs) -> Result<()> { + let repo = Repository::discover(&cli.repo) + .with_context(|| format!("opening repo at {}", cli.repo.display()))?; + let (name, email) = git_identity(repo.git_dir())?; + let seconds = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .context("system clock is before unix epoch")? + .as_secs(); + let raw = format!("{name} <{email}> {seconds} +0000"); + let signature = Signature::parse(&raw, "commit")?; + let oid = repo.commit_index(&args.message, signature.clone(), signature)?; + println!("{oid}"); + Ok(()) +} + +fn git_identity(git_dir: &std::path::Path) -> Result<(String, String)> { + let env_name = std::env::var("GIT_AUTHOR_NAME").ok(); + let env_email = std::env::var("GIT_AUTHOR_EMAIL").ok(); + if let (Some(name), Some(email)) = (env_name, env_email) { + return Ok((name, email)); + } + let config = std::fs::read_to_string(git_dir.join("config")).unwrap_or_default(); + let mut in_user = false; + let mut name = None; + let mut email = None; + for line in config.lines() { + let trimmed = line.trim(); + if trimmed.starts_with('[') { + in_user = trimmed.eq_ignore_ascii_case("[user]"); + continue; + } + if !in_user { + continue; + } + if let Some((key, value)) = trimmed.split_once('=') { + match key.trim().to_ascii_lowercase().as_str() { + "name" => name = Some(value.trim().to_string()), + "email" => email = Some(value.trim().to_string()), + _ => {} + } + } + } + match (name, email) { + (Some(name), Some(email)) if !name.is_empty() && !email.is_empty() => Ok((name, email)), + _ => bail!("configure user.name and user.email or GIT_AUTHOR_NAME/GIT_AUTHOR_EMAIL"), + } +} + +fn cmd_add(cli: &Cli, args: &AddArgs) -> Result<()> { + let repo = Repository::discover(&cli.repo) + .with_context(|| format!("opening repo at {}", cli.repo.display()))?; + repo.add(&args.paths)?; + Ok(()) +} + +fn cmd_status(cli: &Cli) -> Result<()> { + let repo = Repository::discover(&cli.repo) + .with_context(|| format!("opening repo at {}", cli.repo.display()))?; + for entry in repo.tracked_status()? { + if entry.state == WorktreeState::Unmodified { + continue; + } + let code = match entry.state { + WorktreeState::Modified => " M", + WorktreeState::Deleted => " D", + WorktreeState::TypeChanged => " T", + WorktreeState::Unmodified => continue, + }; + println!("{code} {}", String::from_utf8_lossy(&entry.path)); + } + Ok(()) +} + +fn cmd_init(args: &InitArgs) -> Result<()> { + // Create the target directory itself if it does not exist yet (git init + // creates the directory). This does not create missing parents beyond + // what the user named, mirroring `git init`. + if !args.directory.exists() { + std::fs::create_dir_all(&args.directory) + .with_context(|| format!("creating {}", args.directory.display()))?; + } + let existed = if args.bare { + args.directory.join("HEAD").exists() + } else { + args.directory.join(".git").join("HEAD").exists() + }; + let repo = Repository::init(&args.directory, args.bare, args.branch.as_deref()) + .with_context(|| format!("initializing repository at {}", args.directory.display()))?; + let action = if existed { + "Reinitialized existing" + } else { + "Initialized empty" + }; + let kind = if args.bare { "bare " } else { "" }; + let git_dir = std::fs::canonicalize(repo.git_dir()).unwrap_or_else(|_| repo.git_dir().into()); + println!( + "{action} {kind}Git repository in {}/", + git_dir.display() + ); + Ok(()) +} + +fn cmd_show_ref(cli: &Cli) -> Result<()> { + let repo = Repository::discover(&cli.repo) + .with_context(|| format!("opening repo at {}", cli.repo.display()))?; + let refs = repo.refs().list()?; + if refs.is_empty() { + return Ok(()); + } + let mut out = std::io::stdout().lock(); + for (name, oid) in refs { + writeln!(out, "{oid} {name}")?; + } + Ok(()) +} + 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))?; diff --git a/src/merge.rs b/src/merge.rs new file mode 100644 index 0000000..7770634 --- /dev/null +++ b/src/merge.rs @@ -0,0 +1,621 @@ +//! From-scratch three-way merge — merge base, tree merge, and file-content +//! merge — so the forge no longer needs the installed `git merge-tree` bridge. +//! +//! The algorithm is the classic three-way merge: +//! - [`merge_base`] finds a common ancestor via a bounded reachability walk; +//! - [`merge_trees`] flattens the base/ours/theirs trees to path→(mode,oid) maps +//! and applies the standard per-path rules (only-one-side-changed takes that +//! side; both-sides-same takes it; divergent blobs are content-merged); +//! - file content is merged with `diffy`'s diff3 (`merge_bytes`), which returns a +//! clean buffer or one with conflict markers — we treat the latter as a +//! conflicted path. +//! +//! The result is either a clean merged **tree oid** or the list of conflicted +//! paths — exactly what a forge needs to decide mergeability and to build a real +//! two-parent merge commit (`Repository::commit_tree`), with no external Git. + +use std::collections::{BTreeMap, HashSet, VecDeque}; + +use crate::error::{ForgeError, Result}; +use crate::object::{Object, ObjectType}; +use crate::oid::ObjectId; +use crate::repo::Repository; + +/// Bound on commits walked while searching for a merge base. +const MAX_MERGE_WALK: usize = 500_000; +/// Bound on tree recursion / entries. +const MAX_MERGE_DEPTH: usize = 256; +const MAX_MERGE_ENTRIES: usize = 1_000_000; + +/// The outcome of a three-way merge. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum MergeOutcome { + /// A clean merge producing this tree object id. + Clean(ObjectId), + /// Conflicting paths (sorted). No tree is produced. + Conflict(Vec), +} + +/// All lowest common ancestors (merge bases) of two commits. +/// +/// Correctness note: the closest common ancestor reached by a naïve breadth- +/// first walk from one side is **not** necessarily a *lowest* common ancestor — +/// BFS can surface an older common ancestor before a newer one that is itself a +/// descendant of it, which would pick a too-old base and cause spurious +/// conflicts or a silently wrong merge. This instead computes the true LCA set: +/// the common ancestors that are not themselves a proper ancestor of any other +/// common ancestor. The set has exactly one element for ordinary (diamond) +/// histories and more than one only for criss-cross histories. +pub fn merge_bases(repo: &Repository, a: ObjectId, b: ObjectId) -> Result> { + let a_ancestors = ancestors(repo, a)?; + let b_ancestors = ancestors(repo, b)?; + let common: HashSet = a_ancestors.intersection(&b_ancestors).copied().collect(); + if common.is_empty() { + return Ok(Vec::new()); + } + + // `non_maximal` collects every node reachable by walking strictly upward + // (through parents) from any common node. A common node found in this set is + // a *proper* ancestor of another common node, so it is not a merge base. + let mut non_maximal: HashSet = HashSet::new(); + let mut queue: VecDeque = VecDeque::new(); + for &oid in &common { + if let Ok(Object::Commit(commit)) = repo.odb().read(&oid) { + for parent in commit.parents { + queue.push_back(parent); + } + } + } + while let Some(oid) = queue.pop_front() { + if !non_maximal.insert(oid) { + continue; + } + if non_maximal.len() > MAX_MERGE_WALK { + return Err(ForgeError::ResourceLimit( + "merge-base walk exceeds limit".into(), + )); + } + if let Ok(Object::Commit(commit)) = repo.odb().read(&oid) { + for parent in commit.parents { + queue.push_back(parent); + } + } + } + + let mut bases: Vec = common + .into_iter() + .filter(|oid| !non_maximal.contains(oid)) + .collect(); + bases.sort(); // deterministic order for callers and tests + Ok(bases) +} + +/// The single merge base of two commits, if any. +/// +/// Returns the deterministic first of the [`merge_bases`] set. When more than +/// one merge base exists (a criss-cross history) this still returns one, but +/// merging callers should prefer [`merge_commits`], which fails closed rather +/// than committing to an arbitrary base. +pub fn merge_base(repo: &Repository, a: ObjectId, b: ObjectId) -> Result> { + Ok(merge_bases(repo, a, b)?.into_iter().next()) +} + +/// Three-way merge two commits: merge their trees relative to their merge base. +/// +/// Fails closed with [`ForgeError::MergeMultipleBases`] on a criss-cross +/// history (more than one merge base). A single-base three-way merge cannot +/// pick between multiple bases without a recursive/virtual-base strategy (out +/// of scope for this slice), and guessing one risks silently mis-resolving a +/// hunk — so this reports the ambiguity instead of producing a wrong tree. +pub fn merge_commits(repo: &Repository, ours: ObjectId, theirs: ObjectId) -> Result { + let bases = merge_bases(repo, ours, theirs)?; + if bases.len() > 1 { + return Err(ForgeError::MergeMultipleBases(bases.len())); + } + let base_tree = match bases.first() { + Some(oid) => Some(repo.peel_to_commit(*oid)?.tree), + None => None, + }; + let ours_tree = repo.peel_to_commit(ours)?.tree; + let theirs_tree = repo.peel_to_commit(theirs)?.tree; + merge_trees(repo, base_tree, ours_tree, theirs_tree) +} + +/// Three-way merge three trees. `base` is `None` for an empty common ancestor. +pub fn merge_trees( + repo: &Repository, + base: Option, + ours: ObjectId, + theirs: ObjectId, +) -> Result { + let base_map = flatten_opt(repo, base)?; + let ours_map = flatten(repo, ours)?; + let theirs_map = flatten(repo, theirs)?; + + // Union of all paths across the three trees. + let mut paths: Vec> = base_map + .keys() + .chain(ours_map.keys()) + .chain(theirs_map.keys()) + .cloned() + .collect(); + paths.sort(); + paths.dedup(); + + let mut merged: BTreeMap, (String, ObjectId)> = BTreeMap::new(); + let mut conflicts: Vec = Vec::new(); + + for path in paths { + let b = base_map.get(&path); + let o = ours_map.get(&path); + let t = theirs_map.get(&path); + match resolve_path(repo, &path, b, o, t)? { + PathResolution::Take(Some(entry)) => { + merged.insert(path, entry); + } + PathResolution::Take(None) => { /* deleted on both / took a deletion */ } + PathResolution::Conflict => { + conflicts.push(String::from_utf8_lossy(&path).into_owned()); + } + } + } + + // A file/directory name collision (a path is also a directory prefix of + // another) cannot be represented in one tree — treat as a conflict. + detect_collisions(&merged, &mut conflicts); + + if !conflicts.is_empty() { + conflicts.sort(); + conflicts.dedup(); + return Ok(MergeOutcome::Conflict(conflicts)); + } + let tree = build_tree(repo, &merged)?; + Ok(MergeOutcome::Clean(tree)) +} + +/// The per-path decision. +enum PathResolution { + /// Take this entry (or a deletion when `None`). + Take(Option<(String, ObjectId)>), + /// The path conflicts. + Conflict, +} + +type Entry = (String, ObjectId); + +fn resolve_path( + repo: &Repository, + path: &[u8], + b: Option<&Entry>, + o: Option<&Entry>, + t: Option<&Entry>, +) -> Result { + // Both sides identical (including both absent) → take either. + if o == t { + return Ok(PathResolution::Take(o.cloned())); + } + // Ours unchanged vs base → take theirs' change (which may be a deletion). + if o == b { + return Ok(PathResolution::Take(t.cloned())); + } + // Theirs unchanged vs base → take ours' change. + if t == b { + return Ok(PathResolution::Take(o.cloned())); + } + // Both sides changed differently. + match (o, t) { + // Modify/modify: content-merge the two blobs against the base. + (Some((omode, ooid)), Some((tmode, toid))) => { + if omode != tmode { + return Ok(PathResolution::Conflict); // mode conflict + } + let base_bytes = match b { + Some((_, boid)) => repo.blob(boid)?.data, + None => Vec::new(), // added on both sides + }; + let ours_bytes = repo.blob(ooid)?.data; + let theirs_bytes = repo.blob(toid)?.data; + // Binary content that diverges cannot be line-merged safely. + if is_binary(&base_bytes) || is_binary(&ours_bytes) || is_binary(&theirs_bytes) { + return Ok(PathResolution::Conflict); + } + match diffy::merge_bytes(&base_bytes, &ours_bytes, &theirs_bytes) { + Ok(bytes) => { + let oid = repo.hash_object(ObjectType::Blob, &bytes, true)?; + Ok(PathResolution::Take(Some((omode.clone(), oid)))) + } + Err(_) => { + let _ = path; // path captured by caller for the conflict list + Ok(PathResolution::Conflict) + } + } + } + // Modify/delete (one side removed, the other changed) → conflict. + _ => Ok(PathResolution::Conflict), + } +} + +/// A blob is treated as binary if it contains a NUL byte. +fn is_binary(data: &[u8]) -> bool { + data.contains(&0) +} + +/// Flatten an optional tree; `None` → empty map. +fn flatten_opt( + repo: &Repository, + tree: Option, +) -> Result, Entry>> { + match tree { + Some(oid) => flatten(repo, oid), + None => Ok(BTreeMap::new()), + } +} + +/// Flatten a tree to path→(mode,oid) leaf entries. +fn flatten(repo: &Repository, tree: ObjectId) -> Result, Entry>> { + let mut out = BTreeMap::new(); + flatten_into(repo, tree, Vec::new(), 0, &mut out)?; + Ok(out) +} + +fn flatten_into( + repo: &Repository, + tree_oid: ObjectId, + prefix: Vec, + depth: usize, + out: &mut BTreeMap, Entry>, +) -> Result<()> { + if depth > MAX_MERGE_DEPTH { + return Err(ForgeError::ResourceLimit(format!( + "merge tree depth exceeds {MAX_MERGE_DEPTH}" + ))); + } + for entry in repo.tree(&tree_oid)?.entries { + let mut path = prefix.clone(); + if !path.is_empty() { + path.push(b'/'); + } + path.extend_from_slice(&entry.name); + if entry.kind() == "tree" { + flatten_into(repo, entry.oid, path, depth + 1, out)?; + } else { + out.insert(path, (entry.mode.clone(), entry.oid)); + if out.len() > MAX_MERGE_ENTRIES { + return Err(ForgeError::ResourceLimit(format!( + "merge exceeds {MAX_MERGE_ENTRIES} paths" + ))); + } + } + } + Ok(()) +} + +/// Flag any path that is also a directory prefix of another path. +fn detect_collisions(merged: &BTreeMap, Entry>, conflicts: &mut Vec) { + let paths: Vec<&Vec> = merged.keys().collect(); + for (i, p) in paths.iter().enumerate() { + let mut prefix = (*p).clone(); + prefix.push(b'/'); + // Any other path starting with `p/` means `p` is both file and dir. + for q in paths.iter().skip(i + 1) { + if q.starts_with(&prefix) { + conflicts.push(String::from_utf8_lossy(p).into_owned()); + break; + } + } + } +} + +// --- tree reconstruction ---------------------------------------------------- + +#[derive(Default)] +struct TreeNode { + files: BTreeMap, Entry>, + dirs: BTreeMap, TreeNode>, +} + +impl TreeNode { + fn insert(&mut self, path: &[u8], entry: Entry) { + let mut parts = path.split(|b| *b == b'/').peekable(); + let mut node = self; + while let Some(part) = parts.next() { + if parts.peek().is_none() { + node.files.insert(part.to_vec(), entry); + return; + } + node = node.dirs.entry(part.to_vec()).or_default(); + } + } + + fn write(&self, repo: &Repository) -> Result { + use crate::tree::{Tree, TreeEntry}; + let mut entries = Vec::with_capacity(self.files.len() + self.dirs.len()); + for (name, (mode, oid)) in &self.files { + entries.push(TreeEntry { + mode: mode.clone(), + name: name.clone(), + oid: *oid, + }); + } + for (name, child) in &self.dirs { + entries.push(TreeEntry { + mode: "40000".into(), + name: name.clone(), + oid: child.write(repo)?, + }); + } + let mut tree = Tree { entries }; + tree.canonicalize(); + repo.hash_object(ObjectType::Tree, &tree.serialize(), true) + } +} + +fn build_tree(repo: &Repository, merged: &BTreeMap, Entry>) -> Result { + let mut root = TreeNode::default(); + for (path, entry) in merged { + root.insert(path, entry.clone()); + } + root.write(repo) +} + +/// All ancestors of `start` (inclusive), bounded. +fn ancestors(repo: &Repository, start: ObjectId) -> Result> { + let mut set = HashSet::new(); + let mut queue = VecDeque::from([start]); + while let Some(oid) = queue.pop_front() { + if !set.insert(oid) { + continue; + } + if set.len() > MAX_MERGE_WALK { + return Err(ForgeError::ResourceLimit( + "merge-base walk exceeds limit".into(), + )); + } + if let Ok(Object::Commit(commit)) = repo.odb().read(&oid) { + for parent in commit.parents { + queue.push_back(parent); + } + } + } + Ok(set) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{Signature, Tree, TreeEntry}; + + fn sig() -> Signature { + Signature::parse("T 1700000000 +0000", "commit").unwrap() + } + + fn blob(repo: &Repository, data: &[u8]) -> ObjectId { + repo.hash_object(ObjectType::Blob, data, true).unwrap() + } + + fn tree(repo: &Repository, entries: Vec<(&str, ObjectId)>) -> ObjectId { + let mut t = Tree { + entries: entries + .into_iter() + .map(|(name, oid)| TreeEntry { + mode: "100644".into(), + name: name.as_bytes().to_vec(), + oid, + }) + .collect(), + }; + t.canonicalize(); + repo.hash_object(ObjectType::Tree, &t.serialize(), true).unwrap() + } + + fn tree_modes(repo: &Repository, entries: Vec<(&str, &str, ObjectId)>) -> ObjectId { + let mut t = Tree { + entries: entries + .into_iter() + .map(|(name, mode, oid)| TreeEntry { + mode: mode.into(), + name: name.as_bytes().to_vec(), + oid, + }) + .collect(), + }; + t.canonicalize(); + repo.hash_object(ObjectType::Tree, &t.serialize(), true).unwrap() + } + + fn commit(repo: &Repository, tree: ObjectId, parents: Vec) -> ObjectId { + repo.commit_tree(tree, parents, sig(), sig(), b"c\n", None).unwrap() + } + + #[test] + fn clean_merge_of_divergent_changes() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path(), true, Some("main")).unwrap(); + // base: a="1\n2\n3\n", shared="x\n" + let a0 = blob(&repo, b"1\n2\n3\n"); + let shared = blob(&repo, b"x\n"); + let base_tree = tree(&repo, vec![("a.txt", a0), ("shared.txt", shared)]); + let base = commit(&repo, base_tree, vec![]); + // ours: change line 1 + add ours.txt + let a_ours = blob(&repo, b"ONE\n2\n3\n"); + let ours_only = blob(&repo, b"ours\n"); + let ours_tree = tree( + &repo, + vec![("a.txt", a_ours), ("shared.txt", shared), ("ours.txt", ours_only)], + ); + let ours = commit(&repo, ours_tree, vec![base]); + // theirs: change line 3 + add theirs.txt + let a_theirs = blob(&repo, b"1\n2\nTHREE\n"); + let theirs_only = blob(&repo, b"theirs\n"); + let theirs_tree = tree( + &repo, + vec![("a.txt", a_theirs), ("shared.txt", shared), ("theirs.txt", theirs_only)], + ); + let theirs = commit(&repo, theirs_tree, vec![base]); + + match merge_commits(&repo, ours, theirs).unwrap() { + MergeOutcome::Clean(merged_tree) => { + // a.txt must contain BOTH edits; both added files present. + let flat = flatten(&repo, merged_tree).unwrap(); + let a = repo.blob(&flat[b"a.txt".as_slice()].1).unwrap().data; + assert_eq!(a, b"ONE\n2\nTHREE\n"); + assert!(flat.contains_key(b"ours.txt".as_slice())); + assert!(flat.contains_key(b"theirs.txt".as_slice())); + } + other => panic!("expected clean merge, got {other:?}"), + } + } + + #[test] + fn conflicting_edits_report_conflict() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path(), true, Some("main")).unwrap(); + let a0 = blob(&repo, b"line\n"); + let base_tree = tree(&repo, vec![("a.txt", a0)]); + let base = commit(&repo, base_tree, vec![]); + let a_ours = blob(&repo, b"ours change\n"); + let ours = commit(&repo, tree(&repo, vec![("a.txt", a_ours)]), vec![base]); + let a_theirs = blob(&repo, b"theirs change\n"); + let theirs = commit(&repo, tree(&repo, vec![("a.txt", a_theirs)]), vec![base]); + + match merge_commits(&repo, ours, theirs).unwrap() { + MergeOutcome::Conflict(paths) => assert_eq!(paths, vec!["a.txt".to_string()]), + other => panic!("expected conflict, got {other:?}"), + } + } + + // merge_base must return the *lowest* common ancestor, not merely the first + // common ancestor a breadth-first walk happens to reach. In a linear + // base->g1->g2 history where both sides fork at g2, the only merge base is + // g2 — never g1 or the root. + #[test] + fn merge_base_is_the_lowest_common_ancestor() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path(), true, Some("main")).unwrap(); + let blob0 = blob(&repo, b"0\n"); + let root = commit(&repo, tree(&repo, vec![("a.txt", blob0)]), vec![]); + let g1 = commit(&repo, tree(&repo, vec![("a.txt", blob0)]), vec![root]); + let g2 = commit(&repo, tree(&repo, vec![("a.txt", blob0)]), vec![g1]); + // Distinct trees so the two tips are distinct commits (not the same oid). + let ours = commit(&repo, tree(&repo, vec![("a.txt", blob(&repo, b"ours\n"))]), vec![g2]); + let theirs = + commit(&repo, tree(&repo, vec![("a.txt", blob(&repo, b"theirs\n"))]), vec![g2]); + + assert_eq!(merge_bases(&repo, ours, theirs).unwrap(), vec![g2]); + assert_eq!(merge_base(&repo, ours, theirs).unwrap(), Some(g2)); + // No common history → no merge base. + let orphan = commit(&repo, tree(&repo, vec![("z.txt", blob0)]), vec![]); + assert_eq!(merge_bases(&repo, ours, orphan).unwrap(), Vec::new()); + assert_eq!(merge_base(&repo, ours, orphan).unwrap(), None); + } + + // A criss-cross history has two lowest common ancestors. merge_bases must + // report both, and merge_commits must fail closed rather than silently + // pick one and risk mis-resolving content. + #[test] + fn criss_cross_history_reports_both_bases_and_fails_closed() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path(), true, Some("main")).unwrap(); + let blob0 = blob(&repo, b"0\n"); + let root = commit(&repo, tree(&repo, vec![("a.txt", blob0)]), vec![]); + let a = commit(&repo, tree(&repo, vec![("a.txt", blob(&repo, b"a\n"))]), vec![root]); + let b = commit(&repo, tree(&repo, vec![("a.txt", blob(&repo, b"b\n"))]), vec![root]); + // Two independent merges of the same pair of branches => criss-cross. + let m1 = commit(&repo, tree(&repo, vec![("a.txt", blob(&repo, b"m1\n"))]), vec![a, b]); + let m2 = commit(&repo, tree(&repo, vec![("a.txt", blob(&repo, b"m2\n"))]), vec![a, b]); + + let mut bases = merge_bases(&repo, m1, m2).unwrap(); + bases.sort(); + let mut expected = vec![a, b]; + expected.sort(); + assert_eq!(bases, expected, "both a and b are lowest common ancestors"); + + assert!(matches!( + merge_commits(&repo, m1, m2), + Err(ForgeError::MergeMultipleBases(2)) + )); + } + + // A path modified on one side and deleted on the other is a conflict. + #[test] + fn modify_delete_conflicts() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path(), true, Some("main")).unwrap(); + let a0 = blob(&repo, b"base\n"); + let keep = blob(&repo, b"keep\n"); + let base = commit(&repo, tree(&repo, vec![("a.txt", a0), ("k.txt", keep)]), vec![]); + // ours modifies a.txt; theirs deletes a.txt (tree omits it). + let a_mod = blob(&repo, b"changed\n"); + let ours = commit(&repo, tree(&repo, vec![("a.txt", a_mod), ("k.txt", keep)]), vec![base]); + let theirs = commit(&repo, tree(&repo, vec![("k.txt", keep)]), vec![base]); + + match merge_commits(&repo, ours, theirs).unwrap() { + MergeOutcome::Conflict(paths) => assert_eq!(paths, vec!["a.txt".to_string()]), + other => panic!("expected modify/delete conflict, got {other:?}"), + } + } + + // The same path changing mode on one side and content on the other is a + // conflict (a mode change cannot be silently reconciled with a content edit). + #[test] + fn mode_change_conflicts_with_content_change() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path(), true, Some("main")).unwrap(); + let content = blob(&repo, b"x\n"); + let base = commit(&repo, tree_modes(&repo, vec![("s.sh", "100644", content)]), vec![]); + // ours: chmod +x, same content. theirs: same mode, new content. + let ours = commit(&repo, tree_modes(&repo, vec![("s.sh", "100755", content)]), vec![base]); + let new_content = blob(&repo, b"y\n"); + let theirs = + commit(&repo, tree_modes(&repo, vec![("s.sh", "100644", new_content)]), vec![base]); + + match merge_commits(&repo, ours, theirs).unwrap() { + MergeOutcome::Conflict(paths) => assert_eq!(paths, vec!["s.sh".to_string()]), + other => panic!("expected mode conflict, got {other:?}"), + } + } + + // The same new file added identically on both sides merges cleanly. + #[test] + fn add_add_identical_is_clean() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path(), true, Some("main")).unwrap(); + let base = commit(&repo, tree(&repo, vec![]), vec![]); + let same = blob(&repo, b"new file\n"); + // Each side also adds a unique file so the two tips are distinct commits; + // new.txt is the add/add-identical path under test. + let ours_only = blob(&repo, b"o\n"); + let theirs_only = blob(&repo, b"t\n"); + let ours = commit(&repo, tree(&repo, vec![("new.txt", same), ("o.txt", ours_only)]), vec![base]); + let theirs = + commit(&repo, tree(&repo, vec![("new.txt", same), ("t.txt", theirs_only)]), vec![base]); + + match merge_commits(&repo, ours, theirs).unwrap() { + MergeOutcome::Clean(t) => { + let flat = flatten(&repo, t).unwrap(); + assert_eq!(flat[b"new.txt".as_slice()].1, same); + assert!(flat.contains_key(b"o.txt".as_slice())); + assert!(flat.contains_key(b"t.txt".as_slice())); + } + other => panic!("expected clean merge, got {other:?}"), + } + } + + // Divergent binary content cannot be line-merged and must conflict rather + // than corrupt the file. + #[test] + fn divergent_binary_conflicts() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path(), true, Some("main")).unwrap(); + let base_bin = blob(&repo, b"\x00base"); + let base = commit(&repo, tree(&repo, vec![("a.bin", base_bin)]), vec![]); + let ours = commit(&repo, tree(&repo, vec![("a.bin", blob(&repo, b"\x00ours"))]), vec![base]); + let theirs = + commit(&repo, tree(&repo, vec![("a.bin", blob(&repo, b"\x00theirs"))]), vec![base]); + + match merge_commits(&repo, ours, theirs).unwrap() { + MergeOutcome::Conflict(paths) => assert_eq!(paths, vec!["a.bin".to_string()]), + other => panic!("expected binary conflict, got {other:?}"), + } + } +} diff --git a/src/odb.rs b/src/odb.rs index b7407ad..8bec9f7 100644 --- a/src/odb.rs +++ b/src/odb.rs @@ -12,8 +12,10 @@ use crate::pack::PackStore; use flate2::read::ZlibDecoder; use flate2::write::ZlibEncoder; use flate2::Compression; +use std::fs::OpenOptions; use std::io::{Read, Write}; use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; use std::time::{SystemTime, UNIX_EPOCH}; /// The object database rooted at a `.git/objects` directory. @@ -62,6 +64,9 @@ impl Odb { let (oid, full) = object::hash_content(obj_type, payload); let final_path = self.loose_path(&oid); if final_path.exists() { + // Content addressing is only safe if an existing path really holds + // the expected bytes. Never bless a corrupt or symlinked object. + self.read_raw(&oid)?; return Ok(oid); } @@ -69,20 +74,32 @@ impl Odb { .parent() .expect("loose path always has a parent directory"); std::fs::create_dir_all(dir)?; + reject_symlink(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 (tmp_path, mut file) = self.create_temp(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()); + // `hard_link` is an atomic no-clobber publish on the same filesystem. + // Concurrent identical writers converge; neither can replace an + // existing (possibly corrupt) object behind the other's back. + match std::fs::hard_link(&tmp_path, &final_path) { + Ok(()) => { + let _ = std::fs::remove_file(&tmp_path); + sync_dir(dir)?; + } + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => { + let _ = std::fs::remove_file(&tmp_path); + self.read_raw(&oid)?; + } + Err(error) => { + let _ = std::fs::remove_file(&tmp_path); + return Err(error.into()); + } } Ok(oid) } @@ -94,15 +111,21 @@ impl Odb { /// back to `oid` before returning them. pub fn read_raw(&self, oid: &ObjectId) -> Result<(ObjectType, Vec)> { let path = self.loose_path(oid); + if let Ok(metadata) = std::fs::symlink_metadata(&path) { + if metadata.file_type().is_symlink() { + return Err(ForgeError::UnsafePath(path.display().to_string())); + } + } let compressed = match std::fs::read(&path) { Ok(bytes) => bytes, // Not loose — try the packed path (gitoxide) if we have one. - Err(_) => { + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { return match &self.pack { Some(pack) => pack.read_raw(oid), None => Err(ForgeError::ObjectNotFound(oid.to_hex())), }; } + Err(error) => return Err(error.into()), }; let full = zlib_decompress(&compressed)?; @@ -140,7 +163,10 @@ impl Odb { /// Resolve an abbreviated hex prefix (>= 4 chars) to a unique full oid. pub fn resolve_prefix(&self, prefix: &str) -> Result { - if prefix.len() < 4 || prefix.len() > 40 { + if prefix.len() < 4 + || prefix.len() > 40 + || !prefix.bytes().all(|byte| byte.is_ascii_hexdigit()) + { return Err(ForgeError::InvalidOid(format!( "prefix must be 4-40 hex chars: {prefix:?}" ))); @@ -188,16 +214,41 @@ impl Odb { } /// Build a unique temporary file path inside `dir` (no extra deps). - fn temp_path(&self, dir: &Path) -> PathBuf { + fn create_temp(&self, dir: &Path) -> Result<(PathBuf, std::fs::File)> { + static NEXT_TEMP: AtomicU64 = AtomicU64::new(0); 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}")) + for _ in 0..128 { + let sequence = NEXT_TEMP.fetch_add(1, Ordering::Relaxed); + let path = dir.join(format!(".rit-tmp-{pid}-{nanos}-{sequence}")); + match OpenOptions::new().write(true).create_new(true).open(&path) { + Ok(file) => return Ok((path, file)), + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue, + Err(error) => return Err(error.into()), + } + } + Err(ForgeError::Io(std::io::Error::new( + std::io::ErrorKind::AlreadyExists, + "could not allocate a unique loose-object temporary file", + ))) } } +fn reject_symlink(path: &Path) -> Result<()> { + if std::fs::symlink_metadata(path)?.file_type().is_symlink() { + return Err(ForgeError::UnsafePath(path.display().to_string())); + } + Ok(()) +} + +fn sync_dir(path: &Path) -> Result<()> { + std::fs::File::open(path)?.sync_all()?; + Ok(()) +} + /// Compress bytes with zlib (the format Git uses for loose objects). fn zlib_compress(data: &[u8]) -> Result> { let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default()); @@ -274,4 +325,66 @@ mod tests { let short = &oid.to_hex()[..8]; assert_eq!(odb.resolve_prefix(short).unwrap(), oid); } + + #[test] + fn prefix_cannot_escape_objects_directory() { + let dir = temp_git_dir(); + let odb = Odb::new(dir.path()); + assert!(matches!( + odb.resolve_prefix("../../etc"), + Err(ForgeError::InvalidOid(_)) + )); + } + + #[test] + fn write_does_not_bless_an_existing_corrupt_object() { + let dir = temp_git_dir(); + let odb = Odb::new(dir.path()); + let oid = odb.write(ObjectType::Blob, b"important").unwrap(); + std::fs::write(odb.loose_path(&oid), b"corrupt").unwrap(); + assert!(odb.write(ObjectType::Blob, b"important").is_err()); + } + + #[test] + fn concurrent_writers_publish_one_verified_object() { + use std::sync::{Arc, Barrier}; + use std::thread; + + let dir = temp_git_dir(); + let root = Arc::new(dir.path().to_path_buf()); + let barrier = Arc::new(Barrier::new(5)); + let mut workers = Vec::new(); + for _ in 0..4 { + let root = root.clone(); + let barrier = barrier.clone(); + workers.push(thread::spawn(move || { + let odb = Odb::new(&root); + barrier.wait(); + odb.write(ObjectType::Blob, b"same concurrent object") + })); + } + barrier.wait(); + let ids: Vec<_> = workers + .into_iter() + .map(|worker| worker.join().unwrap().unwrap()) + .collect(); + assert!(ids.windows(2).all(|pair| pair[0] == pair[1])); + assert_eq!( + Odb::new(&root).read_raw(&ids[0]).unwrap().1, + b"same concurrent object" + ); + } + + #[test] + fn orphaned_crash_temp_does_not_block_next_publish() { + let dir = temp_git_dir(); + let odb = Odb::new(dir.path()); + let (oid, _) = object::hash_content(ObjectType::Blob, b"after crash"); + let shard = odb.loose_path(&oid).parent().unwrap().to_path_buf(); + std::fs::create_dir_all(&shard).unwrap(); + std::fs::write(shard.join(".rit-tmp-dead-writer"), b"partial zlib").unwrap(); + + assert_eq!(odb.write(ObjectType::Blob, b"after crash").unwrap(), oid); + assert_eq!(odb.read_raw(&oid).unwrap().1, b"after crash"); + } } diff --git a/src/pack.rs b/src/pack.rs index 7af0ace..7ee7930 100644 --- a/src/pack.rs +++ b/src/pack.rs @@ -121,7 +121,7 @@ impl PackStore { } /// Map a gitoxide object kind onto `rit`'s [`ObjectType`]. -fn map_kind(kind: gix_object::Kind) -> ObjectType { +pub(crate) fn map_kind(kind: gix_object::Kind) -> ObjectType { match kind { gix_object::Kind::Blob => ObjectType::Blob, gix_object::Kind::Tree => ObjectType::Tree, diff --git a/src/receive.rs b/src/receive.rs new file mode 100644 index 0000000..466c192 --- /dev/null +++ b/src/receive.rs @@ -0,0 +1,800 @@ +//! Quarantined receive transaction for decoded Git objects. +//! +//! Network and pack negotiation are deliberately outside this module. Once an +//! open-source pack decoder yields typed payloads, this is the safety boundary: +//! isolate them, verify declared identities, fsck the complete overlay graph, +//! enforce fast-forward policy, publish immutable objects, then move one ref by +//! compare-and-swap. A failed transaction never moves the ref. + +use std::collections::BTreeSet; +use std::io::Cursor; +use std::path::{Path, PathBuf}; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::time::{SystemTime, UNIX_EPOCH}; + +use sha1::{Digest, Sha1}; + +use crate::error::{ForgeError, Result}; +use crate::fsck::{check_connectivity, is_ancestor, ConnectivityLimits, ObjectReader}; +use crate::object::{Object, ObjectType, MAX_OBJECT_SIZE}; +use crate::odb::Odb; +use crate::oid::ObjectId; +use crate::refs::validate_refname; +use crate::Repository; + +static NEXT_QUARANTINE: AtomicU64 = AtomicU64::new(0); + +/// Limits applied to the decoded object batch before graph traversal. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ReceiveLimits { + pub max_objects: usize, + pub max_pack_bytes: u64, + pub max_total_bytes: u64, + pub connectivity: ConnectivityLimits, +} + +impl Default for ReceiveLimits { + fn default() -> Self { + Self { + max_objects: 250_000, + max_pack_bytes: 1024 * 1024 * 1024, + max_total_bytes: 2 * 1024 * 1024 * 1024, + connectivity: ConnectivityLimits::default(), + } + } +} + +/// One decoded object supplied by the pack/protocol layer. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ReceiveObject { + pub declared_oid: ObjectId, + pub object_type: ObjectType, + pub payload: Vec, +} + +/// One ref update. This first safe receive slice deliberately owns exactly one +/// ref per transaction; multi-ref atomic transactions remain a later wire-layer +/// capability rather than pretending sequential renames are globally atomic. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct RefUpdate { + pub name: String, + pub expected_old: Option, + pub new: ObjectId, + pub allow_non_fast_forward: bool, +} + +/// Successful publication summary. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ReceiveOutcome { + pub ref_name: String, + pub old: Option, + pub new: ObjectId, + pub published_objects: usize, +} + +/// Isolated decoded-object transaction tied to one target repository. +pub struct ReceiveTransaction<'repo> { + repository: &'repo Repository, + quarantine_root: PathBuf, + quarantine_odb: Odb, + limits: ReceiveLimits, + staged: BTreeSet, + total_bytes: u64, + pack_import_failed: Option, +} + +impl<'repo> ReceiveTransaction<'repo> { + /// Allocate a repository-owned quarantine directory. The caller never + /// supplies its name, preventing path traversal through correlation ids. + pub fn begin(repository: &'repo Repository, limits: ReceiveLimits) -> Result { + let parent = repository.git_dir().join("rit-quarantine"); + std::fs::create_dir_all(&parent)?; + reject_symlink(&parent)?; + let quarantine_root = allocate_quarantine(&parent)?; + std::fs::create_dir(quarantine_root.join("objects"))?; + let quarantine_odb = Odb::new(&quarantine_root); + Ok(Self { + repository, + quarantine_root, + quarantine_odb, + limits, + staged: BTreeSet::new(), + total_bytes: 0, + pack_import_failed: None, + }) + } + + /// Verify and isolate one decoded object. Duplicate identical objects are + /// idempotent and do not consume the batch count/byte budget twice. + pub fn stage(&mut self, object: ReceiveObject) -> Result<()> { + let payload_len = u64::try_from(object.payload.len()) + .map_err(|_| ForgeError::ResourceLimit("object length overflow".into()))?; + if payload_len > MAX_OBJECT_SIZE { + return Err(ForgeError::ResourceLimit(format!( + "received object exceeds {MAX_OBJECT_SIZE} bytes" + ))); + } + let computed = crate::hash_content(object.object_type, &object.payload).0; + if computed != object.declared_oid { + return Err(ForgeError::HashMismatch { + looked_up: object.declared_oid.to_hex(), + computed: computed.to_hex(), + }); + } + if self.staged.contains(&computed) { + return Ok(()); + } + if self.staged.len() == self.limits.max_objects { + return Err(ForgeError::ResourceLimit(format!( + "receive exceeds {} objects", + self.limits.max_objects + ))); + } + let next_total = self + .total_bytes + .checked_add(payload_len) + .ok_or_else(|| ForgeError::ResourceLimit("receive byte count overflow".into()))?; + if next_total > self.limits.max_total_bytes { + return Err(ForgeError::ResourceLimit(format!( + "receive exceeds {} decoded bytes", + self.limits.max_total_bytes + ))); + } + let written = self + .quarantine_odb + .write(object.object_type, &object.payload)?; + if written != object.declared_oid { + return Err(ForgeError::HashMismatch { + looked_up: object.declared_oid.to_hex(), + computed: written.to_hex(), + }); + } + self.staged.insert(written); + self.total_bytes = next_total; + Ok(()) + } + + /// Validate and decode one complete Git pack with gitoxide, then + /// stage every verified object into this transaction's loose quarantine. + /// + /// The import is deliberately two-phase. gitoxide first writes the raw pack + /// and generated index beneath a transaction-owned scratch directory while + /// checking its checksum and resolving in-pack deltas. Decoded objects are + /// re-hashed into a second scratch ODB. Only after the entire pack and all + /// decoded object identities pass their limits are they copied into the + /// publishable quarantine. A malformed pack therefore cannot make a + /// partially decoded graph eligible for [`finish`](Self::finish). + /// + /// Thin-pack REF_DELTA bases are resolved only through [`VerifiedBaseLookup`], + /// which reads from the transaction quarantine or target ODB and therefore + /// retains their normal object-id verification. gitoxide expands the thin + /// input into a self-contained scratch pack before any object is staged. + pub fn stage_pack(&mut self, pack: &[u8]) -> Result { + let staged_before = self.staged.len(); + let header = verify_pack_envelope(pack, self.limits)?; + let scratch_root = allocate_quarantine(&self.quarantine_root)?; + let _scratch = ScratchDirectory(scratch_root.clone()); + let pack_dir = scratch_root.join("pack"); + let decoded_root = scratch_root.join("decoded"); + std::fs::create_dir(&pack_dir)?; + std::fs::create_dir(&decoded_root)?; + std::fs::create_dir(decoded_root.join("objects"))?; + + let mut input = Cursor::new(pack); + let mut progress = gix_features::progress::Discard; + let interrupted = AtomicBool::new(false); + let outcome = gix_pack::Bundle::write_to_directory( + &mut input, + Some(&pack_dir), + &mut progress, + &interrupted, + Some(VerifiedBaseLookup { + quarantine: &self.quarantine_odb, + target: self.repository.odb(), + }), + gix_pack::bundle::write::Options { + thread_limit: Some(1), + iteration_mode: gix_pack::data::input::Mode::Verify, + index_version: gix_pack::index::Version::default(), + object_hash: gix_hash::Kind::Sha1, + }, + ) + .map_err(|error| ForgeError::Pack(format!("validating received pack: {error}")))?; + + let indexed_objects = usize::try_from(outcome.index.num_objects) + .map_err(|_| ForgeError::ResourceLimit("pack object count overflow".into()))?; + if indexed_objects < header.object_count { + return Err(ForgeError::Pack(format!( + "pack header declares {} objects but expanded index contains only {indexed_objects}", + header.object_count + ))); + } + if indexed_objects == 0 { + return Ok(0); + } + + let bundle = outcome + .to_bundle() + .ok_or_else(|| ForgeError::Pack("non-empty pack did not produce an index".into()))? + .map_err(|error| ForgeError::Pack(format!("opening verified pack: {error}")))?; + let decoded_odb = Odb::new(&decoded_root); + let mut decoded = Vec::with_capacity(indexed_objects); + let mut unique = BTreeSet::new(); + let mut new_decoded_bytes = 0u64; + let mut buffer = Vec::new(); + let mut inflate = gix_features::zlib::Inflate::default(); + let mut cache = gix_pack::cache::Never; + + for index in 0..outcome.index.num_objects { + buffer.clear(); + let expected = bundle.index.oid_at_index(index).to_owned(); + let (data, _) = bundle + .get_object_by_index(index, &mut buffer, &mut inflate, &mut cache) + .map_err(|error| ForgeError::Pack(format!("decoding received object: {error}")))?; + let object_type = crate::pack::map_kind(data.kind); + let payload_len = u64::try_from(data.data.len()) + .map_err(|_| ForgeError::ResourceLimit("decoded object length overflow".into()))?; + if payload_len > MAX_OBJECT_SIZE { + return Err(ForgeError::ResourceLimit(format!( + "received object exceeds {MAX_OBJECT_SIZE} bytes" + ))); + } + let expected = gix_oid_to_rit(&expected)?; + let computed = crate::hash_content(object_type, data.data).0; + if computed != expected { + return Err(ForgeError::HashMismatch { + looked_up: expected.to_hex(), + computed: computed.to_hex(), + }); + } + let written = decoded_odb.write(object_type, data.data)?; + if written != expected { + return Err(ForgeError::HashMismatch { + looked_up: expected.to_hex(), + computed: written.to_hex(), + }); + } + if unique.insert(expected) + && !self.staged.contains(&expected) + && !self.repository.odb().contains(&expected) + { + new_decoded_bytes = + new_decoded_bytes.checked_add(payload_len).ok_or_else(|| { + ForgeError::ResourceLimit("receive byte count overflow".into()) + })?; + } + decoded.push((expected, object_type)); + } + + let new_objects = unique + .iter() + .filter(|oid| !self.staged.contains(oid) && !self.repository.odb().contains(oid)) + .count(); + if self.staged.len().saturating_add(new_objects) > self.limits.max_objects { + return Err(ForgeError::ResourceLimit(format!( + "receive exceeds {} objects", + self.limits.max_objects + ))); + } + let combined_bytes = self + .total_bytes + .checked_add(new_decoded_bytes) + .ok_or_else(|| ForgeError::ResourceLimit("receive byte count overflow".into()))?; + if combined_bytes > self.limits.max_total_bytes { + return Err(ForgeError::ResourceLimit(format!( + "receive exceeds {} decoded bytes", + self.limits.max_total_bytes + ))); + } + + for (oid, object_type) in decoded { + if self.staged.contains(&oid) || self.repository.odb().contains(&oid) { + continue; + } + let (_, payload) = decoded_odb.read_raw(&oid)?; + if let Err(error) = self.stage(ReceiveObject { + declared_oid: oid, + object_type, + payload, + }) { + self.pack_import_failed = Some(error.to_string()); + return Err(error); + } + } + Ok(self.staged.len() - staged_before) + } + + /// Validate and publish the complete graph, then atomically compare-and-swap + /// its single destination ref. Object publication precedes the ref update; + /// if a concurrent writer wins, only unreachable immutable objects remain. + pub fn finish(self, update: RefUpdate) -> Result { + if let Some(reason) = &self.pack_import_failed { + return Err(ForgeError::ReceiveRejected(format!( + "an earlier pack import failed after staging began: {reason}" + ))); + } + validate_refname(&update.name)?; + let reader = OverlayReader { + quarantine: &self.quarantine_odb, + target: self.repository.odb(), + staged: &self.staged, + }; + let report = check_connectivity(&reader, &[update.new], self.limits.connectivity)?; + if !self.staged.iter().all(|oid| report.reachable.contains(oid)) { + return Err(ForgeError::ReceiveRejected( + "quarantine contains objects disconnected from the proposed ref tip".into(), + )); + } + + if update.name.starts_with("refs/heads/") { + let Object::Commit(_) = reader.read_object(&update.new)? else { + return Err(ForgeError::Connectivity(format!( + "branch tip {} is not a commit", + update.new + ))); + }; + if let Some(old) = update.expected_old { + if !update.allow_non_fast_forward + && !is_ancestor(&reader, old, update.new, self.limits.connectivity)? + { + return Err(ForgeError::NonFastForward { + name: update.name, + old: old.to_hex(), + new: update.new.to_hex(), + }); + } + } + } + + for oid in &self.staged { + let (object_type, payload) = self.quarantine_odb.read_raw(oid)?; + let published = self.repository.odb().write(object_type, &payload)?; + if published != *oid { + return Err(ForgeError::HashMismatch { + looked_up: oid.to_hex(), + computed: published.to_hex(), + }); + } + } + self.repository + .refs() + .compare_and_swap(&update.name, update.expected_old, update.new)?; + Ok(ReceiveOutcome { + ref_name: update.name, + old: update.expected_old, + new: update.new, + published_objects: self.staged.len(), + }) + } + + pub fn staged_objects(&self) -> usize { + self.staged.len() + } + + pub fn decoded_bytes(&self) -> u64 { + self.total_bytes + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct PackHeader { + object_count: usize, +} + +fn verify_pack_envelope(pack: &[u8], limits: ReceiveLimits) -> Result { + const HEADER_LEN: usize = 12; + const TRAILER_LEN: usize = 20; + if pack.len() < HEADER_LEN + TRAILER_LEN { + return Err(ForgeError::Pack("received pack is truncated".into())); + } + let pack_len = u64::try_from(pack.len()) + .map_err(|_| ForgeError::ResourceLimit("pack length overflow".into()))?; + if pack_len > limits.max_pack_bytes { + return Err(ForgeError::ResourceLimit(format!( + "receive pack exceeds {} bytes", + limits.max_pack_bytes + ))); + } + if &pack[..4] != b"PACK" { + return Err(ForgeError::Pack("received stream is not a Git pack".into())); + } + let version = u32::from_be_bytes(pack[4..8].try_into().expect("fixed-width pack version")); + if !matches!(version, 2 | 3) { + return Err(ForgeError::Pack(format!( + "unsupported pack version {version}" + ))); + } + let object_count = usize::try_from(u32::from_be_bytes( + pack[8..12].try_into().expect("fixed-width pack count"), + )) + .map_err(|_| ForgeError::ResourceLimit("pack object count overflow".into()))?; + if object_count > limits.max_objects { + return Err(ForgeError::ResourceLimit(format!( + "pack declares {object_count} objects, limit is {}", + limits.max_objects + ))); + } + + let trailer_at = pack.len() - TRAILER_LEN; + let computed = Sha1::digest(&pack[..trailer_at]); + if computed.as_slice() != &pack[trailer_at..] { + return Err(ForgeError::Pack("received pack checksum mismatch".into())); + } + Ok(PackHeader { object_count }) +} + +fn gix_oid_to_rit(oid: &gix_hash::oid) -> Result { + let raw: [u8; 20] = oid + .as_bytes() + .try_into() + .map_err(|_| ForgeError::Pack("unexpected non-SHA-1 object id in pack index".into()))?; + Ok(ObjectId::from_bytes(raw)) +} + +struct ScratchDirectory(PathBuf); + +impl Drop for ScratchDirectory { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +/// gitoxide callback used only while expanding thin-pack REF_DELTA bases. Both +/// backing ODBs verify loose/packed object identities before returning bytes, +/// so an attacker cannot substitute a base merely by controlling a filename. +struct VerifiedBaseLookup<'a> { + quarantine: &'a Odb, + target: &'a Odb, +} + +impl gix_object::Find for VerifiedBaseLookup<'_> { + fn try_find<'a>( + &self, + id: &gix_hash::oid, + buffer: &'a mut Vec, + ) -> std::result::Result>, gix_object::find::Error> { + let oid = gix_oid_to_rit(id).map_err(boxed_find_error)?; + let object = match self.quarantine.read_raw(&oid) { + Ok(object) => Some(object), + Err(ForgeError::ObjectNotFound(_)) => match self.target.read_raw(&oid) { + Ok(object) => Some(object), + Err(ForgeError::ObjectNotFound(_)) => None, + Err(error) => return Err(boxed_find_error(error)), + }, + Err(error) => return Err(boxed_find_error(error)), + }; + let Some((object_type, payload)) = object else { + return Ok(None); + }; + buffer.clear(); + buffer.extend_from_slice(&payload); + Ok(Some(gix_object::Data { + kind: rit_kind_to_gix(object_type), + data: buffer.as_slice(), + })) + } +} + +fn rit_kind_to_gix(object_type: ObjectType) -> gix_object::Kind { + match object_type { + ObjectType::Blob => gix_object::Kind::Blob, + ObjectType::Tree => gix_object::Kind::Tree, + ObjectType::Commit => gix_object::Kind::Commit, + ObjectType::Tag => gix_object::Kind::Tag, + } +} + +fn boxed_find_error(error: ForgeError) -> gix_object::find::Error { + Box::new(error) +} + +impl Drop for ReceiveTransaction<'_> { + fn drop(&mut self) { + // This path is allocated internally beneath `.git/rit-quarantine` and + // never accepts user components. Best-effort cleanup cannot affect the + // target object database or refs. + let _ = std::fs::remove_dir_all(&self.quarantine_root); + } +} + +struct OverlayReader<'a> { + quarantine: &'a Odb, + target: &'a Odb, + staged: &'a BTreeSet, +} + +impl ObjectReader for OverlayReader<'_> { + fn read_object(&self, oid: &ObjectId) -> Result { + if self.staged.contains(oid) { + self.quarantine.read(oid) + } else { + self.target.read(oid) + } + } +} + +fn allocate_quarantine(parent: &Path) -> Result { + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|duration| duration.as_nanos()) + .unwrap_or_default(); + for _ in 0..128 { + let sequence = NEXT_QUARANTINE.fetch_add(1, Ordering::Relaxed); + let path = parent.join(format!( + "incoming-{}-{nanos}-{sequence}", + std::process::id() + )); + match std::fs::create_dir(&path) { + Ok(()) => return Ok(path), + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue, + Err(error) => return Err(error.into()), + } + } + Err(ForgeError::ReceiveRejected( + "could not allocate quarantine directory".into(), + )) +} + +fn reject_symlink(path: &Path) -> Result<()> { + if std::fs::symlink_metadata(path)?.file_type().is_symlink() { + return Err(ForgeError::UnsafePath(path.display().to_string())); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::commit::Commit; + use crate::signature::Signature; + use crate::tree::Tree; + + fn bare_repository() -> (tempfile::TempDir, Repository) { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(dir.path().join("objects")).unwrap(); + std::fs::create_dir_all(dir.path().join("refs/heads")).unwrap(); + let repo = Repository::open_git_dir(dir.path()).unwrap(); + (dir, repo) + } + + fn commit_objects(message: &[u8], parent: Option) -> Vec { + let tree = Tree::default().serialize(); + let tree_oid = crate::hash_content(ObjectType::Tree, &tree).0; + let signature = + Signature::parse("Rit 1700000000 +0000", "commit").unwrap(); + let commit = Commit { + tree: tree_oid, + parents: parent.into_iter().collect(), + author: signature.clone(), + committer: signature, + extra_headers: Vec::new(), + message: message.to_vec(), + } + .serialize(); + let commit_oid = crate::hash_content(ObjectType::Commit, &commit).0; + vec![ + ReceiveObject { + declared_oid: tree_oid, + object_type: ObjectType::Tree, + payload: tree, + }, + ReceiveObject { + declared_oid: commit_oid, + object_type: ObjectType::Commit, + payload: commit, + }, + ] + } + + fn empty_pack() -> Vec { + let mut pack = Vec::from(&b"PACK\0\0\0\x02\0\0\0\0"[..]); + let checksum = Sha1::digest(&pack); + pack.extend_from_slice(&checksum); + pack + } + + #[test] + fn empty_raw_pack_is_valid_and_stages_nothing() { + let (_dir, repo) = bare_repository(); + let mut receive = ReceiveTransaction::begin(&repo, ReceiveLimits::default()).unwrap(); + assert_eq!(receive.stage_pack(&empty_pack()).unwrap(), 0); + assert_eq!(receive.staged_objects(), 0); + assert_eq!(receive.decoded_bytes(), 0); + } + + #[test] + fn pack_envelope_rejects_bad_magic_version_count_checksum_and_size() { + let valid = empty_pack(); + assert_eq!( + verify_pack_envelope(&valid, ReceiveLimits::default()) + .unwrap() + .object_count, + 0 + ); + + let mut bad_magic = valid.clone(); + bad_magic[0] = b'B'; + assert!(matches!( + verify_pack_envelope(&bad_magic, ReceiveLimits::default()), + Err(ForgeError::Pack(_)) + )); + + let mut bad_version = valid.clone(); + bad_version[7] = 4; + assert!(matches!( + verify_pack_envelope(&bad_version, ReceiveLimits::default()), + Err(ForgeError::Pack(_)) + )); + + let mut excessive_count = valid.clone(); + excessive_count[8..12].copy_from_slice(&2u32.to_be_bytes()); + let limits = ReceiveLimits { + max_objects: 1, + ..ReceiveLimits::default() + }; + assert!(matches!( + verify_pack_envelope(&excessive_count, limits), + Err(ForgeError::ResourceLimit(_)) + )); + + let mut bad_checksum = valid.clone(); + *bad_checksum.last_mut().unwrap() ^= 1; + assert!(matches!( + verify_pack_envelope(&bad_checksum, ReceiveLimits::default()), + Err(ForgeError::Pack(_)) + )); + + let limits = ReceiveLimits { + max_pack_bytes: (valid.len() - 1) as u64, + ..ReceiveLimits::default() + }; + assert!(matches!( + verify_pack_envelope(&valid, limits), + Err(ForgeError::ResourceLimit(_)) + )); + } + + #[test] + fn publishes_connected_objects_then_moves_one_ref() { + let (_dir, repo) = bare_repository(); + let objects = commit_objects(b"root\n", None); + let tip = objects.last().unwrap().declared_oid; + let mut receive = ReceiveTransaction::begin(&repo, ReceiveLimits::default()).unwrap(); + for object in objects { + receive.stage(object).unwrap(); + } + let outcome = receive + .finish(RefUpdate { + name: "refs/heads/main".into(), + expected_old: None, + new: tip, + allow_non_fast_forward: false, + }) + .unwrap(); + assert_eq!(outcome.published_objects, 2); + assert_eq!(repo.refs().resolve("refs/heads/main").unwrap(), tip); + assert!(repo.odb().contains(&tip)); + } + + #[test] + fn hash_mismatch_and_missing_connectivity_never_move_ref() { + let (_dir, repo) = bare_repository(); + let objects = commit_objects(b"root\n", None); + let tip = objects.last().unwrap().declared_oid; + let mut receive = ReceiveTransaction::begin(&repo, ReceiveLimits::default()).unwrap(); + let mut corrupt = objects.last().unwrap().clone(); + corrupt.payload.push(b'!'); + assert!(matches!( + receive.stage(corrupt), + Err(ForgeError::HashMismatch { .. }) + )); + assert!(repo.refs().resolve("refs/heads/main").is_err()); + + receive.stage(objects.last().unwrap().clone()).unwrap(); + assert!(receive + .finish(RefUpdate { + name: "refs/heads/main".into(), + expected_old: None, + new: tip, + allow_non_fast_forward: false, + }) + .is_err()); + assert!(repo.refs().resolve("refs/heads/main").is_err()); + assert!(!repo.odb().contains(&tip)); + } + + #[test] + fn rejects_disconnected_objects_and_non_fast_forward() { + let (_dir, repo) = bare_repository(); + let root_objects = commit_objects(b"root\n", None); + let root = root_objects.last().unwrap().declared_oid; + let mut first = ReceiveTransaction::begin(&repo, ReceiveLimits::default()).unwrap(); + for object in root_objects { + first.stage(object).unwrap(); + } + first + .finish(RefUpdate { + name: "refs/heads/main".into(), + expected_old: None, + new: root, + allow_non_fast_forward: false, + }) + .unwrap(); + + let replacement = commit_objects(b"replacement\n", None); + let replacement_tip = replacement.last().unwrap().declared_oid; + let mut receive = ReceiveTransaction::begin(&repo, ReceiveLimits::default()).unwrap(); + for object in replacement { + receive.stage(object).unwrap(); + } + let extra_payload = b"disconnected".to_vec(); + let extra_oid = crate::hash_content(ObjectType::Blob, &extra_payload).0; + receive + .stage(ReceiveObject { + declared_oid: extra_oid, + object_type: ObjectType::Blob, + payload: extra_payload, + }) + .unwrap(); + assert!(receive + .finish(RefUpdate { + name: "refs/heads/main".into(), + expected_old: Some(root), + new: replacement_tip, + allow_non_fast_forward: false, + }) + .is_err()); + assert_eq!(repo.refs().resolve("refs/heads/main").unwrap(), root); + + let mut receive = ReceiveTransaction::begin(&repo, ReceiveLimits::default()).unwrap(); + for object in commit_objects(b"replacement\n", None) { + receive.stage(object).unwrap(); + } + assert!(matches!( + receive.finish(RefUpdate { + name: "refs/heads/main".into(), + expected_old: Some(root), + new: replacement_tip, + allow_non_fast_forward: false, + }), + Err(ForgeError::NonFastForward { .. }) + )); + assert_eq!(repo.refs().resolve("refs/heads/main").unwrap(), root); + } + + #[test] + fn accepts_a_fast_forward_whose_parent_is_already_published() { + let (_dir, repo) = bare_repository(); + let root_objects = commit_objects(b"root\n", None); + let root = root_objects.last().unwrap().declared_oid; + let mut first = ReceiveTransaction::begin(&repo, ReceiveLimits::default()).unwrap(); + for object in root_objects { + first.stage(object).unwrap(); + } + first + .finish(RefUpdate { + name: "refs/heads/main".into(), + expected_old: None, + new: root, + allow_non_fast_forward: false, + }) + .unwrap(); + + let next_objects = commit_objects(b"next\n", Some(root)); + let next = next_objects.last().unwrap().declared_oid; + let mut second = ReceiveTransaction::begin(&repo, ReceiveLimits::default()).unwrap(); + // The empty tree is already in the target ODB. Staging only the new + // commit proves connectivity can cross the quarantine overlay boundary. + second.stage(next_objects.last().unwrap().clone()).unwrap(); + second + .finish(RefUpdate { + name: "refs/heads/main".into(), + expected_old: Some(root), + new: next, + allow_non_fast_forward: false, + }) + .unwrap(); + assert_eq!(repo.refs().resolve("refs/heads/main").unwrap(), next); + } +} diff --git a/src/receive_pack.rs b/src/receive_pack.rs new file mode 100644 index 0000000..2dcbcd6 --- /dev/null +++ b/src/receive_pack.rs @@ -0,0 +1,394 @@ +//! Executable receive-pack application boundary. +//! +//! This module composes the strict wire parser with raw/thin pack quarantine and +//! one-ref publication. It is transport-agnostic: an SSH or HTTP adapter can +//! pass the request body here without gaining direct access to the target ODB or +//! refs. Multi-ref commands and packed-ref deletion fail closed until their +//! storage transactions are implemented. + +use crate::error::{ForgeError, Result}; +use crate::receive::{ReceiveLimits, ReceiveOutcome, ReceiveTransaction, RefUpdate}; +use crate::wire::{ + encode_packet_line, encode_receive_status, parse_receive_pack_request, PacketLine, + PacketLineLimits, ReceivePackCommand, ReceivePackRequest, ReceiveStatus, +}; +use crate::Repository; + +/// Capabilities implemented by the current one-ref SHA-1 receive server. +/// Unsupported features (atomic multi-ref, push-options, sideband) +/// are intentionally absent so stock Git will not negotiate them. +pub const RECEIVE_PACK_CAPABILITIES: &[&str] = &[ + "report-status", + "report-status-v2", + "delete-refs", + "ofs-delta", + "object-format=sha1", + "agent=rit/0.1", +]; + +/// Server-side policy which is never inferred from client capabilities. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct ReceivePackPolicy { + /// Allow a branch update whose expected old commit is not an ancestor of + /// the proposed new commit. This is a repository-owner decision, not a + /// capability the pushing client can grant itself. + pub allow_non_fast_forward: bool, +} + +/// Result suitable for an SSH/HTTP transport adapter. +#[derive(Debug)] +pub struct ReceivePackExecution { + /// Protocol report-status bytes, or an empty body when the client did not + /// negotiate report-status/report-status-v2. + pub response: Vec, + /// Present only after objects and the destination ref were published. + pub outcome: Option, + /// Present only after a loose ref was deleted by expected-old CAS. + pub deleted: Option, + /// Typed local diagnostic retained for logs/metrics. The response uses a + /// stable, privacy-safe reason instead of exposing filesystem details. + pub rejection: Option, +} + +/// Successful single-ref deletion summary. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct DeletedRef { + pub name: String, + pub old: crate::ObjectId, +} + +/// Encode protocol-v0 receive-pack ref advertisement for a stateful SSH/local +/// process transport. The first advertised ref carries capabilities. An empty +/// repository uses Git's conventional zero-id `capabilities^{}` pseudo-ref. +pub fn advertise_receive_pack(repository: &Repository, capabilities: &[&str]) -> Result> { + for capability in capabilities { + if capability.is_empty() || capability.bytes().any(|byte| byte <= b' ' || byte == 0x7f) { + return Err(ForgeError::ReceiveRejected( + "receive advertisement contains an invalid capability".into(), + )); + } + } + + let mut refs = Vec::new(); + if let Ok(head) = repository.refs().resolve_head() { + refs.push(("HEAD".to_string(), head)); + } + refs.extend(repository.refs().list()?); + refs.sort_by(|left, right| left.0.cmp(&right.0)); + refs.dedup_by(|left, right| left.0 == right.0); + + let capability_list = capabilities.join(" "); + let mut out = Vec::new(); + if refs.is_empty() { + let line = format!( + "0000000000000000000000000000000000000000 capabilities^{{}}\0{capability_list}\n" + ); + out.extend(encode_packet_line(&PacketLine::Data(line.into_bytes()))?); + } else { + for (index, (name, oid)) in refs.into_iter().enumerate() { + let line = if index == 0 { + format!("{oid} {name}\0{capability_list}\n") + } else { + format!("{oid} {name}\n") + }; + out.extend(encode_packet_line(&PacketLine::Data(line.into_bytes()))?); + } + } + out.extend_from_slice(b"0000"); + Ok(out) +} + +/// Execute one complete receive-pack request body. +/// +/// Syntactically invalid command framing returns `Err` because no trustworthy +/// ref/status vocabulary can be recovered. Valid requests rejected during +/// unpack or ref policy return `Ok(ReceivePackExecution)` with a standard +/// report-status response and a typed local `rejection`. +pub fn execute_receive_pack( + repository: &Repository, + input: &[u8], + advertised_capabilities: &[&str], + packet_limits: PacketLineLimits, + receive_limits: ReceiveLimits, + policy: ReceivePackPolicy, +) -> Result { + let request = parse_receive_pack_request(input, advertised_capabilities, packet_limits)?; + let report_status = request.capabilities.contains("report-status") + || request.capabilities.contains("report-status-v2"); + + if request.commands.len() != 1 { + let rejection = + ForgeError::ReceiveRejected("atomic multi-ref receive is not implemented".into()); + return rejected_refs( + &request, + report_status, + None, + "atomic multi-ref receive is not supported", + rejection, + ); + } + + let update = match &request.commands[0] { + ReceivePackCommand::Create { new, name } => RefUpdate { + name: name.clone(), + expected_old: None, + new: *new, + allow_non_fast_forward: policy.allow_non_fast_forward, + }, + ReceivePackCommand::Update { old, new, name } => RefUpdate { + name: name.clone(), + expected_old: Some(*old), + new: *new, + allow_non_fast_forward: policy.allow_non_fast_forward, + }, + ReceivePackCommand::Delete { old, name } => { + if request.pack_offset != input.len() { + return rejected_refs( + &request, + report_status, + None, + "unexpected pack for deletion", + ForgeError::ReceiveRejected( + "single-ref deletion request carried unexpected pack bytes".into(), + ), + ); + } + return match repository.refs().compare_and_delete(name, *old) { + Ok(()) => Ok(ReceivePackExecution { + response: status_response(&request, report_status, None, None)?, + outcome: None, + deleted: Some(DeletedRef { + name: name.clone(), + old: *old, + }), + rejection: None, + }), + Err(error) => { + let public_reason = public_ref_rejection(&error); + rejected_refs(&request, report_status, None, public_reason, error) + } + }; + } + }; + + let mut transaction = ReceiveTransaction::begin(repository, receive_limits)?; + if let Err(error) = transaction.stage_pack(&input[request.pack_offset..]) { + return rejected_refs( + &request, + report_status, + Some("pack rejected"), + "unpacker error", + error, + ); + } + + match transaction.finish(update) { + Ok(outcome) => Ok(ReceivePackExecution { + response: status_response(&request, report_status, None, None)?, + outcome: Some(outcome), + deleted: None, + rejection: None, + }), + Err(error) => { + let public_reason = public_ref_rejection(&error); + rejected_refs(&request, report_status, None, public_reason, error) + } + } +} + +fn rejected_refs( + request: &ReceivePackRequest, + report_status: bool, + unpack_error: Option<&str>, + reason: &str, + rejection: ForgeError, +) -> Result { + Ok(ReceivePackExecution { + response: status_response(request, report_status, unpack_error, Some(reason))?, + outcome: None, + deleted: None, + rejection: Some(rejection), + }) +} + +fn status_response( + request: &ReceivePackRequest, + report_status: bool, + unpack_error: Option<&str>, + ref_error: Option<&str>, +) -> Result> { + if !report_status { + return Ok(Vec::new()); + } + let statuses = request + .commands + .iter() + .map(|command| ReceiveStatus { + ref_name: command.name().to_string(), + error: ref_error.map(str::to_string), + }) + .collect::>(); + encode_receive_status(unpack_error, &statuses) +} + +fn public_ref_rejection(error: &ForgeError) -> &'static str { + match error { + ForgeError::NonFastForward { .. } => "non-fast-forward", + ForgeError::RefConflict { .. } => "stale info", + ForgeError::RefLocked(_) => "ref locked", + ForgeError::Connectivity(_) | ForgeError::HashMismatch { .. } => "invalid object graph", + ForgeError::ReceiveRejected(_) => "receive policy rejected", + ForgeError::ResourceLimit(_) => "resource limit", + _ => "server error", + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::wire::{decode_packet_lines, encode_packet_line, PacketLine}; + + fn command_line(old: &str, new: &str, name: &str, capabilities: &str) -> Vec { + encode_packet_line(&PacketLine::Data( + format!("{old} {new} {name}\0{capabilities}\n").into_bytes(), + )) + .unwrap() + } + + #[test] + fn rejects_multi_ref_before_unpacking_and_reports_every_ref() { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(dir.path().join("objects")).unwrap(); + let repo = Repository::open_git_dir(dir.path()).unwrap(); + let zero = "0000000000000000000000000000000000000000"; + let new = "1111111111111111111111111111111111111111"; + let mut input = command_line( + zero, + new, + "refs/heads/main", + "report-status-v2 object-format=sha1", + ); + input.extend( + encode_packet_line(&PacketLine::Data( + format!("{zero} {new} refs/heads/topic\n").into_bytes(), + )) + .unwrap(), + ); + input.extend_from_slice(b"0000not-a-pack"); + + let execution = execute_receive_pack( + &repo, + &input, + &["report-status-v2", "object-format=sha1"], + PacketLineLimits::default(), + ReceiveLimits::default(), + ReceivePackPolicy::default(), + ) + .unwrap(); + assert!(execution.outcome.is_none()); + assert!(matches!( + execution.rejection, + Some(ForgeError::ReceiveRejected(_)) + )); + let status = decode_packet_lines(&execution.response, PacketLineLimits::default()).unwrap(); + assert_eq!(status.len(), 4); + assert!( + matches!(&status[1], PacketLine::Data(line) if line.starts_with(b"ng refs/heads/main")) + ); + assert!( + matches!(&status[2], PacketLine::Data(line) if line.starts_with(b"ng refs/heads/topic")) + ); + } + + #[test] + fn omits_status_body_when_client_did_not_negotiate_it() { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(dir.path().join("objects")).unwrap(); + let repo = Repository::open_git_dir(dir.path()).unwrap(); + let zero = "0000000000000000000000000000000000000000"; + let new = "1111111111111111111111111111111111111111"; + let mut input = command_line(zero, new, "refs/heads/main", "object-format=sha1"); + input.extend_from_slice(b"0000not-a-pack"); + let execution = execute_receive_pack( + &repo, + &input, + &["object-format=sha1"], + PacketLineLimits::default(), + ReceiveLimits::default(), + ReceivePackPolicy::default(), + ) + .unwrap(); + assert!(execution.response.is_empty()); + assert!(execution.rejection.is_some()); + } + + #[test] + fn advertises_empty_and_populated_repositories_without_unsupported_caps() { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(dir.path().join("objects")).unwrap(); + std::fs::create_dir_all(dir.path().join("refs/heads")).unwrap(); + let repo = Repository::open_git_dir(dir.path()).unwrap(); + let empty = advertise_receive_pack(&repo, RECEIVE_PACK_CAPABILITIES).unwrap(); + let packets = decode_packet_lines(&empty, PacketLineLimits::default()).unwrap(); + assert!( + matches!(&packets[0], PacketLine::Data(line) if line.starts_with(b"0000000000000000000000000000000000000000 capabilities^{}\0")) + ); + assert!(!empty + .windows(b"atomic".len()) + .any(|window| window == b"atomic")); + assert!(empty + .windows(b"delete-refs".len()) + .any(|window| window == b"delete-refs")); + + let oid = crate::hash_content(crate::ObjectType::Blob, b"advertised").0; + repo.refs() + .compare_and_swap("refs/heads/main", None, oid) + .unwrap(); + let populated = advertise_receive_pack(&repo, RECEIVE_PACK_CAPABILITIES).unwrap(); + let packets = decode_packet_lines(&populated, PacketLineLimits::default()).unwrap(); + let expected = format!("{oid} refs/heads/main"); + assert!(packets.iter().any( + |packet| matches!(packet, PacketLine::Data(line) if line.starts_with(expected.as_bytes())) + )); + } + + #[test] + fn executes_single_loose_ref_deletion_without_a_pack() { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(dir.path().join("objects")).unwrap(); + std::fs::create_dir_all(dir.path().join("refs/heads")).unwrap(); + let repo = Repository::open_git_dir(dir.path()).unwrap(); + let old = "1111111111111111111111111111111111111111"; + std::fs::write(dir.path().join("refs/heads/main"), format!("{old}\n")).unwrap(); + let zero = "0000000000000000000000000000000000000000"; + let mut input = command_line( + old, + zero, + "refs/heads/main", + "report-status-v2 object-format=sha1", + ); + input.extend_from_slice(b"0000"); + + let execution = execute_receive_pack( + &repo, + &input, + RECEIVE_PACK_CAPABILITIES, + PacketLineLimits::default(), + ReceiveLimits::default(), + ReceivePackPolicy::default(), + ) + .unwrap(); + assert!(execution.outcome.is_none()); + assert_eq!( + execution.deleted, + Some(DeletedRef { + name: "refs/heads/main".into(), + old: crate::ObjectId::from_hex(old).unwrap(), + }) + ); + assert!(execution.rejection.is_none()); + assert!(repo.refs().resolve("refs/heads/main").is_err()); + let status = decode_packet_lines(&execution.response, PacketLineLimits::default()).unwrap(); + assert!(matches!(&status[1], PacketLine::Data(line) if line == b"ok refs/heads/main\n")); + } +} diff --git a/src/refs.rs b/src/refs.rs index c846304..d0ba47c 100644 --- a/src/refs.rs +++ b/src/refs.rs @@ -7,6 +7,8 @@ use crate::error::{ForgeError, Result}; use crate::oid::ObjectId; use std::collections::HashMap; +use std::fs::OpenOptions; +use std::io::Write; use std::path::{Path, PathBuf}; /// A resolved-one-step reference value. @@ -51,6 +53,9 @@ impl Refs { /// Resolve a ref name (full or shorthand) to an object id. pub fn resolve(&self, name: &str) -> Result { + if name != "HEAD" && name.starts_with("refs/") { + validate_refname(name)?; + } self.resolve_depth(name, 0) } @@ -94,13 +99,145 @@ impl Refs { /// Read one loose ref file, if present, one step (no recursion). fn read_loose_ref(&self, refname: &str) -> Result> { + validate_refname(refname)?; 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), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None), + Err(error) => Err(error.into()), } } + /// Atomically update a direct ref if it still has `expected` as its current + /// value. `None` means the ref must not exist. A `.lock` file serializes + /// writers; publishing is one fsync + rename + parent-directory fsync. + /// + /// Packed refs are supported: a successful update writes a loose ref that + /// shadows the packed value, matching Git's storage behavior. + pub fn compare_and_swap( + &self, + name: &str, + expected: Option, + new: ObjectId, + ) -> Result<()> { + validate_refname(name)?; + let path = self.git_dir.join(name); + let parent = path + .parent() + .ok_or_else(|| ForgeError::UnsafePath(name.to_string()))?; + std::fs::create_dir_all(parent)?; + reject_symlink_components(&self.git_dir, parent)?; + + let lock_path = PathBuf::from(format!("{}.lock", path.display())); + let mut lock = match OpenOptions::new() + .write(true) + .create_new(true) + .open(&lock_path) + { + Ok(file) => file, + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => { + return Err(ForgeError::RefLocked(name.to_string())) + } + Err(error) => return Err(error.into()), + }; + + let result = (|| { + let actual = match self.resolve(name) { + Ok(oid) => Some(oid), + Err(ForgeError::InvalidRef(_)) => None, + Err(error) => return Err(error), + }; + if actual != expected { + return Err(ForgeError::RefConflict { + name: name.to_string(), + expected: expected + .map(|oid| oid.to_hex()) + .unwrap_or_else(|| "".into()), + actual: actual + .map(|oid| oid.to_hex()) + .unwrap_or_else(|| "".into()), + }); + } + + writeln!(lock, "{new}")?; + lock.sync_all()?; + std::fs::rename(&lock_path, &path)?; + std::fs::File::open(parent)?.sync_all()?; + Ok(()) + })(); + if result.is_err() { + let _ = std::fs::remove_file(&lock_path); + } + result + } + + /// Atomically remove one loose ref if it still resolves to `expected`. + /// + /// The lock and compare step use the same protocol as + /// [`compare_and_swap`](Self::compare_and_swap). A ref backed by + /// `packed-refs` fails closed: removing a loose shadow could reveal an older + /// packed value, and rewriting `packed-refs` belongs to a separate + /// multi-storage transaction rather than this one-file primitive. + pub fn compare_and_delete(&self, name: &str, expected: ObjectId) -> Result<()> { + validate_refname(name)?; + let path = self.git_dir.join(name); + let parent = path + .parent() + .ok_or_else(|| ForgeError::UnsafePath(name.to_string()))?; + std::fs::create_dir_all(parent)?; + reject_symlink_components(&self.git_dir, parent)?; + if std::fs::symlink_metadata(&path) + .map(|metadata| metadata.file_type().is_symlink()) + .unwrap_or(false) + { + return Err(ForgeError::UnsafePath(path.display().to_string())); + } + + let lock_path = PathBuf::from(format!("{}.lock", path.display())); + let lock = match OpenOptions::new() + .write(true) + .create_new(true) + .open(&lock_path) + { + Ok(file) => file, + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => { + return Err(ForgeError::RefLocked(name.to_string())) + } + Err(error) => return Err(error.into()), + }; + + let result = (|| { + let actual = match self.resolve(name) { + Ok(oid) => Some(oid), + Err(ForgeError::InvalidRef(_)) => None, + Err(error) => return Err(error), + }; + if actual != Some(expected) { + return Err(ForgeError::RefConflict { + name: name.to_string(), + expected: expected.to_hex(), + actual: actual + .map(|oid| oid.to_hex()) + .unwrap_or_else(|| "".into()), + }); + } + if self.read_packed_refs()?.contains_key(name) { + return Err(ForgeError::ReceiveRejected(format!( + "cannot safely delete packed ref {name} with the loose-ref transaction" + ))); + } + std::fs::remove_file(&path)?; + std::fs::File::open(parent)?.sync_all()?; + Ok(()) + })(); + drop(lock); + let cleanup = std::fs::remove_file(&lock_path); + if result.is_ok() { + cleanup?; + } + result + } + /// Parse `.git/packed-refs` into a name -> oid map (peel lines ignored). fn read_packed_refs(&self) -> Result> { let path = self.git_dir.join("packed-refs"); @@ -133,8 +270,6 @@ impl 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}"), @@ -152,6 +287,46 @@ fn parse_ref_content(content: &str) -> Result { } } +/// Conservative subset of `git check-ref-format`. Most importantly this keeps +/// repository-controlled names inside `.git/refs` and reserves `.lock` for the +/// transaction protocol. +pub fn validate_refname(name: &str) -> Result<()> { + let invalid = !name.starts_with("refs/") + || name.ends_with('/') + || name.ends_with('.') + || name.ends_with(".lock") + || name.contains("..") + || name.contains("//") + || name.contains("@{") + || name + .bytes() + .any(|b| b <= b' ' || b == 0x7f || b"~^:?*[\\".contains(&b)) + || name + .split('/') + .any(|part| part.is_empty() || part == "." || part == ".."); + if invalid { + return Err(ForgeError::InvalidRef(name.to_string())); + } + Ok(()) +} + +fn reject_symlink_components(root: &Path, target: &Path) -> Result<()> { + let relative = target + .strip_prefix(root) + .map_err(|_| ForgeError::UnsafePath(target.display().to_string()))?; + let mut current = root.to_path_buf(); + for component in relative.components() { + current.push(component); + if std::fs::symlink_metadata(¤t) + .map(|metadata| metadata.file_type().is_symlink()) + .unwrap_or(false) + { + return Err(ForgeError::UnsafePath(current.display().to_string())); + } + } + Ok(()) +} + /// Recursively collect loose refs under `dir`, keyed by full ref name. fn collect_loose(root: &Path, dir: &Path, out: &mut HashMap) -> Result<()> { let read_dir = match std::fs::read_dir(dir) { @@ -160,6 +335,13 @@ fn collect_loose(root: &Path, dir: &Path, out: &mut HashMap) - }; for entry in read_dir.flatten() { let path = entry.path(); + if entry + .file_type() + .map(|kind| kind.is_symlink()) + .unwrap_or(true) + { + continue; + } if path.is_dir() { collect_loose(root, &path, out)?; } else if let Ok(raw) = std::fs::read_to_string(&path) { @@ -178,6 +360,8 @@ fn collect_loose(root: &Path, dir: &Path, out: &mut HashMap) - #[cfg(test)] mod tests { use super::*; + use std::sync::{Arc, Barrier}; + use std::thread; #[test] fn parses_symbolic_head() { @@ -191,4 +375,83 @@ mod tests { let t = parse_ref_content(hex).unwrap(); assert_eq!(t, RefTarget::Direct(ObjectId::from_hex(hex).unwrap())); } + + #[test] + fn rejects_ref_path_traversal() { + for name in [ + "refs/heads/../../outside", + "../outside", + "refs/heads/x.lock", + ] { + assert!(validate_refname(name).is_err(), "accepted {name}"); + } + } + + #[test] + fn compare_and_swap_allows_only_one_concurrent_winner() { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(dir.path().join("refs/heads")).unwrap(); + let old = ObjectId::from_hex("1111111111111111111111111111111111111111").unwrap(); + let left = ObjectId::from_hex("2222222222222222222222222222222222222222").unwrap(); + let right = ObjectId::from_hex("3333333333333333333333333333333333333333").unwrap(); + std::fs::write(dir.path().join("refs/heads/main"), format!("{old}\n")).unwrap(); + + let root = Arc::new(dir.path().to_path_buf()); + let barrier = Arc::new(Barrier::new(3)); + let spawn = |next| { + let root = root.clone(); + let barrier = barrier.clone(); + thread::spawn(move || { + let refs = Refs::new(&root); + barrier.wait(); + refs.compare_and_swap("refs/heads/main", Some(old), next) + }) + }; + let a = spawn(left); + let b = spawn(right); + barrier.wait(); + let results = [a.join().unwrap(), b.join().unwrap()]; + assert_eq!(results.iter().filter(|result| result.is_ok()).count(), 1); + let actual = Refs::new(&root).resolve("refs/heads/main").unwrap(); + assert!(actual == left || actual == right); + assert!(!dir.path().join("refs/heads/main.lock").exists()); + } + + #[test] + fn compare_and_delete_removes_only_the_expected_loose_ref() { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(dir.path().join("refs/heads")).unwrap(); + let old = ObjectId::from_hex("1111111111111111111111111111111111111111").unwrap(); + let wrong = ObjectId::from_hex("2222222222222222222222222222222222222222").unwrap(); + std::fs::write(dir.path().join("refs/heads/main"), format!("{old}\n")).unwrap(); + let refs = Refs::new(dir.path()); + + assert!(matches!( + refs.compare_and_delete("refs/heads/main", wrong), + Err(ForgeError::RefConflict { .. }) + )); + assert_eq!(refs.resolve("refs/heads/main").unwrap(), old); + refs.compare_and_delete("refs/heads/main", old).unwrap(); + assert!(refs.resolve("refs/heads/main").is_err()); + assert!(!dir.path().join("refs/heads/main.lock").exists()); + } + + #[test] + fn compare_and_delete_refuses_a_packed_backing_value() { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(dir.path().join("refs/heads")).unwrap(); + let old = ObjectId::from_hex("1111111111111111111111111111111111111111").unwrap(); + std::fs::write( + dir.path().join("packed-refs"), + format!("# pack-refs with: peeled fully-peeled\n{old} refs/heads/main\n"), + ) + .unwrap(); + let refs = Refs::new(dir.path()); + assert!(matches!( + refs.compare_and_delete("refs/heads/main", old), + Err(ForgeError::ReceiveRejected(_)) + )); + assert_eq!(refs.resolve("refs/heads/main").unwrap(), old); + assert!(!dir.path().join("refs/heads/main.lock").exists()); + } } diff --git a/src/repo.rs b/src/repo.rs index 7df2854..9aeb7df 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -9,13 +9,29 @@ use crate::blob::Blob; use crate::commit::Commit; use crate::error::{ForgeError, Result}; +use crate::index::{validate_index_path, Index, IndexEntry}; use crate::object::{Object, ObjectType}; use crate::odb::Odb; use crate::oid::ObjectId; -use crate::refs::Refs; +use crate::refs::{validate_refname, Refs}; use crate::tree::Tree; -use std::collections::HashSet; -use std::path::{Path, PathBuf}; +use crate::tree::TreeEntry; +use crate::worktree::{tracked_status, StatusEntry}; +use crate::{RefTarget, Signature}; +use globset::{GlobBuilder, GlobMatcher}; +use std::collections::{BTreeMap, BTreeSet, HashSet}; +use std::fs::OpenOptions; +use std::io::Write; +use std::path::{Component, Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; + +/// The default `.git/info/exclude` content Git writes on `init`. +const DEFAULT_INFO_EXCLUDE: &[u8] = b"# git ls-files --others --exclude-from=.git/info/exclude\n# Lines that start with '#' are comments.\n# For a project mostly in C, the following would be a good set of\n# exclude patterns (uncomment them if you want to use them):\n# *.[oa]\n# *~\n"; + +const MAX_LOG_COMMITS: usize = 1_000_000; +const MAX_ADD_PATHS: usize = 1_000_000; +const MAX_TREE_DEPTH: usize = 256; +static WORKTREE_TEMP_SEQUENCE: AtomicU64 = AtomicU64::new(0); /// An opened Git repository (loose-object scope). pub struct Repository { @@ -25,6 +41,63 @@ pub struct Repository { } impl Repository { + /// Create a new Git repository, returning the opened [`Repository`]. + /// + /// Lays down a git-compatible directory skeleton (`objects/`, `refs/`, + /// `HEAD`, `config`, `description`, `info/exclude`). For a non-bare repo the + /// skeleton lives under `/.git`; a bare repo places it directly in + /// ``. Re-running `init` on an existing repository is safe: existing + /// `HEAD` and `config` are preserved (matching `git init`'s reinitialize + /// behavior), only missing pieces are created. + /// + /// The default branch name is `main` unless overridden. It is validated so a + /// hostile branch name cannot escape `refs/heads/`. + pub fn init(path: impl AsRef, bare: bool, branch: Option<&str>) -> Result { + let path = path.as_ref(); + let git_dir = if bare { + path.to_path_buf() + } else { + path.join(".git") + }; + + let branch = branch.unwrap_or("main"); + // Reject a branch name that would produce an invalid ref path. + let head_ref = format!("refs/heads/{branch}"); + validate_refname(&head_ref)?; + + // Directory skeleton. `create_dir_all` is idempotent, so reinitializing + // an existing repository never fails on already-present directories. + for dir in [ + git_dir.join("objects").join("info"), + git_dir.join("objects").join("pack"), + git_dir.join("refs").join("heads"), + git_dir.join("refs").join("tags"), + git_dir.join("info"), + ] { + std::fs::create_dir_all(&dir)?; + } + + // HEAD points at the (not-yet-existing) default branch. Preserve an + // existing HEAD so reinitialization does not move the current branch. + write_if_absent(&git_dir.join("HEAD"), format!("ref: {head_ref}\n").as_bytes())?; + + // Minimal but real config. `bare` reflects the requested mode so that + // downstream tools (and `worktree_root`) behave correctly. + let config = format!( + "[core]\n\trepositoryformatversion = 0\n\tfilemode = true\n\tbare = {bare}\n\tlogallrefupdates = {logallrefupdates}\n", + logallrefupdates = !bare, + ); + write_if_absent(&git_dir.join("config"), config.as_bytes())?; + + write_if_absent( + &git_dir.join("description"), + b"Unnamed repository; edit this file 'description' to name the repository.\n", + )?; + write_if_absent(&git_dir.join("info").join("exclude"), DEFAULT_INFO_EXCLUDE)?; + + Self::open_git_dir(&git_dir) + } + /// Open a repository given its `.git` directory directly. pub fn open_git_dir(git_dir: impl AsRef) -> Result { let git_dir = git_dir.as_ref().to_path_buf(); @@ -70,6 +143,403 @@ impl Repository { &self.refs } + /// Parse and checksum-verify this repository's Git index (v2/v3). + pub fn index(&self) -> Result { + Index::read(&self.git_dir) + } + + /// Compare every stage-0 index entry with the working tree. This is the + /// bounded tracked-file core of `git status`; untracked discovery is not yet + /// claimed. + pub fn tracked_status(&self) -> Result> { + let worktree = self.worktree_root()?; + tracked_status(worktree, &self.index()?) + } + + /// Stage files, recursive directories, and tracked deletions into a + /// Git-compatible index while honoring repository ignore rules. + /// Blob objects are published first; one atomic `index.lock` transaction + /// then installs every path together, so a competing writer cannot be lost. + pub fn add(&self, paths: &[PathBuf]) -> Result { + if paths.is_empty() { + return Ok(0); + } + let worktree = self.worktree_root()?; + let current = self.index().unwrap_or(Index { + version: 2, + entries: Vec::new(), + }); + let expanded = expand_add_paths(worktree, paths, ¤t)?; + let mut changes = Vec::with_capacity(expanded.len()); + for relative in &expanded { + let encoded = path_bytes(relative)?; + validate_index_path(&encoded)?; + let full = safe_worktree_path(worktree, relative)?; + match std::fs::symlink_metadata(&full) { + Ok(metadata) if metadata.file_type().is_symlink() => { + let target = std::fs::read_link(&full)?; + let data = os_path_bytes(&target)?; + let oid = self.odb.write(ObjectType::Blob, &data)?; + changes.push(Some(index_entry(encoded, 0o120000, data.len(), oid)?)); + } + Ok(metadata) if metadata.is_file() => { + let data = std::fs::read(&full)?; + let mode = regular_file_mode(&metadata); + let oid = self.odb.write(ObjectType::Blob, &data)?; + changes.push(Some(index_entry(encoded, mode, data.len(), oid)?)); + } + Ok(_) => { + return Err(ForgeError::UnsafePath(format!( + "{} is not a regular file or symlink", + relative.display() + ))) + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + let tracked = current.entries.iter().any(|entry| entry.path == encoded); + if !tracked { + return Err(error.into()); + } + changes.push(None); + } + Err(error) => return Err(error.into()), + } + } + + Index::mutate(&self.git_dir, |index| { + for (relative, change) in expanded.iter().zip(changes) { + let encoded = path_bytes(relative)?; + match change { + Some(entry) => index.upsert(entry)?, + None => { + index.remove_path(&encoded); + } + } + } + Ok(expanded.len()) + }) + } + + /// Write the current stage-0 index as canonical recursive Git trees. + pub fn write_tree(&self) -> Result { + let index = self.index()?; + if index.entries.iter().any(|entry| entry.stage() != 0) { + return Err(ForgeError::InvalidIndex( + "cannot write a tree with unresolved index stages".into(), + )); + } + let mut root = TreeNode::default(); + for entry in &index.entries { + root.insert(&entry.path, entry)?; + } + root.write(&self.odb) + } + + /// Create a commit from the index and atomically advance symbolic HEAD. + /// Objects are immutable and may safely remain unreachable if the ref CAS + /// loses to another writer. + pub fn commit_index( + &self, + message: impl AsRef<[u8]>, + author: Signature, + committer: Signature, + ) -> Result { + let message = message.as_ref(); + if message.is_empty() { + return Err(ForgeError::MalformedObject { + kind: "commit", + reason: "empty commit message".into(), + }); + } + let RefTarget::Symbolic(head_ref) = self.refs.read_head()? else { + return Err(ForgeError::InvalidRef( + "committing on detached HEAD is not supported yet".into(), + )); + }; + let expected = match self.refs.resolve(&head_ref) { + Ok(oid) => Some(oid), + Err(ForgeError::InvalidRef(_)) => None, + Err(error) => return Err(error), + }; + let tree = self.write_tree()?; + let commit = Commit { + tree, + parents: expected.into_iter().collect(), + author, + committer, + extra_headers: Vec::new(), + message: message.to_vec(), + }; + let oid = self.odb.write(ObjectType::Commit, &commit.serialize())?; + self.refs.compare_and_swap(&head_ref, expected, oid)?; + Ok(oid) + } + + /// Create a commit object from an explicit tree + parents and, optionally, + /// advance a ref to it with an atomic compare-and-swap. This is the + /// `git commit-tree` (+ optional ref update) primitive, and unlike + /// [`commit_index`](Self::commit_index) it needs **no worktree** — so a + /// forge can seed or accept commits directly into a hosted **bare** + /// repository. + /// + /// The commit object is written first (immutable, content-addressed, safe to + /// leave unreachable if the ref CAS loses a race). When `update_ref` is set, + /// the ref is advanced from whatever it currently resolves to (or from + /// "absent" for a brand-new branch) to the new commit; a concurrent writer + /// that moved the ref causes a [`ForgeError::RefConflict`], never a lost + /// update. + pub fn commit_tree( + &self, + tree: ObjectId, + parents: Vec, + author: Signature, + committer: Signature, + message: &[u8], + update_ref: Option<&str>, + ) -> Result { + if message.is_empty() { + return Err(ForgeError::MalformedObject { + kind: "commit", + reason: "empty commit message".into(), + }); + } + // The tree must exist and actually be a tree before we bless it into a + // commit — never mint a commit that points at a missing or wrong object. + match self.odb.read(&tree)? { + Object::Tree(_) => {} + other => return Err(type_error(&tree, "tree", other)), + } + let commit = Commit { + tree, + parents, + author, + committer, + extra_headers: Vec::new(), + message: message.to_vec(), + }; + let oid = self.odb.write(ObjectType::Commit, &commit.serialize())?; + if let Some(refname) = update_ref { + validate_refname(refname)?; + let expected = match self.refs.resolve(refname) { + Ok(oid) => Some(oid), + Err(ForgeError::InvalidRef(_)) => None, + Err(error) => return Err(error), + }; + self.refs.compare_and_swap(refname, expected, oid)?; + } + Ok(oid) + } + + /// Safely materialize a tree snapshot and synchronize the index. This is a + /// plumbing checkout: it deliberately does not move HEAD. Locally modified + /// tracked paths and untracked collisions are rejected before any mutation. + pub fn checkout_tree(&self, tree_oid: ObjectId) -> Result { + let worktree = self.worktree_root()?; + let target = self.index_from_tree(tree_oid)?; + let target_data = self.load_index_data(&target)?; + Index::mutate(&self.git_dir, |current| { + let statuses = tracked_status(worktree, current)?; + let status_by_path: BTreeMap, _> = statuses + .into_iter() + .map(|status| (status.path, status.state)) + .collect(); + let current_by_path: BTreeMap, &IndexEntry> = current + .entries + .iter() + .filter(|entry| entry.stage() == 0) + .map(|entry| (entry.path.clone(), entry)) + .collect(); + let target_by_path: BTreeMap, &IndexEntry> = target + .entries + .iter() + .map(|entry| (entry.path.clone(), entry)) + .collect(); + + for (path, entry) in ¤t_by_path { + let changes_path = target_by_path.get(path).map_or(true, |target| { + target.mode != entry.mode || target.oid != entry.oid + }); + if changes_path + && status_by_path + .get(path) + .is_some_and(|state| *state != crate::worktree::WorktreeState::Unmodified) + { + return Err(ForgeError::WorktreeCollision(format!( + "{} has local changes", + String::from_utf8_lossy(path) + ))); + } + } + + for (path, target_entry) in &target_by_path { + let changed = current_by_path.get(path).map_or(true, |current| { + current.mode != target_entry.mode || current.oid != target_entry.oid + }); + if !changed { + continue; + } + let relative = bytes_path(path)?; + let full = worktree.join(&relative); + preflight_parent_chain(worktree, &relative)?; + if !current_by_path.contains_key(path) && std::fs::symlink_metadata(&full).is_ok() { + return Err(ForgeError::WorktreeCollision(format!( + "{} would overwrite an untracked path", + relative.display() + ))); + } + if std::fs::symlink_metadata(&full).is_ok_and(|metadata| metadata.is_dir()) { + return Err(ForgeError::WorktreeCollision(format!( + "{} is a directory", + relative.display() + ))); + } + } + + let mut deletions: Vec> = current_by_path + .keys() + .filter(|path| !target_by_path.contains_key(*path)) + .cloned() + .collect(); + deletions.sort_by_key(|path| std::cmp::Reverse(path.len())); + for path in deletions { + remove_tracked_path(worktree, &bytes_path(&path)?)?; + } + + let mut written = 0; + for (path, target_entry) in &target_by_path { + let changed = current_by_path.get(path).map_or(true, |current| { + current.mode != target_entry.mode || current.oid != target_entry.oid + }); + if changed { + let data = target_data + .get(path) + .ok_or_else(|| ForgeError::ObjectNotFound(target_entry.oid.to_hex()))?; + materialize_entry(worktree, &bytes_path(path)?, target_entry.mode, data)?; + written += 1; + } + } + *current = target.clone(); + Ok(written) + }) + } + + /// Restore explicit files/directories from the current index. This is the + /// intentional-overwrite operation; repository path confinement and atomic + /// per-file replacement still apply. + pub fn restore_index(&self, paths: &[PathBuf]) -> Result { + if paths.is_empty() { + return Ok(0); + } + let worktree = self.worktree_root()?; + Index::with_locked(&self.git_dir, |index| { + let mut selected = BTreeMap::new(); + for input in paths { + let prefix = normalize_add_input(input)?; + for entry in index.entries.iter().filter(|entry| entry.stage() == 0) { + let path = bytes_path(&entry.path)?; + if path == prefix + || (prefix.as_os_str().is_empty() || path.starts_with(&prefix)) + { + selected.insert(entry.path.clone(), entry); + } + } + } + if selected.is_empty() { + return Err(ForgeError::InvalidIndex( + "restore path is not present in the index".into(), + )); + } + for path in selected.keys() { + preflight_parent_chain(worktree, &bytes_path(path)?)?; + } + let mut written = 0; + for (path, entry) in selected { + let blob = self.blob(&entry.oid)?; + materialize_entry(worktree, &bytes_path(&path)?, entry.mode, &blob.data)?; + written += 1; + } + Ok(written) + }) + } + + fn index_from_tree(&self, tree_oid: ObjectId) -> Result { + let mut entries = Vec::new(); + self.flatten_tree(tree_oid, Vec::new(), 0, &mut entries)?; + entries.sort_by(|a, b| a.path.cmp(&b.path)); + Ok(Index { + version: 2, + entries, + }) + } + + fn flatten_tree( + &self, + tree_oid: ObjectId, + prefix: Vec, + depth: usize, + out: &mut Vec, + ) -> Result<()> { + if depth > MAX_TREE_DEPTH { + return Err(ForgeError::ResourceLimit(format!( + "tree depth exceeds {MAX_TREE_DEPTH}" + ))); + } + for entry in self.tree(&tree_oid)?.entries { + if entry.name.is_empty() || entry.name.contains(&b'/') { + return Err(ForgeError::UnsafePath( + String::from_utf8_lossy(&entry.name).into_owned(), + )); + } + let mut path = prefix.clone(); + if !path.is_empty() { + path.push(b'/'); + } + path.extend_from_slice(&entry.name); + validate_index_path(&path)?; + match entry.kind() { + "tree" => self.flatten_tree(entry.oid, path, depth + 1, out)?, + "commit" => { + return Err(ForgeError::InvalidIndex( + "submodule checkout is not supported".into(), + )) + } + _ => { + let mode = u32::from_str_radix(&entry.mode, 8).map_err(|_| { + ForgeError::MalformedObject { + kind: "tree", + reason: "invalid entry mode".into(), + } + })?; + let size = self.blob(&entry.oid)?.data.len(); + out.push(index_entry(path, mode, size, entry.oid)?); + if out.len() > MAX_ADD_PATHS { + return Err(ForgeError::ResourceLimit(format!( + "tree exceeds {MAX_ADD_PATHS} paths" + ))); + } + } + } + } + Ok(()) + } + + fn load_index_data(&self, index: &Index) -> Result, Vec>> { + index + .entries + .iter() + .map(|entry| Ok((entry.path.clone(), self.blob(&entry.oid)?.data))) + .collect() + } + + fn worktree_root(&self) -> Result<&Path> { + if self.git_dir.file_name().is_some_and(|name| name == ".git") { + return self + .git_dir + .parent() + .ok_or_else(|| ForgeError::NoWorktree(self.git_dir.display().to_string())); + } + Err(ForgeError::NoWorktree(self.git_dir.display().to_string())) + } + // --- object plumbing -------------------------------------------------- /// `git hash-object [-w]`: hash `data` as `obj_type`, optionally writing it. @@ -194,6 +664,11 @@ impl Repository { if !seen.insert(oid) { continue; } + if seen.len() > MAX_LOG_COMMITS { + return Err(ForgeError::ResourceLimit(format!( + "history walk exceeds {MAX_LOG_COMMITS} commits" + ))); + } let commit = self.peel_to_commit(oid)?; for parent in &commit.parents { if !seen.contains(parent) { @@ -209,6 +684,529 @@ impl Repository { } } +#[derive(Default)] +struct TreeNode { + files: BTreeMap, (String, ObjectId)>, + directories: BTreeMap, TreeNode>, +} + +impl TreeNode { + fn insert(&mut self, path: &[u8], entry: &IndexEntry) -> Result<()> { + let mut parts = path.split(|byte| *byte == b'/').peekable(); + let mut node = self; + while let Some(part) = parts.next() { + if parts.peek().is_none() { + if node.directories.contains_key(part) { + return Err(ForgeError::InvalidIndex( + "file/directory path collision".into(), + )); + } + node.files + .insert(part.to_vec(), (format!("{:o}", entry.mode), entry.oid)); + } else { + if node.files.contains_key(part) { + return Err(ForgeError::InvalidIndex( + "file/directory path collision".into(), + )); + } + node = node.directories.entry(part.to_vec()).or_default(); + } + } + Ok(()) + } + + fn write(&self, odb: &Odb) -> Result { + let mut entries = Vec::with_capacity(self.files.len() + self.directories.len()); + for (name, (mode, oid)) in &self.files { + entries.push(TreeEntry { + mode: mode.clone(), + name: name.clone(), + oid: *oid, + }); + } + for (name, child) in &self.directories { + entries.push(TreeEntry { + mode: "40000".into(), + name: name.clone(), + oid: child.write(odb)?, + }); + } + let mut tree = Tree { entries }; + tree.canonicalize(); + odb.write(ObjectType::Tree, &tree.serialize()) + } +} + +/// Write `content` to `path` only if the file does not already exist, so +/// reinitializing a repository never clobbers an existing HEAD or config. +fn write_if_absent(path: &Path, content: &[u8]) -> Result<()> { + match OpenOptions::new().write(true).create_new(true).open(path) { + Ok(mut file) => { + file.write_all(content)?; + file.sync_all()?; + Ok(()) + } + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => Ok(()), + Err(error) => Err(error.into()), + } +} + +fn expand_add_paths(worktree: &Path, inputs: &[PathBuf], index: &Index) -> Result> { + let mut visible = BTreeSet::new(); + let mut rules = Vec::new(); + load_ignore_file(worktree, &worktree.join(".git/info/exclude"), &mut rules)?; + collect_visible_paths(worktree, worktree, &rules, &mut visible)?; + + let tracked: Vec = index + .entries + .iter() + .filter(|entry| entry.stage() == 0) + .map(|entry| bytes_path(&entry.path)) + .collect::>()?; + let mut expanded = BTreeSet::new(); + for input in inputs { + let relative = normalize_add_input(input)?; + let full = if relative.as_os_str().is_empty() { + worktree.to_path_buf() + } else { + safe_worktree_path(worktree, &relative)? + }; + match std::fs::symlink_metadata(&full) { + Ok(metadata) if metadata.is_dir() => { + expanded.extend( + visible + .iter() + .filter(|path| { + relative.as_os_str().is_empty() || path.starts_with(&relative) + }) + .cloned(), + ); + expanded.extend( + tracked + .iter() + .filter(|path| { + (relative.as_os_str().is_empty() || path.starts_with(&relative)) + && !worktree.join(path).exists() + }) + .cloned(), + ); + } + Ok(metadata) if metadata.is_file() || metadata.file_type().is_symlink() => { + let encoded = path_bytes(&relative)?; + let is_tracked = index.entries.iter().any(|entry| entry.path == encoded); + if visible.contains(&relative) || is_tracked { + expanded.insert(relative); + } + } + Ok(_) => return Err(ForgeError::UnsafePath(input.display().to_string())), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + let before = expanded.len(); + expanded.extend( + tracked + .iter() + .filter(|path| *path == &relative || path.starts_with(&relative)) + .cloned(), + ); + if expanded.len() == before { + return Err(error.into()); + } + } + Err(error) => return Err(error.into()), + } + } + Ok(expanded.into_iter().collect()) +} + +#[derive(Clone)] +struct IgnoreRule { + base: PathBuf, + matcher: GlobMatcher, + include: bool, + directory_only: bool, +} + +fn load_ignore_file(base: &Path, path: &Path, rules: &mut Vec) -> Result<()> { + let content = match std::fs::read_to_string(path) { + Ok(content) => content, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()), + Err(error) => return Err(error.into()), + }; + for raw in content.lines() { + let mut pattern = raw.trim_end(); + if pattern.is_empty() || pattern.starts_with('#') { + continue; + } + let include = pattern.starts_with('!'); + if include { + pattern = &pattern[1..]; + } + if pattern.is_empty() { + continue; + } + let directory_only = pattern.ends_with('/'); + pattern = pattern.trim_start_matches('/').trim_end_matches('/'); + let normalized = if pattern.contains('/') { + pattern.to_string() + } else { + format!("**/{pattern}") + }; + let matcher = GlobBuilder::new(&normalized) + .literal_separator(true) + .backslash_escape(true) + .build() + .map_err(|error| ForgeError::InvalidIndex(format!("invalid ignore rule: {error}")))? + .compile_matcher(); + rules.push(IgnoreRule { + base: base.to_path_buf(), + matcher, + include, + directory_only, + }); + } + Ok(()) +} + +fn ignored(path: &Path, is_dir: bool, rules: &[IgnoreRule]) -> bool { + let mut ignored = false; + for rule in rules { + let Ok(relative) = path.strip_prefix(&rule.base) else { + continue; + }; + let candidate = relative.to_string_lossy().replace('\\', "/"); + if rule.matcher.is_match(&candidate) && (!rule.directory_only || is_dir) { + ignored = !rule.include; + } + } + ignored +} + +fn collect_visible_paths( + worktree: &Path, + directory: &Path, + inherited: &[IgnoreRule], + visible: &mut BTreeSet, +) -> Result<()> { + let mut rules = Vec::from(inherited); + load_ignore_file(directory, &directory.join(".gitignore"), &mut rules)?; + let mut entries: Vec<_> = std::fs::read_dir(directory)?.collect::>()?; + entries.sort_by_key(|entry| entry.file_name()); + for entry in entries { + let path = entry.path(); + if path == worktree.join(".git") { + continue; + } + let kind = entry.file_type()?; + if ignored(&path, kind.is_dir(), &rules) { + continue; + } + if kind.is_dir() { + collect_visible_paths(worktree, &path, &rules, visible)?; + } else if kind.is_file() || kind.is_symlink() { + let relative = path + .strip_prefix(worktree) + .map_err(|_| ForgeError::UnsafePath(path.display().to_string()))?; + visible.insert(relative.to_path_buf()); + if visible.len() > MAX_ADD_PATHS { + return Err(ForgeError::ResourceLimit(format!( + "recursive add exceeds {MAX_ADD_PATHS} paths" + ))); + } + } + } + Ok(()) +} + +fn normalize_add_input(path: &Path) -> Result { + if path == Path::new(".") { + return Ok(PathBuf::new()); + } + normalize_worktree_path(path) +} + +#[cfg(unix)] +fn bytes_path(path: &[u8]) -> Result { + use std::os::unix::ffi::OsStringExt; + Ok(PathBuf::from(std::ffi::OsString::from_vec(path.to_vec()))) +} + +#[cfg(not(unix))] +fn bytes_path(path: &[u8]) -> Result { + String::from_utf8(path.to_vec()) + .map(PathBuf::from) + .map_err(|_| ForgeError::UnsafePath("non-UTF-8 path unsupported".into())) +} + +fn normalize_worktree_path(path: &Path) -> Result { + if path.is_absolute() { + return Err(ForgeError::UnsafePath(path.display().to_string())); + } + let mut normalized = PathBuf::new(); + for component in path.components() { + match component { + Component::Normal(part) => normalized.push(part), + Component::CurDir => {} + Component::ParentDir | Component::RootDir | Component::Prefix(_) => { + return Err(ForgeError::UnsafePath(path.display().to_string())) + } + } + } + if normalized.as_os_str().is_empty() { + return Err(ForgeError::UnsafePath(path.display().to_string())); + } + Ok(normalized) +} + +fn safe_worktree_path(worktree: &Path, relative: &Path) -> Result { + let mut current = worktree.to_path_buf(); + let count = relative.components().count(); + for (position, component) in relative.components().enumerate() { + let Component::Normal(part) = component else { + return Err(ForgeError::UnsafePath(relative.display().to_string())); + }; + current.push(part); + if position + 1 < count { + match std::fs::symlink_metadata(¤t) { + Ok(metadata) if metadata.file_type().is_symlink() => { + return Err(ForgeError::UnsafePath(relative.display().to_string())) + } + Ok(metadata) if metadata.is_dir() => {} + Ok(_) => return Err(ForgeError::UnsafePath(relative.display().to_string())), + Err(error) => return Err(error.into()), + } + } + } + Ok(current) +} + +fn preflight_parent_chain(worktree: &Path, relative: &Path) -> Result<()> { + let mut current = worktree.to_path_buf(); + let count = relative.components().count(); + for (position, component) in relative.components().enumerate() { + let Component::Normal(part) = component else { + return Err(ForgeError::UnsafePath(relative.display().to_string())); + }; + current.push(part); + if position + 1 == count { + break; + } + match std::fs::symlink_metadata(¤t) { + Ok(metadata) if metadata.file_type().is_symlink() => { + return Err(ForgeError::UnsafePath(relative.display().to_string())) + } + Ok(metadata) if metadata.is_dir() => {} + Ok(_) => { + return Err(ForgeError::WorktreeCollision(format!( + "{} blocks a directory", + current.display() + ))) + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => break, + Err(error) => return Err(error.into()), + } + } + Ok(()) +} + +fn ensure_parent_directories(worktree: &Path, relative: &Path) -> Result { + let parent = relative + .parent() + .ok_or_else(|| ForgeError::UnsafePath(relative.display().to_string()))?; + let mut current = worktree.to_path_buf(); + for component in parent.components() { + let Component::Normal(part) = component else { + return Err(ForgeError::UnsafePath(relative.display().to_string())); + }; + current.push(part); + match std::fs::symlink_metadata(¤t) { + Ok(metadata) if metadata.is_dir() && !metadata.file_type().is_symlink() => {} + Ok(_) => return Err(ForgeError::UnsafePath(relative.display().to_string())), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + std::fs::create_dir(¤t)?; + if let Some(parent) = current.parent() { + std::fs::File::open(parent)?.sync_all()?; + } + } + Err(error) => return Err(error.into()), + } + } + Ok(worktree.join(relative)) +} + +fn materialize_entry(worktree: &Path, relative: &Path, mode: u32, data: &[u8]) -> Result<()> { + validate_index_path(&path_bytes(relative)?)?; + preflight_parent_chain(worktree, relative)?; + let destination = ensure_parent_directories(worktree, relative)?; + if std::fs::symlink_metadata(&destination).is_ok_and(|metadata| metadata.is_dir()) { + return Err(ForgeError::WorktreeCollision(format!( + "{} is a directory", + relative.display() + ))); + } + let parent = destination + .parent() + .ok_or_else(|| ForgeError::UnsafePath(relative.display().to_string()))?; + let sequence = WORKTREE_TEMP_SEQUENCE.fetch_add(1, Ordering::Relaxed); + let temporary = parent.join(format!( + ".rit-checkout-{}-{sequence}.tmp", + std::process::id() + )); + let result = if mode == 0o120000 { + create_checkout_symlink(&temporary, data) + } else if matches!(mode, 0o100644 | 0o100755) { + let mut file = std::fs::OpenOptions::new() + .create_new(true) + .write(true) + .open(&temporary)?; + file.write_all(data)?; + set_checkout_permissions(&file, mode)?; + file.sync_all()?; + drop(file); + Ok(()) + } else { + Err(ForgeError::InvalidIndex(format!( + "unsupported checkout mode {mode:o}" + ))) + }; + if let Err(error) = result { + let _ = std::fs::remove_file(&temporary); + return Err(error); + } + if let Err(error) = std::fs::rename(&temporary, &destination) { + let _ = std::fs::remove_file(&temporary); + return Err(error.into()); + } + std::fs::File::open(parent)?.sync_all()?; + Ok(()) +} + +fn remove_tracked_path(worktree: &Path, relative: &Path) -> Result<()> { + preflight_parent_chain(worktree, relative)?; + let full = worktree.join(relative); + match std::fs::symlink_metadata(&full) { + Ok(metadata) if !metadata.is_dir() => std::fs::remove_file(&full)?, + Ok(_) => { + return Err(ForgeError::WorktreeCollision(format!( + "{} became a directory", + relative.display() + ))) + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()), + Err(error) => return Err(error.into()), + } + let mut parent = full.parent(); + while let Some(directory) = parent { + if directory == worktree { + break; + } + match std::fs::remove_dir(directory) { + Ok(()) => parent = directory.parent(), + Err(error) + if matches!( + error.kind(), + std::io::ErrorKind::DirectoryNotEmpty | std::io::ErrorKind::NotFound + ) => + { + break + } + Err(error) => return Err(error.into()), + } + } + Ok(()) +} + +#[cfg(unix)] +fn create_checkout_symlink(path: &Path, data: &[u8]) -> Result<()> { + use std::os::unix::ffi::OsStrExt; + std::os::unix::fs::symlink(std::ffi::OsStr::from_bytes(data), path)?; + Ok(()) +} + +#[cfg(not(unix))] +fn create_checkout_symlink(_path: &Path, _data: &[u8]) -> Result<()> { + Err(ForgeError::UnsafePath( + "symlink checkout is unsupported on this platform".into(), + )) +} + +#[cfg(unix)] +fn set_checkout_permissions(file: &std::fs::File, mode: u32) -> Result<()> { + use std::os::unix::fs::PermissionsExt; + file.set_permissions(std::fs::Permissions::from_mode(if mode == 0o100755 { + 0o755 + } else { + 0o644 + }))?; + Ok(()) +} + +#[cfg(not(unix))] +fn set_checkout_permissions(_file: &std::fs::File, _mode: u32) -> Result<()> { + Ok(()) +} + +fn index_entry(path: Vec, mode: u32, size: usize, oid: ObjectId) -> Result { + let file_size = u32::try_from(size) + .map_err(|_| ForgeError::ResourceLimit("index file exceeds u32 size".into()))?; + Ok(IndexEntry { + mode, + file_size, + oid, + flags: path.len().min(0x0fff) as u16, + path, + }) +} + +#[cfg(unix)] +fn regular_file_mode(metadata: &std::fs::Metadata) -> u32 { + use std::os::unix::fs::PermissionsExt; + if metadata.permissions().mode() & 0o111 != 0 { + 0o100755 + } else { + 0o100644 + } +} + +#[cfg(not(unix))] +fn regular_file_mode(_: &std::fs::Metadata) -> u32 { + 0o100644 +} + +#[cfg(unix)] +fn path_bytes(path: &Path) -> Result> { + use std::os::unix::ffi::OsStrExt; + let mut bytes = Vec::new(); + for (index, component) in path.components().enumerate() { + let Component::Normal(part) = component else { + return Err(ForgeError::UnsafePath(path.display().to_string())); + }; + if index > 0 { + bytes.push(b'/'); + } + bytes.extend_from_slice(part.as_bytes()); + } + Ok(bytes) +} + +#[cfg(not(unix))] +fn path_bytes(path: &Path) -> Result> { + path.to_str() + .map(|value| value.replace('\\', "/").into_bytes()) + .ok_or_else(|| ForgeError::UnsafePath("non-UTF-8 path unsupported".into())) +} + +#[cfg(unix)] +fn os_path_bytes(path: &Path) -> Result> { + use std::os::unix::ffi::OsStrExt; + Ok(path.as_os_str().as_bytes().to_vec()) +} + +#[cfg(not(unix))] +fn os_path_bytes(path: &Path) -> Result> { + path.to_str() + .map(|value| value.as_bytes().to_vec()) + .ok_or_else(|| ForgeError::UnsafePath("non-UTF-8 symlink target unsupported".into())) +} + /// Build a helpful "wrong type" error. fn type_error(oid: &ObjectId, expected: &'static str, got: Object) -> ForgeError { ForgeError::malformed( diff --git a/src/server.rs b/src/server.rs new file mode 100644 index 0000000..ab76ab2 --- /dev/null +++ b/src/server.rs @@ -0,0 +1,168 @@ +//! Git **hosting** surface: the small, faithful SDK a forge needs to serve a +//! repository over the *dumb* HTTP protocol. +//! +//! The dumb protocol is deliberately the simplest correct path: a client +//! fetches a plain-text ref advertisement (`info/refs`), reads `HEAD` to learn +//! the default branch, then walks the object graph by fetching individual +//! loose objects (and any packs) by their on-disk path. Because `rit` already +//! stores loose objects at `objects/xx/` and resolves refs, the only +//! genuinely git-specific thing a host must *generate* is the `info/refs` +//! advertisement — which is exactly what `git update-server-info` writes. This +//! module owns that generation so the forge never re-implements git internals. +//! +//! Nothing here starts a listener or speaks HTTP; that transport belongs to the +//! forge (`rithub-server`). This is pure content generation over a +//! [`Repository`], keeping the transport swappable and testable. + +use crate::error::Result; +use crate::repo::Repository; +use std::io::Write; + +/// Produce the **dumb-HTTP `info/refs`** advertisement for `repo`. +/// +/// The format is one `"<40-hex-oid>\t\n"` line per ref, sorted by ref +/// name (the ordering `Refs::list` already guarantees). `HEAD` is intentionally +/// omitted — the dumb client fetches `HEAD` separately as its own resource. +/// +/// This is byte-for-byte what a static `git update-server-info` produces for +/// loose repositories, so a stock `git clone`/`git fetch` over dumb HTTP can +/// discover refs and begin an object walk. +pub fn dumb_info_refs(repo: &Repository) -> Result { + let mut out = String::new(); + for (name, oid) in repo.refs().list()? { + out.push_str(&oid.to_hex()); + out.push('\t'); + out.push_str(&name); + out.push('\n'); + } + Ok(out) +} + +/// Produce the **`objects/info/packs`** listing for `repo`. +/// +/// One `"P \n"` line per `*.pack` under `objects/pack`, followed +/// by a trailing blank line, exactly as Git writes it. A repository whose +/// objects are all loose (the common `rit`-authored case) yields an empty-but- +/// valid listing, and the dumb client simply skips straight to loose objects. +pub fn objects_info_packs(repo: &Repository) -> Result { + let pack_dir = repo.git_dir().join("objects").join("pack"); + let mut packs: Vec = Vec::new(); + if let Ok(read_dir) = std::fs::read_dir(&pack_dir) { + for entry in read_dir.flatten() { + let name = entry.file_name().to_string_lossy().into_owned(); + if name.ends_with(".pack") { + packs.push(name); + } + } + } + // Stable output keeps the advertisement deterministic across calls. + packs.sort(); + let mut out = String::new(); + for name in packs { + out.push_str("P "); + out.push_str(&name); + out.push('\n'); + } + out.push('\n'); + Ok(out) +} + +/// Write the on-disk `info/refs` and `objects/info/packs` files — the exact +/// artifacts `git update-server-info` maintains so a *plain static file server* +/// can also host the repository over dumb HTTP. +/// +/// A forge that serves these resources dynamically does not strictly need this, +/// but calling it after each ref change keeps the on-disk repository a valid +/// standalone dumb-HTTP root (e.g. for backup, mirroring, or a fallback static +/// server). Writes are best-effort-atomic: temp file, fsync, rename. +pub fn update_server_info(repo: &Repository) -> Result<()> { + let git_dir = repo.git_dir(); + let info_dir = git_dir.join("info"); + std::fs::create_dir_all(&info_dir)?; + write_atomic(&info_dir.join("refs"), dumb_info_refs(repo)?.as_bytes())?; + + let objects_info = git_dir.join("objects").join("info"); + std::fs::create_dir_all(&objects_info)?; + write_atomic(&objects_info.join("packs"), objects_info_packs(repo)?.as_bytes())?; + Ok(()) +} + +/// Atomically replace `path` with `bytes` (temp file in the same directory, +/// fsync, rename) so a concurrent reader never observes a torn advertisement. +fn write_atomic(path: &std::path::Path, bytes: &[u8]) -> Result<()> { + let parent = path.parent().unwrap_or(std::path::Path::new(".")); + let tmp = parent.join(format!( + ".rit-serverinfo-{}-{}.tmp", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_nanos()) + .unwrap_or(0) + )); + { + let mut file = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&tmp)?; + file.write_all(bytes)?; + file.sync_all()?; + } + if let Err(error) = std::fs::rename(&tmp, path) { + let _ = std::fs::remove_file(&tmp); + return Err(error.into()); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::object::ObjectType; + use crate::Signature; + + fn sig() -> Signature { + Signature::parse("Tester 1700000000 +0000", "commit").unwrap() + } + + #[test] + fn dumb_info_refs_lists_seeded_ref() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path(), true, Some("main")).unwrap(); + let blob = repo.hash_object(ObjectType::Blob, b"hi\n", true).unwrap(); + let mut tree = crate::Tree { + entries: vec![crate::tree::TreeEntry { + mode: "100644".into(), + name: b"a.txt".to_vec(), + oid: blob, + }], + }; + tree.canonicalize(); + let tree_oid = repo + .hash_object(ObjectType::Tree, &tree.serialize(), true) + .unwrap(); + let commit = repo + .commit_tree( + tree_oid, + Vec::new(), + sig(), + sig(), + b"seed\n", + Some("refs/heads/main"), + ) + .unwrap(); + + let advertised = dumb_info_refs(&repo).unwrap(); + assert_eq!(advertised, format!("{}\trefs/heads/main\n", commit.to_hex())); + + // update_server_info writes the same content to disk. + update_server_info(&repo).unwrap(); + let on_disk = std::fs::read_to_string(repo.git_dir().join("info").join("refs")).unwrap(); + assert_eq!(on_disk, advertised); + assert!(repo + .git_dir() + .join("objects") + .join("info") + .join("packs") + .exists()); + } +} diff --git a/src/upload_pack.rs b/src/upload_pack.rs new file mode 100644 index 0000000..72ff470 --- /dev/null +++ b/src/upload_pack.rs @@ -0,0 +1,310 @@ +//! From-scratch **upload-pack**: the fetch/clone side of the git wire protocol, +//! served entirely from `rit`'s object store — no `git upload-pack` bridge. +//! +//! Three pieces: +//! - [`advertise_upload_pack`] — the v0 ref advertisement a client reads first; +//! - [`reachable_objects`] — the object set for a `want`/`have` negotiation +//! (everything reachable from the wants, minus everything reachable from the +//! haves), computed by walking commits → trees → blobs (+ tags); +//! - [`generate_pack`] — a real, self-describing **packfile** (`PACK` header, +//! per-object type/size varint + zlib payload, SHA-1 trailer). Objects are +//! emitted **undeltified** (full), which is a valid pack `git index-pack` +//! accepts and `git fsck` validates. +//! +//! [`upload_pack_v0`] ties them together for the stateless-RPC (HTTP) case: +//! parse the request, compute the set, and return `NAK` + the pack. + +use std::collections::{HashSet, VecDeque}; + +use crate::error::{ForgeError, Result}; +use crate::object::{Object, ObjectType}; +use crate::oid::ObjectId; +use crate::repo::Repository; +use crate::wire::{decode_packet_lines, encode_packet_line, PacketLine, PacketLineLimits}; +use flate2::write::ZlibEncoder; +use flate2::Compression; +use sha1::{Digest, Sha1}; +use std::io::Write; + +/// Capabilities `rit` advertises for upload-pack. Intentionally minimal: no +/// multi-ack and no side-band, so the negotiation is a single `NAK` + raw pack. +pub const UPLOAD_PACK_CAPABILITIES: &[&str] = &["agent=rit/0.1", "object-format=sha1"]; + +/// Upper bound on objects walked/sent (fail-closed against pathological repos). +const MAX_OBJECTS: usize = 5_000_000; + +/// Produce the **v0 upload-pack ref advertisement**: `HEAD` first (carrying the +/// capabilities and a `symref=HEAD:` hint), then every ref, then flush. +pub fn advertise_upload_pack(repo: &Repository) -> Result> { + let mut refs: Vec<(String, ObjectId)> = Vec::new(); + if let Ok(head) = repo.refs().resolve_head() { + refs.push(("HEAD".to_string(), head)); + } + refs.extend(repo.refs().list()?); + refs.sort_by(|a, b| a.0.cmp(&b.0)); + refs.dedup_by(|a, b| a.0 == b.0); + + // A `symref=HEAD:refs/heads/` cap lets `clone` check out the right + // branch; derive it from the HEAD symbolic ref when present. + let mut caps: Vec = UPLOAD_PACK_CAPABILITIES.iter().map(|c| c.to_string()).collect(); + if let Ok(crate::RefTarget::Symbolic(target)) = repo.refs().read_head() { + caps.push(format!("symref=HEAD:{target}")); + } + let capability_list = caps.join(" "); + + let mut out = Vec::new(); + if refs.is_empty() { + // An empty repository advertises the zero-id capabilities pseudo-ref. + let line = format!( + "0000000000000000000000000000000000000000 capabilities^{{}}\0{capability_list}\n" + ); + out.extend(encode_packet_line(&PacketLine::Data(line.into_bytes()))?); + } else { + for (index, (name, oid)) in refs.into_iter().enumerate() { + let line = if index == 0 { + format!("{oid} {name}\0{capability_list}\n") + } else { + format!("{oid} {name}\n") + }; + out.extend(encode_packet_line(&PacketLine::Data(line.into_bytes()))?); + } + } + out.extend_from_slice(b"0000"); + Ok(out) +} + +/// The parsed contents of an upload-pack request body. +#[derive(Default, Debug)] +pub struct FetchRequest { + pub wants: Vec, + pub haves: Vec, + pub done: bool, +} + +/// Parse `want`/`have`/`done` pkt-lines out of an upload-pack request body. +pub fn parse_fetch_request(body: &[u8]) -> Result { + let lines = decode_packet_lines(body, PacketLineLimits::default())?; + let mut request = FetchRequest::default(); + for line in lines { + if let PacketLine::Data(data) = line { + let text = String::from_utf8_lossy(&data); + let text = text.trim_end(); + if let Some(rest) = text.strip_prefix("want ") { + if let Some(hex) = rest.split_whitespace().next() { + request.wants.push(ObjectId::from_hex(hex)?); + } + } else if let Some(rest) = text.strip_prefix("have ") { + if let Some(hex) = rest.split_whitespace().next() { + if let Ok(oid) = ObjectId::from_hex(hex) { + request.haves.push(oid); + } + } + } else if text == "done" { + request.done = true; + } + } + } + Ok(request) +} + +/// Serve a **stateless** (HTTP smart) upload-pack request: parse it, compute the +/// object set, and return `NAK` followed by the raw packfile. +pub fn upload_pack_v0(repo: &Repository, body: &[u8]) -> Result> { + let request = parse_fetch_request(body)?; + if request.wants.is_empty() { + // Nothing requested — a bare flush is a valid "no-op" response. + return Ok(b"0000".to_vec()); + } + let send = reachable_objects(repo, &request.wants, &request.haves)?; + let pack = generate_pack(repo, &send)?; + let mut out = encode_packet_line(&PacketLine::Data(b"NAK\n".to_vec()))?; + out.extend(pack); + Ok(out) +} + +/// Objects reachable from `wants` but not from `haves` (the fetch send set). +pub fn reachable_objects( + repo: &Repository, + wants: &[ObjectId], + haves: &[ObjectId], +) -> Result> { + // Everything the client already has — pruned from the walk. + let mut seen: HashSet = HashSet::new(); + if !haves.is_empty() { + let mut sink = Vec::new(); + walk(repo, haves, &mut seen, &mut sink)?; + } + // Now collect the new objects reachable from the wants. + let mut send = Vec::new(); + walk(repo, wants, &mut seen, &mut send)?; + Ok(send) +} + +/// Walk the object graph from `roots`, adding newly-seen oids to `out`. `seen` +/// is pre-seeded with objects to skip (the have-closure), so those are pruned. +fn walk( + repo: &Repository, + roots: &[ObjectId], + seen: &mut HashSet, + out: &mut Vec, +) -> Result<()> { + let mut queue: VecDeque = roots.iter().copied().collect(); + while let Some(oid) = queue.pop_front() { + if !seen.insert(oid) { + continue; + } + if seen.len() > MAX_OBJECTS { + return Err(ForgeError::ResourceLimit("fetch object walk too large".into())); + } + out.push(oid); + match repo.odb().read(&oid)? { + Object::Commit(commit) => { + walk_tree(repo, commit.tree, seen, out)?; + for parent in commit.parents { + queue.push_back(parent); + } + } + Object::Tag(tag) => queue.push_back(tag.object), + Object::Tree(_) => walk_tree(repo, oid, seen, out)?, + Object::Blob(_) => {} + } + } + Ok(()) +} + +/// Add a tree and everything under it (subtrees + blobs) to `seen`/`out`. +fn walk_tree( + repo: &Repository, + tree_oid: ObjectId, + seen: &mut HashSet, + out: &mut Vec, +) -> Result<()> { + let mut stack = vec![tree_oid]; + while let Some(oid) = stack.pop() { + if !seen.insert(oid) { + continue; + } + if seen.len() > MAX_OBJECTS { + return Err(ForgeError::ResourceLimit("fetch tree walk too large".into())); + } + out.push(oid); + for entry in repo.tree(&oid)?.entries { + match entry.kind() { + "tree" => stack.push(entry.oid), + "commit" => { /* gitlink: not part of this repo's objects */ } + _ => { + if seen.insert(entry.oid) { + out.push(entry.oid); + } + } + } + } + } + Ok(()) +} + +/// The pack object type code for an object type. +fn type_code(object_type: ObjectType) -> u8 { + match object_type { + ObjectType::Commit => 1, + ObjectType::Tree => 2, + ObjectType::Blob => 3, + ObjectType::Tag => 4, + } +} + +/// Encode a pack object header (type + size, git's base-128 varint with the +/// type in the first byte). +fn encode_object_header(type_code: u8, size: usize) -> Vec { + let mut out = Vec::new(); + let mut byte = ((type_code & 0x07) << 4) | (size & 0x0f) as u8; + let mut remaining = size >> 4; + while remaining > 0 { + out.push(byte | 0x80); + byte = (remaining & 0x7f) as u8; + remaining >>= 7; + } + out.push(byte); + out +} + +/// Build a full (undeltified) packfile containing exactly `oids`. +pub fn generate_pack(repo: &Repository, oids: &[ObjectId]) -> Result> { + let count = u32::try_from(oids.len()) + .map_err(|_| ForgeError::ResourceLimit("pack object count exceeds u32".into()))?; + let mut pack: Vec = Vec::new(); + pack.extend_from_slice(b"PACK"); + pack.extend_from_slice(&2u32.to_be_bytes()); + pack.extend_from_slice(&count.to_be_bytes()); + + for oid in oids { + let (object_type, payload) = repo.odb().read_raw(oid)?; + pack.extend(encode_object_header(type_code(object_type), payload.len())); + let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default()); + encoder.write_all(&payload)?; + let compressed = encoder + .finish() + .map_err(|e| ForgeError::Decompress(format!("pack compression failed: {e}")))?; + pack.extend(compressed); + } + + // Trailing SHA-1 over the whole pack (excluding the trailer itself). + let mut hasher = Sha1::new(); + hasher.update(&pack); + pack.extend_from_slice(&hasher.finalize()); + Ok(pack) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::object::ObjectType; + + #[test] + fn pack_header_and_trailer_are_wellformed() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path(), true, Some("main")).unwrap(); + let a = repo.hash_object(ObjectType::Blob, b"alpha\n", true).unwrap(); + let b = repo.hash_object(ObjectType::Blob, b"beta\n", true).unwrap(); + let pack = generate_pack(&repo, &[a, b]).unwrap(); + + assert_eq!(&pack[0..4], b"PACK"); + assert_eq!(u32::from_be_bytes(pack[4..8].try_into().unwrap()), 2); + assert_eq!(u32::from_be_bytes(pack[8..12].try_into().unwrap()), 2); + // Trailer is the SHA-1 of everything before it. + let body = &pack[..pack.len() - 20]; + let mut h = Sha1::new(); + h.update(body); + assert_eq!(&pack[pack.len() - 20..], h.finalize().as_slice()); + } + + #[test] + fn reachable_set_covers_commit_tree_blob() { + let dir = tempfile::tempdir().unwrap(); + let repo = Repository::init(dir.path(), true, Some("main")).unwrap(); + let blob = repo.hash_object(ObjectType::Blob, b"x\n", true).unwrap(); + let mut tree = crate::Tree { + entries: vec![crate::tree::TreeEntry { + mode: "100644".into(), + name: b"f".to_vec(), + oid: blob, + }], + }; + tree.canonicalize(); + let tree_oid = repo.hash_object(ObjectType::Tree, &tree.serialize(), true).unwrap(); + let sig = crate::Signature::parse("A 1700000000 +0000", "commit").unwrap(); + let commit = repo + .commit_tree(tree_oid, Vec::new(), sig.clone(), sig, b"c\n", None) + .unwrap(); + + let send = reachable_objects(&repo, &[commit], &[]).unwrap(); + assert!(send.contains(&commit)); + assert!(send.contains(&tree_oid)); + assert!(send.contains(&blob)); + assert_eq!(send.len(), 3); + + // With the commit already had, nothing new is sent. + let none = reachable_objects(&repo, &[commit], &[commit]).unwrap(); + assert!(none.is_empty()); + } +} diff --git a/src/wire.rs b/src/wire.rs new file mode 100644 index 0000000..ff13b04 --- /dev/null +++ b/src/wire.rs @@ -0,0 +1,519 @@ +//! Strict Git pkt-line framing and receive-pack command parsing. +//! +//! Pkt-line is the shared binary framing used by Git's pack protocols and +//! protocol v2. Push itself still arrives as receive-pack ref-update commands +//! followed by raw pack data; this module intentionally follows that documented +//! boundary instead of inventing a proprietary "v2 push" command. + +use std::collections::BTreeSet; + +use crate::error::{ForgeError, Result}; +use crate::oid::ObjectId; +use crate::refs::validate_refname; + +pub const MAX_PACKET_PAYLOAD: usize = 65_516; +pub const MAX_PACKET_LENGTH: usize = MAX_PACKET_PAYLOAD + 4; +const ZERO_OID: &str = "0000000000000000000000000000000000000000"; + +/// One decoded pkt-line control or binary data frame. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum PacketLine { + Data(Vec), + Flush, + Delimiter, + ResponseEnd, +} + +/// Aggregate parser limits beyond Git's per-packet 65,516-byte ceiling. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct PacketLineLimits { + pub max_packets: usize, + pub max_total_payload: usize, +} + +impl Default for PacketLineLimits { + fn default() -> Self { + Self { + max_packets: 100_000, + max_total_payload: 64 * 1024 * 1024, + } + } +} + +/// One create/update/delete command decoded from receive-pack. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ReceivePackCommand { + Create { + new: ObjectId, + name: String, + }, + Update { + old: ObjectId, + new: ObjectId, + name: String, + }, + Delete { + old: ObjectId, + name: String, + }, +} + +impl ReceivePackCommand { + pub fn name(&self) -> &str { + match self { + Self::Create { name, .. } | Self::Update { name, .. } | Self::Delete { name, .. } => { + name + } + } + } +} + +/// Parsed ref commands and the offset at which raw pack bytes begin. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ReceivePackRequest { + pub commands: Vec, + pub capabilities: BTreeSet, + pub pack_offset: usize, +} + +/// One receive-pack report-status result. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ReceiveStatus { + pub ref_name: String, + pub error: Option, +} + +/// Encode one packet, including special control frames. +pub fn encode_packet_line(packet: &PacketLine) -> Result> { + match packet { + PacketLine::Flush => Ok(b"0000".to_vec()), + PacketLine::Delimiter => Ok(b"0001".to_vec()), + PacketLine::ResponseEnd => Ok(b"0002".to_vec()), + PacketLine::Data(payload) => { + if payload.len() > MAX_PACKET_PAYLOAD { + return Err(ForgeError::ResourceLimit(format!( + "pkt-line payload exceeds {MAX_PACKET_PAYLOAD} bytes" + ))); + } + let total = payload.len() + 4; + let mut encoded = format!("{total:04x}").into_bytes(); + encoded.extend_from_slice(payload); + Ok(encoded) + } + } +} + +/// Decode an entire pkt-line buffer and reject trailing/truncated bytes. +pub fn decode_packet_lines(input: &[u8], limits: PacketLineLimits) -> Result> { + let mut offset = 0; + let mut payload_bytes = 0usize; + let mut packets = Vec::new(); + while offset < input.len() { + if packets.len() == limits.max_packets { + return Err(ForgeError::ResourceLimit(format!( + "pkt-line stream exceeds {} packets", + limits.max_packets + ))); + } + let (packet, consumed) = decode_one(&input[offset..])?; + if let PacketLine::Data(payload) = &packet { + payload_bytes = payload_bytes + .checked_add(payload.len()) + .ok_or_else(|| ForgeError::ResourceLimit("pkt-line byte count overflow".into()))?; + if payload_bytes > limits.max_total_payload { + return Err(ForgeError::ResourceLimit(format!( + "pkt-line stream exceeds {} payload bytes", + limits.max_total_payload + ))); + } + } + packets.push(packet); + offset += consumed; + } + Ok(packets) +} + +/// Parse receive-pack's command pkt-lines and return the raw pack boundary. +/// Client capabilities are accepted only when the caller advertised them. +pub fn parse_receive_pack_request( + input: &[u8], + advertised_capabilities: &[&str], + limits: PacketLineLimits, +) -> Result { + let mut offset = 0usize; + let mut payload_bytes = 0usize; + let mut commands = Vec::new(); + let mut capabilities = BTreeSet::new(); + let advertised = advertised_capabilities + .iter() + .copied() + .collect::>(); + + loop { + let (packet, consumed) = decode_one(input.get(offset..).unwrap_or_default())?; + offset += consumed; + match packet { + PacketLine::Flush => break, + PacketLine::Data(mut line) => { + if commands.len() == limits.max_packets { + return Err(ForgeError::ResourceLimit(format!( + "receive-pack exceeds {} commands", + limits.max_packets + ))); + } + payload_bytes = payload_bytes.checked_add(line.len()).ok_or_else(|| { + ForgeError::ResourceLimit("receive command byte count overflow".into()) + })?; + if payload_bytes > limits.max_total_payload { + return Err(ForgeError::ResourceLimit( + "receive commands exceed payload limit".into(), + )); + } + if line.last() == Some(&b'\n') { + line.pop(); + } + let command_bytes = if commands.is_empty() { + let (command, requested) = split_first_capabilities(&line)?; + for capability in requested { + if !capability_was_advertised(&capability, &advertised) { + return Err(ForgeError::ReceiveRejected(format!( + "client requested unadvertised capability {capability}" + ))); + } + capabilities.insert(capability); + } + command + } else { + if line.contains(&0) { + return Err(ForgeError::ReceiveRejected( + "only the first receive command may carry capabilities".into(), + )); + } + line.as_slice() + }; + commands.push(parse_receive_command(command_bytes)?); + } + PacketLine::Delimiter | PacketLine::ResponseEnd => { + return Err(ForgeError::ReceiveRejected( + "receive-pack command list contains a protocol-v2 control packet".into(), + )); + } + } + } + + if commands.is_empty() { + return Err(ForgeError::ReceiveRejected( + "receive-pack command list is empty".into(), + )); + } + let mut names = BTreeSet::new(); + for command in &commands { + if !names.insert(command.name()) { + return Err(ForgeError::ReceiveRejected(format!( + "receive-pack updates {} more than once", + command.name() + ))); + } + if matches!(command, ReceivePackCommand::Delete { .. }) + && !advertised.contains("delete-refs") + { + return Err(ForgeError::ReceiveRejected( + "delete command requires the server to advertise delete-refs".into(), + )); + } + } + Ok(ReceivePackRequest { + commands, + capabilities, + pack_offset: offset, + }) +} + +/// Encode the standard report-status response after unpack/ref processing. +pub fn encode_receive_status( + unpack_error: Option<&str>, + statuses: &[ReceiveStatus], +) -> Result> { + if statuses.is_empty() { + return Err(ForgeError::ReceiveRejected( + "receive status requires at least one ref result".into(), + )); + } + let mut out = Vec::new(); + let unpack = match unpack_error { + Some(error) => { + validate_status_error(error)?; + format!("unpack {error}\n") + } + None => "unpack ok\n".to_string(), + }; + out.extend(encode_packet_line(&PacketLine::Data(unpack.into_bytes()))?); + for status in statuses { + validate_refname(&status.ref_name)?; + let line = match status.error.as_deref() { + Some(error) => { + validate_status_error(error)?; + format!("ng {} {error}\n", status.ref_name) + } + None => format!("ok {}\n", status.ref_name), + }; + out.extend(encode_packet_line(&PacketLine::Data(line.into_bytes()))?); + } + out.extend_from_slice(b"0000"); + Ok(out) +} + +fn validate_status_error(error: &str) -> Result<()> { + if error.is_empty() || error == "ok" || error.contains(['\0', '\n', '\r']) { + return Err(ForgeError::ReceiveRejected( + "receive status contains an invalid error message".into(), + )); + } + Ok(()) +} + +fn decode_one(input: &[u8]) -> Result<(PacketLine, usize)> { + if input.len() < 4 { + return Err(ForgeError::ReceiveRejected( + "truncated pkt-line length".into(), + )); + } + let header = std::str::from_utf8(&input[..4]) + .map_err(|_| ForgeError::ReceiveRejected("pkt-line length is not ASCII".into()))?; + if !header.bytes().all(|byte| byte.is_ascii_hexdigit()) { + return Err(ForgeError::ReceiveRejected( + "pkt-line length is not hexadecimal".into(), + )); + } + let length = usize::from_str_radix(header, 16) + .map_err(|_| ForgeError::ReceiveRejected("invalid pkt-line length".into()))?; + match length { + 0 => Ok((PacketLine::Flush, 4)), + 1 => Ok((PacketLine::Delimiter, 4)), + 2 => Ok((PacketLine::ResponseEnd, 4)), + 3 => Err(ForgeError::ReceiveRejected( + "reserved pkt-line length 0003".into(), + )), + 4..=MAX_PACKET_LENGTH => { + if input.len() < length { + return Err(ForgeError::ReceiveRejected(format!( + "truncated pkt-line: declared {length} bytes, received {}", + input.len() + ))); + } + Ok((PacketLine::Data(input[4..length].to_vec()), length)) + } + _ => Err(ForgeError::ResourceLimit(format!( + "pkt-line length exceeds {MAX_PACKET_LENGTH} bytes" + ))), + } +} + +fn split_first_capabilities(line: &[u8]) -> Result<(&[u8], Vec)> { + let Some(nul) = line.iter().position(|byte| *byte == 0) else { + return Ok((line, Vec::new())); + }; + let command = &line[..nul]; + let capabilities = std::str::from_utf8(&line[nul + 1..]) + .map_err(|_| ForgeError::ReceiveRejected("capability list is not UTF-8".into()))? + .split_whitespace() + .map(str::to_string) + .collect(); + Ok((command, capabilities)) +} + +fn capability_was_advertised(capability: &str, advertised: &BTreeSet<&str>) -> bool { + if advertised.contains(capability) { + return true; + } + let name = capability + .split_once('=') + .map_or(capability, |(name, _)| name); + // Agent values identify each endpoint independently. Other valued + // capabilities (notably object-format) must match exactly; accepting a + // value merely because its key was advertised could negotiate semantics the + // server cannot execute. + name == "agent" + && advertised.iter().any(|candidate| { + candidate == &"agent" + || candidate + .split_once('=') + .is_some_and(|(key, _)| key == "agent") + }) +} + +fn parse_receive_command(line: &[u8]) -> Result { + let line = std::str::from_utf8(line) + .map_err(|_| ForgeError::ReceiveRejected("receive command is not UTF-8".into()))?; + let fields = line.split(' ').collect::>(); + if fields.len() != 3 || fields.iter().any(|field| field.is_empty()) { + return Err(ForgeError::ReceiveRejected( + "receive command must be ' '".into(), + )); + } + let old = fields[0]; + let new = fields[1]; + let name = fields[2].to_string(); + validate_refname(&name)?; + match (old == ZERO_OID, new == ZERO_OID) { + (true, true) => Err(ForgeError::ReceiveRejected( + "receive command cannot create and delete simultaneously".into(), + )), + (true, false) => Ok(ReceivePackCommand::Create { + new: ObjectId::from_hex(new)?, + name, + }), + (false, true) => Ok(ReceivePackCommand::Delete { + old: ObjectId::from_hex(old)?, + name, + }), + (false, false) => Ok(ReceivePackCommand::Update { + old: ObjectId::from_hex(old)?, + new: ObjectId::from_hex(new)?, + name, + }), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn oid(byte: char) -> String { + std::iter::repeat(byte).take(40).collect() + } + + #[test] + fn pkt_line_is_binary_safe_and_handles_v2_controls() { + let packets = vec![ + PacketLine::Data(vec![0, 1, b'\n', 255]), + PacketLine::Delimiter, + PacketLine::ResponseEnd, + PacketLine::Flush, + ]; + let encoded = packets + .iter() + .map(encode_packet_line) + .collect::>>() + .unwrap() + .concat(); + assert_eq!( + decode_packet_lines(&encoded, PacketLineLimits::default()).unwrap(), + packets + ); + } + + #[test] + fn pkt_line_rejects_truncation_reserved_and_oversize_frames() { + for invalid in [&b"000"[..], &b"0003"[..], &b"0008abc"[..], &b"zzzz"[..]] { + assert!(decode_packet_lines(invalid, PacketLineLimits::default()).is_err()); + } + assert!(encode_packet_line(&PacketLine::Data(vec![0; MAX_PACKET_PAYLOAD + 1])).is_err()); + } + + #[test] + fn parses_receive_commands_capabilities_and_pack_boundary() { + let old = oid('1'); + let new = oid('2'); + let first = format!("{old} {new} refs/heads/main\0report-status-v2 agent=rit-test\n"); + let mut input = encode_packet_line(&PacketLine::Data(first.into_bytes())).unwrap(); + input.extend_from_slice(b"0000PACKraw-data"); + + let parsed = parse_receive_pack_request( + &input, + &["report-status-v2", "agent=rit/0.1"], + PacketLineLimits { + max_packets: 1, + max_total_payload: 1024, + }, + ) + .unwrap(); + assert_eq!(parsed.pack_offset, input.len() - b"PACKraw-data".len()); + assert_eq!(&input[parsed.pack_offset..], b"PACKraw-data"); + assert!(parsed.capabilities.contains("report-status-v2")); + assert!(matches!( + &parsed.commands[0], + ReceivePackCommand::Update { name, .. } if name == "refs/heads/main" + )); + } + + #[test] + fn rejects_unadvertised_capability_duplicate_ref_and_unsafe_delete() { + let old = oid('1'); + let new = oid('2'); + let line = format!("{old} {new} refs/heads/main\0atomic\n"); + let mut input = encode_packet_line(&PacketLine::Data(line.into_bytes())).unwrap(); + input.extend_from_slice(b"0000"); + assert!(parse_receive_pack_request( + &input, + &["report-status-v2"], + PacketLineLimits::default() + ) + .is_err()); + + let wrong_format = format!("{old} {new} refs/heads/main\0object-format=sha256\n"); + let mut wrong_format_input = + encode_packet_line(&PacketLine::Data(wrong_format.into_bytes())).unwrap(); + wrong_format_input.extend_from_slice(b"0000"); + assert!(parse_receive_pack_request( + &wrong_format_input, + &["object-format=sha1"], + PacketLineLimits::default() + ) + .is_err()); + + let first = format!("{old} {new} refs/heads/main\n"); + let second = format!("{new} {old} refs/heads/main\n"); + let mut duplicate = encode_packet_line(&PacketLine::Data(first.into_bytes())).unwrap(); + duplicate.extend(encode_packet_line(&PacketLine::Data(second.into_bytes())).unwrap()); + duplicate.extend_from_slice(b"0000"); + assert!(parse_receive_pack_request(&duplicate, &[], PacketLineLimits::default()).is_err()); + + let delete = format!("{old} {ZERO_OID} refs/heads/main\n"); + let mut delete_input = encode_packet_line(&PacketLine::Data(delete.into_bytes())).unwrap(); + delete_input.extend_from_slice(b"0000"); + assert!( + parse_receive_pack_request(&delete_input, &[], PacketLineLimits::default()).is_err() + ); + assert!(matches!( + &parse_receive_pack_request( + &delete_input, + &["delete-refs"], + PacketLineLimits::default() + ) + .unwrap() + .commands[0], + ReceivePackCommand::Delete { name, .. } if name == "refs/heads/main" + )); + } + + #[test] + fn status_response_roundtrips_through_pkt_line_parser() { + let encoded = encode_receive_status( + None, + &[ + ReceiveStatus { + ref_name: "refs/heads/main".into(), + error: None, + }, + ReceiveStatus { + ref_name: "refs/heads/topic".into(), + error: Some("non-fast-forward".into()), + }, + ], + ) + .unwrap(); + let decoded = decode_packet_lines(&encoded, PacketLineLimits::default()).unwrap(); + assert_eq!(decoded.last(), Some(&PacketLine::Flush)); + assert_eq!(decoded.len(), 4); + assert!(encode_receive_status(None, &[]).is_err()); + assert!(encode_receive_status( + Some("bad\nstatus"), + &[ReceiveStatus { + ref_name: "refs/heads/main".into(), + error: None, + }] + ) + .is_err()); + } +} diff --git a/src/worktree.rs b/src/worktree.rs new file mode 100644 index 0000000..8754bd4 --- /dev/null +++ b/src/worktree.rs @@ -0,0 +1,122 @@ +//! Safe, read-only tracked-worktree status over a verified Git index. + +use std::path::{Component, Path, PathBuf}; + +use crate::index::{validate_index_path, Index}; +use crate::object::{hash_content, ObjectType, MAX_OBJECT_SIZE}; +use crate::{ForgeError, ObjectId, Result}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum WorktreeState { + Unmodified, + Modified, + Deleted, + TypeChanged, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct StatusEntry { + pub path: Vec, + pub index_oid: ObjectId, + pub state: WorktreeState, +} + +pub fn tracked_status(worktree: &Path, index: &Index) -> Result> { + let mut status = Vec::new(); + for entry in index.entries.iter().filter(|entry| entry.stage() == 0) { + validate_index_path(&entry.path)?; + let relative = bytes_to_relative_path(&entry.path)?; + let path = worktree.join(relative); + reject_symlink_ancestors(worktree, &path)?; + let state = match std::fs::symlink_metadata(&path) { + Err(error) if error.kind() == std::io::ErrorKind::NotFound => WorktreeState::Deleted, + Err(error) => return Err(error.into()), + Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_file() => { + WorktreeState::TypeChanged + } + Ok(metadata) if metadata.len() > MAX_OBJECT_SIZE => { + return Err(ForgeError::ResourceLimit(format!( + "tracked file {} exceeds {MAX_OBJECT_SIZE} byte cap", + path.display() + ))) + } + Ok(_) => { + let data = std::fs::read(&path)?; + if hash_content(ObjectType::Blob, &data).0 == entry.oid { + WorktreeState::Unmodified + } else { + WorktreeState::Modified + } + } + }; + status.push(StatusEntry { + path: entry.path.clone(), + index_oid: entry.oid, + state, + }); + } + Ok(status) +} + +fn bytes_to_relative_path(path: &[u8]) -> Result { + let text = std::str::from_utf8(path) + .map_err(|_| ForgeError::UnsafePath("non-UTF-8 index path unsupported".into()))?; + let candidate = Path::new(text); + if candidate + .components() + .any(|component| !matches!(component, Component::Normal(_))) + { + return Err(ForgeError::UnsafePath(text.into())); + } + Ok(candidate.to_path_buf()) +} + +fn reject_symlink_ancestors(root: &Path, target: &Path) -> Result<()> { + let relative = target + .strip_prefix(root) + .map_err(|_| ForgeError::UnsafePath(target.display().to_string()))?; + let mut current = root.to_path_buf(); + let component_count = relative.components().count(); + for (index, component) in relative.components().enumerate() { + if index + 1 == component_count { + break; + } + current.push(component); + if std::fs::symlink_metadata(¤t) + .map(|metadata| metadata.file_type().is_symlink()) + .unwrap_or(false) + { + return Err(ForgeError::UnsafePath(current.display().to_string())); + } + } + Ok(()) +} + +#[cfg(all(test, unix))] +mod tests { + use super::*; + use crate::IndexEntry; + use std::os::unix::fs::symlink; + + #[test] + fn tracked_status_never_follows_a_symlinked_parent() { + let root = tempfile::tempdir().unwrap(); + let outside = tempfile::tempdir().unwrap(); + std::fs::write(outside.path().join("secret"), b"outside").unwrap(); + symlink(outside.path(), root.path().join("linked")).unwrap(); + let index = Index { + version: 2, + entries: vec![IndexEntry { + mode: 0o100644, + file_size: 7, + oid: hash_content(ObjectType::Blob, b"outside").0, + flags: 0, + path: b"linked/secret".to_vec(), + }], + }; + assert!(matches!( + tracked_status(root.path(), &index), + Err(ForgeError::UnsafePath(_)) + )); + } +} diff --git a/tests/interop.rs b/tests/interop.rs index 84bb33f..f8873a4 100644 --- a/tests/interop.rs +++ b/tests/interop.rs @@ -8,11 +8,16 @@ //! //! Requires `git` on PATH (the task environment has it). +use std::io::Write; use std::path::Path; -use std::process::Command; +use std::process::{Command, Stdio}; use rit::object::{hash_content, Object}; -use rit::{ObjectId, ObjectType, Repository}; +use rit::{ + decode_packet_lines, encode_packet_line, execute_receive_pack, ForgeError, Index, ObjectId, + ObjectType, PacketLine, PacketLineLimits, ReceiveLimits, ReceiveObject, ReceivePackPolicy, + ReceiveTransaction, RefUpdate, Repository, Signature, WorktreeState, +}; use tempfile::TempDir; /// Run `git` with args in `cwd`, returning trimmed stdout; panics on failure. @@ -45,6 +50,46 @@ fn git_bytes(cwd: &Path, args: &[&str]) -> Vec { output.stdout } +/// Run Git and require a non-zero result, returning stderr for assertions. +fn git_fails(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:?} unexpectedly succeeded: {}", + String::from_utf8_lossy(&output.stdout) + ); + String::from_utf8_lossy(&output.stderr).into_owned() +} + +/// Run Git with binary stdin and return binary stdout. +fn git_bytes_with_stdin(cwd: &Path, args: &[&str], stdin: &[u8]) -> Vec { + let mut child = Command::new("git") + .args(args) + .current_dir(cwd) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("git must be installed and on PATH"); + child + .stdin + .take() + .expect("piped stdin") + .write_all(stdin) + .expect("write git stdin"); + let output = child.wait_with_output().expect("wait for git"); + 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(); @@ -109,6 +154,720 @@ fn assert_has_packfile(p: &Path) { assert!(has_pack, "expected a .pack file under {pack_dir:?}"); } +#[test] +fn quarantined_receive_publishes_a_real_git_graph_that_git_fsck_accepts() { + let source = init_repo_with_commit(); + let source_repo = Repository::discover(source.path()).unwrap(); + let reachable = git(source.path(), &["rev-list", "--objects", "--all"]); + let ids = reachable + .lines() + .filter_map(|line| line.split_whitespace().next()) + .map(|hex| ObjectId::from_hex(hex).unwrap()) + .collect::>(); + assert!( + ids.len() >= 5, + "fixture should include commit/tree/blob graph" + ); + + let target = tempfile::tempdir().unwrap(); + git(target.path(), &["init", "--bare", "-q"]); + let target_repo = Repository::open_git_dir(target.path()).unwrap(); + let mut receive = ReceiveTransaction::begin(&target_repo, ReceiveLimits::default()).unwrap(); + for oid in &ids { + let (object_type, payload) = source_repo.odb().read_raw(oid).unwrap(); + receive + .stage(ReceiveObject { + declared_oid: *oid, + object_type, + payload, + }) + .unwrap(); + } + let tip = source_repo.refs().resolve("refs/heads/main").unwrap(); + let outcome = receive + .finish(RefUpdate { + name: "refs/heads/main".into(), + expected_old: None, + new: tip, + allow_non_fast_forward: false, + }) + .unwrap(); + + assert_eq!(outcome.published_objects, ids.len()); + assert_eq!( + git(target.path(), &["rev-parse", "refs/heads/main"]), + tip.to_hex() + ); + git(target.path(), &["fsck", "--strict"]); + assert!(std::fs::read_dir(target.path().join("rit-quarantine")) + .unwrap() + .next() + .is_none()); +} + +#[test] +fn raw_git_pack_is_verified_decoded_and_published_through_quarantine() { + let source = init_repo_with_commit(); + let root = source.path(); + + // Similar successive payloads give stock Git useful delta candidates. Rit + // does not implement those delta instructions itself: gitoxide resolves + // them before the normal decoded-object quarantine boundary. + for revision in 0..4 { + let mut payload = vec![b'a'; 32 * 1024]; + payload[revision * 101] = b'b' + revision as u8; + std::fs::write(root.join("large.txt"), payload).unwrap(); + commit_all(root, &format!("large revision {revision}")); + } + let pack = git_bytes_with_stdin( + root, + &[ + "pack-objects", + "--stdout", + "--revs", + "--delta-base-offset", + "--window=10", + "--depth=10", + ], + b"refs/heads/main\n", + ); + assert_eq!(&pack[..4], b"PACK"); + + let target = tempfile::tempdir().unwrap(); + git(target.path(), &["init", "--bare", "-q"]); + let target_repo = Repository::open_git_dir(target.path()).unwrap(); + let mut receive = ReceiveTransaction::begin(&target_repo, ReceiveLimits::default()).unwrap(); + let imported = receive.stage_pack(&pack).unwrap(); + let tip = ObjectId::from_hex(&git(root, &["rev-parse", "refs/heads/main"])).unwrap(); + let outcome = receive + .finish(RefUpdate { + name: "refs/heads/main".into(), + expected_old: None, + new: tip, + allow_non_fast_forward: false, + }) + .unwrap(); + + assert!(imported >= 10, "fixture should produce a non-trivial pack"); + assert_eq!(outcome.published_objects, imported); + assert_eq!( + git(target.path(), &["rev-parse", "refs/heads/main"]), + tip.to_hex() + ); + git(target.path(), &["fsck", "--strict"]); + assert!(std::fs::read_dir(target.path().join("rit-quarantine")) + .unwrap() + .next() + .is_none()); +} + +#[test] +fn complete_receive_pack_body_moves_one_ref_and_returns_report_status() { + let source = init_repo_with_commit(); + let pack = git_bytes_with_stdin( + source.path(), + &["pack-objects", "--stdout", "--revs", "--delta-base-offset"], + b"refs/heads/main\n", + ); + let tip = git(source.path(), &["rev-parse", "refs/heads/main"]); + let zero = "0000000000000000000000000000000000000000"; + let command = format!( + "{zero} {tip} refs/heads/main\0report-status-v2 object-format=sha1 agent=git/test\n" + ); + let mut request = encode_packet_line(&PacketLine::Data(command.into_bytes())).unwrap(); + request.extend_from_slice(b"0000"); + request.extend_from_slice(&pack); + + let target = tempfile::tempdir().unwrap(); + git(target.path(), &["init", "--bare", "-q"]); + let target_repo = Repository::open_git_dir(target.path()).unwrap(); + let execution = execute_receive_pack( + &target_repo, + &request, + &["report-status-v2", "object-format=sha1", "agent=rit/0.1"], + PacketLineLimits::default(), + ReceiveLimits::default(), + ReceivePackPolicy::default(), + ) + .unwrap(); + + assert!(execution.rejection.is_none()); + assert_eq!(execution.outcome.unwrap().new.to_hex(), tip); + let response = decode_packet_lines(&execution.response, PacketLineLimits::default()).unwrap(); + assert!(matches!(&response[0], PacketLine::Data(line) if line == b"unpack ok\n")); + assert!(matches!(&response[1], PacketLine::Data(line) if line == b"ok refs/heads/main\n")); + assert_eq!(git(target.path(), &["rev-parse", "refs/heads/main"]), tip); + git(target.path(), &["fsck", "--strict"]); +} + +#[test] +fn stock_git_pushes_create_and_update_through_rit_receive_pack_process() { + let source = init_repo_with_commit(); + let target = tempfile::tempdir().unwrap(); + git(target.path(), &["init", "--bare", "-q"]); + let receive_pack = format!("{} receive-pack", env!("CARGO_BIN_EXE_rit")); + let target_path = target.path().to_str().unwrap(); + + git( + source.path(), + &[ + "push", + "--quiet", + "--receive-pack", + &receive_pack, + target_path, + "main:main", + ], + ); + let first = git(source.path(), &["rev-parse", "HEAD"]); + assert_eq!(git(target.path(), &["rev-parse", "refs/heads/main"]), first); + + std::fs::write( + source.path().join("hello.txt"), + b"second push through rit\n", + ) + .unwrap(); + commit_all(source.path(), "second push"); + git( + source.path(), + &[ + "push", + "--quiet", + "--receive-pack", + &receive_pack, + target_path, + "main:main", + ], + ); + let second = git(source.path(), &["rev-parse", "HEAD"]); + assert_eq!( + git(target.path(), &["rev-parse", "refs/heads/main"]), + second + ); + + git(source.path(), &["reset", "--hard", "HEAD~1"]); + std::fs::write(source.path().join("divergent.txt"), b"rewrite attempt\n").unwrap(); + commit_all(source.path(), "divergent rewrite"); + let error = git_fails( + source.path(), + &[ + "push", + "--force", + "--receive-pack", + &receive_pack, + target_path, + "main:main", + ], + ); + assert!( + error.contains("non-fast-forward") || error.contains("remote rejected"), + "unexpected force-push error: {error}" + ); + assert_eq!( + git(target.path(), &["rev-parse", "refs/heads/main"]), + second, + "client --force must not override server history policy" + ); + git( + source.path(), + &[ + "push", + "--quiet", + "--receive-pack", + &receive_pack, + target_path, + ":main", + ], + ); + let deletion_check = git_fails(target.path(), &["show-ref", "--verify", "refs/heads/main"]); + assert!(deletion_check.is_empty() || deletion_check.contains("refs/heads/main")); + git(target.path(), &["fsck", "--strict"]); +} + +#[test] +fn stock_git_clones_and_fetches_through_rit_upload_pack_bridge() { + let source = init_repo_with_commit(); + let transport_root = tempfile::tempdir().unwrap(); + let bare = transport_root.path().join("origin.git"); + let checkout = transport_root.path().join("checkout"); + git( + transport_root.path(), + &[ + "clone", + "--bare", + "--quiet", + source.path().to_str().unwrap(), + bare.to_str().unwrap(), + ], + ); + let upload_pack = format!("{} upload-pack", env!("CARGO_BIN_EXE_rit")); + git( + transport_root.path(), + &[ + "clone", + "--quiet", + "--no-local", + "--upload-pack", + &upload_pack, + bare.to_str().unwrap(), + checkout.to_str().unwrap(), + ], + ); + assert_eq!( + git(&checkout, &["rev-parse", "HEAD"]), + git(source.path(), &["rev-parse", "HEAD"]) + ); + + std::fs::write(source.path().join("fetched.txt"), b"through upload-pack\n").unwrap(); + commit_all(source.path(), "fetched through bridge"); + git( + source.path(), + &["push", "--quiet", bare.to_str().unwrap(), "main:main"], + ); + git( + &checkout, + &["fetch", "--quiet", "--upload-pack", &upload_pack, "origin"], + ); + assert_eq!( + git(&checkout, &["rev-parse", "origin/main"]), + git(source.path(), &["rev-parse", "HEAD"]) + ); + git(bare.as_path(), &["fsck", "--strict"]); +} + +#[test] +fn rit_receive_pack_refuses_deleting_a_packed_ref_without_exposing_old_state() { + let source = init_repo_with_commit(); + let target = tempfile::tempdir().unwrap(); + git(target.path(), &["init", "--bare", "-q"]); + git( + source.path(), + &[ + "push", + "--quiet", + target.path().to_str().unwrap(), + "main:main", + ], + ); + let tip = git(target.path(), &["rev-parse", "refs/heads/main"]); + git(target.path(), &["pack-refs", "--all", "--prune"]); + assert!(!target.path().join("refs/heads/main").exists()); + + let receive_pack = format!("{} receive-pack", env!("CARGO_BIN_EXE_rit")); + let error = git_fails( + source.path(), + &[ + "push", + "--receive-pack", + &receive_pack, + target.path().to_str().unwrap(), + ":main", + ], + ); + assert!( + error.contains("remote rejected") || error.contains("receive policy rejected"), + "unexpected packed-ref deletion error: {error}" + ); + assert_eq!(git(target.path(), &["rev-parse", "refs/heads/main"]), tip); + assert!(!target.path().join("refs/heads/main.lock").exists()); +} + +#[test] +fn corrupt_or_truncated_raw_pack_never_publishes_objects_or_refs() { + let source = init_repo_with_commit(); + let pack = git_bytes_with_stdin( + source.path(), + &["pack-objects", "--stdout", "--revs"], + b"refs/heads/main\n", + ); + let tip = ObjectId::from_hex(&git(source.path(), &["rev-parse", "HEAD"])).unwrap(); + + for bad_pack in [ + { + let mut corrupt = pack.clone(); + let last = corrupt.len() - 1; + corrupt[last] ^= 0xff; + corrupt + }, + pack[..pack.len() - 1].to_vec(), + ] { + let target = tempfile::tempdir().unwrap(); + git(target.path(), &["init", "--bare", "-q"]); + let target_repo = Repository::open_git_dir(target.path()).unwrap(); + let mut receive = + ReceiveTransaction::begin(&target_repo, ReceiveLimits::default()).unwrap(); + assert!(matches!( + receive.stage_pack(&bad_pack), + Err(ForgeError::Pack(_)) + )); + assert!(!target_repo.odb().contains(&tip)); + assert!(target_repo.refs().resolve("refs/heads/main").is_err()); + } +} + +#[test] +fn thin_git_pack_resolves_verified_bases_from_target_odb() { + let source = init_repo_with_commit(); + let root = source.path(); + let mut base_payload = vec![b'x'; 64 * 1024]; + base_payload[17] = b'a'; + std::fs::write(root.join("delta-base.txt"), &base_payload).unwrap(); + commit_all(root, "delta base"); + let old = git(root, &["rev-parse", "HEAD"]); + + let target = tempfile::tempdir().unwrap(); + git( + target.path(), + &["clone", "--bare", "-q", root.to_str().unwrap(), "."], + ); + + for revision in 0..3 { + base_payload[1000 + revision * 37] = b'b' + revision as u8; + std::fs::write(root.join("delta-base.txt"), &base_payload).unwrap(); + commit_all(root, &format!("thin revision {revision}")); + } + let new = git(root, &["rev-parse", "HEAD"]); + let revisions = format!("{new}\n^{old}\n"); + let thin_pack = git_bytes_with_stdin( + root, + &[ + "pack-objects", + "--stdout", + "--revs", + "--thin", + "--delta-base-offset", + "--window=10", + "--depth=10", + ], + revisions.as_bytes(), + ); + + // The same transport pack must fail in a repository that does not own its + // advertised base. This also proves the fixture is genuinely thin rather + // than merely requested with `--thin` but self-contained by chance. + let empty_target = tempfile::tempdir().unwrap(); + git(empty_target.path(), &["init", "--bare", "-q"]); + let empty_repo = Repository::open_git_dir(empty_target.path()).unwrap(); + let mut missing_base = + ReceiveTransaction::begin(&empty_repo, ReceiveLimits::default()).unwrap(); + assert!(matches!( + missing_base.stage_pack(&thin_pack), + Err(ForgeError::Pack(_)) + )); + assert!(empty_repo.refs().resolve("refs/heads/main").is_err()); + + let target_repo = Repository::open_git_dir(target.path()).unwrap(); + let mut receive = ReceiveTransaction::begin(&target_repo, ReceiveLimits::default()).unwrap(); + let staged = receive.stage_pack(&thin_pack).unwrap(); + assert!(staged > 0); + receive + .finish(RefUpdate { + name: "refs/heads/main".into(), + expected_old: Some(ObjectId::from_hex(&old).unwrap()), + new: ObjectId::from_hex(&new).unwrap(), + allow_non_fast_forward: false, + }) + .unwrap(); + + assert_eq!(git(target.path(), &["rev-parse", "refs/heads/main"]), new); + git(target.path(), &["fsck", "--strict"]); +} + +#[test] +fn reads_real_git_index_and_reports_tracked_worktree_changes() { + let dir = init_repo_with_commit(); + let repo = Repository::discover(dir.path()).unwrap(); + let index = repo.index().expect("real Git v2 index should parse"); + let paths: Vec<_> = index.entries.iter().map(|entry| entry.path_str()).collect(); + assert_eq!(paths, vec!["hello.txt", "sub/nested.txt"]); + assert!(repo + .tracked_status() + .unwrap() + .iter() + .all(|entry| entry.state == WorktreeState::Unmodified)); + + std::fs::write(dir.path().join("hello.txt"), b"modified\n").unwrap(); + std::fs::remove_file(dir.path().join("sub/nested.txt")).unwrap(); + let changed = repo.tracked_status().unwrap(); + assert_eq!(changed[0].state, WorktreeState::Modified); + assert_eq!(changed[1].state, WorktreeState::Deleted); + + let index_path = dir.path().join(".git/index"); + let mut corrupt = std::fs::read(&index_path).unwrap(); + corrupt[16] ^= 0xff; + std::fs::write(index_path, corrupt).unwrap(); + assert!(matches!(repo.index(), Err(ForgeError::InvalidIndex(_)))); +} + +#[test] +fn rit_add_writes_an_index_real_git_can_commit() { + let dir = init_repo_with_commit(); + let root = dir.path(); + std::fs::write(root.join("hello.txt"), b"staged by rit\n").unwrap(); + std::fs::write(root.join("new.txt"), b"new through rit\n").unwrap(); + + let repo = Repository::discover(root).unwrap(); + assert_eq!( + repo.add(&["hello.txt".into(), "new.txt".into()]).unwrap(), + 2 + ); + + assert_eq!( + git(root, &["diff", "--cached", "--name-only"]), + "hello.txt\nnew.txt" + ); + assert_eq!(git_bytes(root, &["show", ":hello.txt"]), b"staged by rit\n"); + assert_eq!(git_bytes(root, &["show", ":new.txt"]), b"new through rit\n"); + let tree = git(root, &["write-tree"]); + assert_eq!(tree.len(), 40); + assert!(!root.join(".git/index.lock").exists()); +} + +#[test] +fn rit_add_stages_a_tracked_deletion() { + let dir = init_repo_with_commit(); + let root = dir.path(); + std::fs::remove_file(root.join("hello.txt")).unwrap(); + Repository::discover(root) + .unwrap() + .add(&["hello.txt".into()]) + .unwrap(); + assert_eq!( + git(root, &["diff", "--cached", "--name-status"]), + "D\thello.txt" + ); +} + +#[test] +fn recursive_add_honors_root_and_nested_ignore_rules() { + let dir = init_repo_with_commit(); + let root = dir.path(); + std::fs::create_dir_all(root.join("src/generated")).unwrap(); + std::fs::write(root.join(".gitignore"), b"*.log\nbuild/\n").unwrap(); + std::fs::write(root.join("src/.gitignore"), b"generated/\n!keep.log\n").unwrap(); + std::fs::write(root.join("src/main.rs"), b"fn main() {}\n").unwrap(); + std::fs::write(root.join("src/debug.log"), b"ignored\n").unwrap(); + std::fs::write(root.join("src/keep.log"), b"included\n").unwrap(); + std::fs::write(root.join("src/generated/code.rs"), b"ignored\n").unwrap(); + std::fs::create_dir(root.join("build")).unwrap(); + std::fs::write(root.join("build/output.bin"), b"ignored\n").unwrap(); + + let count = Repository::discover(root) + .unwrap() + .add(&[".".into()]) + .unwrap(); + assert!(count >= 3); + assert_eq!( + git(root, &["diff", "--cached", "--name-only"]), + ".gitignore\nsrc/.gitignore\nsrc/keep.log\nsrc/main.rs" + ); +} + +#[test] +fn rit_write_tree_is_byte_identical_to_git_write_tree() { + let dir = init_repo_with_commit(); + let root = dir.path(); + std::fs::create_dir_all(root.join("deep/a")).unwrap(); + std::fs::write(root.join("deep/a/file.txt"), b"recursive tree\n").unwrap(); + let repo = Repository::discover(root).unwrap(); + repo.add(&[".".into()]).unwrap(); + assert_eq!( + repo.write_tree().unwrap().to_hex(), + git(root, &["write-tree"]) + ); +} + +#[test] +fn rit_commit_writes_git_compatible_commit_and_advances_head_with_cas() { + let dir = init_repo_with_commit(); + let root = dir.path(); + let old = git(root, &["rev-parse", "HEAD"]); + std::fs::write(root.join("hello.txt"), b"committed by rit\n").unwrap(); + let repo = Repository::discover(root).unwrap(); + repo.add(&["hello.txt".into()]).unwrap(); + let signature = + Signature::parse("Rit Author 1700000000 +0000", "commit").unwrap(); + let oid = repo + .commit_index("rit local authoring\n", signature.clone(), signature) + .unwrap(); + + assert_eq!(git(root, &["rev-parse", "HEAD"]), oid.to_hex()); + assert_eq!(git(root, &["rev-parse", "HEAD^"]), old); + assert_eq!( + git(root, &["show", "-s", "--format=%s", "HEAD"]), + "rit local authoring" + ); + assert_eq!( + repo.commit(&oid).unwrap().tree.to_hex(), + git(root, &["rev-parse", "HEAD^{tree}"]) + ); + assert_eq!( + git_bytes(root, &["show", "HEAD:hello.txt"]), + b"committed by rit\n" + ); +} + +#[test] +fn restore_replaces_tracked_content_from_index_without_changing_index() { + let dir = init_repo_with_commit(); + let root = dir.path(); + let before_index = std::fs::read(root.join(".git/index")).unwrap(); + std::fs::write(root.join("hello.txt"), b"local damage\n").unwrap(); + let restored = Repository::discover(root) + .unwrap() + .restore_index(&["hello.txt".into()]) + .unwrap(); + assert_eq!(restored, 1); + assert_eq!( + std::fs::read(root.join("hello.txt")).unwrap(), + b"Hello, Forge!\n" + ); + assert_eq!( + std::fs::read(root.join(".git/index")).unwrap(), + before_index + ); + assert!( + !std::fs::read_dir(root).unwrap().flatten().any(|entry| entry + .file_name() + .to_string_lossy() + .starts_with(".rit-checkout-")) + ); +} + +#[test] +fn checkout_tree_materializes_snapshot_and_synchronizes_index_without_moving_head() { + let dir = init_repo_with_commit(); + let root = dir.path(); + let old_head = git(root, &["rev-parse", "HEAD"]); + let old_tree = ObjectId::from_hex(&git(root, &["rev-parse", "HEAD^{tree}"])).unwrap(); + std::fs::write(root.join("hello.txt"), b"new head\n").unwrap(); + std::fs::write(root.join("added.txt"), b"remove on checkout\n").unwrap(); + commit_all(root, "new head"); + let new_head = git(root, &["rev-parse", "HEAD"]); + + let written = Repository::discover(root) + .unwrap() + .checkout_tree(old_tree) + .unwrap(); + assert_eq!(written, 1); + assert_eq!(git(root, &["rev-parse", "HEAD"]), new_head); + assert_ne!(new_head, old_head); + assert_eq!(git(root, &["write-tree"]), old_tree.to_hex()); + assert_eq!( + std::fs::read(root.join("hello.txt")).unwrap(), + b"Hello, Forge!\n" + ); + assert!(!root.join("added.txt").exists()); + assert_eq!(git(root, &["diff", "--name-only"]), ""); +} + +#[test] +fn checkout_tree_rejects_modified_and_untracked_collisions_before_mutation() { + let dir = init_repo_with_commit(); + let root = dir.path(); + std::fs::write(root.join("hello.txt"), b"target tracked version\n").unwrap(); + std::fs::write(root.join("future.txt"), b"target version\n").unwrap(); + commit_all(root, "future target"); + let target_tree = ObjectId::from_hex(&git(root, &["rev-parse", "HEAD^{tree}"])).unwrap(); + git(root, &["reset", "--hard", "HEAD^"]); + let before_index = std::fs::read(root.join(".git/index")).unwrap(); + std::fs::write(root.join("future.txt"), b"untracked version\n").unwrap(); + + let collision = Repository::discover(root) + .unwrap() + .checkout_tree(target_tree); + assert!(matches!(collision, Err(ForgeError::WorktreeCollision(_)))); + assert_eq!( + std::fs::read(root.join("future.txt")).unwrap(), + b"untracked version\n" + ); + assert_eq!( + std::fs::read(root.join(".git/index")).unwrap(), + before_index + ); + + std::fs::remove_file(root.join("future.txt")).unwrap(); + std::fs::write(root.join("hello.txt"), b"local tracked edit\n").unwrap(); + let modified = Repository::discover(root) + .unwrap() + .checkout_tree(target_tree); + assert!(matches!(modified, Err(ForgeError::WorktreeCollision(_)))); + assert_eq!( + std::fs::read(root.join("hello.txt")).unwrap(), + b"local tracked edit\n" + ); + assert_eq!( + std::fs::read(root.join(".git/index")).unwrap(), + before_index + ); +} + +#[cfg(unix)] +#[test] +fn restore_never_follows_a_symlinked_parent_outside_the_worktree() { + let dir = init_repo_with_commit(); + let root = dir.path(); + let outside = tempfile::tempdir().unwrap(); + std::fs::remove_file(root.join("sub/nested.txt")).unwrap(); + std::fs::remove_dir(root.join("sub")).unwrap(); + std::os::unix::fs::symlink(outside.path(), root.join("sub")).unwrap(); + + let result = Repository::discover(root) + .unwrap() + .restore_index(&["sub".into()]); + assert!(matches!(result, Err(ForgeError::UnsafePath(_)))); + assert!(!outside.path().join("nested.txt").exists()); +} + +#[test] +fn existing_index_lock_blocks_without_losing_the_index() { + let dir = init_repo_with_commit(); + let root = dir.path(); + let index_path = root.join(".git/index"); + let before = std::fs::read(&index_path).unwrap(); + std::fs::write(root.join("blocked.txt"), b"must not stage\n").unwrap(); + std::fs::write(root.join(".git/index.lock"), b"other writer").unwrap(); + + let result = Repository::discover(root) + .unwrap() + .add(&["blocked.txt".into()]); + assert!(matches!(result, Err(ForgeError::IndexLocked(_)))); + assert_eq!(std::fs::read(index_path).unwrap(), before); + assert_eq!( + std::fs::read(root.join(".git/index.lock")).unwrap(), + b"other writer" + ); +} + +#[test] +fn concurrent_index_writer_is_rejected_without_lost_update() { + let dir = init_repo_with_commit(); + let root = dir.path(); + std::fs::write(root.join("racer.txt"), b"racing writer\n").unwrap(); + let git_dir = root.join(".git"); + let (locked_tx, locked_rx) = std::sync::mpsc::channel(); + let (release_tx, release_rx) = std::sync::mpsc::channel(); + let writer_git_dir = git_dir.clone(); + let writer = std::thread::spawn(move || { + Index::mutate(&writer_git_dir, |_: &mut Index| { + locked_tx.send(()).unwrap(); + release_rx.recv().unwrap(); + Ok(()) + }) + }); + locked_rx.recv().unwrap(); + + let competing = Repository::discover(root) + .unwrap() + .add(&["racer.txt".into()]); + assert!(matches!(competing, Err(ForgeError::IndexLocked(_)))); + release_tx.send(()).unwrap(); + writer.join().unwrap().unwrap(); + + assert_eq!(git(root, &["diff", "--cached", "--name-only"]), ""); + assert!(!git_dir.join("index.lock").exists()); +} + #[test] fn blob_oid_matches_git_hash_object() { let dir = tempfile::tempdir().unwrap(); @@ -602,3 +1361,43 @@ fn corrupt_packfile_is_rejected_not_panicked() { "reading from a corrupt packfile must return an error, not succeed or panic" ); } + +/// A packfile generated natively by `rit` (no `git upload-pack`) must be a valid +/// git packfile: `git unpack-objects` decodes every object, and the resulting +/// repository `fsck`s clean. +#[test] +fn rit_generated_pack_is_decoded_by_stock_git() { + let source = init_repo_with_commit(); + let repo = rit::Repository::open_git_dir(source.path().join(".git")).unwrap(); + let head = repo.head().unwrap(); + + // Object set reachable from HEAD (commit + trees + blobs), packed by rit. + let send = rit::reachable_objects(&repo, &[head], &[]).unwrap(); + assert!(send.len() >= 4, "expected commit+trees+blobs, got {}", send.len()); + let pack = rit::generate_pack(&repo, &send).unwrap(); + assert_eq!(&pack[0..4], b"PACK"); + + // Feed the pack to stock git in a fresh repo. + let target = tempfile::tempdir().unwrap(); + git(target.path(), &["init", "-q"]); + let mut child = Command::new("git") + .arg("unpack-objects") + .arg("-q") + .current_dir(target.path()) + .stdin(Stdio::piped()) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + child.stdin.take().unwrap().write_all(&pack).unwrap(); + let out = child.wait_with_output().unwrap(); + assert!( + out.status.success(), + "git unpack-objects rejected rit's pack: {}", + String::from_utf8_lossy(&out.stderr) + ); + + // Every object is now present and valid. + assert_eq!(git(target.path(), &["cat-file", "-t", &head.to_hex()]), "commit"); + git(target.path(), &["fsck", "--strict"]); +}