mergestate: extract overridable methods for making/restoring file backups
authorMartin von Zweigbergk <martinvonz@google.com>
Tue, 15 Sep 2020 11:33:26 -0700
changeset 45497 e833ff4dd0ea
parent 45496 ef3273ac5f6e
child 45498 cc5f811b1f15
mergestate: extract overridable methods for making/restoring file backups In-memory merge currently still uses on-disk mergestate. That's mostly harmless (I don't think I've seen any problem it's actually caused). It's still a little weird. I'm planning to add an in-memory mergestate, which will be used with `overlayworkingctx`. This patch prepares for that by extracting that logic, so it's easier to change per subclass. Differential Revision: https://phab.mercurial-scm.org/D9038
mercurial/mergestate.py
--- a/mercurial/mergestate.py	Wed Sep 16 13:39:26 2020 -0700
+++ b/mercurial/mergestate.py	Tue Sep 15 11:33:26 2020 -0700
@@ -545,6 +545,13 @@
 
         return hex(hashutil.sha1(path).digest())
 
+    def _make_backup(self, fctx, localkey):
+        self._repo.vfs.write(b'merge/' + localkey, fctx.data())
+
+    def _restore_backup(self, fctx, localkey, flags):
+        with self._repo.vfs(b'merge/' + localkey) as f:
+            fctx.write(f.read(), flags)
+
     def add(self, fcl, fco, fca, fd):
         """add a new (potentially?) conflicting file the merge state
         fcl: file context for local,
@@ -558,7 +565,7 @@
             localkey = nullhex
         else:
             localkey = mergestate.getlocalkey(fcl.path())
-            self._repo.vfs.write(b'merge/' + localkey, fcl.data())
+            self._make_backup(fcl, localkey)
         self._state[fd] = [
             MERGE_RECORD_UNRESOLVED,
             localkey,
@@ -664,9 +671,7 @@
         if preresolve:
             # restore local
             if localkey != nullhex:
-                f = self._repo.vfs(b'merge/' + localkey)
-                wctx[dfile].write(f.read(), flags)
-                f.close()
+                self._restore_backup(wctx[dfile], localkey, flags)
             else:
                 wctx[dfile].remove(ignoremissing=True)
             complete, merge_ret, deleted = filemerge.premerge(