Coverity fixes

This fixes a few problems pointed out by the coverity scan including missing checks for OOM, not checking error codes, memory leaks etc.

This doesn't fix nearly all of the problems but does address lots of the low hanging fruit.
Why not `if (git_buf_joinpath(...) < 0) return -1;`? That would leave the normal flow be at the same level of indentation.