Owner:@mahis1067
Module: 1 - Object storage
File you own:minigit/objects.py
Week: 2 of 10 - Real Bytes on Disk
What changes this week
- Week 1 was fake: crc32 hash, dict in memory
- Week 2 is real: SHA-1, zlib, files under
.minigit/objects/ - signatures do not change - everyone else's code keeps working
Steps
- Branch:
git checkout main && git pull && git checkout -b week2/m1-real-object-store - Delete
self._fake_store and the crc32 placeholder hash_object -> hashlib.sha1(f"{obj_type} {len(data)}".encode() + b"\0" + data).hexdigest()- the header is inside the hash -
blob 2\0hi, not just hi
_object_path(hash) -> self.objects_dir / hash[:2] / hash[2:]- 2-char folder split keeps one directory from holding 100k files
write_object -> hash, mkdir(parents=True, exist_ok=True), zlib.compress(header + data), write bytes
- path already exists -> return the hash, write nothing (same bytes = same object)
read_object -> missing path = ObjectNotFoundError(hash)- read,
zlib.decompress, split once on b"\0" -> header + content - parse
"<type> <len>" from the header - re-hash the content, mismatch ->
ObjectCorruptError - return
(type, content)
- New command
minigit init (you own it, @DanisLol owns what goes in HEAD):
- create
.minigit/, .minigit/objects/, .minigit/refs/heads/, empty .minigit/index, empty .minigit/config - write
.minigit/HEAD = ref: refs/heads/main\n - already a repo -> print
already a minigit repository and return 0, do not wipe anything
- Drop the module-level
_cli_store singleton - build an ObjectStore(".") inside each handler instead - Tests - all on
tmp_path, no fakes anymore:
hash_object(b"hi", "blob") == "32f95c0d1244a78b2be1bab8de17906fabb2c4a8"hash_object(b"", "blob") == "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"- round-trip through disk returns the same bytes
- the file on disk is compressed (raw bytes != content)
- writing twice does not error and leaves one file
- unknown hash ->
ObjectNotFoundError - overwrite an object file with junk ->
ObjectCorruptError init creates every path, running it twice is safe
scripts/quality-check.sh green -> commit, push, PR into main
Notes
- those two hashes match real
git hash-object output - check with printf hi | git hash-object --stdin - you are still the base of the stack - land first
Done when
Owner:@mahis1067
Module: 1 - Object storage
File you own:
minigit/objects.pyWeek: 2 of 10 - Real Bytes on Disk
What changes this week
.minigit/objects/Steps
git checkout main && git pull && git checkout -b week2/m1-real-object-storeself._fake_storeand the crc32 placeholderhash_object->hashlib.sha1(f"{obj_type} {len(data)}".encode() + b"\0" + data).hexdigest()blob 2\0hi, not justhi_object_path(hash)->self.objects_dir / hash[:2] / hash[2:]write_object-> hash,mkdir(parents=True, exist_ok=True),zlib.compress(header + data), write bytesread_object-> missing path =ObjectNotFoundError(hash)zlib.decompress, split once onb"\0"-> header + content"<type> <len>"from the headerObjectCorruptError(type, content)minigit init(you own it, @DanisLol owns what goes in HEAD):.minigit/,.minigit/objects/,.minigit/refs/heads/, empty.minigit/index, empty.minigit/config.minigit/HEAD=ref: refs/heads/main\nalready a minigit repositoryand return 0, do not wipe anything_cli_storesingleton - build anObjectStore(".")inside each handler insteadtmp_path, no fakes anymore:hash_object(b"hi", "blob") == "32f95c0d1244a78b2be1bab8de17906fabb2c4a8"hash_object(b"", "blob") == "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"ObjectNotFoundErrorObjectCorruptErrorinitcreates every path, running it twice is safescripts/quality-check.shgreen -> commit, push, PR intomainNotes
git hash-objectoutput - check withprintf hi | git hash-object --stdinDone when
<type> <len>\0headerobjects/ab/cdef...minigit initscaffolds a repo, idempotent