branchmap: use a context manager when writing the branchmap

This is cleaner and safer. The previous code date from long before we had
context manager available.
12 jobs for topic/default/branch-write in 26 minutes and 56 seconds (queued for 7 seconds)