Skip to content

Week 2 - M1: Real SHA-1 + zlib object store on disk #10

Description

@aman-a-shah

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

  1. Branch: git checkout main && git pull && git checkout -b week2/m1-real-object-store
  2. Delete self._fake_store and the crc32 placeholder
  3. 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
  4. _object_path(hash) -> self.objects_dir / hash[:2] / hash[2:]
    • 2-char folder split keeps one directory from holding 100k files
  5. 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)
  6. 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)
  7. 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
  8. Drop the module-level _cli_store singleton - build an ObjectStore(".") inside each handler instead
  9. 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
  10. 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

  • SHA-1 with the <type> <len>\0 header
  • objects zlib-compressed at objects/ab/cdef...
  • corrupt object detected on read
  • minigit init scaffolds a repo, idempotent
  • tests use real disk, no dict
  • quality-check green
  • PR open + reviewed

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

module-1Object storageweek-2Week 2 - real storage and wire

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions