branchmap: use a context manager when writing the branchmap
authorPierre-Yves David <pierre-yves.david@octobus.net>
Fri, 13 May 2022 15:19:57 +0200
changeset 49233 f923bdd7477d
parent 49232 71774d799de7
child 49234 b5858e02e3ba
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.
mercurial/branchmap.py
--- a/mercurial/branchmap.py	Tue Apr 05 05:01:58 2022 +0200
+++ b/mercurial/branchmap.py	Fri May 13 15:19:57 2022 +0200
@@ -428,22 +428,22 @@
             self._delayed = True
             return
         try:
-            f = repo.cachevfs(self._filename(repo), b"w", atomictemp=True)
-            cachekey = [hex(self.tipnode), b'%d' % self.tiprev]
-            if self.filteredhash is not None:
-                cachekey.append(hex(self.filteredhash))
-            f.write(b" ".join(cachekey) + b'\n')
-            nodecount = 0
-            for label, nodes in sorted(self._entries.items()):
-                label = encoding.fromlocal(label)
-                for node in nodes:
-                    nodecount += 1
-                    if node in self._closednodes:
-                        state = b'c'
-                    else:
-                        state = b'o'
-                    f.write(b"%s %s %s\n" % (hex(node), state, label))
-            f.close()
+            filename = self._filename(repo)
+            with repo.cachevfs(filename, b"w", atomictemp=True) as f:
+                cachekey = [hex(self.tipnode), b'%d' % self.tiprev]
+                if self.filteredhash is not None:
+                    cachekey.append(hex(self.filteredhash))
+                f.write(b" ".join(cachekey) + b'\n')
+                nodecount = 0
+                for label, nodes in sorted(self._entries.items()):
+                    label = encoding.fromlocal(label)
+                    for node in nodes:
+                        nodecount += 1
+                        if node in self._closednodes:
+                            state = b'c'
+                        else:
+                            state = b'o'
+                        f.write(b"%s %s %s\n" % (hex(node), state, label))
             repo.ui.log(
                 b'branchcache',
                 b'wrote %s with %d labels and %d nodes\n',