Fill the tree cache when reading in a tree into an index

It is convenient to use a standalone index as a way to perform some modifications to a tree at arbitrary levels. When doing this however, we did not fill the cache, which means we'd end up calculating a lot of trees which are already in the repository.

Filling the index on read means we can skip a lot of work; I also think that the current behaviour could lead to having the wrong cache entries.

- [x] Fill the cache on read-tree
- [x] Write the cache to the index file
- [x] Fill the cache on write-tree