Minor: fix comment to reflect previous commit.
Avoid unnecessary copying in listdir and simply locking logic with atomic.
Refactor checkout_in_dir() in preparation for a commit() implementation.
Implement a first version of commit.

We're still relying JSON which is sub-optimal, and there's no place to store the tree reference,
as there's no snapshot index yet.
Have commit store the new snapshot in a repo-wide snapshot index.