projects > from_scratch > git.go
1
A Git implementation, from scratch, in Go[github]
1
2
A Git implementation built from scratch in Go with no Git
3
libraries — it reads and writes the real .git object store by
4
hand (blobs, trees, commits), content-addressed with SHA-1 and
5
zlib-compressed, byte-for-byte compatible with real git.
6
7
Implements the plumbing (init, hash-object, cat-file, ls-tree,
8
write-tree, commit-tree) and a full clone of public repositories
9
over the Smart HTTP protocol — pkt-line framing, packfile
10
parsing, and ref-delta resolution including delta chains, then
11
checkout. Built end-to-end as the CodeCrafters 'Build Your Own
12
Git' challenge.
13
14
[Go][Git][GitHub]
15
17
18
// parses blob object file
19
func ParseBlob(object []byte) (*Blob, error) {
20
hash := utils.Hash(object)
21
t, rest, found := bytes.Cut(object, []byte(" "))
22
if !found {
23
return nil, fmt.Errorf("malformed blob object - missing whitespace after type")
24
}
25
if string(t) != "blob" {
26
return nil, fmt.Errorf("malformed blob object - expected type 'blob' but was %s", string(t))
27
}
28
sizeBytes, contentBytes, found := bytes.Cut(rest, []byte{byte(0)})
29
if !found {
30
return nil, fmt.Errorf("malformed blob object %s", object)
31
}
32
size, err := strconv.Atoi(string(sizeBytes))
33
if err != nil {
34
return nil, fmt.Errorf("malformed blob object: %v", err)
35
}
36
37
return &Blob{Hash: hash, Size: size, Content: contentBytes[:size]}, nil
38
}
39
40
NORMAL~/oleksandr/projects/from_scratch/git.go.../from_scratch/git.gomainutf-8
Enter to open · :q to quit
keys
j / kmove the cursor line, or the tree cursor
hfocus the tree; in the tree, fold or go to the parent
l / Enterin the tree, unfold or open the file
gg / Gfirst / last line
clickput the cursor on a line; on README.sh, run the session again
:qquit to the shell
:help ?this table
q / Escclose this