Shamelessly stolen from bzrlib project (GPL 2 or above)

