Corrupted index is bad for your health

While checking out a test repo from .git repository, its index has been unexpectedly LF->CRLF converted.

While git.git was able to gracefully detect the corruption...

```
$ git status
error: bad index file sha1 signature
fatal: index file corrupt
```

...libgit2 was segfaulting.

This PR attempts at fixing this, along with a few issues discovered while working in this area.