Better .gitignore parsing

This improves the `.gitignore` parsing once again and fixes #506.
Hopefully doesn't introduce any new bugs.