The second stage of the script could be rewritten to use the git fast-import format which would allow us to let Git deal with the making of the commits.
This would decouple us from the updates to refs and keeping in step with Git's CLI parsing allowing the script to focus on getting the final structure right. Additionally, it allows someone to take the produced file and tweak it before importing into an actual Git repository.