Back to list

Git - Object Storage

Lv.5746@mukitaro14 playsDec 31, 2025

Git object storage system with SHA-1 hashing. Shows how Git stores commits, trees, blobs, and tags.

preview.c
C
1struct object_id {
2 unsigned char hash[20];
3};
4
5struct object {
6 unsigned parsed : 1;
7 unsigned type : 3;
8 unsigned flags : 28;
9 struct object_id oid;
10};
11
12enum object_type {
13 OBJ_NONE = 0,
14 OBJ_COMMIT = 1,
15 OBJ_TREE = 2,
16 OBJ_BLOB = 3,
17 OBJ_TAG = 4
18};
19
20struct object *parse_object(const struct object_id *oid)
21{
22 void *buffer;
23 unsigned long size;
24 enum object_type type;
25
26 buffer = read_object_file(oid, &type, &size);
27 if (!buffer)
28 return NULL;
29
30 struct object *obj = lookup_object(oid);
31 if (!obj)
32 obj = create_object(oid, type);
33
34 if (parse_object_buffer(obj, type, size, buffer) < 0) {
35 free(buffer);
36 return NULL;
37 }
38
39 free(buffer);
40 return obj;
41}

Custom problems are not included in rankings