magit-{git-dir,get-top-dir}: fix for real

See issues #1201, #1202, #1210, and #1214 - oh boy.

This time around I have actually run some automated tests (but because
they aren't implemented as ert tests I have not yet committed them).