When I do a first-principles exercise, I like thinking in nouns and verbs i.e. what kinds of nouns and verbs would I need to be able to describe what I want to implement?
First and foremost, I think of Git as a storage service: one that saves the state of your source code throughout time, and allows you travel to different states in history. What nouns and verbs do I need to describe a time-travelling service for a file system?
A time-travelling service needs snapshots of the file system. A file system consists of files and folders. I take snapshots of the file system. I connect different snapshots together. I travel to different snapshots. I create new branches in time. I keep track of where I am in time.
The identified nouns are:
- Snapshots
- File system
- Files
- Folders
The identified verb-noun pairs are:
- take-snapshot
- connect-snapshots
- travel to-snapshot
- create-branch
- keep track-snapshot
What does it mean "to take a snapshot" of a file system? I need a way to represent files and folders. In Git, files are called blob objects and folders are tree objects. All Git objects are stored in .git/objects, which means that this folder serves as a database!
Blobs and trees are both named by SHA-1 hashing their contents. The first two characters of the hash constitute the folder in which in the object will reside; the remaining characters make up the name of the object.
Blobs and trees have different structures. Trees are nested structures, so they have a 3-tuple form. In code,