Do not write directly to this pack's file. The pack name is the SHA-1 of the object names. If the repository is already full packed and we are doing a repack (such as to change compression settings), the output file name is also the input file. If you just write to it immediately we truncate the data and destroy the repository.

Instead write both the pack and the idx file into temporary files and try to perform an atomic rename over onto the real names as close as possible. Yes when repacking an existing pack back onto itself there is a window of time where the pack would appear corrupt as the idx and pack don't match.

You could first rename away the old files to a temp name, then rename the new files over.

This problem does not arise on the DFS system because there we use unique file names that have nothing to do with the contents.