Designing a Mini Version Control System

Difficulty: Advanced Patterns: Composite, Memento, Command Asked at: Atlassian, Amazon, Microsoft, Dropbox


Build something you could run add, commit, branch and merge against. The commands are familiar enough that candidates start typing immediately, which is the trap: the obvious model is a list of commits where each commit holds a copy of every file, and that model is wrong in a way that does not show up until the interviewer asks a second question.

Work out what it costs. A repository with ten thousand files, a commit per hour, and a one-line change in each commit. Copy the whole tree per commit and storage grows by ten thousand files an hour to record ten thousand lines of actual change. Store diffs against the previous commit instead and the cost problem goes away, but now reading any version means replaying every patch since the beginning, and a branch is a second chain of patches with no cheap way to ask what the two chains have in common. Neither answer is what real version control does.

The answer is to change what an object is called. Name every stored object by the hash of its own bytes rather than by a path or a sequence number, and the expensive problems collapse into cheap ones. Two files with identical content get the same name, so they are stored once no matter how many commits or directories contain them. A directory listing becomes a small object holding the hashes of its children, so a commit that edits one file in a deep directory needs new objects only along that single path, and every subtree it did not touch is shared by reference with the previous commit. A full snapshot per commit stops being extravagant and becomes the cheapest thing on the table.

That one decision carries the first two thirds of this design. The last third is the part most candidates have never thought about, and it is the question interviewers use to separate people who have read about Git from people who have reasoned about it: merging two branches requires their common ancestor, and a two-way comparison cannot find one. Comparing your version of a file with mine tells you the two differ. It does not tell you who changed what, which means it cannot tell the difference between “you edited this line and I left it alone” and “we both edited it and disagree”. Only the ancestor can, and finding the ancestor is a walk over the commit graph rather than anything to do with files.

So the three hard parts are: hash content so that storage, immutability, integrity and tree comparison all fall out of one idea; find a real merge base and decide per region instead of per file; and keep branches as nothing more than names holding a hash, which is why creating one is free and why deleting one leaves garbage that a reachability sweep has to find.

💡 Content addressing = naming a stored object by a hash of its own bytes instead of by a location or an id. The name is derived from the content, so identical content is automatically the same object and altered content is automatically a different one.

💡 Structural sharing = two versions of a tree physically sharing the parts that are identical, so a snapshot costs only the path that changed. It needs immutability, since a shared node somebody can edit in place would corrupt every version pointing at it. Learn more →

💡 Merge base = the most recent commit that both branches descend from. It is the third input a three-way merge needs, and it is found by walking the commit graph, not by looking at files.

💡 Working tree, index and commit = the three places a file lives. The working tree is what you are editing, the index is what you have chosen to commit, and the commit is the frozen snapshot. Keeping them separate is what makes a commit deliberate.


Functional Requirements

  1. Stage – add copies a file’s current content into the object store and records it in an index, so the next commit snapshots what you chose rather than whatever the working directory happens to hold.
  2. Commit – freeze the index into an immutable snapshot of the whole tree, named by its own hash and carrying its parent commits.
  3. History – walk the commit graph from any ref, yielding each reachable commit once.
  4. Branch – create, list and delete a ref, which is a name holding one commit hash, and check one out into the working tree.
  5. Merge – fast-forward when the target already contains your tip, otherwise a three-way merge against the common ancestor, decided per path.
  6. Conflicts – record a conflict per path with all three sides kept, so a merge reports rather than throws, and accept a resolution afterwards.
  7. Garbage collection – reclaim every unreachable object once the refs that pointed at it are gone.

Non-Functional Requirements

  1. Identical content is stored once – writing content already present is an O(1) hash plus a map probe that adds no bytes, however many paths or commits reference it.
  2. A commit costs O(changed paths multiplied by depth) new objects, not O(files in the repository). A one-line change in a four-deep directory writes one blob, four trees and one commit, and every sibling subtree keeps its previous hash.
  3. Creating a branch writes zero objects and is O(1): one entry in the ref map holding one hash. Comparing two trees for equality is one hash comparison rather than a walk.
  4. Integrity is locally verifiable – rehashing any stored object must reproduce the name it is filed under, so corruption is detectable without a remote. Collection is one mark-and-sweep: O(reachable) to mark, O(stored) to sweep.