Fix Repository.diff for references an empty trees

This should fix issue #432

By the way make the code a little more robust.