write-tree: properly detect failure to write tree objects