Version Control System

Git-Like Version Control

Design a small Git-like version control system: content-addressed file storage, commits that form a graph, branches as pointers, fast-forward and three-way merges, and diffs.

Overview

A version control system keeps the history of a project, one saved version at a time. Each saved version is a commit: the full list of files and their contents, a message, an author, a time, and a link to the commit or commits it came from. Branches let several lines of work grow from the same point, and a merge joins two lines back together.

The problem tests whether you can pick the right data structures. Three ideas carry the design, and all three come from Git. File contents are stored by the hash of their bytes, so a file that did not change costs nothing in the next commit. Commits point at their parents, so history is a directed acyclic graph (a DAG: arrows only go backward in time, never in a loop). A branch is only a name for one commit, so creating one costs O(1).

A strong answer draws the commit graph, explains a fast-forward and a three-way merge on it, names the merge base, and writes code whose demo shows a clean merge and a conflict.

Scoping the problem

Seven questions set the scope, and the usual answers keep the core small and leave the rest as follow-ups.

  • Local only, or push and pull to a remote too? A remote adds networking and sync. Keep it local; a remote is a follow-up.
  • Is there a staging area between editing and committing? It adds the index. The usual answer is yes: add, then commit.
  • Which merges? The choices are fast-forward, three-way and rebase. Support fast-forward and three-way.
  • Conflicts at file level or line level? Line level needs a line merge algorithm. Use file level; line level is a follow-up.
  • Directories as tree objects, or flat paths? Trees save space for big repositories. Use flat paths in the core.
  • Does diff show lines? That needs a diff algorithm. The usual answer is yes, a unified diff.
  • Several threads using one repository? This decides the locking. Operations must be atomic.

Functional requirements

  • write and delete files in a working directory; add(path) stages the change into the index
  • commit(message, author, now) freezes the index into a commit and moves the current branch; an empty commit is refused
  • branch(name) creates a pointer to the current commit; checkout(name) switches branches and refuses when there are uncommitted changes
  • log() lists first-parent history from HEAD back to the first commit
  • merge(other) reports up-to-date, fast-forwards, or does a three-way merge with a two-parent commit; conflicts are reported and HEAD does not move
  • diff(a, b) returns a unified diff per changed file between two commits

Non-functional requirements

  • Blobs and commits are immutable; only branch refs change
  • Equal contents are stored once (content addressing)
  • Creating a branch is O(1); a commit costs O(files) to list ids, not O(bytes)
  • Every public operation is atomic under one repository lock
  • Deterministic: time is passed in, so commit ids are stable in tests

Interview questions

  • How do you find the merge base, and what if there is more than one?
  • How would you merge at the line level instead of the file level?
  • Storing a full snapshot per commit sounds wasteful. Is it?
  • Two threads commit to the same branch at the same moment. What can go wrong?
  • How would you add revert?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio