Skip to content

Week 1 - M2: WorkingTree skeleton + index types #3

Description

@aman-a-shah

Owner: @Saanvi521
Module: 2 - Index & working tree
File you own: minigit/index.py
Week: 1 of 10 - Contract & Skeletons

What "skeleton week" means

  • no real tree building, no Myers diff, no checkout yet -> Weeks 3-5
  • you depend on Module 1 (@mahis1067) -> do not wait for it, stub it (step 12)

Interface contract - exact signatures, do not change alone

IndexEntry = (mode: str, hash: str, path: str)

class WorkingTree:
    read_index() -> list[IndexEntry]        build_tree_from_index() -> str
    write_index(entries) -> None            diff_working_tree_vs(tree_hash) -> DiffResult
    stage_file(path) -> None                checkout(tree_hash) -> None
  • mode: "100644" file, "100755" exec, "40000" dir
  • index file format (Week 2+): "<mode> <blob-hash> <path>\n", sorted by path

Steps

  1. Setup (once): clone, cd mini-git, scripts/init.sh, skim README.md + minigit/errors.py
  2. Branch week1/m2-working-tree-skeleton
  3. Types first: IndexEntry(NamedTuple) with mode/hash/path; @dataclass DiffResult with added/deleted/modified lists (line-level hunks are Week 5 - names only now)
  4. __init__(self, repo_path=".", store=None) -> self.root, self.index_path, self.store = store or ObjectStore(repo_path), self._entries: list[IndexEntry] = []
    • the store=None argument is the important part: fake store in tests, real one for free later
  5. read_index -> return list(self._entries) (copy, not the live list), always sorted by path
  6. write_index -> self._entries = sorted(entries, key=lambda e: e.path); no disk write yet
  7. stage_file -> read bytes off disk, store.write_object(data, "blob"), replace any entry with the same path, append IndexEntry("100644", blob_hash, path), re-sort
    • key rule: snapshot - editing the file after staging must NOT change the index
  8. build_tree_from_index -> return self.store.write_object(b"", "tree")
    • # Week 3 - group by directory, one tree object per level, bottom-up
  9. diff_working_tree_vs -> return DiffResult() # Week 3 real diff, Week 5 Myers
  10. checkout -> pass # Week 4 - write files from a tree, delete the rest
  11. CLI: minigit add <path>, minigit status
  12. Wire into minigit/cli.py - one import + one line; expect a small conflict
  13. Tests tests/test_index.py with a FakeObjectStore class at the top (dict + len-based hash)
    • use pytest tmp_path; cover: staging adds one entry, staging twice replaces not duplicates, read_index sorted, editing after staging does not change the entry
  14. quality-check green -> commit, push, PR

Team checkpoint (all four, end of week)

  • contract signatures confirmed in the group chat before anyone merges
  • everyone imports everyone else's class and calls one method -> shaped fake result back
  • @DanisLol calls your build_tree_from_index() and checkout() -> keep those names exact

Done when

  • IndexEntry + DiffResult defined
  • all 6 methods, exact signatures
  • store injectable via __init__
  • both CLI commands run
  • tests pass with the fake store
  • quality-check green
  • PR open + reviewed

Metadata

Metadata

Assignees

Labels

module-2Index and working treeweek-1Week 1 - contract and skeletons

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions