Use ref iterator

Switch to using a `git_reference_iterator` when iterating over references. Also fixes a memory leak when an exception is raised during the iteration.