filemerge: store backups in the overlayworkingctx if using imm
authorPhil Cohen <phillco@fb.com>
Fri, 13 Oct 2017 12:34:22 -0700
changeset 34784 123a68e6b473
parent 34783 0c812885586b
child 34785 1af4561b6bfe
filemerge: store backups in the overlayworkingctx if using imm Differential Revision: https://phab.mercurial-scm.org/D1059
mercurial/filemerge.py
--- a/mercurial/filemerge.py	Fri Oct 13 12:34:22 2017 -0700
+++ b/mercurial/filemerge.py	Fri Oct 13 12:34:22 2017 -0700
@@ -606,9 +606,24 @@
     from . import context
     a = _workingpath(repo, fcd)
     back = scmutil.origpath(ui, repo, a)
-    if premerge:
-        util.copyfile(a, back)
-    return context.arbitraryfilectx(back, repo=repo)
+    inworkingdir = (back.startswith(repo.wvfs.base) and not
+        back.startswith(repo.vfs.base))
+
+    if isinstance(fcd, context.overlayworkingfilectx) and inworkingdir:
+        # If the backup file is to be in the working directory, and we're
+        # merging in-memory, we must redirect the backup to the memory context
+        # so we don't disturb the working directory.
+        relpath = back[len(repo.wvfs.base) + 1:]
+        wctx[relpath].write(fcd.data(), fcd.flags())
+        return wctx[relpath]
+    else:
+        # Otherwise, write to wherever the user specified the backups should go.
+        #
+        # A arbitraryfilectx is returned, so we can run the same functions on
+        # the backup context regardless of where it lives.
+        if premerge:
+            util.copyfile(a, back)
+        return context.arbitraryfilectx(back, repo=repo)
 
 def _maketempfiles(repo, fco, fca):
     """Writes out `fco` and `fca` as temporary files, so an external merge