# HG changeset patch
# User Idan Kamara <idankk86@gmail.com>
# Date 2012-04-19 15:11:42
# Node ID 14a4e17f08178424bb05ff63dfb89e431f46f5c0
# Parent  85c7602e283a91db86448a02c5ae16ed5a699d60

dirstate: write branch file atomically

diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -244,7 +244,11 @@ class dirstate(object):
         if branch in ['tip', '.', 'null']:
             raise util.Abort(_('the name \'%s\' is reserved') % branch)
         self._branch = encoding.fromlocal(branch)
-        self._opener.write("branch", self._branch + '\n')
+        f = self._opener('branch', 'w', atomictemp=True)
+        try:
+            f.write(self._branch + '\n')
+        finally:
+            f.close()
 
     def _read(self):
         self._map = {}