use more libgit2 for getting refs and better error handling

We have some crash reports in libgit for parsing all refs, so I tried to move to libgit2 for this. Also I improved the error handling, so that we get less crashes (libgit throws an exception on error, with libgit2 we can use a return value).

Should we include this into 1.8? If something goes wrong, ppl. can simple disable libgit2 using the advanced option.

@ch3cooli
@lznuaa
What do you think of this?