Skip to main content

gunnar_server/
first_commit.rs

1//! **A repository's first commit, written by the server** (the owner, 2026-10-08: a sold
2//! private Gunnar is handed over with ONE welcome repository whose README thanks the buyer
3//! and says how to start — with no git client anywhere in the sale).
4//!
5//! `Repos.Create{readme}` makes the repository and then this: one blob (the README), one tree
6//! holding it, one commit of that tree with no parent, as ONE pack in the repository's own
7//! hash (sha1 or sha256), put through the repository's store with the default branch set to
8//! the commit — the same `GitOps::put` a push lands through, so the transaction log (and the
9//! twin) carries it like any push.
10//!
11//! The objects are git's own formats, written here without `gix-object` (which this crate
12//! keeps out of its shipped dependencies); the unit tests read them back through the store's
13//! reader and check every id against `gix-object`'s (`tests/first_commit.rs`).
14
15use std::io::Write as _;
16
17/// The identity the first commit is written as: the server, never a person.
18pub const AUTHOR: &str = "Gunnar <noreply@gunnar.invalid>";
19
20/// git's pack object type numbers.
21const OBJ_COMMIT: u8 = 1;
22const OBJ_TREE: u8 = 2;
23const OBJ_BLOB: u8 = 3;
24
25/// One object: its type number, its kind word and its content.
26struct Object {
27    num: u8,
28    word: &'static str,
29    content: Vec<u8>,
30}
31
32/// The id of `content` as a `word` object in `kind`: the hash of `<word> <len>\0<content>`.
33pub fn object_id(kind: gix_hash::Kind, word: &str, content: &[u8]) -> Result<gix_hash::ObjectId, String> {
34    let mut h = gix_hash::hasher(kind);
35    h.update(format!("{word} {}\0", content.len()).as_bytes());
36    h.update(content);
37    h.try_finalize().map_err(|e| format!("hashing a {word}: {e:?}"))
38}
39
40/// Pure: **the pack of a repository's first commit** — one file `name` holding `text`, in a
41/// tree, committed with `message` at `when_unix` (UTC). Answers the pack's bytes and the
42/// commit's id (hex).
43pub fn first_commit_pack(kind: gix_hash::Kind, name: &str, text: &[u8], message: &str, when_unix: i64) -> Result<(Vec<u8>, String), String> {
44    if name.is_empty() || name.contains('/') || name.contains('\0') {
45        return Err(format!("{name:?} is not a file name at the repository's root"));
46    }
47    let blob_id = object_id(kind, "blob", text)?;
48    let mut tree = Vec::new();
49    tree.extend_from_slice(format!("100644 {name}\0").as_bytes());
50    tree.extend_from_slice(blob_id.as_bytes());
51    let tree_id = object_id(kind, "tree", &tree)?;
52    let ident = format!("{AUTHOR} {when_unix} +0000");
53    let message = if message.ends_with('\n') { message.to_string() } else { format!("{message}\n") };
54    let commit = format!("tree {}\nauthor {ident}\ncommitter {ident}\n\n{message}", tree_id.to_hex()).into_bytes();
55    let commit_id = object_id(kind, "commit", &commit)?;
56    let objects = [
57        Object { num: OBJ_BLOB, word: "blob", content: text.to_vec() },
58        Object { num: OBJ_TREE, word: "tree", content: tree },
59        Object { num: OBJ_COMMIT, word: "commit", content: commit },
60    ];
61    Ok((pack(kind, &objects)?, commit_id.to_hex().to_string()))
62}
63
64/// A version-2 pack of whole objects, its trailer in `kind`.
65fn pack(kind: gix_hash::Kind, objects: &[Object]) -> Result<Vec<u8>, String> {
66    let mut out = Vec::new();
67    out.extend_from_slice(b"PACK");
68    out.extend_from_slice(&2u32.to_be_bytes());
69    out.extend_from_slice(&(objects.len() as u32).to_be_bytes());
70    for o in objects {
71        debug_assert!(!o.word.is_empty());
72        // The header: type in bits 4-6 of the first byte, the size in 4 + 7·n bits.
73        let mut size = o.content.len() as u64;
74        let mut byte = (o.num << 4) | (size & 0x0f) as u8;
75        size >>= 4;
76        while size > 0 {
77            out.push(byte | 0x80);
78            byte = (size & 0x7f) as u8;
79            size >>= 7;
80        }
81        out.push(byte);
82        let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
83        z.write_all(&o.content).map_err(|e| e.to_string())?;
84        out.extend_from_slice(&z.finish().map_err(|e| e.to_string())?);
85    }
86    let mut h = gix_hash::hasher(kind);
87    h.update(&out);
88    let trailer = h.try_finalize().map_err(|e| format!("the pack's trailer: {e:?}"))?;
89    out.extend_from_slice(trailer.as_bytes());
90    Ok(out)
91}
92
93// The proof (every id against `gix-object`, read back through the store a push lands in) is
94// `tests/first_commit.rs`: this tier names no `gix_object` (xtask `engine_seam`).