Data integrity fix, only expose final file

fs.writeFile will truncate the file, then write, during which time
other readers will get a zero length file or a partial (corrupted)
file.  Fix by writing to a temporary file as the original git
tools do, add .lock to a refs file, and tmp_obj_<random> for object
files.
Merge pull request #104 from dfries/master

Data integrity fix, only expose final file