Skip to content

Week 4 - M3: Branch checkout + fast-forward merge #24

Description

@aman-a-shah

Owner: @DanisLol
Files: minigit/commits.py, tests/test_commits.py
Depends on: #18 and #23 (M2 checkout)
Branch: week4/m3-checkout-and-fast-forward

Make branch switching restore files. Add fast-forward merging: move the current branch to a later commit without creating a merge commit.

Steps

  1. Start from updated main after Week 3 - M3: Real tree commits + history helpers #18 merges. Use a fake checkout for ordering tests until M2 lands.
  2. Before a switch or fast-forward, compare the index with get_head_tree().
    • Treat an unborn HEAD as an empty tree.
    • Staged changes raise MiniGitError.
    • Let M2 reject unstaged changes and untracked files that would be overwritten.
  3. Update switch_branch(name): read the target ref, read its commit, call self.tree.checkout(commit.tree), then write HEAD.
    • Missing branch: RefNotFoundError.
    • Failed checkout: HEAD and refs stay unchanged.
  4. Add is_ancestor(ancestor_hash: str, descendant_hash: str) -> bool.
    • Reuse walk_history; follow every parent.
    • A commit is its own ancestor. Share this helper with M4.
  5. Implement merge(branch_name) in this order:
    • Missing target: RefNotFoundError.
    • Target equals the current tip, or is already its ancestor: return None; change nothing.
    • Current tip is the target's ancestor, or the current branch is unborn: checkout the target tree, then update the current branch ref. Return None.
    • Otherwise raise MiniGitError("three-way merge is not implemented yet"); change nothing.
  6. Register minigit merge <branch>. Keep HEAD on the current branch after merging. Branch creation and listing keep their existing behavior.
  7. Run scripts/quality-check.sh. Open a PR linked to this issue and request a teammate review.

Tests

  • Switch between two real snapshots; check files, index, HEAD, and unchanged branch tips.
  • Fast-forward updates the current ref and files without creating a commit.
  • Check same-tip merges, already-merged targets, unborn branches, and diverged history.
  • Check ancestry through a second parent.
  • Staged edits or checkout failure leave refs and HEAD unchanged.

Team checkpoint

Commit A on main, create feature, switch to it, and commit B. Switch to main: files show A. Merge feature: files show B, both refs point to B, and status is clean.

Done when

  • Branch switching and fast-forward merge pass with real M1/M2.
  • Ancestry helper is ready for M4.
  • Team checkpoint passes.
  • Quality check passes; PR open and review requested.

Activity

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

Metadata

Metadata

Assignees

Labels

module-3Commits and branchingweek-4Week 4 - branching and checkout

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions