Git's cat-file command inspects an object by splitting the header back off. Today you build it, giving us the first real porcelain command and a clean way to read any object's type, size, and content.
Parse a stored object into its type, size, and content, and fail clearly on a missing object.
The header we so carefully wrote now pays off. To turn raw serialized bytes into
a useful answer, find the first NUL byte: everything before it is the
"<type> <size>" header, everything after it is the content. Split the header on
the space to recover the type word and the declared size. That is the whole of
Git’s cat-file inspection, and it works for every object type.
This is our first porcelain command, the friendly layer over the plumbing.
It also fixes where errors surface: asking for an id that was never stored should
give a clear “object not found” message, not a crash deep in the reader. Real Git
answers git cat-file -t, -s, and -p for exactly these three pieces, and
running it against your stored blob prints the same hello. That cross-check with
real Git is a theme we return to at every layer.
// ReadRaw, then split on the first NUL into header and contentfunc (r *Repo) CatFile(id string) (typ string, size int, content []byte, err error) {raw, err := r.ReadRaw(id) // handles a missing filei := bytes.IndexByte(raw, 0) // header is raw[:i], content is raw[i+1:]// header is "<type> <size>"; parse the two fields}