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.

Test case: https://gist.github.com/arthurschreiber/5653887