Instead of trying to reimplement parts of it, make use of Object.peel() and Reference.peel() to get to a Blob or Tree.