Introduce a refs iterator

This is a proof-of-concept for #1384. It's only tested as far as it compiles, but the idea is there. For loose refs, we might also need a path iterator.

As we're about to merge the refs backend work, I'll be holding off until that's done to see how it fits in that. This would then become part of the file-based backend.