dirstate: use a context manager to handle the file used for writing the branch
authorPierre-Yves David <pierre-yves.david@octobus.net>
Thu, 02 Mar 2023 14:50:17 +0100
changeset 50254 df76808d5f21
parent 50253 444fa55f5dd2
child 50255 fa04407bda7a
dirstate: use a context manager to handle the file used for writing the branch This is more modern.
mercurial/dirstate.py
--- a/mercurial/dirstate.py	Thu Mar 02 11:54:21 2023 +0100
+++ b/mercurial/dirstate.py	Thu Mar 02 14:50:17 2023 +0100
@@ -613,19 +613,17 @@
 
     def setbranch(self, branch):
         self.__class__._branch.set(self, encoding.fromlocal(branch))
-        f = self._opener(b'branch', b'w', atomictemp=True, checkambig=True)
-        try:
+        vfs = self._opener
+        with vfs(b'branch', b'w', atomictemp=True, checkambig=True) as f:
             f.write(self._branch + b'\n')
-            f.close()
-
             # make sure filecache has the correct stat info for _branch after
             # replacing the underlying file
+            #
+            # XXX do we actually need this,
+            # refreshing the attribute is quite cheap
             ce = self._filecache[b'_branch']
             if ce:
                 ce.refresh()
-        except:  # re-raises
-            f.discard()
-            raise
 
     def invalidate(self):
         """Causes the next access to reread the dirstate.