Patchwork D9038: mergestate: extract overridable methods for making/restoring file backups

login
register
mail settings
Submitter phabricator
Date Sept. 17, 2020, 8:11 p.m.
Message ID <differential-rev-PHID-DREV-etwnsj24g4e66ynpazua-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47198/
State Superseded
Headers show

Comments

phabricator - Sept. 17, 2020, 8:11 p.m.
martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9038

AFFECTED FILES
  mercurial/mergestate.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/mergestate.py b/mercurial/mergestate.py
--- a/mercurial/mergestate.py
+++ b/mercurial/mergestate.py
@@ -546,6 +546,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,
@@ -559,7 +566,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,
@@ -665,9 +672,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(