Lesson 2 of 5 · 2 min
A model of history
A commit is identified by a fingerprint of its content and its parent, so changing the past changes every later fingerprint.
import hashlib
history = []
def commit(message, files):
parent = history[-1]["id"] if history else ""
ident = hashlib.sha1((parent + message + str(sorted(files.items()))).encode()).hexdigest()[:7]
history.append({"id": ident, "parent": parent, "message": message})
commit("start", {"app.py": "print(1)"})
commit("fix typo", {"app.py": "print(2)"})
for c in reversed(history):
print(c["id"], c["message"], "<-", c["parent"] or "root")
